From b125901717a448031db24ad7b3b51e63529fc74e Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 4 Sep 2023 23:54:13 +0200 Subject: [PATCH] added cx16 plasma example --- compiler/test/TestCompilerOnExamples.kt | 1 + examples/cx16/plasma.p8 | 108 ++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 examples/cx16/plasma.p8 diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index 73427e4c2..a7b58d7eb 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -118,6 +118,7 @@ class TestCompilerOnExamplesCx16: FunSpec({ "mandelbrot", "mandelbrot-gfx-colors", "multipalette", + "plasma", "rasterbars", "sincos", "snow", diff --git a/examples/cx16/plasma.p8 b/examples/cx16/plasma.p8 new file mode 100644 index 000000000..91b1e8f95 --- /dev/null +++ b/examples/cx16/plasma.p8 @@ -0,0 +1,108 @@ +%import syslib +%import textio +%import math +%zeropage basicsafe + +; converted from plasma test program for cc65. +; which is (w)2001 by groepaz/hitmen +; +; Cleanup and porting to C by Ullrich von Bassewitz. +; See https://github.com/cc65/cc65/tree/master/samples/cbm/plasma.c +; +; Converted to prog8 by Irmen de Jong. + + +main { + const uword VERA_CHARSET = $f000 ; $1f000 + const uword VERA_TXTSCREEN = $b000 ; %1b000 + + sub start() { + txt.color(6) + txt.clear_screen() + makechar() + + uword frames = 0 + cbm.SETTIM(0,0,0) + + while cbm.GETIN()==0 { + doplasma() + frames ++ + } + + uword jiffies = cbm.RDTIM16() + + ; restore screen and displays stats + + cx16.screen_set_charset(2, 0) + txt.color(7) + txt.clear_screen() + txt.print("time in jiffies: ") + txt.print_uw(jiffies) + txt.print("\nframes: ") + txt.print_uw(frames) + uword fps = (frames*60)/jiffies + txt.print("\nfps: ") + txt.print_uw(fps) + txt.nl() + } + + ; several variables outside of doplasma to make them retain their value + ubyte c1A + ubyte c1B + ubyte c2A + ubyte c2B + + sub doplasma() { + ubyte[txt.DEFAULT_WIDTH] xbuf + ubyte[txt.DEFAULT_HEIGHT] ybuf + ubyte c1a = c1A + ubyte c1b = c1B + ubyte c2a = c2A + ubyte c2b = c2B + ubyte @zp x + ubyte @zp y + + for y in 0 to txt.DEFAULT_HEIGHT-1 { + ybuf[y] = math.sin8u(c1a) + math.sin8u(c1b) + c1a += 4 + c1b += 9 + } + c1A += 3 + c1B -= 5 + for x in 0 to txt.DEFAULT_WIDTH-1 { + xbuf[x] = math.sin8u(c2a) + math.sin8u(c2b) + c2a += 3 + c2b += 7 + } + c2A += 2 + c2B -= 3 + + ; use vera auto increment writes to avoid slow txt.setchr(x, y, xbuf[x] + ybuf[y]) + for y in 0 to txt.DEFAULT_HEIGHT-1 { + cx16.vaddr_autoincr(1, VERA_TXTSCREEN + y*$0100, 0, 2) + for x in 0 to txt.DEFAULT_WIDTH-1 { + cx16.VERA_DATA0 = xbuf[x] + ybuf[y] + } + } + } + + sub makechar() { + ubyte[8] bittab = [ $01, $02, $04, $08, $10, $20, $40, $80 ] + ubyte c + cx16.vaddr_autoincr(1, VERA_CHARSET, 0, 1) + for c in 0 to 255 { + ubyte @zp s = math.sin8u(c) ; chance + ubyte i + ; for all the pixels in the 8x8 character grid, determine (with a rnd chance) if they should be on or off + for i in 0 to 7 { + ubyte b=0 + ubyte @zp ii + for ii in 0 to 7 { + if math.rnd() > s + b |= bittab[ii] + } + cx16.VERA_DATA0 = b + } + } + } +}