Skip to content

Commit a260fbb

Browse files
authored
Merge pull request #1099 from wilco2009/main
Add zx81sd architecture: ZX81 + SD81 Booster support
2 parents dab8799 + 1af6a11 commit a260fbb

18 files changed

Lines changed: 1823 additions & 90 deletions

File tree

‎examples/sd81/bounce_sd81.bas‎

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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

‎src/arch/zx81sd/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ fully working.
6363

6464
## Documentation
6565

66+
- **[doc/PORTING_GUIDE.md](doc/PORTING_GUIDE.md)** — step-by-step
67+
checklist for porting a Spectrum program to zx81sd. Start here.
6668
- **[doc/USAGE.md](doc/USAGE.md)** — how to compile a program, package
6769
it for the ZX81 and load it from the SD card.
6870
- **[doc/PRECAUTIONS.md](doc/PRECAUTIONS.md)** — what to keep in mind
6971
when writing or porting software for this architecture (memory map,
7072
sysvars, keyboard, things that do NOT exist here even though they do
7173
on a Spectrum).
74+
- **[doc/SYSVARS.md](doc/SYSVARS.md)** — full Spectrum ↔ zx81sd sysvar
75+
and I/O port (ULA, screen address) equivalence table.
7276
- **[doc/BASIC_CHANGES.md](doc/BASIC_CHANGES.md)** — what BASIC source
7377
changes were needed to port each official example in `examples/`
7478
(with the why for each one). Mandatory starting point before porting

‎src/arch/zx81sd/backend/main.py‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
# ---------------------------------------------------------------------------
1919

2020
# Memory map (flat, ORG $0000):
21-
# $0000-$00FF vectors.asm — RST vectors + padding up to $0100
21+
# $0000-$00FF RST vector table, emitted directly below (emit_prologue)
22+
# + padding up to $0100
2223
# $0100-$0FFF stage 2 bootstrap (prologue) + system routines
2324
# $1000-$7FFF ZX BASIC runtime + user code (28 KB)
2425
# $8000-$80FF runtime sysvars
@@ -27,7 +28,7 @@
2728
# $D800-$DAFF screen attributes
2829
# $E000-$FFFF block 7 — data banking (maps, sprites...)
2930

30-
_ORG = 0x0000 # the binary starts at $0000 (vectors.asm pads up to $0100)
31+
_ORG = 0x0000 # the binary starts at $0000 (RST vectors pad up to $0100)
3132
_STAGE2_ENTRY = 0x0100 # stage 2 bootstrap's entry point
3233

3334
_HEAP_ADDR = 0x8100 # heap in the data area ($8100-$BFFF)
@@ -47,6 +48,7 @@
4748
(3, 11), # block 3 -> page 11
4849
(4, 12), # block 4 -> page 12
4950
(5, 13), # block 5 -> page 13
51+
(7, 15), # block 7 -> page 15 (data banking: large static tables via explicit ORG)
5052
]
5153

5254
_SD81_PAGE_PORT = 0xE7 # memory mapper port (full mode: OUT (C), A)
@@ -98,7 +100,7 @@ def emit_prologue() -> list[str]:
98100
Program prologue for ZX81 + SD81 Booster.
99101
100102
Structure of the generated binary (ORG $0000):
101-
$0000-$00FF vectors.asm (RST vectors, included from sysvars.asm)
103+
$0000-$00FF RST vector table (emitted directly below)
102104
$0100 START_LABEL (stage 2 bootstrap's entry point)
103105
- mapping blocks 1-5 to their final pages
104106
- SP = $7FFF
@@ -155,8 +157,23 @@ def emit_prologue() -> list[str]:
155157
output.append("di")
156158
output.append("halt")
157159
output.append("org $0038")
158-
output.append("di") # $0038: RST $38 IM1 — permanent DI, never reached
159-
output.append("halt")
160+
# $0038: RST $38 (IM1 interrupt). Interrupts stay disabled for the
161+
# whole program, so this should never really be reached; if
162+
# something did reactivate them, behave like a real Z80 HALT
163+
# instead of hanging forever (di+halt): wait for the next VSYNC
164+
# pulse (port $AF, bits 6-1 = pulse counter, reset on read) and
165+
# return, resuming right after the RST $38 that got us here.
166+
# AF must be preserved: ported code that executes EI (Spectrum
167+
# habit) makes stray ZX81 INTs land here mid-computation, and
168+
# clobbering A/F corrupted arithmetic at random (see MAP.md,
169+
# oilpanic sprite(13,2) bug).
170+
output.append("push af")
171+
output.append(".core.__RST38_WAIT:")
172+
output.append("in a, ($AF)")
173+
output.append("and $7E")
174+
output.append("jr z, .core.__RST38_WAIT")
175+
output.append("pop af")
176+
output.append("ret")
160177
output.append("org $0066")
161178
output.append("retn") # $0066: NMI disabled, but the vector must exist
162179

0 commit comments

Comments
 (0)