-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.s
1268 lines (1145 loc) · 25.8 KB
/
main.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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.segment "HEADER" ; what code is
.byte "NES" ; beginning of the NES Header
.byte $1a ; signature of NES Header
.byte $02 ; 2*16 KiB PRG ROM
.byte $01 ; 1*8 KiB CHR ROM
.byte %00000000 ; mapper and mirroring
.byte $00 ; not important in this project
.byte $00
.byte $00
.byte $00
.byte $00, $00, $00, $00, $00 ; filler bytes
.segment "ZEROPAGE"
debug: .res 1
move: .res 1
gameLoaded: .res 1 ; for disabling start after title screen
menuBg: .res 2
gameBg: .res 2
pressing: .res 1
pressingOld: .res 1
position: .res 1 ; 0 - top left, 8 - bottm right
turn: .res 1 ; 0 = x, 1 = o
clearflag: .res 1 ; determines if the game ended
unoccupied: .res 2
square: .res 9
; 00000000 - unoccupied
; 00000001 - X (1)
; 11111111 - O (-1)
.segment "STARTUP" ; where code starts
Reset:
sei ; disables all interrupts on NES
cld ; disable decimal mode - NES doesn't support it
ldx #$40 ; disable sound IRQ (load -> reg. x, store -> 4017)
stx $4017
ldx #$FF ; initialize stack register
txs ; transfer x stack
inx ; ff + 1 = 00
; Clean PPU registers
stx $2000
stx $2001
stx $4010 ; Disable PCM channel
stx gameLoaded
: ; anonymous label
bit $2002; $2002 is telling if PPU is currently drawing
bpl :- ; branch if not in VBLANK (not waiting for another screen)
; :- means go to last anonymous label,
; :+ go to next anonymous label
txa
; Memory clearing
CLEARMEM:
sta $0000, X ; $0000 -> $00FF
sta $0100, X
sta $0300, X
sta $0400, X
sta $0500, X
sta $0600, X
sta $0700, X
; one of the ranges is for sprite data. we can choose any
lda #$FF
sta $0200, X
lda #$00
inx
bne CLEARMEM
; wait for VBLANK again
:
bit $2002
bpl :-
lda #$02 ; MSB of sprite data memory range
sta $4014 ; Object Attribute Memory DMA register
nop ; ppu needs a moment to load data
; preparing ppu memory
lda #$3F
sta $2006
lda #$00
sta $2006
ldx #$00
LoadPalettes:
lda PaletteData, X
sta $2007 ; $3F00, $3F01, and so on
; loop
inx
cpx #$20 ; 32 in decimal
bne LoadPalettes
ldx #$00
LoadSprites:
lda SpriteData, X
sta $0200, X
inx
cpx #$20
bne LoadSprites
LoadMenu:
; initialize world variable to point to world data
lda #<MenuData
sta menuBg
lda #>MenuData
sta menuBg+1
; setup address in PPU for nametable data
bit $2002
lda #$20
sta $2006
lda #$00
sta $2006
ldx #$00
ldy #$00
LoadMenuLoop:
lda (menuBg), Y
sta $2007
iny
; 960 px is 03 C0 in hex
cpx #$03
bne :+
cpy #$C0
beq MenuDone
:
cpy #$00
bne LoadMenuLoop
inx
inc menuBg+1
jmp LoadMenuLoop
MenuDone:
ldx #$00
LoadGame:
; initialize world variable to point to world data
lda #<GridData
sta gameBg
lda #>GridData
sta gameBg+1
; setup address in PPU for nametable data
bit $2002
lda #$28
sta $2006
lda #$00
sta $2006
ldx #$00
ldy #$00
LoadGameLoop:
lda (gameBg), Y
sta $2007
iny
; 960 px is 03 C0 in hex
cpx #$03
bne :+
cpy #$C0
beq FinishLoadingGame
:
cpy #$00
bne LoadGameLoop
inx
inc gameBg+1
jmp LoadGameLoop
FinishLoadingGame:
lda #$04 ; default arrow position - middle tile
; will be changed on keyboard hits
sta position
ldx #$00
stx turn
inx
stx move
cli ; enable interrupts
lda #%10000000 ; enable NMI
sta $2000
; enable drawing (but no sprites -> 4th bit)
lda #%00001110
sta $2001
Forever:
jsr CheckController
jsr EndgameConditions
jmp Forever ; game loop
; ; ; ; ; ; ; ; ; ; ; ;
; SUBROUTINES SECTION ;
; ; ; ; ; ; ; ; ; ; ; ;
CheckController:
lda #$01
sta $4016 ; strobe controller
ldx #$00
stx $4016 ; latch controller status
lda pressing
sta pressingOld
ConLoop:
; reading 4016 eight times in order to check buttons.
; we use carry flag to write button status to variables
; first we use lsr to shift LSB to carry flag, then
; we use ROR to shift 0 to carry and carry to variable.
lda $4016 ; %0000001
lsr
ror pressing ; RLDUSsBA
inx
cpx #$08
bne ConLoop
CheckRight:
lda pressing
eor pressingOld
and pressing
and #%10000000
beq CheckLeft
jsr MoveRight
CheckLeft:
lda pressing
eor pressingOld
and pressing
and #%01000000
beq CheckDown
jsr MoveLeft
CheckDown:
lda pressing
eor pressingOld
and pressing
and #%00100000
beq CheckUp
jsr MoveDown
CheckUp:
lda pressing
eor pressingOld
and pressing
and #%00010000
beq CheckStart
jsr MoveUp
CheckStart:
lda pressing
eor pressingOld
and pressing
and #%00001000
beq CheckA
jsr StartGame
CheckA:
lda pressing
eor pressingOld
and pressing
and #%00000001
beq EndController
jsr PressA
; CheckB:
; lda pressing
; eor pressingOld
; and pressing
; and #%00000010
; beq EndController
; jsr PressB
EndController:
rts
MoveRight:
lda $0203
cmp #$B4 ; 180 pixels, max right position
bne :+
sbc #$0070 ; go max left
sta $0203
dec position
dec position
rts
:
adc #$0038 ; 56 pixels (7 tiles) to the right
sta $0203
inc position
rts
MoveLeft:
lda $0203
cmp #$44 ; 68 pixels, max left position
bne :+
lda #$B4
sta $0203
inc position
inc position
rts
:
sbc #$0038
sta $0203
dec position
rts
MoveUp:
lda $0200
cmp #$32 ; 50 pixels, max up position
bne :+
lda #$A2
sta $0200
inc position
inc position
inc position
inc position
inc position
inc position
rts
:
sbc #$0038
sta $0200
dec position
dec position
dec position
rts
MoveDown:
lda $0200
cmp #$A2 ; 162 pixels, max down position
bne :+
lda #$32
sta $0200
dec position
dec position
dec position
dec position
dec position
dec position
rts
:
adc #$0038
sta $0200
inc position
inc position
inc position
rts
StartGame:
lda #$01
cmp gameLoaded
bne :+
rts ; back if game already loaded
: sta gameLoaded
LoadGrid:
bit $2002
lda #$00
sta $2005
lda #$EF ; scroll to 2nd screen
sta $2005
lda #%10000000 ; default values
sta $2000
lda #%00011110 ; enable sprites
sta $2001
rts
PressA:
; check if in main menu
lda gameLoaded
cmp #$00
bne :+
rts
; check whose turn
: ldx turn
cpx #$00
bne :+
; this happens when turn=0 (X turn)
inx
stx turn
jsr DrawX
jsr LoadGrid ; load defaults
inc move
rts
; this happens when turn=1 (O turn)
: dex
stx turn
jsr DrawO
jsr LoadGrid
inc move
rts
; PressB:
; lda gameLoaded
; cmp #$00
;bne :+
;rts
; : jsr DrawO
; jsr LoadGrid ; load defaults
; rts
DrawX:
lda #$00
cmp position
bne :+
jmp DrawX0
:
lda #$01
cmp position
bne :+
jmp DrawX1
:
lda #$02
cmp position
bne :+
jmp DrawX2
:
lda #$03
cmp position
bne :+
jmp DrawX3
:
lda #$04
cmp position
bne :+
jmp DrawX4
:
lda #$05
cmp position
bne :+
jmp DrawX5
:
lda #$06
cmp position
bne :+
jmp DrawX6
:
lda #$07
cmp position
bne :+
jmp DrawX7
:
lda #$08
cmp position
bne :+
jmp DrawX8
:
DrawX0:
lda #$01
sta square
ldx #$00
bit $2002
: lda #$28 ; high byte = 28
cpx #$04 ; high byte = 29
bmi :+
adc #$00 ; cpx sets carry to 1 so its +1 actually
: sta $2006
lda topleft, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX1:
lda #$01
sta square+1
ldx #$00
bit $2002
: lda #$28
cpx #$04
bmi :+
adc #$00
: sta $2006
lda top, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX2:
lda #$01
sta square+2
ldx #$00
bit $2002
: lda #$28
cpx #$04
bmi :+
adc #$00
: sta $2006
lda topright, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX3:
lda #$01
sta square+3
ldx #$00
bit $2002
: lda #$29
cpx #$08
bmi :+
adc #$00
: sta $2006
lda left, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX4:
lda #$01
sta square+4
ldx #$00
bit $2002
: lda #$29
cpx #$08
bmi :+
adc #$00
: sta $2006
lda center, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX5:
lda #$01
sta square+5
ldx #$00
bit $2002
: lda #$29
cpx #$08
bmi :+
adc #$00
: sta $2006
lda right, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX6:
lda #$01
sta square+6
ldx #$00
bit $2002
: lda #$2a
cpx #$0c
bmi :+
adc #$00
: sta $2006
lda bottomleft, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX7:
lda #$01
sta square+7
ldx #$00
bit $2002
: lda #$2a
cpx #$0c
bmi :+
adc #$00
: sta $2006
lda bottom, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawX8:
lda #$01
sta square+8
ldx #$00
bit $2002
: lda #$2a
cpx #$0c
bmi :+
adc #$00
: sta $2006
lda bottomright, X
sta $2006
lda xspr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO:
lda #$00
cmp position
bne :+
jmp DrawO0
:
lda #$01
cmp position
bne :+
jmp DrawO1
:
lda #$02
cmp position
bne :+
jmp DrawO2
:
lda #$03
cmp position
bne :+
jmp DrawO3
:
lda #$04
cmp position
bne :+
jmp DrawO4
:
lda #$05
cmp position
bne :+
jmp DrawO5
:
lda #$06
cmp position
bne :+
jmp DrawO6
:
lda #$07
cmp position
bne :+
jmp DrawO7
:
lda #$08
cmp position
bne :+
jmp DrawO8
:
DrawO0:
lda #$FF
sta square
ldx #$00
bit $2002
: lda #$28 ; high byte = 28
cpx #$04 ; high byte = 29
bmi :+
adc #$00 ; cpx sets carry to 1 so its +1 actually
: sta $2006
lda topleft, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO1:
lda #$FF
sta square+1
ldx #$00
bit $2002
: lda #$28
cpx #$04
bmi :+
adc #$00
: sta $2006
lda top, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO2:
lda #$FF
sta square+2
ldx #$00
bit $2002
: lda #$28
cpx #$04
bmi :+
adc #$00
: sta $2006
lda topright, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO3:
lda #$FF
sta square+3
ldx #$00
bit $2002
: lda #$29
cpx #$08
bmi :+
adc #$00
: sta $2006
lda left, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO4:
lda #$FF
sta square+4
ldx #$00
bit $2002
: lda #$29
cpx #$08
bmi :+
adc #$00
: sta $2006
lda center, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO5:
lda #$FF
sta square+5
ldx #$00
bit $2002
: lda #$29
cpx #$08
bmi :+
adc #$00
: sta $2006
lda right, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO6:
lda #$FF
sta square+6
ldx #$00
bit $2002
: lda #$2a
cpx #$0c
bmi :+
adc #$00
: sta $2006
lda bottomleft, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO7:
lda #$FF
sta square+7
ldx #$00
bit $2002
: lda #$2a
cpx #$0c
bmi :+
adc #$00
: sta $2006
lda bottom, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
DrawO8:
lda #$FF
sta square+8
ldx #$00
bit $2002
: lda #$2a
cpx #$0c
bmi :+
adc #$00
: sta $2006
lda bottomright, X
sta $2006
lda ospr, X
sta $2007
inx
cpx #$10
bne :--
rts
EndgameConditions:
; if clearflag is set then this has already
; been done and we wait for vblank so we can
; return
lda clearflag
cmp #$00
beq CheckRow1
rts
; 00000000 - unoccupied
; 00000001 - X (1)
; 11111111 - O (-1)
; we check if any sum is equal to 3
; or -3
CheckRow1:
lda square
clc
adc square+1
clc
adc square+2
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckRow2:
lda square+3
clc
adc square+4
clc
adc square+5
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckRow3:
lda square+6
clc
adc square+7
clc
adc square+8
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckCol1:
lda square
clc
adc square+3
clc
adc square+6
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckCol2:
lda square+1
clc
adc square+4
clc
adc square+7
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckCol3:
lda square+2
clc
adc square+6
clc
adc square+8
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckSlash:
lda square
clc
adc square+4
clc
adc square+8
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckBackSlash:
lda square+2
clc
adc square+4
clc
adc square+6
cmp #$03
beq WinX
cmp #$FD ; -3 in U2
beq WinO
CheckDraw:
lda move
cmp #$09
beq Draw
; all conditions false - return
rts
WinX:
; @ Increment X score
inc $0205
jsr ResetGrid
rts
WinO:
; @ Increment O score
inc $0209
jsr ResetGrid
rts
Draw:
; do not increment anything
jsr ResetGrid
rts
ResetGrid:
ldx #$01
stx clearflag
dex
stx move
rts
; deleting Xs and Os graphics from grid
ClearSquares:
; here we have to clear all variables
; flip turn to other value
; clear square variable
lda #$00
sta clearflag
ldy #$00
: sta square, Y
iny
cpy #$09
bne :-
; first row
ldx #$00
bit $2002
lda #$28
sta $2006
lda #$e7 ; low byte top left position
sta $2006
lda #$00
jsr ClearRow