-
Notifications
You must be signed in to change notification settings - Fork 20
/
start.asm
382 lines (302 loc) · 5.26 KB
/
start.asm
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
; This file is part of Pwn Adventure Z.
; Pwn Adventure Z is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; Pwn Adventure Z is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with Pwn Adventure Z. If not, see <http://www.gnu.org/licenses/>.
.include "defines.inc"
.segment "FIXED"
PROC start
; Initialize CPU state
sei
cld
ldx #$ff ; Set up stack
txs
inx ; X = 0
; Disable interrupts until we are ready
stx PPUCTRL ; Disable NMI
stx PPUMASK ; Disable rendering
stx DMC_FREQ ; Disable IRQ
; Need to wait for PPU init, wait for the first vblank interval
bit PPUSTATUS ; Clear initial vblank flag
wait1:
bit PPUSTATUS
bpl wait1
; One vblank interval has passed, but we need to wait another before actually starting to render. Clear
; RAM now during this period
; X is 0, set A to 0 as well and perform a RAM clear
txa
clearmem:
sta $0000, x
sta $0100, x
sta $0200, x
sta $0300, x
sta $0400, x
sta $0500, x
sta $0600, x
sta $0700, x
inx
bne clearmem
; Ensure mapper has bank #0 at 0x8000-0xc000
jsr reset_mapper
; Initialize APU
lda #$f
sta SND_CHN
ldy #$0
apuinit:
lda apu_init_regs, y
sta $4000, y
iny
cpy #$18
bne apuinit
; Wait for PPU to be fully ready
wait2:
bit PPUSTATUS
bpl wait2
; Enable vblank interrupts
lda PPUSTATUS
lda #PPUCTRL_ENABLE_NMI
sta ppu_settings
sta PPUCTRL
; Ensure CHR-RAM is cleared
jsr clear_tiles
; Start the game
jsr main
jmp start
apu_init_regs:
.byte $30, $08, $00, $00
.byte $30, $08, $00, $00
.byte $80, $00, $00, $00
.byte $30, $00, $00, $00
.byte $00, $00, $00, $00
.byte $00, $0f, $00, $40
.endproc
PROC nmi
pha
txa
pha
tya
pha
lda #1
sta in_nmi
; Update vblank count so that waiters will wake up
ldx vblank_count
inx
stx vblank_count
; Don't do anything with sprites when rendering is off
lda rendering_enabled
beq no_rendering
; Copy updated sprites to the PPU using DMA
lda #0
sta OAMADDR
lda #>sprites
sta OAMDMA
lda #0
sta in_nmi
pla
tay
pla
tax
pla
rti
no_rendering:
; When rendering is disabled, update audio in the NMI handler
jsr update_audio
lda #0
sta in_nmi
pla
tay
pla
tax
pla
rti
.endproc
PROC wait_for_vblank
pha
txa
pha
tya
pha
; Update audio, unless rendering is disabled (in that case the audio update is
; performed in the NMI handler to simply processing)
lda rendering_enabled
beq noaudioupdate
jsr update_audio
noaudioupdate:
; Update frames played
ldx time_played + 5
inx
stx time_played + 5
cpx #59
bcc timeupdatedone
lda #0
sta time_played + 5
; Update seconds played
ldx time_played + 4
inx
stx time_played + 4
cpx #59
bcc timeupdatedone
lda #0
sta time_played + 4
; Update minutes played
ldx time_played + 3
inx
stx time_played + 3
cpx #9
bcc timeupdatedone
lda #0
sta time_played + 3
ldx time_played + 2
inx
stx time_played + 2
cpx #5
bcc timeupdatedone
lda #0
sta time_played + 2
; Update hours played
ldx time_played + 1
inx
stx time_played + 1
cpx #9
bcc timeupdatedone
lda #0
sta time_played + 1
ldx time_played
inx
stx time_played
timeupdatedone:
lda PPUSTATUS
lda vblank_count
loop:
cmp vblank_count
beq loop
pla
tay
pla
tax
pla
rts
.endproc
PROC wait_for_frame_count
loop:
jsr wait_for_vblank
dex
bne loop
rts
.endproc
PROC call_ptr
jmp (ptr)
.endproc
PROC call_temp
jmp (temp)
.endproc
PROC irq
rti
.endproc
PROC zero_unused_stack_page
tsx
lda #0
loop:
dex
sta $0100, x
cpx #0
bne loop
rts
.endproc
PROC nothing
rts
.endproc
.zeropage
VAR ptr
.word 0
VAR temp
.word 0
VAR arg0
.byte 0
VAR arg1
.byte 0
VAR arg2
.byte 0
VAR arg3
.byte 0
VAR arg4
.byte 0
VAR arg5
.byte 0
VAR rendering_enabled
.byte 0
VAR ppu_settings
.byte 0
VAR vblank_count
.byte 0
VAR in_nmi
.byte 0
.segment "SPRITE"
; Map generator will use sprite memory as a working buffer, as the screen
; is off during map generation
VAR map_gen_buf
; Define RAM buffer for sprites that will be updated using DMA during vblank
VAR sprites
.repeat 256
.byte 0
.endrepeat
.segment "STACK"
VAR scratch ; 32 bytes of temporary space
.repeat $20
.byte 0
.endrepeat
.segment "VECTORS"
VAR version_hash
.word 0, 0
.word nmi
.word start
.word irq
.segment "CHR1"
.segment "CHR2"
.segment "CHR3"
.segment "CHR4"
.segment "AUDIO0"
.segment "AUDIO1"
.segment "AUDIO2"
.segment "AUDIO3"
.segment "AUDIO4"
.segment "AUDIO5"
.segment "AUDIO6"
.segment "AUDIO7"
.segment "CODEIDENT"
VAR current_bank
.byte 0
.segment "CHR1IDENT"
.byte 1
.segment "CHR2IDENT"
.byte 2
.segment "CHR3IDENT"
.byte 3
.segment "CHR4IDENT"
.byte 4
.segment "AUDIO0IDENT"
.byte 5
.segment "AUDIO1IDENT"
.byte 6
.segment "AUDIO2IDENT"
.byte 7
.segment "AUDIO3IDENT"
.byte 8
.segment "AUDIO4IDENT"
.byte 9
.segment "AUDIO5IDENT"
.byte 10
.segment "AUDIO6IDENT"
.byte 11
.segment "AUDIO7IDENT"
.byte 12
.segment "UIIDENT"
.byte 13
.segment "EXTRAIDENT"
.byte 14