Skip to content

Commit 3b46347

Browse files
committed
Part 4B: introducing EQU constants instead of magic numbers
1 parent c971b3e commit 3b46347

File tree

2 files changed

+351
-23
lines changed

2 files changed

+351
-23
lines changed

SpecBong.asm

+35-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
OPT --zxnext=cspect ;DEBUG enable break/exit fake instructions of CSpect (remove for real board)
1919

20+
; ------------------------------------------------------------------------------------
21+
; Part 4B - changing magic numbers in source to symbolic constants
22+
; ------------------------------------------------------------------------------------
23+
; the many EQU directives in the constants file are included here, the EQU does not
24+
; emit any machine code byte, just assigns certain symbolic name the numeric value
25+
; John is somewhat afraid of them, as the "names" hide the values, and I can agree
26+
; on that. So my constants for next registers and ports use suffix with the actual
27+
; value, like `ZXN_DMA_P_6B` (port $6B) or `CLIP_LAYER2_NR_18` (NextReg $18).
28+
INCLUDE "constants.i.asm"
29+
2030
STRUCT S_SPRITE_4B_ATTR ; helper structure to work with 4B sprites attributes
2131
x BYTE 0 ; X0:7
2232
y BYTE 0 ; Y0:7
@@ -43,26 +53,28 @@ start:
4353
; disable interrupts, we will avoid using them to keep code simpler to understand
4454
di
4555
; make the Layer 2 visible and reset some registers (should be reset by NEXLOAD, but to be safe)
46-
nextreg $69,$80 ; Layer 2 visible, ULA bank 5, Timex mode 0
47-
nextreg $15,$01 ; LoRes off, layer priority SLU, sprites visible
48-
nextreg $12,9 ; visible Layer 2 starts at bank 9
49-
nextreg $70,0 ; 256x192x8 Layer 2 mode, L2 palette offset +0
50-
nextreg $16,0 ; Layer 2 X,Y offset = [0,0]
51-
nextreg $71,0 ; including the new NextReg 0x71 for cores 3.0.6+
52-
nextreg $17,0
56+
nextreg DISPLAY_CONTROL_NR_69,$80 ; Layer 2 visible, ULA bank 5, Timex mode 0
57+
nextreg SPRITE_CONTROL_NR_15,$01 ; LoRes off, layer priority SLU, sprites visible
58+
nextreg LAYER2_RAM_BANK_NR_12,9 ; visible Layer 2 starts at bank 9
59+
nextreg LAYER2_CONTROL_NR_70,0 ; 256x192x8 Layer 2 mode, L2 palette offset +0
60+
nextreg LAYER2_XOFFSET_NR_16,0 ; Layer 2 X,Y offset = [0,0]
61+
nextreg LAYER2_XOFFSET_MSB_NR_71,0 ; including the new NextReg 0x71 for cores 3.0.6+
62+
nextreg LAYER2_YOFFSET_NR_17,0
5363

5464
; setup Layer 2 palette - map palette data to $E000 region, to process them
55-
nextreg $57,$$BackGroundPalette ; map the memory with palette to the $E000..$FFFF
65+
nextreg MMU7_E000_NR_57,$$BackGroundPalette ; map the memory with palette
5666
; the "$$" is special operator of sjasmplus to get memory page of particular
5767
; label (the 8kiB memory page)
58-
nextreg $43,%0'001'0'0'0'0 ; write to Layer 2 palette, select first palettes
59-
nextreg $40,0 ; color index
60-
ld b,0 ; 256 colors (loop counter)
61-
ld hl,BackGroundPalette ; address of first byte of 256x 24 bit color def.
68+
nextreg PALETTE_CONTROL_NR_43,%0'001'0'0'0'0 ; write to Layer 2 palette, select first palettes
69+
nextreg PALETTE_INDEX_NR_40,0 ; color index
70+
ld b,0 ; 256 colors (loop counter)
71+
ld hl,BackGroundPalette ; address of first byte of 256x 24 bit color def.
6272
; calculate 9bit color from 24bit value for every color
6373
; -> will produce pair of bytes -> write that to nextreg $44
6474
SetPaletteLoop:
6575
; TGA palette data are three bytes per color, [B,G,R] order in memory
76+
; so palette data are: BBBbbbbb GGGggggg RRRrrrrr
77+
; (B/G/R = 3 bits for Next, b/g/r = 5bits too fine for Next, thrown away)
6678
; first byte to calculate: RRR'GGG'BB
6779
ld a,(hl) ; Blue
6880
inc hl
@@ -83,12 +95,12 @@ SetPaletteLoop:
8395
and %111'000'00 ; top three red bits
8496
or d ; add green bits
8597
or e ; add blue bits
86-
nextreg $44,a ; RRR'GGG'BB
98+
nextreg PALETTE_VALUE_9BIT_NR_44,a ; RRR'GGG'BB
8799
; second byte is: p000'000B (priority will be 0 in this app)
88100
xor a
89101
rl c ; move top bit from C to bottom bit in A (Blue third bit)
90102
rla
91-
nextreg $44,a ; p000'000B p=0 in this image always
103+
nextreg PALETTE_VALUE_9BIT_NR_44,a ; p000'000B p=0 in this image always
92104
djnz SetPaletteLoop
93105

