aboutsummaryrefslogtreecommitdiffstats
path: root/main.s
blob: cce0ae420ad2e03f96fb72cbe7e4a1fc74ef85a0 (plain) (blame)
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
; configuration {{{
.asciitable
MAP "-" = 1
MAP "|" = 2
MAP ":" = 3
MAP "." = 4
MAP "," = 5
MAP "'" = 6
.enda
.struct point
x db
y db
.endst
; }}}
; memory layout {{{
; rom {{{
.ROMBANKMAP
BANKSTOTAL  2
BANKSIZE    $4000
BANKS       1
BANKSIZE    $2000
BANKS       1
.ENDRO
; }}}
; ram {{{
.MEMORYMAP
DEFAULTSLOT  0
SLOTSIZE     $4000
SLOT 0       $C000
SLOTSIZE     $2000
SLOT 1       $0000 ; location doesn't matter, CHR data isn't in main memory
.ENDME

.ENUM $0000
buttons_pressed DB
sleeping        DB
game_state      DB ; 0: menu, 1: playing, 2: redrawing
head            DW
length          DB
direction       DB ; 0: up, 1: down, 2: left, 3: right
frame_skip      DB
frame_count     DB
rand_state      DB
apple           INSTANCEOF point
.ENDE
; }}}
; }}}
; prg {{{
  .bank 0
  .org $0000
; main codepath {{{
; initialization {{{
; the ppu takes two frames to initialize, so we have some time to do whatever
; initialization of our own that we want to while we wait. we choose here to
; set up cpu flags in the first frame and clear out system ram in the second
; frame (clearing out ram isn't at all necessary, but we can't do anything
; useful at this point anyway, so we may as well in order to make things more
; predictable).
RESET:
  SEI              ; disable IRQs
  CLD              ; disable decimal mode
  LDX #$40
  STX $4017.w      ; disable APU frame IRQ
  LDX #$FF
  TXS              ; Set up stack (grows down from $FF to $00, at $0100-$01FF)
  INX              ; now X = 0
  STX $2000.w      ; disable NMI (we'll enable it later once the ppu is ready)
  STX $2001.w      ; disable rendering (same)
  STX $4010.w      ; disable DMC IRQs

  ; First wait for vblank to make sure PPU is ready
- BIT $2002        ; bit 7 of $2002 is reset once vblank ends
  BPL -            ; and bit 7 is what is checked by BPL

  ; set everything in ram ($0000-$07FF) to $00, except for $0200-$02FF which
  ; is conventionally used to hold sprite attribute data. we set that range
  ; to $FE, since that value as a position moves the sprites offscreen, and
  ; when the sprites are offscreen, it doesn't matter which sprites are
  ; selected or what their attributes are
clrmem:
  LDA #$00
  STA $0000, x
  STA $0100, x
  STA $0300, x
  STA $0400, x
  STA $0500, x
  STA $0600, x
  STA $0700, x
  LDA #$FE
  STA $0200, x
  INX
  BNE clrmem

  ; initialize variables in ram
  LDA #$03
  LDX #$01
  STA head, x
  LDA #30
  STA frame_skip

  ; Second wait for vblank, PPU is ready after this
- BIT $2002
  BPL -

  ; now that the ppu is ready, we can start initializing it
LoadPalettes:
  LDA $2002    ; read PPU status to reset the high/low latch
  LDA #$3F
  STA $2006    ; write the high byte of $3F00 address
  LDA #$00
  STA $2006    ; write the low byte of $3F00 address
  LDX #$00
LoadPalettesLoop:
  LDA palette.w, x      ;load palette byte
  STA $2007             ;write to PPU
  INX                   ;set index to next byte
  CPX #$20
  BNE LoadPalettesLoop  ;if x = $20, 32 bytes copied, all done

  LDA #%10000000   ; enable NMI interrupts
  STA $2000

  JSR end_game
; }}}
; main loop {{{
loop:
  INC sleeping
- LDA sleeping
  BNE -

  JSR read_controller1

  LDA game_state
  BNE +
  JSR start_screen_loop
  JMP loop
+ JSR game_loop
  JMP loop
; }}}
; }}}
; nmi interrupt {{{
NMI:
  PHA
  TXA
  PHA
  TYA
  PHA

  LDA game_state
  BEQ reset_sprites
  CMP #$01
  BEQ draw_game
  CMP #$02
  BEQ end_nmi

draw_game:
  LDY #$01
  LDA (head), y
  STA $0200
  LDA #$00
  STA $0201
  LDA #$00
  STA $0202
  LDY #$00
  LDA (head), y
  STA $0203

  LDA apple.y
  STA $0204
  LDA #$07
  STA $0205
  LDA #$00
  STA $0206
  LDA apple.x
  STA $0207
  JMP do_dmi

reset_sprites:
  LDA #$FE
  STA $0200
  STA $0203
  STA $0204
  STA $0207

do_dmi:
  LDA #$00
  STA $2003
  LDA #$02
  STA $4014

end_nmi:
  LDA #$00
  STA sleeping

  PLA
  TAY
  PLA
  TAX
  PLA
  RTI
; }}}
; subroutines {{{
start_screen_loop: ; {{{
  LDX rand_state
  INX
  STX rand_state

handle_start:
  LDA buttons_pressed
  AND #%00010000
  CMP #$00
  BEQ end_start_screen_loop
  JSR start_game

end_start_screen_loop:
  RTS ; }}}
game_loop: ; {{{
handle_up:
  LDA buttons_pressed
  AND #%00001000
  CMP #$00
  BEQ handle_down
  LDA #$00
  STA direction

handle_down:
  LDA buttons_pressed
  AND #%00000100
  CMP #$00
  BEQ handle_left
  LDA #$01
  STA direction

handle_left:
  LDA buttons_pressed
  AND #%00000010
  CMP #$00
  BEQ handle_right
  LDA #$02
  STA direction

handle_right:
  LDA buttons_pressed
  AND #%00000001
  CMP #$00
  BEQ handle_frame
  LDA #$03
  STA direction

handle_frame:
  LDX frame_count
  INX
  STX frame_count
  CPX frame_skip
  BMI end_game_loop

  LDA #$00
  STA frame_count

set_offset:
  LDX #$F8             ; i.e., -8
  LDA direction
  AND #%00000001       ; low bit determines negative or positive
  BEQ set_axis
  LDX #$08

set_axis:
  LDY #$01
  LDA direction
  AND #%00000010       ; high bit determines which axis to change
  BEQ apply_direction
  LDY #$00

apply_direction:
  TXA
  CLC
  ADC (head), y        ; head.x offset by 1 is head.y
  STA (head), y

check_collisions
  LDY #$00
  LDA (head), y
  CMP #$40
  BCC collision
  CMP #$C0
  BCS collision

  LDY #$01
  LDA (head), y
  CMP #$3D
  BCC collision
  CMP #$BD
  BCS collision

  JMP end_game_loop

collision:
  JSR end_game

end_game_loop:
  RTS ; }}}
read_controller1: ; {{{
  ; latch
  LDA #$01
  STA $4016
  LDA #$00
  STA $4016

  ; clock
  LDX #$00
read_controller1_values:
  CPX #$08
  BPL end_read_controller1

  LDA $4016
  AND #%00000001
  ASL buttons_pressed
  ORA buttons_pressed
  STA buttons_pressed
  INX
  JMP read_controller1_values

end_read_controller1:
  RTS ; }}}
start_game: ; {{{
  LDA #$02
  STA game_state

  LDA #$80
  LDY #$00
  STA (head), y
  LDA #$7D
  LDY #$01
  STA (head), y

  JSR rand
  LDA rand_state
  AND #%01111000
  CLC
  ADC #$40
  STA apple.x
  JSR rand
  LDA rand_state
  AND #%01111000
  CLC
  ADC #$3D
  STA apple.y

- BIT $2002
  BPL -

  LDA #%00000000
  STA $2001 ; disable rendering (since this will take longer than vblank)

  LDA #$20
  STA $2006
  LDA #$00
  STA $2006

  LDA #$20
  LDY #$07
-- LDX #$00
- STA $2007
  INX
  CPX #$20
  BNE -
  DEY
  BNE --

  LDX #$00
- LDA game_background_top.w, x
  STA $2007
  INX
  CPX #$20
  BNE -

  LDY #$10
-- LDX #$00
- LDA game_background_middle.w, x
  STA $2007
  INX
  CPX #$20
  BNE -
  DEY
  BNE --

  LDX #$00
- LDA game_background_bottom.w, x
  STA $2007
  INX
  CPX #$20
  BNE -

  LDA #$20
  LDX #$00
- STA $2007
  INX
  CPX #$20
  BNE -

  LDA #%00011000
  STA $2001 ; reenable rendering

  LDA #$01
  STA game_state
  RTS ; }}}
end_game: ; {{{
  LDA #$02
  STA game_state

- BIT $2002
  BPL -

  LDA #%00000000
  STA $2001 ; disable rendering (since this will take longer than vblank)

  LDA #$20
  STA $2006
  LDA #$00
  STA $2006

  LDA #$20
  LDX #$0F
-- LDY #$00
- STA $2007
  INY
  CPY #$20
  BNE -
  DEX
  BNE --

  LDY #$00
- LDA intro_screen, y
  STA $2007
  INY
  CPY #$20
  BNE -

  LDA #$20
  LDX #$0E
-- LDY #$00
- STA $2007
  INY
  CPY #$20
  BNE -
  DEX
  BNE --

  LDA #%00011000
  STA $2001 ; reenable rendering

  LDA #$00
  STA game_state
  RTS ; }}}
rand: ; {{{
  ; linear feedback shift register with taps at 8, 6, 5, and 4
  LDY rand_state
  TYA
  AND #%10000000
  STA rand_state
  TYA
  AND #%00100000
  ASL
  ASL
  EOR rand_state
  STA rand_state
  TYA
  AND #%00010000
  ASL
  ASL
  ASL
  EOR rand_state
  STA rand_state
  TYA
  AND #%00001000
  ASL
  ASL
  ASL
  ASL
  EOR rand_state
  ROL
  STA rand_state
  TYA
  ASL
  ORA rand_state
  STA rand_state
  RTS ; }}}
; }}}
; data {{{
palette: ; {{{
  .db $0F,$31,$32,$33,$0F,$35,$36,$37,$0F,$39,$3A,$3B,$0F,$3D,$3E,$0F
  .db $0F,$1C,$15,$14,$0F,$02,$38,$3C,$0F,$1C,$15,$14,$0F,$02,$38,$3C
; }}}
intro_screen: ; {{{
  .asc "           SNAKE                "
; }}}
game_background_top: ; {{{
  .asc "       ,----------------.       "
; }}}
game_background_middle: ; {{{
  .asc "       |                |       "
; }}}
game_background_bottom: ; {{{
  .asc "       '----------------:       "
; }}}
; }}}
  .orga $FFFA    ;first of the three vectors starts here
; interrupt vectors {{{
  .dw NMI        ;when an NMI happens (once per frame if enabled) the 
                   ;processor will jump to the label NMI:
  .dw RESET      ;when the processor first turns on or is reset, it will jump
                   ;to the label RESET:
  .dw 0          ;external interrupt IRQ is not used in this tutorial
; }}}
; }}}
; chr {{{
  .bank 1 slot 1
  .org $0000
  .incbin "sprites.chr"
; }}}