-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
example.s
785 lines (739 loc) · 13 KB
/
example.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
;
; example.s
; Brad Smith (rainwarrior), 4/06/2014
; http://rainwarrior.ca
;
; This is intended as an introductory example to NES programming with ca65.
; It covers the basic use of background, sprites, and the controller.
; This does not demonstrate how to use sound.
;
; This is not intended as a ready-made game. It is only a very minimal
; playground to assist getting started in NES programming. The idea here is
; to get you past the most difficult parts of a minimal NES program setup
; so that you can experiment from an almost blank slate.
;
; To use your own graphics, replace the two 4k tile banks provided.
; They are named "background.chr" and "sprite.chr".
;
; The reset and nmi routines are provided as a simple working example of
; these things. Writing these from scratch is a more advanced topic, so they
; will not be fully explained here.
;
; Under "drawing utilities" are some very primitive routines for working
; with the NES graphics. See the "main" section for examples of how to use them.
;
; Finally at the bottom you will find the "main" section that provides
; a small example program. A cursor is shown. Pressing the d-pad will move
; - pressing the d-pad will move the cursor around the screen
; - pressing B will draw a tile to the screen
; - pressing A will draw several tiles to the screen
; - pressing SELECT will reset the background
; - holding START will demonstrate scrolling
;
; Please note that this example code is intended to be simple, not necessarily
; efficient. I have tried to avoid optimization in favour of easier to understand code.
;
; You may notice some odd behaviour when using the A button around the edges of the screen.
; I will leave it as an exercise for the curious to understand what is going on.
;
;
; iNES header
;
.segment "HEADER"
INES_MAPPER = 0 ; 0 = NROM
INES_MIRROR = 1 ; 0 = horizontal mirroring, 1 = vertical mirroring
INES_SRAM = 0 ; 1 = battery backed SRAM at $6000-7FFF
.byte 'N', 'E', 'S', $1A ; ID
.byte $02 ; 16k PRG chunk count
.byte $01 ; 8k CHR chunk count
.byte INES_MIRROR | (INES_SRAM << 1) | ((INES_MAPPER & $f) << 4)
.byte (INES_MAPPER & %11110000)
.byte $0, $0, $0, $0, $0, $0, $0, $0 ; padding
;
; CHR ROM
;
.segment "TILES"
.incbin "background.chr"
.incbin "sprite.chr"
;
; vectors placed at top 6 bytes of memory area
;
.segment "VECTORS"
.word nmi
.word reset
.word irq
;
; reset routine
;
.segment "CODE"
reset:
sei ; mask interrupts
lda #0
sta $2000 ; disable NMI
sta $2001 ; disable rendering
sta $4015 ; disable APU sound
sta $4010 ; disable DMC IRQ
lda #$40
sta $4017 ; disable APU IRQ
cld ; disable decimal mode
ldx #$FF
txs ; initialize stack
; wait for first vblank
bit $2002
:
bit $2002
bpl :-
; clear all RAM to 0
lda #0
ldx #0
:
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 :-
; place all sprites offscreen at Y=255
lda #255
ldx #0
:
sta oam, X
inx
inx
inx
inx
bne :-
; wait for second vblank
:
bit $2002
bpl :-
; NES is initialized, ready to begin!
; enable the NMI for graphical updates, and jump to our main program
lda #%10001000
sta $2000
jmp main
;
; nmi routine
;
.segment "ZEROPAGE"
nmi_lock: .res 1 ; prevents NMI re-entry
nmi_count: .res 1 ; is incremented every NMI
nmi_ready: .res 1 ; set to 1 to push a PPU frame update, 2 to turn rendering off next NMI
nmt_update_len: .res 1 ; number of bytes in nmt_update buffer
scroll_x: .res 1 ; x scroll position
scroll_y: .res 1 ; y scroll position
scroll_nmt: .res 1 ; nametable select (0-3 = $2000,$2400,$2800,$2C00)
temp: .res 1 ; temporary variable
.segment "BSS"
nmt_update: .res 256 ; nametable update entry buffer for PPU update
palette: .res 32 ; palette buffer for PPU update
.segment "OAM"
oam: .res 256 ; sprite OAM data to be uploaded by DMA
.segment "CODE"
nmi:
; save registers
pha
txa
pha
tya
pha
; prevent NMI re-entry
lda nmi_lock
beq :+
jmp @nmi_end
:
lda #1
sta nmi_lock
; increment frame counter
inc nmi_count
;
lda nmi_ready
bne :+ ; nmi_ready == 0 not ready to update PPU
jmp @ppu_update_end
:
cmp #2 ; nmi_ready == 2 turns rendering off
bne :+
lda #%00000000
sta $2001
ldx #0
stx nmi_ready
jmp @ppu_update_end
:
; sprite OAM DMA
ldx #0
stx $2003
lda #>oam
sta $4014
; palettes
lda #%10001000
sta $2000 ; set horizontal nametable increment
lda $2002
lda #$3F
sta $2006
stx $2006 ; set PPU address to $3F00
ldx #0
:
lda palette, X
sta $2007
inx
cpx #32
bcc :-
; nametable update
ldx #0
cpx nmt_update_len
bcs @scroll
@nmt_update_loop:
lda nmt_update, X
sta $2006
inx
lda nmt_update, X
sta $2006
inx
lda nmt_update, X
sta $2007
inx
cpx nmt_update_len
bcc @nmt_update_loop
lda #0
sta nmt_update_len
@scroll:
lda scroll_nmt
and #%00000011 ; keep only lowest 2 bits to prevent error
ora #%10001000
sta $2000
lda scroll_x
sta $2005
lda scroll_y
sta $2005
; enable rendering
lda #%00011110
sta $2001
; flag PPU update complete
ldx #0
stx nmi_ready
@ppu_update_end:
; if this engine had music/sound, this would be a good place to play it
; unlock re-entry flag
lda #0
sta nmi_lock
@nmi_end:
; restore registers and return
pla
tay
pla
tax
pla
rti
;
; irq
;
.segment "CODE"
irq:
rti
;
; drawing utilities
;
.segment "CODE"
; ppu_update: waits until next NMI, turns rendering on (if not already), uploads OAM, palette, and nametable update to PPU
ppu_update:
lda #1
sta nmi_ready
:
lda nmi_ready
bne :-
rts
; ppu_skip: waits until next NMI, does not update PPU
ppu_skip:
lda nmi_count
:
cmp nmi_count
beq :-
rts
; ppu_off: waits until next NMI, turns rendering off (now safe to write PPU directly via $2007)
ppu_off:
lda #2
sta nmi_ready
:
lda nmi_ready
bne :-
rts
; ppu_address_tile: use with rendering off, sets memory address to tile at X/Y, ready for a $2007 write
; Y = 0- 31 nametable $2000
; Y = 32- 63 nametable $2400
; Y = 64- 95 nametable $2800
; Y = 96-127 nametable $2C00
ppu_address_tile:
lda $2002 ; reset latch
tya
lsr
lsr
lsr
ora #$20 ; high bits of Y + $20
sta $2006
tya
asl
asl
asl
asl
asl
sta temp
txa
ora temp
sta $2006 ; low bits of Y + X
rts
; ppu_update_tile: can be used with rendering on, sets the tile at X/Y to tile A next time you call ppu_update
ppu_update_tile:
pha ; temporarily store A on stack
txa
pha ; temporarily store X on stack
ldx nmt_update_len
tya
lsr
lsr
lsr
ora #$20 ; high bits of Y + $20
sta nmt_update, X
inx
tya
asl
asl
asl
asl
asl
sta temp
pla ; recover X value (but put in A)
ora temp
sta nmt_update, X
inx
pla ; recover A value (tile)
sta nmt_update, X
inx
stx nmt_update_len
rts
; ppu_update_byte: like ppu_update_tile, but X/Y makes the high/low bytes of the PPU address to write
; this may be useful for updating attribute tiles
ppu_update_byte:
pha ; temporarily store A on stack
tya
pha ; temporarily store Y on stack
ldy nmt_update_len
txa
sta nmt_update, Y
iny
pla ; recover Y value (but put in Y)
sta nmt_update, Y
iny
pla ; recover A value (byte)
sta nmt_update, Y
iny
sty nmt_update_len
rts
;
; gamepad
;
PAD_A = $01
PAD_B = $02
PAD_SELECT = $04
PAD_START = $08
PAD_U = $10
PAD_D = $20
PAD_L = $40
PAD_R = $80
.segment "ZEROPAGE"
gamepad: .res 1
.segment "CODE"
; gamepad_poll: this reads the gamepad state into the variable labelled "gamepad"
; This only reads the first gamepad, and also if DPCM samples are played they can
; conflict with gamepad reading, which may give incorrect results.
gamepad_poll:
; strobe the gamepad to latch current button state
lda #1
sta $4016
lda #0
sta $4016
; read 8 bytes from the interface at $4016
ldx #8
:
pha
lda $4016
; combine low two bits and store in carry bit
and #%00000011
cmp #%00000001
pla
; rotate carry into gamepad variable
ror
dex
bne :-
sta gamepad
rts
;
; main
;
.segment "RODATA"
example_palette:
.byte $0F,$15,$26,$37 ; bg0 purple/pink
.byte $0F,$09,$19,$29 ; bg1 green
.byte $0F,$01,$11,$21 ; bg2 blue
.byte $0F,$00,$10,$30 ; bg3 greyscale
.byte $0F,$18,$28,$38 ; sp0 yellow
.byte $0F,$14,$24,$34 ; sp1 purple
.byte $0F,$1B,$2B,$3B ; sp2 teal
.byte $0F,$12,$22,$32 ; sp3 marine
.segment "ZEROPAGE"
cursor_x: .res 1
cursor_y: .res 1
temp_x: .res 1
temp_y: .res 1
.segment "CODE"
main:
; setup
ldx #0
:
lda example_palette, X
sta palette, X
inx
cpx #32
bcc :-
jsr setup_background
; center the cursor
lda #128
sta cursor_x
lda #120
sta cursor_y
; show the screen
jsr draw_cursor
jsr ppu_update
; main loop
@loop:
; read gamepad
jsr gamepad_poll
; respond to gamepad state
lda gamepad
and #PAD_START
beq :+
jsr push_start
jmp @draw ; start trumps everything, don't check other buttons
:
jsr release_start ; releasing start restores scroll
lda gamepad
and #PAD_U
beq :+
jsr push_u
:
lda gamepad
and #PAD_D
beq :+
jsr push_d
:
lda gamepad
and #PAD_L
beq :+
jsr push_l
:
lda gamepad
and #PAD_R
beq :+
jsr push_r
:
lda gamepad
and #PAD_SELECT
beq :+
jsr push_select
:
lda gamepad
and #PAD_B
beq :+
jsr push_b
:
lda gamepad
and #PAD_A
beq :+
jsr push_a
:
@draw:
; draw everything and finish the frame
jsr draw_cursor
jsr ppu_update
; keep doing this forever!
jmp @loop
push_u:
dec cursor_y
; Y wraps at 240
lda cursor_y
cmp #240
bcc :+
lda #239
sta cursor_y
:
rts
push_d:
inc cursor_y
; Y wraps at 240
lda cursor_y
cmp #240
bcc :+
lda #0
sta cursor_y
:
rts
push_l:
dec cursor_x
rts
push_r:
inc cursor_x
rts
push_select:
; turn off rendering so we can manually update entire nametable
jsr ppu_off
jsr setup_background
; wait for user to release select before continuing
:
jsr gamepad_poll
lda gamepad
and #PAD_SELECT
bne :-
rts
push_start:
inc scroll_x
inc scroll_y
; Y wraps at 240
lda scroll_y
cmp #240
bcc :+
lda #0
sta scroll_y
:
; when X rolls over, toggle the high bit of nametable select
lda scroll_x
bne :+
lda scroll_nmt
eor #$01
sta scroll_nmt
:
rts
release_start:
lda #0
sta scroll_x
sta scroll_y
sta scroll_nmt
rts
push_b:
jsr snap_cursor
lda cursor_x
lsr
lsr
lsr
tax ; X = cursor_x / 8
lda cursor_y
lsr
lsr
lsr
tay ; Y = cursor_y / 8
lda #4
jsr ppu_update_tile ; puts tile 4 at X/Y
rts
push_a:
jsr snap_cursor
lda cursor_x
lsr
lsr
lsr
sta temp_x ; cursor_x / 8
lda cursor_y
lsr
lsr
lsr
sta temp_y ; cursor_y / 8
; draw a ring of 8 tiles around the cursor
dec temp_x ; x-1
dec temp_y ; y-1
ldx temp_x
ldy temp_y
lda #5
jsr ppu_update_tile
inc temp_x ; x
ldx temp_x
ldy temp_y
lda #6
jsr ppu_update_tile
inc temp_x ; x+1
ldx temp_x
ldy temp_y
lda #5
jsr ppu_update_tile
dec temp_x
dec temp_x ; x-1
inc temp_y ; y
ldx temp_x
ldy temp_y
lda #6
jsr ppu_update_tile
inc temp_x
inc temp_x ; x+1
ldx temp_x
ldy temp_y
lda #6
jsr ppu_update_tile
dec temp_x
dec temp_x ; x-1
inc temp_y ; y+1
ldx temp_x
ldy temp_y
lda #5
jsr ppu_update_tile
inc temp_x ; x
ldx temp_x
ldy temp_y
lda #6
jsr ppu_update_tile
inc temp_x ; x+1
ldx temp_x
ldy temp_y
lda #5
jsr ppu_update_tile
rts
; snap_cursor: snap cursor to nearest tile
snap_cursor:
lda cursor_x
clc
adc #4
and #$F8
sta cursor_x
lda cursor_y
clc
adc #4
and #$F8
sta cursor_y
; Y wraps at 240
cmp #240
bcc :+
lda #0
sta cursor_y
:
rts
draw_cursor:
; four sprites centred around the currently selected tile
; y position (note, needs to be one line higher than sprite's appearance)
lda cursor_y
sec
sbc #5 ; Y-5
sta oam+(0*4)+0
sta oam+(1*4)+0
lda cursor_y
clc
adc #3 ; Y+3
sta oam+(2*4)+0
sta oam+(3*4)+0
; tile
lda #1
sta oam+(0*4)+1
sta oam+(1*4)+1
sta oam+(2*4)+1
sta oam+(3*4)+1
; attributes
lda #%00000000 ; no flip
sta oam+(0*4)+2
lda #%01000000 ; horizontal flip
sta oam+(1*4)+2
lda #%10000000 ; vertical flip
sta oam+(2*4)+2
lda #%11000000 ; both flip
sta oam+(3*4)+2
; x position
lda cursor_x
sec
sbc #4 ; X-4
sta oam+(0*4)+3
sta oam+(2*4)+3
lda cursor_x
clc
adc #4 ; X+4
sta oam+(1*4)+3
sta oam+(3*4)+3
rts
setup_background:
; first nametable, start by clearing to empty
lda $2002 ; reset latch
lda #$20
sta $2006
lda #$00
sta $2006
; empty nametable
lda #0
ldy #30 ; 30 rows
:
ldx #32 ; 32 columns
:
sta $2007
dex
bne :-
dey
bne :--
; set all attributes to 0
ldx #64 ; 64 bytes
:
sta $2007
dex
bne :-
; fill in an area in the middle with 1/2 checkerboard
lda #1
ldy #8 ; start at row 8
:
pha ; temporarily store A, it will be clobbered by ppu_address_tile routine
ldx #8 ; start at column 8
jsr ppu_address_tile
pla ; recover A
; write a line of checkerboard
ldx #8
:
sta $2007
eor #$3
inx
cpx #(32-8)
bcc :-
eor #$3
iny
cpy #(30-8)
bcc :--
; second nametable, fill with simple pattern
lda #$24
sta $2006
lda #$00
sta $2006
lda #$00
ldy #30
:
ldx #32
:
sta $2007
clc
adc #1
and #3
dex
bne :-
clc
adc #1
and #3
dey
bne :--
; 4 stripes of attribute
lda #0
ldy #4
:
ldx #16
:
sta $2007
dex
bne :-
clc
adc #%01010101
dey
bne :--
rts
;
; end of file
;