-
Notifications
You must be signed in to change notification settings - Fork 3
/
tetris.asm
1133 lines (1032 loc) · 28.5 KB
/
tetris.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
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
/*
tetris.asm (C=64)
=================
Christian Jauvin
June 2012
http://christianjauv.in
Assemble into a ".prg" with KickAssembler (http://theweb.dk/KickAssembler),
and optionally run with x64 (from the VICE emu suite):
$ java -jar KickAss.jar tetris.asm [-execute x64]
With the "-execute" option, it should autoload/start.
Keyboard controls:
'A': left
'D': right
'W': rotate
'S': speed up fall
ZP pointers (for data structures requiring indirect indexing):
$f7: start of current video page (flips between $0400 and $0800)
$f9: buffer of "frozen" cells (outline grid + fixed pieces)
$fb: current piece data
$fd: current piece data state
$b0: multi-purpose helper ptr1
$b2: multi-purpose helper ptr2
$b4: multi-purpose helper ptr3
*/
:BasicUpstart2(main) // autostart macro
.pc = $2000 "Variables and data"
frozen: .fill 1000, 0 // frozen cells (bools)
use_page_flipping:
.byte 1 // set to 0 to turn off
is_page_flipping_required:
.byte 0
d018_value: .byte 0
pos: .word 0 // lowbyte: row (0 to 24), hibyte: col (0 to 39)
pos_ahead: .word 0 // lateral move lookahead for collision detection
state: .byte 0 // piece rotation lookahead for collision detection
state_ahead: .byte 0
color: .byte 0
z: .byte 0 /* used to map row/col piece indices to a single value (z = x * 4 + j)
I suspect that there might a clever bitwise way of doing it (that wouldn't
require a variable at all, but for the moment that will dd)
*/
timer1: .byte 0, 30 // falling animation (current value, target)
timer2: .byte 0, 8 // keyboard check (idem)
is_falling: .byte 0 // bool
check_keyboard: .byte 0 // bool
var_add0: .word 0 // used by math.asm add2 and add3
var_add1: .word 0
var_add2: .word 0
var_add3: .word 0
is_w_key_pressed:
.byte 0 // bool, to debounce
is_s_key_pressed:
.byte 0 // idem
grid_outline_color:
.byte 15 // light grey
// piece data (tetrominos)
piece_i:
.byte 2 // number of states
.byte 2 // color
.byte 0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0 // |
.byte 0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0 // -
piece_s:
.byte 2
.byte 3
.byte 0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0
.byte 0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0
piece_z:
.byte 2
.byte 4
.byte 0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0
.byte 0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0
piece_t:
.byte 4
.byte 5
.byte 0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0
.byte 0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0
.byte 0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0
.byte 0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0
piece_o:
.byte 1
.byte 6
.byte 0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0
piece_l:
.byte 4
.byte 7
.byte 0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0
.byte 0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0
.byte 1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0
.byte 0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0
piece_j:
.byte 4
.byte 8
.byte 0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0
.byte 1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0
.byte 0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0
.byte 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0
.import source "math.asm"
//////////////////////////////////////////////////////////////////////
// set kbd and falling anim flags according to timers
raster_interrupt_handler:
pha // not sure if register state preserving is really needed?
txa
pha
tya
pha
lda is_page_flipping_required // commit page flipping if required
beq check_timer1
lda d018_value
sta $d018
lda #0
sta is_page_flipping_required
check_timer1:
lda timer1
cmp timer1+1
bne !wait+
lda #1 // timer1 has reached its target value
sta is_falling // set falling animation flag
lda #0
sta timer1 // reset animation timer
jmp check_timer2
!wait:
inc timer1
check_timer2:
lda timer2
cmp timer2+1
bne !wait+
lda #1
sta check_keyboard // set check kbd flag
lda #0
sta timer2 // reset kbd check timer
jmp !done+
!wait:
inc timer2
!done:
lda #$ff // needed?
sta $d019
pla
tay
pla
tax
pla
rti
//////////////////////////////////////////////////////////////////////
// toggles video ram between $0400 and $0800 (actually just engage it,
// as the actual toggling need to be performed in the raster irq)
flip_page:
lda use_page_flipping
bne !continue+
rts
!continue:
lda $f8
eor #%00001100 // flip video addr hibyte between $04 and $08
sta $f8 // (the lowbyte stays $00)
lda $d018
eor #%00110000 // flip the $d018 video ptr value, but don't set it
sta d018_value // right away: wait until until in the irq
lda #1
sta is_page_flipping_required
rts
//////////////////////////////////////////////////////////////////////
// draw piece in current video ram, at current position (pos)
draw_piece:
// jump to video location: base_loc + (pos[0] * 40) + pos[1]
lda $f7 // var_add0 = ($f7)
sta var_add0
lda $f8
sta var_add0+1
lda pos
ldx #40
jsr mult
stx var_add1
sta var_add1+1 // var_add1 = pos[0] * 40
lda pos+1
sta var_add2
lda #0
sta var_add2+1 // var_add2 = pos[1]
jsr add3 // var_add3 = var_add0 + var_add1 + var_add2
lda var_add3
sta $b0 // working ptr (will be incremented)
lda var_add3+1
sta $b1
lda #$00 // ptr to start of color ram
sta var_add0
lda #$d8
sta var_add0+1
lda pos
ldx #40
jsr mult
stx var_add1
sta var_add1+1 // var_add1 = pos[0] * 40
lda pos+1
sta var_add2
lda #0
sta var_add2+1 // var_add2 = pos[1]
jsr add3 // var_add3 = var_add0 + var_add1 + var_add2
lda var_add3
sta $b2 // working ptr (will be incremented)
lda var_add3+1
sta $b3
lda #0
sta z // 0 to 15 (x * 4 + y)
ldx #0 // 0 to 3
!row_x:
ldy #0 // 0 to 3
!col_y:
tya
pha // push y on stack (to let the z var use y)
ldy z
lda ($fd),y // use piece data offset (k)
beq !cell_off+
pla // pop y back
tay
lda #160
sta ($b0),y
lda color
sta ($b2),y
jmp !continue+
!cell_off:
pla // pop y back
tay
!continue:
inc z
iny
cpy #4
bne !col_y-
lda $b0 // += 40 (i.e. go to next line)
sta var_add0
lda $b1
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $b0
lda var_add2+1
sta $b1
lda $b2 // += 40 (i.e. go to next line)
sta var_add0
lda $b3
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $b2
lda var_add2+1
sta $b3
inx
cpx #4
bne !row_x-
rts
//////////////////////////////////////////////////////////////////////
// functions to add grid outline cells to the "frozen" buffer,
// to ease collision detection
init_grid_outline_side:
ldx #0
ldy #0
!loop:
clc
lda $f9
adc #40
sta $f9
lda $fa
adc #00
sta $fa
lda $b0
adc #40
sta $b0
lda $b1
adc #00
sta $b1
lda #1
sta ($f9),y
lda grid_outline_color
sta ($b0),y
inx
cpx #21
bne !loop-
rts
init_grid_outline:
// (1) bottom
lda #1 /// cell on
ldx #0
!loop:
sta $23a6,x // frozen + (23 * 40) + 14
lda grid_outline_color
sta $dba6,x // grey
inx
cpx #12
bne !loop-
// (2) left
lda #$36 // 8192 + (1 * 40) + 14
sta $f9
lda #$20
sta $fa
lda #$36 // color ram
sta $b0
lda #$d8
sta $b1
jsr init_grid_outline_side
// (3) right
lda #$41 // 8192 + (1 * 40) + 25
sta $f9
lda #$20
sta $fa
lda #$41
sta $b0
lda #$d8
sta $b1
jsr init_grid_outline_side
rts
//////////////////////////////////////////////////////////////////////
// set cells off in frozen buffer (in 4 blocks of 250 cells)
clear_frozen:
lda #<frozen
sta $f9
lda #>frozen
sta $fa
ldx #0
!xloop4:
ldy #0
!yloop250:
lda #0 // cell off
sta ($f9),y
iny
cpy #250
bne !yloop250-
// switch to next block of 250 cells
lda $f9 // frozen pointer += 250
sta var_add0
lda $fa
sta var_add0+1
lda #250
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $f9
lda var_add2+1
sta $fa
inx
cpx #4
bne !xloop4-
rts
//////////////////////////////////////////////////////////////////////
// redraw video while adding current frozen cells (in 4 blocks of 250 cells)
redraw_screen:
lda $f7 // copy video ptr to working ptr
sta $b0
lda $f8
sta $b1
lda #<frozen
sta $f9
lda #>frozen
sta $fa
ldx #0
!xloop4:
ldy #0
!yloop250:
lda ($f9),y // if frozen cell at location..
bne cell_on // set cell on
lda #32 // if not, set cell off
jmp !continue+
cell_on:
lda #160
!continue:
sta ($b0),y
iny
cpy #250
bne !yloop250-
// switch to next block of 250 cells
lda $b0 // current video pointer += 250
sta var_add0
lda $b1
sta var_add0+1
lda #250
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $b0
lda var_add2+1
sta $b1
lda $f9 // frozen pointer += 250
sta var_add0
lda $fa
sta var_add0+1
lda #250
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $f9
lda var_add2+1
sta $fa
inx
cpx #4
bne !xloop4-
rts
//////////////////////////////////////////////////////////////////////
/*
Input:
acc: 0=down, 1=left, 2=right, 3=rotate
Output:
acc=bool
*/
can_move:
ldx pos // pos_ahead is the queried possible next position
ldy pos+1
stx pos_ahead
sty pos_ahead+1
ldx state
stx state_ahead
cmp #0
beq test_down
cmp #1
beq test_left
cmp #2
beq test_right
cmp #3
beq test_rotate
test_down:
inc pos_ahead
jmp !continue+
test_left:
dec pos_ahead+1
jmp !continue+
test_right:
inc pos_ahead+1
jmp !continue+
test_rotate:
inc state_ahead
lda state_ahead
ldy #0
cmp ($fb),y
bne !inc_state_ahead+
sty state_ahead // reset state to 0
!inc_state_ahead:
ldy #1 // use state_ahead
jsr update_piece_data_pointer
!continue:
// jump to video location: base_loc + (pos[0] * 40) + pos[1]
lda $f7 // var_add0 = ($f7)
sta var_add0
lda $f8
sta var_add0+1
lda pos_ahead
ldx #40
jsr mult
stx var_add1
sta var_add1+1 // var_add1 = pos[0] * 40
lda pos_ahead+1
sta var_add2
lda #0
sta var_add2+1 // var_add2 = pos[1]
jsr add3 // var_add3 = var_add0 + var_add1 + var_add2
lda var_add3
sta $b0
lda var_add3+1
sta $b1
// jump to frozen location: base_loc + (pos[0] * 40) + pos[1]
lda #<frozen
sta var_add0
lda #>frozen
sta var_add0+1
lda pos_ahead
ldx #40
jsr mult
stx var_add1
sta var_add1+1
lda pos_ahead+1
sta var_add2
lda #0
sta var_add2+1
jsr add3
lda var_add3
sta $f9
lda var_add3+1
sta $fa
lda #0
sta z // 0 to 15 (x * 4 + y)
ldx #0 // 0 to 3
!row_x:
ldy #0 // 0 to 3
!col_y:
lda ($f9),y // is there a frozen cell at loc?
beq !continue+ // if not, no collision possible, continue
tya
pha // push y on stack (to let the z var use y)
ldy z
lda ($fd),y // use piece data offset (k): is there also a piece cell at location (there's already a frozen cell)?
beq !no_piece_coll+ // there's a frozen cell, but no piece cell, so no collision finally: pop y from stack, and continue
pla // clear stack of its item (very important!)
ldy #0 // restore normal state ptr
jsr update_piece_data_pointer
lda #0
rts // collision detected, return right away
!no_piece_coll:
pla // pop y back
tay
!continue:
inc z
iny
cpy #4
bne !col_y-
lda $b0 // current video ptr += 40 (i.e. go to next line)
sta var_add0
lda $b1
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $b0
lda var_add2+1
sta $b1
lda $f9 // frozen ptr += 40 (i.e. go to next line)
sta var_add0
lda $fa
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $f9
lda var_add2+1
sta $fa
inx
cpx #4
bne !row_x-
ldy #0 // restore normal state ptr
jsr update_piece_data_pointer
lda #1 // no collision found
rts
//////////////////////////////////////////////////////////////////////
// make $fd point to piece data (with respect to piece and state vars)
// y: 0=use state, 1=use state_ahead
update_piece_data_pointer:
lda $fb
sta $fd
lda $fc
sta $fe
lda $fd
sta var_add0
lda $fe
sta var_add0+1
cpy #1
beq !use_state_ahead+
lda state
jmp !continue+
!use_state_ahead:
lda state_ahead
!continue:
ldx #16
jsr mult
inx
inx
stx var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $fd
lda var_add2+1
sta $fe
rts
//////////////////////////////////////////////////////////////////////
// transfer a piece to the frozen buffer
freeze_piece:
// jump to frozen location: base_loc + (pos[0] * 40) + pos[1]
lda #<frozen
sta var_add0
lda #>frozen
sta var_add0+1
lda pos
ldx #40
jsr mult
stx var_add1
sta var_add1+1 // var_add1 = pos[0] * 40
lda pos+1
sta var_add2
lda #0
sta var_add2+1 // var_add2 = pos[1]
jsr add3 // var_add3 = var_add0 + var_add1 + var_add2
lda var_add3
sta $f9 // working ptr (will be incremented)
lda var_add3+1
sta $fa
lda #0
sta z // 0 to 15 (i * 4 + j)
ldx #0 // 0 to 3
!row_x:
ldy #0 // 0 to 3
!col_y:
tya
pha // push y on stack (to let the z var use y)
ldy z
lda ($fd),y // use piece data offset (k)
beq !cell_off+
pla // pop y back
tay
lda #1 // cell on
sta ($f9),y
jmp !continue+
!cell_off:
pla // pop y back
tay
!continue:
inc z
iny
cpy #4
bne !col_y-
lda $f9 // loc += 40 (i.e. go to next line)
sta var_add0
lda $fa
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr add2
lda var_add2
sta $f9
lda var_add2+1
sta $fa
inx
cpx #4
bne !row_x-
rts
//////////////////////////////////////////////////////////////////////
// (1) $f9/$b0: frozen curr / frozen 1r above
// (2) $b2/$b4: color curr / color 1r above
// $f9/$b2 -= #40
shift_curr_ptrs_to_row_above:
lda $f9
sta var_add0
lda $fa
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr sub2
lda var_add2
sta $f9
lda var_add2+1
sta $fa
lda $b2
sta var_add0
lda $b3
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr sub2
lda var_add2
sta $b2
lda var_add2+1
sta $b3
rts
// $b0/$b4 -= #40
shift_1ra_ptrs_to_row_above:
lda $b0
sta var_add0
lda $b1
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr sub2
lda var_add2
sta $b0
lda var_add2+1
sta $b1
lda $b4
sta var_add0
lda $b5
sta var_add0+1
lda #40
sta var_add1
lda #0
sta var_add1+1
jsr sub2
lda var_add2
sta $b4
lda var_add2+1
sta $b5
rts
// x is already set at proper value (row at which to begin the collapse)
collapse:
lda $f9 // get frozen helper ptr, pointing one row above
sta $b0
lda $fa
sta $b1
lda $b2
sta $b4
lda $b3
sta $b5
jsr shift_1ra_ptrs_to_row_above
!row_x:
ldy #0
!col_y:
lda ($b0),y // get what's one row above
sta ($f9),y // and copy it to current row
lda ($b4),y // same for color
sta ($b2),y
iny
cpy #10
bne !col_y-
jsr shift_curr_ptrs_to_row_above
jsr shift_1ra_ptrs_to_row_above
inx
cpx #20
bne !row_x-
rts
clear_rows:
lda #$7f // start at bottom row: frozen + (22 * 40) + 15
sta $f9
lda #$23
sta $fa
lda #$7f // idem for color ram: $d800 + (22 * 40) + 15
sta $b2
lda #$db
sta $b3
ldx #0
!row_x:
ldy #0 // cols
!col_y:
lda ($f9),y
beq incomplete // hole found, go to next row (above)
iny
cpy #10
bne !col_y-
complete:
//inc $d020 // debug: change border color
jsr collapse
jmp clear_rows // we possibly need to redo it up to 4 times
incomplete:
jsr shift_curr_ptrs_to_row_above
inx
cpx #20
bne !row_x-
rts
//////////////////////////////////////////////////////////////////////
get_random_number:
lda $d012
eor $dc04
sbc $dc05
rts
//////////////////////////////////////////////////////////////////////
pick_random_piece:
jsr get_random_number
and #%00000111
cmp #%00000000
bne !next+
ldx #<piece_i
ldy #>piece_i
jmp !found+
!next:
cmp #%00000001
bne !next+
ldx #<piece_s
ldy #>piece_s
jmp !found+
!next:
cmp #%00000010
bne !next+
ldx #<piece_z
ldy #>piece_z
jmp !found+
!next:
cmp #%00000011
bne !next+
ldx #<piece_t
ldy #>piece_t
jmp !found+
!next:
cmp #%00000100
bne !next+
ldx #<piece_o
ldy #>piece_o
jmp !found+
!next:
cmp #%00000101
bne !next+
ldx #<piece_l
ldy #>piece_l
jmp !found+
!next:
cmp #%00000110
bne pick_random_piece // xxxxx111
ldx #<piece_j
ldy #>piece_j
!found:
stx $fb
sty $fc
// set_piece_color
ldy #1
lda ($fb),y // to change: 4
//lda #13
sta color
rts
//////////////////////////////////////////////////////////////////////
// With the value reached by $f9 (ptr in frozen buffer) in the last
// call to can_move, we know where the last piece was set; if it's
// less than a certain value, we know we are too high in the grid, and
// can thus set the game over flag (which will simply restart the game)
is_game_over:
lda $fa // hibyte first
cmp #$20
bne no
lda $f9 // lowbyte
cmp #$f0
bcs no // if ($fc) is <= $f0 (6 rows from the top): game over
lda #1
rts
no:
lda #0
rts
//////////////////////////////////////////////////////////////////////
// start point
main:
lda #0
sta $d020
sta $d021 // screen colors
// clear frozen buffer (in case the game is restarting after game over)
jsr clear_frozen // clear frozen buffer (in case the game is restarting after game over)
lda #$00
sta $f7
lda #$04
sta $f8
jsr redraw_screen // clear page 0
lda #$00
sta $f7
lda #$08
sta $f8
jsr redraw_screen // clear page 1 (that's the one we're first pointing to)
jsr init_grid_outline
jsr pick_random_piece
// reset piece pos and state
lda #0
sta pos
lda #18 // starting pos: row=0, col=18
sta pos+1
lda #0
sta state
ldy #0 // use state (i.e. not state_ahead)
jsr update_piece_data_pointer
jsr redraw_screen
jsr draw_piece
jsr flip_page
// interrupt handler, taken from: http://codebase64.org/doku.php?id=base:introduction_to_raster_irqs
sei //disable maskable IRQs
lda #$7f
sta $dc0d //disable timer interrupts which can be generated by the two CIA chips
sta $dd0d //the kernal uses such an interrupt to flash the cursor and scan the keyboard, so we better
//stop it.
lda $dc0d //by reading this two registers we negate any pending CIA irqs.
lda $dd0d //if we don't do this, a pending CIA irq might occur after we finish setting up our irq.
//we don't want that to happen.
lda #$01 //this is how to tell the VICII to generate a raster interrupt
sta $d01a
lda #$fa //this is how to tell at which rasterline we want the irq to be triggered
sta $d012
lda #$1b //as there are more than 256 rasterlines, the topmost bit of $d011 serves as
sta $d011 //the 8th bit for the rasterline we want our irq to be triggered.
//here we simply set up a character screen, leaving the topmost bit 0.
lda #$35 //we turn off the BASIC and KERNAL rom here
sta $01 //the cpu now sees RAM everywhere except at $d000-$e000, where still the registers of
//SID/VICII/etc are visible
lda #<raster_interrupt_handler //this is how we set up
sta $fffe //the address of our interrupt code
lda #>raster_interrupt_handler
sta $ffff
cli //enable maskable interrupts again
//////////////////////////////////////////////////////////////////////
main_loop:
lda is_falling
beq *+5
jmp do_fall
lda check_keyboard
bne scan_keyboard
jmp main_loop
scan_keyboard:
// non-debounced keys (A/D)
lda #$fd // check A key
sta $dc00
lda $dc01
cmp #$fb
bne *+5
jmp do_left
lda #$fb // check D key
sta $dc00
lda $dc01