|
| 1 | +' bounce_sd81.bas — SD81 Booster double buffer (dbuf) demo, port $E7 |
| 2 | +' |
| 3 | +' ZX BASIC port of bounce.asm (SD81-Booster/EXAMPLES/DBUF), reworked for |
| 4 | +' the zxbasic runtime: |
| 5 | +' - the screen ($C000), Superfast HiRes Spectrum mode and Chroma81 |
| 6 | +' colour are already set up by tools/boot1.asm for every zx81sd |
| 7 | +' program -- no need to POKE 2043/2044/2045 or write to $7FEF here. |
| 8 | +' - screen + attributes are already cleared at program start by |
| 9 | +' runtime/bootstrap.asm -- no need to clear them here either. |
| 10 | +' - memory-mapped IO (POKE 2056/2057) is disabled by boot1.asm before |
| 11 | +' the compiled program starts, so the double buffer is controlled |
| 12 | +' through port $E7's pseudo-block-8 path (SD81DBufOn/SD81DBufOff, |
| 13 | +' see stdlib/dbuf.bas) instead of the classic POKE 2057. |
| 14 | +' |
| 15 | +' A 16x16 ball bounces around the screen. Each frame: wait for VSYNC, |
| 16 | +' erase the ball, waste ~6ms on purpose (simulates heavy drawing that |
| 17 | +' crosses the beam), move, redraw. |
| 18 | +' - dbuf ON (green border): solid image, no flicker. |
| 19 | +' - dbuf OFF (red border): visible flicker/tearing. |
| 20 | +' SPACE toggles the double buffer live; M freezes/unfreezes movement; |
| 21 | +' Q quits. |
| 22 | +' |
| 23 | +' This is also the first real test of the port $E7 pseudo-block-8 dbuf |
| 24 | +' path (only the POKE 2057 path had been validated on real hardware |
| 25 | +' before this): see stdlib/dbuf.bas for the front-buffer-block notes. |
| 26 | + |
| 27 | +#include <dbuf.bas> |
| 28 | + |
| 29 | +dim bally as ubyte |
| 30 | +dim ballx as ubyte |
| 31 | +dim dy as byte |
| 32 | +dim dx as byte |
| 33 | +dim moven as ubyte |
| 34 | +dim dbufst as ubyte |
| 35 | +dim k$ as string |
| 36 | + |
| 37 | +bally = 80 |
| 38 | +ballx = 14 |
| 39 | +dy = 2 |
| 40 | +dx = 1 |
| 41 | +moven = 1 |
| 42 | +dbufst = 1 |
| 43 | + |
| 44 | +' PaintBall(y, x, fillVal): fills 16 rows x 2 bytes at screen row y, |
| 45 | +' byte-column x, with fillVal (0 = erase, 255 = draw). Same pixel |
| 46 | +' address formula as the Spectrum, but with $C0 as the screen's base |
| 47 | +' high byte instead of Spectrum's $40 (zx81sd's screen is fixed at |
| 48 | +' $C000, set once by boot1.asm). |
| 49 | +sub PaintBall(y as ubyte, x as ubyte, fillVal as ubyte) |
| 50 | + asm |
| 51 | + PROC |
| 52 | + LOCAL PB_LOOP, PB_PIXAD, PB_DONE |
| 53 | + |
| 54 | + ld a, (ix + 5) ; y |
| 55 | + ld d, a |
| 56 | + ld a, (ix + 7) ; x |
| 57 | + ld e, a |
| 58 | + ld a, (ix + 9) ; fillVal |
| 59 | + ld c, a |
| 60 | + ld b, 16 |
| 61 | + |
| 62 | +PB_LOOP: |
| 63 | + push bc |
| 64 | + push de |
| 65 | + call PB_PIXAD |
| 66 | + ld (hl), c |
| 67 | + inc l |
| 68 | + ld (hl), c |
| 69 | + pop de |
| 70 | + pop bc |
| 71 | + inc d |
| 72 | + djnz PB_LOOP |
| 73 | + jr PB_DONE |
| 74 | + |
| 75 | +PB_PIXAD: |
| 76 | + ld a, d |
| 77 | + and $C0 |
| 78 | + rrca |
| 79 | + rrca |
| 80 | + rrca |
| 81 | + ld h, a |
| 82 | + ld a, d |
| 83 | + and 7 |
| 84 | + or h |
| 85 | + or $C0 ; screen base high byte ($C000) |
| 86 | + ld h, a |
| 87 | + ld a, d |
| 88 | + and $38 |
| 89 | + add a, a |
| 90 | + add a, a |
| 91 | + or e |
| 92 | + ld l, a |
| 93 | + ret |
| 94 | + |
| 95 | +PB_DONE: |
| 96 | + ENDP |
| 97 | + end asm |
| 98 | +end sub |
| 99 | + |
| 100 | +' ~6ms busy loop at 3.25MHz: simulates a heavy redraw that crosses the |
| 101 | +' video beam, so the difference between dbuf ON/OFF is visible. |
| 102 | +sub Delay |
| 103 | + asm |
| 104 | + PROC |
| 105 | + LOCAL DL_LOOP |
| 106 | + |
| 107 | + ld bc, 1024 |
| 108 | +DL_LOOP: |
| 109 | + dec bc |
| 110 | + ld a, b |
| 111 | + or c |
| 112 | + jr nz, DL_LOOP |
| 113 | + |
| 114 | + ENDP |
| 115 | + end asm |
| 116 | +end sub |
| 117 | + |
| 118 | +' On bounce, the position is left unchanged for this frame (matching |
| 119 | +' bounce.asm) and only the direction flips; it moves next frame. |
| 120 | +' |
| 121 | +' Kept entirely in ubyte (8-bit, mod-256) arithmetic on purpose, same |
| 122 | +' as bounce.asm's "add a,b / cp 177": mixing ubyte+byte and widening to |
| 123 | +' integer sign-extends the 8-bit sum based on its own bit 7 (bally=126, |
| 124 | +' dy=2 -> 8-bit sum 128 gets read as -128, not +128), which is wrong |
| 125 | +' here since bally is genuinely unsigned. Comparing unsigned in ubyte |
| 126 | +' space sidesteps that entirely. |
| 127 | +sub Move |
| 128 | + dim newY as ubyte |
| 129 | + dim newX as ubyte |
| 130 | + |
| 131 | + if moven <> 0 then |
| 132 | + newY = bally + dy |
| 133 | + if newY >= 177 then |
| 134 | + dy = -dy |
| 135 | + else |
| 136 | + bally = newY |
| 137 | + end if |
| 138 | + |
| 139 | + newX = ballx + dx |
| 140 | + if newX >= 31 then |
| 141 | + dx = -dx |
| 142 | + else |
| 143 | + ballx = newX |
| 144 | + end if |
| 145 | + end if |
| 146 | +end sub |
| 147 | + |
| 148 | +' Returns 0 if Q was pressed (caller should quit), 1 otherwise. |
| 149 | +' SPACE toggles dbuf live; M freezes/unfreezes movement. |
| 150 | +function Keys() as ubyte |
| 151 | + k$ = INKEY$ |
| 152 | + |
| 153 | + if k$ = " " then |
| 154 | + dbufst = 1 - dbufst |
| 155 | + if dbufst <> 0 then |
| 156 | + SD81DBufOn(5) |
| 157 | + border 4 |
| 158 | + else |
| 159 | + SD81DBufOff() |
| 160 | + border 2 |
| 161 | + end if |
| 162 | + do |
| 163 | + loop while INKEY$ <> "" |
| 164 | + end if |
| 165 | + |
| 166 | + if k$ = "m" or k$ = "M" then |
| 167 | + moven = 1 - moven |
| 168 | + do |
| 169 | + loop while INKEY$ <> "" |
| 170 | + end if |
| 171 | + |
| 172 | + if k$ = "q" or k$ = "Q" then |
| 173 | + return 0 |
| 174 | + end if |
| 175 | + |
| 176 | + return 1 |
| 177 | +end function |
| 178 | + |
| 179 | +SD81DBufOn(5) |
| 180 | +border 4 |
| 181 | + |
| 182 | +do |
| 183 | + SD81WaitVSync() |
| 184 | + PaintBall(bally, ballx, 0) |
| 185 | + Delay |
| 186 | + Move |
| 187 | + PaintBall(bally, ballx, 255) |
| 188 | +loop while Keys() <> 0 |
| 189 | + |
| 190 | +SD81DBufOff() |
| 191 | +border 7 |
0 commit comments