-
Notifications
You must be signed in to change notification settings - Fork 0
/
bouncing-toad.s
283 lines (244 loc) · 7.06 KB
/
bouncing-toad.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
; PPU control register
; 7 bit 0
; ---- ----
; VPHB SINN
; |||| ||||
; |||| ||++- Base nametable address
; |||| || (0 = $2000; 1 = $2400; 2 = $2800; 3 = $2C00)
; |||| |+--- VRAM address increment per CPU read/write of PPUDATA
; |||| | (0: add 1, going across; 1: add 32, going down)
; |||| +---- Sprite pattern table address for 8x8 sprites
; |||| (0: $0000; 1: $1000; ignored in 8x16 mode)
; |||+------ Background pattern table address (0: $0000; 1: $1000)
; ||+------- Sprite size (0: 8x8 pixels; 1: 8x16 pixels – see PPU OAM#Byte 1)
; |+-------- PPU master/slave select
; | (0: read backdrop from EXT pins; 1: output color on EXT pins)
; +--------- Generate an NMI at the start of the
; vertical blanking interval (0: off; 1: on)
PPUCTRL = $2000
; PPU Mask register: controls the rendering of sprites
; 7 bit 0
; ---- ----
; BGRs bMmG
; |||| ||||
; |||| |||+- Greyscale (0: normal color, 1: produce a greyscale display)
; |||| ||+-- 1: Show background in leftmost 8 pixels of screen, 0: Hide
; |||| |+--- 1: Show sprites in leftmost 8 pixels of screen, 0: Hide
; |||| +---- 1: Show background
; |||+------ 1: Show sprites
; ||+------- Emphasize red (green on PAL/Dendy)
; |+-------- Emphasize green (red on PAL/Dendy)
; +--------- Emphasize blue
PPUMASK = $2001
; PPU Status register: reflects the state of various functions inside the PPU
; 7 bit 0
; ---- ----
; VSO. ....
; |||| ||||
; |||+-++++- PPU open bus. Returns stale PPU bus contents.
; ||+------- Sprite overflow. The intent was for this flag to be set
; || whenever more than eight sprites appear on a scanline, but a
; || hardware bug causes the actual behavior to be more complicated
; || and generate false positives as well as false negatives; see
; || PPU sprite evaluation. This flag is set during sprite
; || evaluation and cleared at dot 1 (the second dot) of the
; || pre-render line.
; |+-------- Sprite 0 Hit. Set when a nonzero pixel of sprite 0 overlaps
; | a nonzero background pixel; cleared at dot 1 of the pre-render
; | line. Used for raster timing.
; +--------- Vertical blank has started (0: not in vblank; 1: in vblank).
; Set at dot 1 of line 241 (the line *after* the post-render
; line); cleared after reading $2002 and at dot 1 of the
; pre-render line.
PPUSTATUS = $2002
; Object Attribute Memory address: write the address of OAM to be accessed here.
OAMADDR = $2003
; PPU scrolling position register: used to change the scroll position
PPUSCROLL = $2005
; PPU address register
PPUADDR = $2006
; PPU data port register: VRAM read/write data register
PPUDATA = $2007
; OAM data register
OAMDMA = $4014
.segment "HEADER"
.byte $4e, $45, $53, $1a ; Magic string that always begins an iNES header
.byte $02 ; Number of 16KB PRG-ROM banks
.byte $01 ; Number of 8KB CHR-ROM banks
.byte %00000001 ; Vertical mirroring, no save RAM, no mapper
.byte %00000000 ; No special-case flags set, no mapper
.byte $00 ; No PRG-RAM present
.byte $00 ; NTSC format
.segment "ZEROPAGE"
toad_x: .res 1 ; reserve 1 byte of memory
toad_y: .res 1
toad_dir: .res 1
.segment "STARTUP"
.segment "CODE"
.proc irq_handler
rti
.endproc
;;; NMI handler - ---------------------------------------------------------+
.proc nmi_handler
lda #$02
sta OAMDMA
lda #$00
sta OAMADDR
jsr update_toad
jsr draw_toad
lda #$00
sta $2005
sta $2005
rti
.endproc
;;; -----------------------------------------------------------------------+
;;; Reset handler - -------------------------------------------------------+
.proc reset_handler
sei
cld
ldx #$00
stx PPUCTRL
stx PPUMASK
vblankwait:
bit PPUSTATUS
bpl vblankwait
; Initialize zeropage values here
lda #$a0
sta toad_y
lda #$44
sta toad_x
.endproc
;;; -----------------------------------------------------------------------+
;;; main - load palettes, enable NMIs and turn on the screen --------------+
.proc main
;; Prepare PPU to load a palette
ldx PPUSTATUS
ldx #$3f
stx PPUADDR
ldx #$00
stx PPUADDR
;; Load all 8 4-color palettes into PPU
ldx #$00
LoadPalette:
lda PaletteData, x
sta PPUDATA
inx
cpx #$20
bne LoadPalette
vblankwait: ; wait for another vblank before continuing
bit PPUSTATUS
bpl vblankwait
lda #%10010000 ; turn on NMIs, sprites use first pattern table
sta PPUCTRL
lda #%00011110 ; turn on screen
sta PPUMASK
forever:
jmp forever
.endproc
;;; +----------------------------------------------------------------------+
;;; update_toad - update position of toad for movement --------------------+
.proc update_toad
; save registers
php
pha
txa
pha
tya
pha
;; Check for boundary conditions
lda toad_x
cmp #$e0
bcc NoCollideRight ; branch if away from the right edge
lda #$00
sta toad_dir ; set direction to left
jmp DirectionSet
NoCollideRight:
lda toad_x
cmp #$0a
bcs DirectionSet ; branch if away from the left edge
lda #$01
sta toad_dir ; set direction to right
DirectionSet:
lda toad_dir
cmp #$01
beq MoveRight
dec toad_x
jmp Done
MoveRight:
inc toad_x
Done:
; restore registers
pla
tay
pla
tax
pla
plp
rts
.endproc
;;; -----------------------------------------------------------------------+
;;; draw_toad - to render the toad sprite at toad_y, toad_x ---------------+
.proc draw_toad
; save registers
php
pha
txa
pha
tya
pha
; write tile numbers
lda #$00
sta $0201
lda #$01
sta $0205
lda #$10
sta $0209
lda #$11
sta $020d
; write tile attributes
lda #$00
sta $0202
sta $0206
sta $020a
sta $020e
; write tile locations
lda toad_y
sta $0200 ; top left tile
sta $0204 ; top right tile
clc
adc #$08 ; add 8 to move to the next row
sta $0208 ; bottom left tile
sta $020c ; bottom right tile
lda toad_x
sta $0203
sta $020b
clc
adc #$08
sta $0207
sta $020f
; restore registers
pla
tay
pla
tax
pla
plp
rts
.endproc
;;; +----------------------------------------------------------------------+
;;; Palette Data goes here ------------------------------------------------+
PaletteData:
.byte $00, $0F, $00, $10, $00, $0A, $15, $01, $00, $29, $28, $27, $00, $34, $24, $14 ;background palettes
.byte $31, $0F, $15, $30, $00, $0F, $11, $30, $00, $0F, $30, $27, $00, $3C, $2C, $1C ;sprite palettes
;;; -----------------------------------------------------------------------+
;;; Sprite Data goes here -------------------------------------------------+
ToadSpriteData:
.byte $40, $00, $00, $40
.byte $40, $01, $00, $48
.byte $48, $10, $00, $40
.byte $48, $11, $00, $48
;;; -----------------------------------------------------------------------+
.segment "VECTORS"
.addr nmi_handler, reset_handler, irq_handler
.segment "CHARS"
.incbin "cart-rom.chr"