-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDISP.SRC
596 lines (523 loc) · 7.03 KB
/
DISP.SRC
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
;
;Magician sprite displayer
;
radix 10
option 0,0 ;"<" means lo-byte
send computer1
;
;MACROS
;
;Define variables
;@1=variable name
;@2=bytes required
;
zp macro
@1 equ z
z = z+@2
endm
;
;Load prg or chr data (@2..@5 are optional)
;@1=pathname (C:\DEV\DAT\@1)
;@2=physical origin
;@3=logical origin
;@4=4k chr bank/8k prg bank
;@5=label which takes value of origin before INCBIN (if @5="!"
; then @1 is used as the label name)
;
load macro
ifs [@2] []
else
ifs [@3] []
org @2
else
org @2,@3
endif
endif
ifs [@5] []
else
ifs [@5] [!]
@1 equ *
else
@5 equ *
endif
endif
incbin dat\@1
ifs [@4] []
else
if *>$7fff
bank @4
else
error
bank @4*$04
endif
endif
endm
;RAM variables (zero-page)
;** Do not alter order! **
z = $00
zp r0,1 ;copy of MMC3 bank registers
zp r1,1
zp r2,1
zp r3,1
zp r4,1
zp r5,1
zp r6,1
zp r7,1
zp p0,1 ;copy of PPU register 0 ($2000)
zp t0,1 ;general-purpose temporary storage
zp t1,1
zp t2,1
zp t3,1
zp t4,1
zp t5,1
zp t6,1
zp t7,1
zp t8,1
zp t9,1
zp ta,1
zp tb,1
zp tc,1
zp td,1
zp te,1
zp tf,1
zp t10,1
zp t11,1
zp t12,1
zp t13,1
zp jt,2 ;dynamic joypad vars
zp lr,1
zp ud,1
zp sta,1
zp sel,1
zp fireb,1
zp firea,1
zp dlr,1 ;debounced joypad vars
zp dud,1
zp dsta,1
zp dsel,1
zp dfireb,1
zp dfirea,1
zp oldlr,6 ;debounce vars
zp second,1 ;1 second timer
zp sprind,1 ;spr insertion index
zp fbase,2 ;base of frames
zp numspr,1
zp autodel,1
;RAM variables (non zero-page)
;** Do not alter order! **
z = $0200 ;** Unexpanded RAM $0000-$07FF **
;Spr variables
zp spr,$100 ;Sprite data page
org $a000
include \zdev\seq.src
org $c000
;
;Reset routine
;
reset lda #$08 ;NMI off,DMA+1
sta $2000
sei ;IRQ off
ldx #$00 ;blank screen
stx $2001
cld ;binary mode
sta p0 ;save copy of $2000
inx
!a lda $2002 ;wait till PPU reset
bpl !a
dex
bpl !a
txs ;init stack ptr
inx
stx $8000 ;set MMC3 map mode0,interval timer in-use
stx $e000 ;disable MMC3 interval timer
stx $4015 ;disable sound chip channels A..E
stx $4010 ;stop delta modulation generating IRQs
lda #$40
sta $4017 ;make vertical blanks cause NMIs
lda $2002 ;clear NMI flag
lda #$10 ;init MMC3 interval timer
tax
!b sta $2006
sta $2006
eor #$00
dex
bne !b
stx $a000 ;select MMC3 horizontal scroll mapping
stx fbase
stx fbase+1
lda #$06
sta $8000
lda #$00
sta $8001
lda #$07
sta $8000
lda #$01
sta $8001
jsr initscn
lda #$88
sta p0
sta $2000
loop jmp loop
;
;Main game NMI
;NB:this routine does some very subtle stuff,but no time to explain
;
nmi0 jsr pfrm
lda #$3f ;set chr & spr palettes
sta $2006
ldy #$00
sty $2006
ldx #$60
!b lda curchrpal-$60,x
sta $2007
inx
bpl !b
!b0 ldy #$00
sty $2003 ;initialise & start sprite DMA
lda #>spr
sta $4014
lda #$f8 ;enable scn & sprs with left-side blanking
sta $2001
ldx #$3f ;re-stabilise colour generator
stx $2006
sty $2006 ;Y=$00
sty $2006 ;& set end of 'undetermined zone'
sty $2006
and p0 ;A=$F8 - set scroll x,y after DMA access
sta p0
sta $2000
sty $2005
sty $2005
sty sprind
jsr setmmc3a ;Y=$00 - set MMC3 chr/spr banks
jsr joykey ;scan joypad
jsr doauto
bcs !y
lda ud
tax
beq !y
bmi !x
dex
!x clc
adc fbase
sta fbase
txa
adc fbase+1
sta fbase+1
!y jsr frame
lda sprind
asl a
asl a
bcs niexit
tax
lda #$ff
!z sta spr,x
inx
inx
inx
inx
bne !z
niexit pla ;exit NMI/IRQ
tay
pla
tax
pla
irq rti
;
;Handle up/down auto-repeat
;OUT:C=1=no u/d
;
doauto sec
lda ud ;any stick u/d?
beq !b
lda #$14 ;yes, lead-in delay
dec autodel
bmi !a
bne !c
lda #$04 ;repeat delay
!a clc
!b sta autodel
!c rts ;& exit with C=0/1
nmi pha
txa
pha
tya
pha
lda $2002 ;clear NMI flag
jmp nmi0
;
;Scan joypad, returning dynamic & debounced values
;
joykey jsr jk0 ;debounce joypad
ldx jt ;by waiting until the joypad returns
jsr jk0 ;two consecutive equal values
cpx jt
bne joykey
txa ;extract bits into bytes
ldx #$07
!a asl a
ldy #$00
bcc !b
iny
!b sty jt,x
dex
bpl !a
ldx lr ;set ud
bne !c
ldx lr+1
beq !c
ldx #$ff
!c stx ud
ldx jt ;set lr
bne !d
ldx jt+1
beq !d
ldx #$ff
!d stx lr
ldx #$05 ;set debounced values
!e lda lr,x
beq !f
cmp oldlr,x
beq !g
!f sta oldlr,x
db $2c
!g lda #$00
sta dlr,x
dex
bpl !e
rts
jk0 lda #$01
sta jt ;init for 8 reads
sta $4016 ;init I/O port for joystick read
lsr a
sta $4016
!a lda $4016 ;serially read 8 2-bit values from I/O port
and #$03 ;%00=%0,%01=%1,%10=%1,%11=%1
cmp #$01
rol jt
bcc !a
rts
;
;Set MMC3 banks
;
setmmc3 ldy #$00 ;set MMC3 chr banks
setmmc3a ldx #$7c
sty $8000
stx $8001
iny
inx
inx
sty $8000
stx $8001
iny ;set MMC3 spr banks
ldx r2
sty $8000
stx $8001
iny
ldx r3
sty $8000
stx $8001
iny
ldx r4
sty $8000
stx $8001
iny
ldx r5
sty $8000
stx $8001
rts
;
;Display sprite frame
;IN :FBASE=frame no.
;USES:T0..T13 (TA unused)
;
frame lda #$00 ;set spr def bits 6-7 mask
sta tc
sta td ;set MMC3 index
sta t8 ;set no x/y flip
lda #<$0080
sta t10
lda #>$0080
sta t11
lda #<$0080
sta t12
lda #>$0080
sta t13
lda fbase
sta te
lda fbase+1
asl te
rol a
sta tf
lda te
asl a
tax
lda tf
rol a
tay
txa
adc te
sta te
tya
adc tf
ora #>$a000
sta tf
ldy #$05 ;set ptrs to tables
lda (te),y
tax
lda cpltab,x
sta t6
lda cphtab,x
sta t7
dey
lda (te),y
tax
lda dpltab,x
sta t4
lda dphtab,x
sta t5
dey
lda (te),y
tax
lda ypltab,x
sta t2
lda yphtab,x
sta t3
dey
lda (te),y
tax
lda xpltab,x
sta t0
lda xphtab,x
sta t1
dey ;set hardware spr bank 0..3
ldx td
lda (te),y
sta r2,x
dey
lda (te),y ;set no. of sprs in this frame
sta t9
sta numspr
lda (t6),y ;set palette byte
sta td
lda #$04 ;set count
sta te
sty tf ;set index
lda sprind ;get spr index
asl a
asl a
bcs !z
tax
!b lda t8 ;get x/y flip / 2
asl td ;extract 2-bit palette
rol a
asl td
rol a
sta spr+2,x ;set col+flip status
dec te ;need new palette byte?
bne !c
lda #$04 ;yes, reset count
sta te
sty tb ;save Y
inc tf ;get new byte
ldy tf
lda (t6),y
sta td
ldy tb ;restore Y
!c bit t8 ;x
lda (t0),y
clc
bvc !d
eor #$ff
adc #$01
clc
!d bmi !f
adc t10
sta spr+3,x
lda #$00
adc t11
beq !g
bne !j
!f adc t10
sta spr+3,x
lda #$ff
adc t11
bne !j
!g lda (t2),y ;y
clc
bmi !h
adc t12
sta spr,x
lda #$00
adc t13
beq !i
bne !j
!h adc t12
sta spr,x
lda #$ff
adc t13
bne !j
!i lda (t4),y ;def
ora tc ;+ bank
sta spr+1,x
inc sprind ;move to next spr
inx
inx
inx
inx
beq !z
!j iny ;do all sprs in this frame
cpy t9
bcc !b
!z rts ;exit
initscn lda #$20
sta $2006
lda #$00
sta $2006
ldx #<-$03c0
ldy #>-$03c0
lda #$20
!a sta $2007
inx
bne !a
iny
bne !a
lda #$00
ldx #$40
!b sta $2007
dex
bne !b
rts
pfrm lda #>$2000+4+6*32
sta $2006
lda #<$2000+4+6*32
sta $2006
lda fbase+1
jsr phex2
lda fbase
jsr phex2
lda #$20
sta $2007
lda r2
jsr phex2
lda #$20
sta $2007
lda numspr
phex2 pha
lsr a
lsr a
lsr a
lsr a
jsr !a
pla
and #$0f
!a cmp #$0a
bcc !b
adc #$36
!b sta $2007
rts
curchrpal HEX 0F2838300F1031300F2839300F001030
HEX 100F27370F0F26360F0F10300F0F2A3A
org $fffa
dw nmi,reset,irq
end