94106
; the image pixel data are already in the correct banks 9,10,11 - loaded by NEX loader
@@ -105,14 +117,14 @@ SetPaletteLoop:
105117

106118
; upload the sprite gfx patterns to patterns memory (from regular memory - loaded by NEX loader)
107119
; preconfigure the Next for uploading patterns from slot 0
108-
ld bc,$303B
120+
ld bc,SPRITE_STATUS_SLOT_SELECT_P_303B
109121
xor a
110122
out (c),a ; select slot 0 for patterns (selects also index 0 for attributes)
111123
; we will map full 16kiB to memory region $C000..$FFFF (to pages 25,26 with sprite pixels)
112-
nextreg $56,$$SpritePixelData ; C000..DFFF <- 8k page 25
113-
nextreg $57,$$SpritePixelData+1 ; E000..FFFF <- 8k page 26
124+
nextreg MMU6_C000_NR_56,$$SpritePixelData ; C000..DFFF <- 8k page 25
125+
nextreg MMU7_E000_NR_57,$$SpritePixelData+1 ; E000..FFFF <- 8k page 26
114126
ld hl,SpritePixelData ; HL = $C000 (beginning of the sprite pixels)
115-
ld bc,$5B ; sprite pattern-upload I/O port, B=0 (inner loop counter)
127+
ld bc,SPRITE_PATTERN_P_5B ; sprite pattern-upload I/O port, B=0 (inner loop counter)
116128
ld a,64 ; 64 patterns (outer loop counter), each pattern is 256 bytes long
117129
UploadSpritePatternsLoop:
118130
; upload 256 bytes of pattern data (otir increments HL and decrements B until zero)
@@ -169,11 +181,11 @@ GameLoop:
169181
call WaitForScanlineUnderUla
170182
; upload sprite data from memory array to the actual HW sprite engine
171183
; reset sprite index for upload
172-
ld bc,$303B
184+
ld bc,SPRITE_STATUS_SLOT_SELECT_P_303B
173185
xor a
174186
out (c),a ; select slot 0 for sprite attributes
175187
ld hl,Sprites
176-
ld bc,$57 ; B = 0 (repeat 256x), C = sprite pattern-upload I/O port
188+
ld bc,SPRITE_ATTRIBUTE_P_57 ; B = 0 (repeat 256x), C = sprite pattern-upload I/O port
177189
; out 512 bytes in total (whole sprites buffer)
178190
otir
179191
otir
@@ -248,10 +260,10 @@ WaitForScanlineUnderUla:
248260
ld (TotalFrames+2),hl
249261
.totalFramesUpdated:
250262
; read NextReg $1F - LSB of current raster line
251-
ld bc,$243B
252-
ld a,$1F
263+
ld bc,TBBLUE_REGISTER_SELECT_P_243B
264+
ld a,RASTER_LINE_LSB_NR_1F
253265
out (c),a ; select NextReg $1F
254-
inc b
266+
inc b ; BC = TBBLUE_REGISTER_ACCESS_P_253B
255267
; if already at scanline 192, then wait extra whole frame (for super-fast game loops)
256268
.cantStartAt192:
257269
in a,(c) ; read the raster line LSB

0 commit comments

Comments
 (0)