-
Notifications
You must be signed in to change notification settings - Fork 2
/
reset.asm
533 lines (429 loc) · 6.17 KB
/
reset.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
;------------------------------------------------------------------------------
; RESET CODE
;------------------------------------------------------------------------------
RESET:
sei
ldx #$FF
txs
lda #$00
sta PPU0
sta PPU1
sta vblankLastFlag
sta vblankFlag
lda #$01
sta doNotInterrupt
jsr vblankwait
jsr vblankwait
jsr blankPalette
jsr resetMMC1
lda #$00
jsr setPRGBank
jsr vblankwait
jsr vblankwait
lda #%00001100 ;Set bank layout, H&V mirror, 16kb ROM at $C000. 8KB CHR
jsr setMMC1r0
jsr clearRam
.IF EXT_SYNC=1
lda #$00
sta old4017
sta newStepNeeded
.ENDIF
lda #BANK_SCREEN
jsr setPRGBank
jsr clearGraphics
lda #WRAM_BANK_00
jsr setMMC1r1
lda #$01
sta $7FFF
lda #WRAM_BANK_01
jsr setMMC1r1
lda #$02
sta $7FFF
lda #WRAM_BANK_02
jsr setMMC1r1
lda #$04
sta $7FFF
lda #WRAM_BANK_03
jsr setMMC1r1
lda #$08
sta $7FFF
lda #WRAM_BANK_00
jsr setMMC1r1
lda $7FFF
cmp #$01
bne @lockUp
lda #WRAM_BANK_01
jsr setMMC1r1
lda $7FFF
cmp #$02
bne @lockUp
lda #WRAM_BANK_02
jsr setMMC1r1
lda $7FFF
cmp #$04
bne @lockUp
lda #WRAM_BANK_03
jsr setMMC1r1
lda $7FFF
cmp #$08
beq @noLock
@lockUp: lda #BANK_SCREEN
jsr setPRGBank
jsr errorScreen
lda #%00001000
sta PPU0
lda #%00011110
sta PPU1
lda #$00
sta $2005
sta $2006
sta $2006
jmp *
@noLock:
lda #BANK_SCREEN
jsr setPRGBank
jsr initGraphics
lda #BANK_PLAYER
jsr setPRGBank
jsr initSound
lda #$00
jsr setPRGBank
lda #%00001100 ;Set bank layout, H&V mirror, 16kb ROM at $C000. 8KB CHR
jsr setMMC1r0
jsr initProcBuffer
;jsr compress
;jsr decompress
;lda #procTrackDrumAll
;jsr addProcessToBuffer
jsr vblankwait ;warm up
jsr vblankwait
lda #%10001000
sta PPU0
lda #%00011110
sta PPU1
mainLoop:
lda #$01
sta doNotInterrupt
lda #BANK_EDITOR
jsr setPRGBank
jsr editorInit
lda #BANK_PLAYER
jsr setPRGBank
jsr plyrSetupSong
lda #$00
sta doNotInterrupt
.IF EXT_SYNC=0
mainLoop2:
lda vblankLastFlag
: cmp vblankFlag
beq :-
lda vblankFlag
sta vblankLastFlag
jsr readPad1
jsr refreshAudio
lda #BANK_PLAYER
jsr setPRGBank
jsr playNotes
lda #BANK_EDITOR
jsr setPRGBank
jsr editorRefresh
jmp mainLoop2
.ELSE
mainLoop2:
lda vblankLastFlag ;EXT SYNC VERSION
: cmp vblankFlag
beq :-
lda vblankFlag
sta vblankLastFlag
jsr readPad2
lda #BANK_PLAYER
jsr setPRGBank
jsr syncFunctions
bcs mainLoop2
jsr readPad1
jsr refreshAudio
lda #BANK_PLAYER
jsr setPRGBank
jsr playNotes
lda #BANK_EDITOR
jsr setPRGBank
jsr editorRefresh
jmp mainLoop2
.ENDIF
vblankwait:
: bit $2002
bpl :-
rts
vblankendwait:
: bit $2002
bmi :-
rts
NMI: pha
txa
pha
tya
pha
bit $2002
;inc nmiCounter
lda curPrgBank
sta oldPrgBank
lda curWramBank
sta oldWramBank
inc vblankFlag
lda doNotInterrupt
bne :+ ;@exitNMI
.IF DEBUG=1
jsr debugNumbers
.ENDIF
lda #BANK_SCREEN
jsr setPRGBank
jsr process_DMA_queue
jsr shiftProcessBuffer
jsr spriteWriteFlashColour
lda #BANK_0
jsr setPRGBank
jsr spriteDMA
lda #$00
sta $2005
sta $2006
sta $2006
: ;jsr refreshAudio
.IF DEBUG_COLOURS=1
lda #%00111110
sta PPU1
.ENDIF
;lda #BANK_PLAYER
;jsr setPRGBank
;.IF EXT_SYNC=0
;jsr playNotes
;.ENDIF
.IF DEBUG_COLOURS=1
lda #%00011110
sta PPU1
.ENDIF
lda oldPrgBank
jsr setPRGBank
lda oldWramBank
jsr setMMC1r1
@exitNMI: ;dec nmiCounter
pla
tay
pla
tax
pla
rti
IRQ: rti
initProcBuffer:
ldx #$00
lda #$FF
: sta dmaProcessBuffer,x
inx
cpx #$20
bcc :-
rts
.IF 0=1
countProcesses:
ldx #$00
: lda dmaProcessBuffer,x
cmp #$FF
beq :+
inx
cpx #$1F
bcc :-
: cpx maxDma
bcc :+
stx maxDma
: rts
.ENDIF
addProcessToBuffer:
sta tmp0
ldx #$00
: lda dmaProcessBuffer,x
cmp tmp0
beq :++
cmp #$FF
beq :+
inx
cpx #$1F
bcc :-
inc errorCount
rts
: lda tmp0
sta dmaProcessBuffer,x
inx
lda #$FF
sta dmaProcessBuffer,x
: rts
shiftProcessBuffer:
lda dmaProcessBuffer
cmp #$FF
bne :+
rts
: ldx #$01
: lda dmaProcessBuffer,x
sta dmaProcessBuffer-1,x
cmp #$FF
beq :+
inx
bpl :-
: rts
refreshAudio:
lda editorDecoding
beq :+
rts
:
lda #BANK_SYNTH
jsr setPRGBank
jsr synthRefresh
lda #BANK_PLAYER
jsr setPRGBank
jsr plyrUpdateEchoIndex
ldx #$05 ;only do this delay if no new note needed
ldy #$00 ;in which case, time taken to setup note is enough
: dey
bne :-
dex
bne :-
lda #BANK_SYNTH
jsr setPRGBank
jsr synthRefresh
lda #BANK_PLAYER
jsr setPRGBank
jsr plyrUpdateEchoIndex
rts
clearRam:
lda #0
tax
@a: sta $000,x
sta $200,x
sta $300,x
sta $400,x
sta $500,x
sta $600,x
inx
bne @a
; Clear below stack
tsx
inx
@b: dex
sta $100,x
bne @b
rts
.IF 0=1
clearWRAM: lda #%00010000 ;WRAM bank 0?
jsr setMMC1r1
lda #$FF
jsr @clear
lda #%00010100 ;WRAM bank 1?
jsr setMMC1r1
lda #$FF
jsr @clear
lda #%00011000 ;WRAM bank 2?
jsr setMMC1r1
lda #$FF
jsr @clear
lda #%00011100 ;WRAM bank 3?
jsr setMMC1r1
lda #$FF
@clear: ldx #<WRAM
stx tmp0
ldx #>WRAM
stx tmp1
ldx #$20
ldy #$00
:
sta (tmp0),y
iny
bne :-
inc tmp1
dex
bne :-
rts
.ENDIF
blankPalette:
lda #$3F
sta DMA_ADDR
lda #$00
sta DMA_ADDR
ldx #$0F
txa
: sta $2007
dex
bpl :-
rts
spriteDMA:
lda #$00
sta $2003
lda #>sprBuf
sta $4014
rts
spriteWriteFlashColour:
;Flash palette
lda #>$3F13
sta $2006
lda #<$3F13
sta $2006
lda cursorFlashColour
sta $2007
;lda #$00
;sta $2006
;sta $2006
rts
.include "debug.asm"
.include "joypad.asm"
SET_BITS: .BYTE %00000001,%00000010,%00000100,%00001000,%00010000
CLR_BITS: .BYTE %11111110,%11111101,%11111011,%11110111,%11101111
.include "patternTable.asm"
;------------------------------------------------------------------------------
; MMC1 Resgister Handling
;------------------------------------------------------------------------------
resetMMC1:
ldx #$80
stx $8000
rts
setPRGBank:
sta curPrgBank
sta $E000
lsr a
sta $E000
lsr a
sta $E000
lsr a
sta $E000
lsr a
sta $E000
rts
setMMC1r0:
sta $9fff
lsr a
sta $9fff
lsr a
sta $9fff
lsr a
sta $9fff
lsr a
sta $9fff
rts
setMMC1r1:
sta curWramBank
sta $Bfff
lsr a
sta $Bfff
lsr a
sta $Bfff
lsr a
sta $Bfff
lsr a
sta $Bfff
rts
setMMC1r2:
sta $Dfff
lsr a
sta $Dfff
lsr a
sta $Dfff
lsr a
sta $Dfff
lsr a
sta $Dfff
rts