From 340b1c2e4289a305f5ebc9348e9e0d4a040fc5f1 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 21 Nov 2020 18:03:57 +0100 Subject: [PATCH] added balls demo/benchmark --- docs/source/todo.rst | 1 - examples/balls.p8 | 96 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 examples/balls.p8 diff --git a/docs/source/todo.rst b/docs/source/todo.rst index bfccc0ca4..f3a0f2912 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO ==== - check cpu stack consistency in all examples -- convert balls.bas/balls.mfk to prog8 example - reduce the amount of translateExpression() calls when the result can be directly assigned to register or variable - make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_' - option to load the built-in library files from a directory instead of the embedded ones (for easier library development/debugging) diff --git a/examples/balls.p8 b/examples/balls.p8 new file mode 100644 index 000000000..020bda74b --- /dev/null +++ b/examples/balls.p8 @@ -0,0 +1,96 @@ +%import textio +%zeropage basicsafe + +; Note: this program is compatible with C64 and CX16. + +main { + + sub start() { + str input = ".........." + ubyte ballCount + ubyte[255] BX + ubyte[255] BY + ubyte[255] BC + ubyte[255] DX + ubyte[255] DY + + txt.print("number of balls (1-255)? ") + void txt.input_chars(input) + ballCount = conv.str2ubyte(input) + txt.fill_screen(81, 0) + + ; Setup Starting Ball Positions + ubyte lp + for lp in 0 to ballCount-1 { + BX[lp] = rnd() % txt.DEFAULT_WIDTH + BY[lp] = rnd() % txt.DEFAULT_HEIGHT + BC[lp] = rnd() & 15 + DX[lp] = rnd() & 1 + DY[lp] = rnd() & 1 + } + + ; start clock + c64.SETTIM(0,0,0) + + ; display balls + uword frame + for frame in 0 to 999 { + ; Loop though all balls clearing current spot and setting new spot + for lp in 0 to ballCount-1 { + + ; Clear existing Location the ball is at + txt.setclr(BX[lp], BY[lp], 0) + + if DX[lp] == 0 { ; TODO how is this optimized now? why does it still push a value on the stack? + if (BX[lp] == 0) + { + DX[lp] = 1 + } else { + BX[lp]=BX[lp]-1 + } + } else if DX[lp] == 1 { + if (BX[lp] == txt.DEFAULT_WIDTH-1) + { + BX[lp] = txt.DEFAULT_WIDTH-2 + DX[lp] = 0 + } else { + BX[lp]=BX[lp]+1 + } + } + if DY[lp] == 0 { + if (BY[lp] == 0) + { + DY[lp] = 1 + } else { + BY[lp]=BY[lp]-1 + } + } else if DY[lp] == 1 { + if (BY[lp] == txt.DEFAULT_HEIGHT-1) + { + BY[lp] = txt.DEFAULT_HEIGHT-2 + DY[lp] = 0 + } else { + BY[lp]=BY[lp]+1 + } + } + + ; Put the new ball possition + txt.setclr(BX[lp], BY[lp], BC[lp]) + } + + ;txt.plot(0,0) + ;txt.print_uw(frame) + } + + ; read clock + uword jiffies + %asm {{ + jsr c64.RDTIM + sta jiffies + stx jiffies+1 + }} + txt.print("\nbenchmark: ") + txt.print_uw(jiffies) + txt.print(" jiffies for 1000 frames.\n") + } +}