From d4d8e1b1ba926936d3d36ab095bd15dedb63c858 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 6 Sep 2024 16:14:10 +0200 Subject: [PATCH] comment about implementation in life example --- docs/source/todo.rst | 8 +++++++- examples/cx16/life.p8 | 7 +++++++ examples/test.p8 | 9 ++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index e004e1f1c..d30944e49 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,14 +1,20 @@ TODO ==== -TODO creates buggy code in 6502: (ok in IR) +TODO creates buggy code in 6502 (except PET!!!): (also ok in IR) main { sub start() { uword active_world = memory("world", 80*50, 0) uword cell_off = 500 const uword STRIDE = 40 sys.memset(active_world, 80*50, 1) + + txt.print_ub(active_world[500] + active_world[501]) ; TODO prints 1, must be 2 + txt.nl() + txt.print_ub(active_world[cell_off] + active_world[cell_off_1]) ; TODO prints 1, must be 2 + txt.nl() txt.print_ub(count()) ; TODO prints 1, must be 8 + txt.nl() sub count() -> ubyte { return active_world[cell_off-STRIDE-1] + active_world[cell_off-STRIDE] + active_world[cell_off-STRIDE+1] + diff --git a/examples/cx16/life.p8 b/examples/cx16/life.p8 index 79b21a95a..79bf1424b 100644 --- a/examples/cx16/life.p8 +++ b/examples/cx16/life.p8 @@ -95,6 +95,13 @@ main { if active_world == world1 new_world = world2 + ; To avoid re-calculating word index lookups into the new- and active world arrays, + ; we calculate the required pointer values upfront. + ; Inside the loop we can use ptr+x just fine (results in efficient LDA (ptr),Y instruction because x is a byte type), + ; and for each row we simply add the stride to the pointer. + ; It's more readable to use active_world[offset] etc, but offset is a word value, and this produces + ; inefficient assembly code because we can't use a register indexed mode in this case. Costly inside a loop. + uword @requirezp new_world_ptr = new_world + STRIDE+1-DXOFFSET uword @requirezp active_world_ptr = active_world + STRIDE+1-DXOFFSET diff --git a/examples/test.p8 b/examples/test.p8 index 9616cea82..f580260b8 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,10 +5,17 @@ main { sub start() { uword active_world = memory("world", 80*50, 0) - uword cell_off = 500 + uword @shared cell_off = 500 + uword @shared cell_off_1 = cell_off+1 const uword STRIDE = 40 sys.memset(active_world, 80*50, 1) + + txt.print_ub(active_world[500] + active_world[501]) ; TODO prints 1, must be 2 + txt.nl() + txt.print_ub(active_world[cell_off] + active_world[cell_off_1]) ; TODO prints 1, must be 2 + txt.nl() txt.print_ub(count()) ; TODO prints 1, must be 8 + txt.nl() sub count() -> ubyte { return active_world[cell_off-STRIDE-1] + active_world[cell_off-STRIDE] + active_world[cell_off-STRIDE+1] +