zdebel

zdebel / sprites.a65

Last active 1 month ago

Like 0

revised this gist 1 month ago · d64f540

1 file changed, 263 insertions

sprites.a65 (file created)
@@ -0,0 +1,263 @@
1 + ; Sprite demo for the 65C02 + TMS9918 machine.
2 + ; Four 16x16 sprites bounce around the screen; positions are updated once per
3 + ; frame from the VBlank IRQ.
4 + ;
5 + ; Build: asm/build.sh (ca65 + ld65, output roms/sprites.bin)
6 +
7 + .setcpu "65C02"
8 +
9 + VDPD = $7F00 ; VDP data port
10 + VDPC = $7F01 ; VDP control/status port
11 +
12 + ; VRAM layout (matches the register values below)
13 + NAMETAB = $0800 ; R2 = $02
14 + COLTAB = $2000 ; R3 = $80
15 + PATTAB = $0000 ; R4 = $00
16 + SPRATTR = $1B00 ; R5 = $36
17 + SPRPAT = $3800 ; R6 = $07
18 +
19 + NSPR = 4
20 + MAXX = 240 ; 256 - 16
21 + MAXY = 176 ; 192 - 16
22 +
23 + .segment "ZEROPAGE"
24 + frame: .res 1 ; incremented by the IRQ handler
25 + ptr: .res 2 ; source pointer for vcopy
26 + cnt: .res 2 ; byte count for vcopy
27 + state: .res NSPR*4 ; sx[4], sy[4], dx[4], dy[4]
28 + sx = state
29 + sy = state + NSPR
30 + dx = state + NSPR*2
31 + dy = state + NSPR*3
32 +
33 + .segment "CODE"
34 +
35 + ; ----------------------------------------------------------------- macros
36 + .macro SETWRITE addr
37 + lda #<(addr)
38 + sta VDPC
39 + lda #(>(addr)) | $40
40 + sta VDPC
41 + .endmacro
42 +
43 + .macro SETREG reg, val
44 + lda #val
45 + sta VDPC
46 + lda #$80 | reg
47 + sta VDPC
48 + .endmacro
49 +
50 + ; ----------------------------------------------------------------- reset
51 + reset:
52 + sei
53 + cld
54 + ldx #$FF
55 + txs
56 +
57 + ; --- VDP registers: Graphics I, 16x16 sprites, display on, IRQ on
58 + SETREG 0, $00
59 + SETREG 1, $E2 ; 16K, display, IE, 16x16 sprites
60 + SETREG 2, NAMETAB >> 10
61 + SETREG 3, COLTAB >> 6
62 + SETREG 4, PATTAB >> 11
63 + SETREG 5, SPRATTR >> 7
64 + SETREG 6, SPRPAT >> 11
65 + SETREG 7, $04 ; backdrop: dark blue
66 +
67 + ; --- clear all 16 KB of VRAM
68 + SETWRITE 0
69 + ldx #$40
70 + ldy #0
71 + lda #0
72 + @clr: sta VDPD
73 + iny
74 + bne @clr
75 + dex
76 + bne @clr
77 +
78 + ; --- background patterns (tile 0 blank, tile 1 = dot)
79 + SETWRITE PATTAB
80 + lda #<tiles
81 + sta ptr
82 + lda #>tiles
83 + sta ptr+1
84 + lda #<(tiles_end - tiles)
85 + sta cnt
86 + lda #>(tiles_end - tiles)
87 + sta cnt+1
88 + jsr vcopy
89 +
90 + ; --- color table: 32 entries
91 + SETWRITE COLTAB
92 + ldx #32
93 + lda #$E4 ; gray on dark blue
94 + @col: sta VDPD
95 + dex
96 + bne @col
97 +
98 + ; --- name table: 768 x tile 1
99 + SETWRITE NAMETAB
100 + ldx #3
101 + ldy #0
102 + lda #1
103 + @nam: sta VDPD
104 + iny
105 + bne @nam
106 + dex
107 + bne @nam
108 +
109 + ; --- sprite patterns
110 + SETWRITE SPRPAT
111 + lda #<sprites
112 + sta ptr
113 + lda #>sprites
114 + sta ptr+1
115 + lda #<(sprites_end - sprites)
116 + sta cnt
117 + lda #>(sprites_end - sprites)
118 + sta cnt+1
119 + jsr vcopy
120 +
121 + ; --- initial sprite state (ROM -> zero page)
122 + ldx #NSPR*4-1
123 + @ini: lda init_state,x
124 + sta state,x
125 + dex
126 + bpl @ini
127 +
128 + cli
129 +
130 + ; ----------------------------------------------------------------- main loop
131 + main:
132 + lda frame
133 + @wait: cmp frame ; wait for the next VBlank IRQ
134 + beq @wait
135 + jsr move
136 + jsr write_sprites
137 + bra main
138 +
139 + ; ----------------------------------------------------------------- IRQ
140 + irq:
141 + pha
142 + lda VDPC ; read status: acknowledges the VDP interrupt
143 + inc frame
144 + pla
145 + rti
146 +
147 + nmi:
148 + rti
149 +
150 + ; ----------------------------------------------------------------- vcopy
151 + ; Copy cnt bytes from (ptr) to VRAM (write address already set).
152 + vcopy:
153 + ldy #0
154 + @loop: lda (ptr),y
155 + sta VDPD
156 + iny
157 + bne @nowrap
158 + inc ptr+1
159 + @nowrap:
160 + lda cnt
161 + bne @dec
162 + dec cnt+1
163 + @dec: dec cnt
164 + lda cnt
165 + ora cnt+1
166 + bne @loop
167 + rts
168 +
169 + ; ----------------------------------------------------------------- move
170 + ; Advance all sprites, bouncing off the screen edges.
171 + move:
172 + ldx #NSPR-1
173 + @next: ; --- X axis
174 + lda sx,x
175 + clc
176 + adc dx,x
177 + cmp #MAXX ; negative positions wrap to >= MAXX too
178 + bcc @xok
179 + lda dx,x ; hit an edge: reverse direction (negate)
180 + eor #$FF
181 + inc a
182 + sta dx,x
183 + bra @ypos
184 + @xok: sta sx,x
185 + @ypos: ; --- Y axis
186 + lda sy,x
187 + clc
188 + adc dy,x
189 + cmp #MAXY
190 + bcc @yok
191 + lda dy,x
192 + eor #$FF
193 + inc a
194 + sta dy,x
195 + bra @done
196 + @yok: sta sy,x
197 + @done: dex
198 + bpl @next
199 + rts
200 +
201 + ; ----------------------------------------------------------------- write_sprites
202 + ; Write the sprite attribute table (Y, X, name, color) + terminator.
203 + write_sprites:
204 + SETWRITE SPRATTR
205 + ldx #0
206 + @loop: lda sy,x
207 + sta VDPD
208 + lda sx,x
209 + sta VDPD
210 + lda spr_name,x
211 + sta VDPD
212 + lda spr_color,x
213 + sta VDPD
214 + inx
215 + cpx #NSPR
216 + bne @loop
217 + lda #$D0 ; end of sprite list
218 + sta VDPD
219 + rts
220 +
221 + ; ----------------------------------------------------------------- data
222 + .segment "RODATA"
223 +
224 + ; sx[4], sy[4], dx[4], dy[4] (negative speeds as two's complement)
225 + init_state:
226 + .byte 20, 100, 180, 60 ; sx
227 + .byte 20, 120, 60, 150 ; sy
228 + .byte 1, $FE, 2, $FF ; dx: +1, -2, +2, -1
229 + .byte 1, 1, $FF, 2 ; dy: +1, +1, -1, +2
230 +
231 + spr_name: .byte 0, 4, 8, 12 ; 16x16 pattern numbers (multiples of 4)
232 + spr_color: .byte 8, 10, 3, 15 ; red, yellow, green, white
233 +
234 + tiles:
235 + .byte 0,0,0,0,0,0,0,0 ; tile 0: blank
236 + .byte 0,0,0,%00011000,%00011000,0,0,0 ; tile 1: dot
237 + tiles_end:
238 +
239 + ; 16x16 sprite: 16 rows of 16 bits. VDP order is: left-top, left-bottom,
240 + ; right-top, right-bottom (8 bytes each).
241 + .macro SPRITE16 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15
242 + .byte >r0,>r1,>r2,>r3,>r4,>r5,>r6,>r7
243 + .byte >r8,>r9,>r10,>r11,>r12,>r13,>r14,>r15
244 + .byte <r0,<r1,<r2,<r3,<r4,<r5,<r6,<r7
245 + .byte <r8,<r9,<r10,<r11,<r12,<r13,<r14,<r15
246 + .endmacro
247 +
248 + sprites:
249 + ; 0: filled circle
250 + SPRITE16 %0000011111100000, %0001111111111000, %0011111111111100, %0111111111111110, %0111111111111110, %1111111111111111, %1111111111111111, %1111111111111111, %1111111111111111, %1111111111111111, %1111111111111111, %0111111111111110, %0111111111111110, %0011111111111100, %0001111111111000, %0000011111100000
251 + ; 1: hollow box
252 + SPRITE16 %1111111111111111, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1000000000000001, %1111111111111111
253 + ; 2: diamond
254 + SPRITE16 %0000000110000000, %0000001111000000, %0000011111100000, %0000111111110000, %0001111111111000, %0011111111111100, %0111111111111110, %1111111111111111, %1111111111111111, %0111111111111110, %0011111111111100, %0001111111111000, %0000111111110000, %0000011111100000, %0000001111000000, %0000000110000000
255 + ; 3: face
256 + SPRITE16 %0000011111100000, %0001100000011000, %0010000000000100, %0100000000000010, %0100011001100010, %1000011001100001, %1000000000000001, %1000000000000001, %1000000000000001, %1000100000010001, %1000011111100001, %0100000000000010, %0100000000000010, %0010000000000100, %0001100000011000, %0000011111100000
257 + sprites_end:
258 +
259 + ; ----------------------------------------------------------------- vectors
260 + .segment "VECTORS"
261 + .word nmi
262 + .word reset
263 + .word irq