From c812b5ee097849d10d10ef4651ca6e6494127726 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 7 Dec 2021 22:25:14 +0100 Subject: [PATCH] elaborate pointervar indexing a bit more in the docs --- docs/source/programming.rst | 6 ++++++ docs/source/syntaxreference.rst | 7 ++++++- docs/source/todo.rst | 1 + examples/test.p8 | 11 +++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 4698ce30f..6fc7cb6bd 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -294,6 +294,11 @@ This way you can set the second character on the second row from the top like th top5screenrows[41] = '!' +**Array indexing on a pointer variable:** +An uword variable can be used in limited scenarios as a 'pointer' to a byte in memory at a specific, +dynamic, location. You can use array indexing on a pointer variable to use it as a byte array at +a dynamic location in memory: currently this is equivalent to directly referencing the bytes in +memory at the given index. See also :ref:`pointervars_programming` Strings ^^^^^^^ @@ -368,6 +373,7 @@ address you specified, and setting the varible will directly modify that memory const byte max_age = 2000 - 1974 ; max_age will be the constant value 26 &word SCREENCOLORS = $d020 ; a 16-bit word at the addres $d020-$d021 +.. _pointervars_programming: Direct access to memory locations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/source/syntaxreference.rst b/docs/source/syntaxreference.rst index 49718d6c4..b24862de1 100644 --- a/docs/source/syntaxreference.rst +++ b/docs/source/syntaxreference.rst @@ -361,6 +361,8 @@ should be the *memory address* where the value is located:: &ubyte[5*40] top5screenrows = $0400 ; works for array as well +.. _pointervars: + Direct access to memory locations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Instead of defining a memory mapped name for a specific memory location, you can also @@ -370,7 +372,7 @@ directly access the memory. Enclose a numeric expression or literal with ``@(... @($d020) = 0 ; set the c64 screen border to black ("poke 53280,0") @(vic+$20) = 6 ; a dynamic expression to 'calculate' the address -The array indexing notation is syntactic sugar for such a direct memory access expression:: +The array indexing notation on a uword 'pointer variable' is syntactic sugar for such a direct memory access expression:: pointervar[999] = 0 ; equivalent to @(pointervar+999) = 0 @@ -420,6 +422,9 @@ Syntax is familiar with brackets: ``arrayvar[x]`` :: array[2] ; the third byte in the array (index is 0-based) string[4] ; the fifth character (=byte) in the string +Note: you can also use array indexing on a 'pointer variable', which is basically an uword variable +containing a memory address. Currently this is equivalent to directly referencing the bytes in +memory at the given index. See :ref:`pointervars` String ^^^^^^ diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 87e75a91e..697ef4568 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -21,6 +21,7 @@ Future - improve testability further, add more tests - use more of Result<> and Either<> to handle errors/ nulls better - can we get rid of pieces of asmgen.AssignmentAsmGen by just reusing the AugmentableAssignment ? generated code should not suffer +- add a switch to not create the globals-initialization logic, but instead create a smaller program (that can only run once though) - c64: make the graphics.BITMAP_ADDRESS configurable (VIC banking) - optimize several inner loops in gfx2 even further? - add modes 2 and 3 to gfx2 (lowres 4 color and 16 color)? diff --git a/examples/test.p8 b/examples/test.p8 index 12b29ea06..b3d41af31 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,5 +4,16 @@ main { sub start() { + uword array = $8000 + array[0] = 10 + array[1] = 20 + array[2] = 30 + + txt.print_ub(@($8000)) + txt.spc() + txt.print_ub(@($8001)) + txt.spc() + txt.print_ub(@($8002)) + txt.spc() } }