-
Notifications
You must be signed in to change notification settings - Fork 2
/
tetris.asm
2264 lines (2147 loc) · 58.1 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 for ALTAIR 8800
;; ----------------------
;;
;; MIT License
;;
;; Copyright (c) 2022 Paul Hatchman
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;
; BUILD OPTIONS
; Note the IF x - 1 syntax (means NOT x) for the conditional assembly.
; This format is used to have code compile under both linux (asl assembler)
; and CPM as they use different conditional logic.
CPM EQU 1 ; Set to 0 for building without CPM.
; Will load to address 0 and HLT on exit
ALTSHAPECHARS EQU 0 ; If set to 1, then use all
; hashes for the tetromino chars
DAZZLER EQU 0 ; Set to 1 for dazzler support
DEBUG EQU 0 ; Includes some debug/helper routines
; Serial Port Configuration
; - Currently only works on SIO-2
; - SIO doesn't just use different status bits,
; the status logic is also inverted compared to SIO-2
SIOSTAT EQU 16 ; status port
SIODATA EQU 17 ; data port
SIOREADY EQU 2 ; output ready mask
SIOAVAIL EQU 1 ; input avail mask
; ARENA (Playing Field) Definition
ARENAW EQU 10 ; Arena is 10 blocks wide
ARENADN EQU (ARENAW+5) ; Offset to move down one row in arena
; Arena width+4 side chars+1 null char
ARENAH EQU 20 ; Arena is 20 blocks high
ARENALST EQU (ARENADN*(ARENAH-1) + 2)
; offset to first col of last arena line
IF DAZZLER-1
ARENAY EQU 2 ; Top / left arena draw location
ARENAX EQU 0 ; in screen co-ordinates
; Make sure to change these if ARENAX or ARENAY are changed
MINUSARENAY EQU 0FEh ; Negative of ARENAY. The CPM assembler
; doesn't like -ARENAY for ADI
MINUSARENAX EQU 000h ; Negative of ARENAX
SPAWNYX EQU 0206h ; Spawn on row 2, col 6
; except for I shape, which is col 5
ENDIF
IF DAZZLER
ARENAY EQU 6 ; Top / left arena draw location
ARENAX EQU 9
; Make sure to change these if ARENAX or ARENAY are changed
MINUSARENAY EQU 0FAh ; Negative of ARENAY. The CPM assembler
; doesn't like -ARENAY for ADI
MINUSARENAX EQU 0F7h ; Negative of ARENAX
SPAWNYX EQU 060Fh ; Spawn on row 6, column 15
; except for I shape, which is col 14
; DAZZLER-specific
VIDEO EQU 1400h ; VRAM location (1400h = 5k)
NRCLRS EQU 12 ; Nr of colour table entries (clrtbl)
ENDIF
; Make these speed values higher if you want a bigger challenge
; However, the speed gets fast pretty quickly as it is, so suggest to leave them
SPEEDINIT EQU 0 ; initial speed
SPEEDINC EQU 1 ; The value to increment speed each time
; a shape is locked in place
; Must be a power of 2
HDSCORE EQU 2 ; + score each row that was hard-dropped
SDSCORE EQU 1 ; + score each row that was soft-dropped
;
; About DAZZLER Support
;
; Uses the 512k vram, 32x32 pixel, colour mode.
; It is implemented by converting the ascii output of the SIO version into
; coloured pixels to be written to the DAZZLER vram. Each character that would
; normally be output to the SIO is mapped to a colour and stored in the vram.
; The cursor movement outputs are stored as pointers into the associated VRAM
; address.
; While this is not the most efficient way create a tetris for the DAZZLER,
; it does provide a relatively simple way to use the same source code for
; the SIO and DAZZLER versions, with only minor conditional assembly
;
; Output the should still go to the serial port must use the outstrsio and
; outchsio subroutines.
;
;; START OF CODE ;;
IF CPM
org 100h
ENDIF
IF CPM-1 ; Logic done this way to be compatible
org 000h ; with CPM ASM and the ASL assembler
ENDIF
; Set up stack
lxi h,0
dad sp ; get current stack
shld stacko ; save as original stack
lxi sp,stack ; set new stack
lxi h,invis ; set cursor invisible
call outstrsio
IF DAZZLER
call dazinit
ENDIF
nwgame: lxi h,clr ; clear screen
call outstrsio
IF DAZZLER - 1 ; Only output in the SIO version
lxi d,0000h
call csrpossio ; Set cursor to 0,0
lxi h,scrstr ; Print "SCORE"
call outstrsio
ENDIF
mvi a,0
sta score ; reset score
sta score+1
sta score+2
sta nrclr ; Reset number of cleared rows
call displayscore ; Show score, draw arena
call drawarena ; and display help
call displaycontrols
; Wait for "S - START or Q - QUIT" keys
wait4s:
lxi h,seed1 ; increment random seed1 for
mvi a,1 ; each wait loop
add m ; We leave seed2 as a constant
mov m,a
inx h
mvi a,0
adc m
mov m,a
call inch
jz wait4s ; no char received, try again
call toupper
cpi 'S'
jnz wait4q ; Did the user press S - START?
call drawarena ; then blank out the controls text
lda seed1 ; make sure seed 1 is not 0 as
ora a ; will break rng
jnz gameloop
inr a
sta seed1
jmp gameloop ; Start the game
wait4q: cpi 'Q' ; is this 'quit'?
jnz wait4s
jmp done ; Quit
; Main Game Loop
; Create a random shape in the start position
; Each "tick", move it down until it collides
; with another shape or the arena floor
gameloop:
lda nrclr ; was something cleared this turn?
ora a ; Set Z flag
jz nodrop ; If nothing cleared, then continue
call delay ; delay half a cycle
call droprows ; Remove cleared rows and drop higher
; rows down.
nodrop: call spawnshape ; spawn a new, random shape
iploop: ; input loop for processing keystrokes and
; then dropping the shape 1 row
lda updscr ; has the score been changed?
ora a
jz skpscr ; if not, skip
call displayscore ; otherwise update the score
xra a
sta updscr ; and set updscr = FALSE
skpscr: call inputdelay ; Process input for one game "tick"
jc lock ; CY set if a collision from user
; performing a soft or hard drop
; If collided, lock shape in place
call movdown ; Otherwise, move the shape to next row
jc lock ; if collision, then lock shape
jmp iploop ; otherwise process more input
lock: ; lock the current piece in place on the arena
lda shapy
cpi ARENAY ; are we at top of arena?
jz over ; if so then game is over
; as shape collided on row 0
call lockshape ; otherwise lock shape in place
call checkrows ; check if there are any full rows.
jmp gameloop ; process main loop
over: ; Game is over
lhld anyyx ; display "PRESS ANY KEY"
xchg
call csrpossio ; DE = screen pos to display
lxi h,anystr
call outstrsio
mvi a,SPEEDINIT ; reset speed
sta speed
call delay ; delay slightly so user can read the
call delay ; ANY KEY message (in case they were in
call delay ; middle of hitting a key)
call inflush ; flush any left over keys
overlp: call inch ; wait for new key press
ora a ; set Z flag
jz overlp
call cleararena ; blank out arena
jmp nwgame ; start new game
done: call inflush ; flush out incoming serial chars
lxi h,vis ; Make the cursor visible again
call outstrsio
lxi h,clr ; Clear the screen
call outstrsio
IF DAZZLER
call dazclose
ENDIF
IF CPM
lhld stacko ; restore the CPM stack
sphl
ret ; return to CPM on quit
ENDIF
IF CPM - 1
hlt ; halt on quit
ENDIF
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shape Drawing and Manipulation Subroutines ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; prepshape - Prepare shape to draw
; INPUTS: shapno - number of shape to draw
; shapo - orientation of shape
; Find the pointer to the shape in shaptbl then get the orientation of the shape
; Save a pointer to the shape and orientation in the shape pointer.
; prepshape must be called whenever the shapno or shapo (orientation) changes
prepshape:
; Add 2*shapno to the shaptbl pointer to get a pointer
; to the first shape's orientation
lda shapno ; get the shape to be drawn
add a ; double it to get the word offset into shaptbl
mvi b,0
mov c,a ; BC will now contain the offset into shaptbl
lxi h,shaptbl ; load address of shaptbl into HL
dad b ; add the offset to the shape pointer
; HL now contains a pointer into shaptbl with the address of the
; orientation 0 version of the shape
mov e,m ; move low byte of shape address into c
inx h ; increment to high addr
mov d,m ; move high byte of shape address into b
xchg ; move DE into HL
; HL now contains a pointer to the shape with orientation 0
lxi b,10h ; Each shape orientation takes up 16 bytes
lda shapo ; load shape orientation
psori: cpi 0
jz psnx ; if done finding orientation, then jump to draw
dad b ; add the 16 byte offset
dcr a ; dec the number of orientations
jmp psori
; HL will now contain a pointer to the correct shape and orientation
; store it to the shape pointer
psnx: shld shape ; shape HL to shape
ret
; spawnshape - Spawn a new, random shape at the top of the arena.
; 1. Get random shape
; 2. Set shape pointer to that shape in orientation 0
; 3. Spawn at origin for that shape.
; 4, Draw the shape
; Note: that shapx and shapy are relative to screen top-left
; 0 = Z ; 1 = S ; 2 = L ; 3 = J
; 4 = T ; 5 = I ; 6 = O
spawnshape:
ssrnd: call random ; get random number from 0-6
ani 7 ; this returns 0-7
cpi 7 ; if 7, then try again
jz ssrnd
sta shapno ; store the new shape
xra a ; a = 0
sta shapo ; shapo (orientation) = 0
call prepshape ; load the shape and orientation
lxi h,SPAWNYX ; H = spawn y, L = spawn x
lda shapno
cpi 5 ; if shape is 5, then spawn 1 pos to left
dcr l ; spawn x = spawn x - 1
shld shapx ; store spawn y to shapy and x to shapx
call setarenaptr ; Set the arena pointer.
call drawshape ; draw
ret
;
; drawshape - Draw shape on screen
; For blank parts of shape, draw the corresponding "arena" character
; INPUTS: shapx, shapy ; position of shape
; shape ; pointer to the shape and orientation
; If shape or orientation changes, call prepshape to populate the
; shape pointer before calling drawshape
; If shape position changes, call setarenaptr to update pointer to the shape's
; position in the arena
drawshape:
lhld shapx
xchg ; D = shapy ; E = shapx
call csrpos ; set cursor to draw position
lhld araptr ; load pointer to the location in the arena
; to be used by drawline
xchg ; and move to DE
lhld shape ; load current shape
call drawline ; draw all 4 lines of the shape
inx h
xchg ; swap, ready for the add below
lxi b,ARENADN-3 ; move to next arena line minus the 4 shape cols
dad b ; + 1 to increment to next character
xchg
call drawline
inx h
xchg ; swap in prep for the add below
lxi b,ARENADN-3 ; advance to next arena line
dad b
xchg
call drawline
inx h
xchg ; swap in prep for the add below
lxi b,ARENADN-3 ; advance to next arena line
dad b
xchg
call drawline
ret
; drawline - Draw 1 line of the shape
; For blank chars of the shape, draw the corresponding
; arena "background" character instead.
; INPUTS:
; HL = pointer to current shape line to output
; DE = arena location pointer (araptr)
drawline:
mov a,m ; load the shape character to display
cpi ' ' ; is it blank?
jnz dl1 ; if not blank then display it
ldax d ; otherwise get the background char to draw
dl1: mov b,a ; store in b to output via outch
call outch ; output it
inx h ; get next char
inx d ; and next arena position
mov a,m ; and repeat
cpi ' ' ;
jnz dl2 ;
ldax d ;
dl2: mov b,a ;
call outch ;
inx h ; get next char
inx d
mov a,m ; and repeat
cpi ' ' ;
jnz dl3 ;
ldax d ;
dl3: mov b,a ;
call outch ;
inx h ; get next char
inx d
mov a,m ; and repeat
cpi ' ' ;
jnz dl4 ;
ldax d ;
dl4: mov b,a ;
call outch ;
; OPT: Potential optimisation to use csrpos here instead.
; Would only need 1 vt100 escape sequence instead of the current 2
push h ; save the shape pointer
push d ; and arena pointer
call csrdownback ; set cursor to start of shape on next line
pop d ; and arena pointer
pop h ; restore shape pointer
ret
IF DAZZLER-1
;
; Move cursor down 1 line and back 4 chars
;
csrdownback:
lxi h,dwn ; go down 1 line
call outstr
lxi h,bw4 ; and back 4 characters
call outstr
ret
ENDIF
IF DAZZLER
;
; Move cursor down 1 line and back 4 chars
; Dazzler version
csrdownback:
lhld vrptr ; load vram pointer into HL
lxi d,16-2 ; move pointer to next line - 4 chars
dad d ; (2 chars per byte = -2)
shld vrptr
ret
ENDIF
; erasehape - Erase the currently drawn shape
;
eraseshape:
lhld shapx
xchg ; D = shapy ; E = shapx
call csrpos ; set cursor to draw position
lhld araptr
call eraseline ; erase current line
lxi d,ARENADN-3 ; move to next arena line
dad d ; -3 to go back to start of shape
call eraseline ; erase current line
lxi d,ARENADN-3 ; move to next arena line
dad d
call eraseline ; erase current line
lxi d,ARENADN-3 ; move to next arena line
dad d
call eraseline ; erase current line
ret
; eraseshapetop - Erase the top line of the currently drawn shape
; This is a slight optimisation for when the shape moves down
; Only the top row needs to be blanked as the rest of the shape
; will be redraw in the next drawshape.
; OPT: Similar optimisations could be provided in future for
; moving the shape left and right.
eraseshapetop:
lhld shapx
xchg ; D = shapy ; E = shapx
call csrpos ; set cursor to draw position
lhld araptr
call eraseline ; erase current line
ret
;
; eraseline - Erase the current shape line
; INPUTS: HL = pointer to arena line to erase
eraseline:
mov b,m ; load arena character
call outch ; output
inx h ; move to next and repeat
mov b,m
call outch
inx h
mov b,m
call outch
inx h
mov b,m
call outch
push h ; move to the next line
call csrdownback ; OPT: this isn't needed for the last line
pop h
ret
;
; lockshape - copies the current shape into the arena
; called when shape collides with something in the arena
; or with the arena itself.
; INPUTS: HL is pointer to arena
; DE is pointer to shape
; Loops through each shape character and copies it into the arena.
; This makes the shape "permanent" and thus subject to collision and
; "full row" checking
lockshape:
lhld shape ; load current shape into HL
xchg ; and copy to DE
lhld araptr ; load the shape's position in the arena into HL
mvi b,4 ; B is col counter - each shape is 4 chars wide
mov c,b ; C is row counter - each shape is 4 lines tall
jmp lscpy ; don't increment pointers on first check
lsinc: inx h ; move to next arena pos
inx d ; move to next shape char
lscpy: ldax d ; get the first shape char
cpi ' ' ; only copy non-space chars
jz lsskip ; skip if space
mov m,a ; and copy into the arena
lsskip: dcr b ; decrement column counter
jnz lsinc ; and jmp to copy next char
mvi b,4 ; at end of column, reset
dcr c ; and move to next row
jz lsspd ; if no more rows to check, then done
; move to next line of arena
mov a,c ; save the value of the row counter in A
lxi b,ARENADN-3 ; move to next arena line minus the 4 shape cols
; + 1 to increment to next character
dad b ; change HL to next arena row
mvi b,4 ; reset the col counter
mov c,a ; restore the row counter
inx d ; move to the next shape character
jmp lscpy ; check the next row.
lsspd: lxi h,speed ; speed+=SPEEDINC for each piece locked in place
mov a,m
cpi 0ffh ; is speed already max?
jz lsdon ; then don't increase
mvi a,SPEEDINC
add m ; speed += SPEEDINC
mov m,a ; store
jnz lsdon ; if speed not wrapped to 0, then done
dcr a ; Set speed to FFh
mov m,a ; and store
lsdon:
ret ; copy is done
;
; kickshape - "Kicks" the shape to a different position if the current rotation
; of the shape causes a collision. Called if rotcw detects a
; collision.
; Note: This function does not restore old x/y values on a failed kick.
; The calling routine must store them before calling.
; Nor does it check if the new position still causes a collision.
; The calling routine must do a collision check and deny the rotation
; if there is still a collision after the "kick"
; Note: This should never be called for shape 6 as it can't collide when
; rotating. Kick on shape 6 is undefined.
; 1. Get the shape number
; 2. Get the orientation
; 3. Get the "pixel" which collided
; 4. For not shape5, kick based on orientation
; 5. For shape 5, use the kick table to determine which way to kick
; Shapes can be moved up to 2 chars in any direction
kickshape:
lda shapno ; the type of kick depends on the shape
; For shape 0-4, the kick is determined by the orientation
cpi 4 ; is shape <= 4
jnc ksnext
lda shapo ; load the shape orientation
cpi 0 ; is it orientation 0
jnz ks1
lxi h,shapx ; then kick to left
dcr m ; decrement shapx by 1
jmp ksdone ; done.
ks1: cpi 1 ; is orientation 1?
jnz ks2
lxi h,shapy ; then kick up
dcr m
jmp ksdone
ks2: cpi 2 ; is it orientation 2?
jnz ks3
lxi h,shapx ; then kick right
inr m
jmp ksdone
ks3: lxi h,shapy ; otherwise orientation 3
inr m ; which needs to kick down
jmp ksdone
ksnext: ; Must be shape 5 - the I shape
mvi b,0 ; B/C used for offset into kick table
lda shapo ; get the orientation
cpi 0
jnz ks51
lda clsnx ; get column which collided
mov c,a ; into low of BC
lxi h,kick1
dad b ; find kick value
lda shapx ; get shape's current x position
add m ; move x l/r based on kick table
sta shapx ; and save
jmp ksdone
ks51: cpi 1
jnz ks52 ; Orientation 1 - as above but for y
lda clsny ; get column which collided
mov c,a ; into low of BC
lxi h,kick1
dad b ; find kick value
lda shapy ; get shape's current x position
add m ; move y l/r based on kick table
sta shapy ; and save
jmp ksdone
ks52: cpi 2 ; Orientation 2 - either right 1 or 2
jnz ks53 ; or left 1
lda clsnx ; get column which collided
mov c,a ; into low of BC
lxi h,kick2
dad b ; find kick value
lda shapx ; get shape's current x position
add m ; move x l/r based on kick table
sta shapx ; and save
jmp ksdone
ks53: ; Orientation 3 - either down 1 or 2
; or up 1
lda clsny ; get column which collided
mov c,a ; into low of BC
lxi h,kick2
dad b ; find kick value
lda shapy ; get shape's current x position
add m ; move y l/r based on kick table
sta shapy ; and save
; fallthrough to ksdone
ksdone: ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shape Movement Subroutines ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; rotcw - Rotate current shape clockwise
rotcw:
lda shapo ; load the shape orientation
sta tmp ; store the old shape orientation
; in case have to restore
inr a ; inc the orientation
cpi 4 ; if orientation is 4
jnz rlnxt
xra a ; then wrap back to 0
rlnxt: sta shapo ; save the new orientation
call prepshape ; update the shape pointer
call collischeck ; does the rotation cause a collision
jnc rldraw ; if not, then draw the shape
; Kick may change the current x/y so save them
lhld shapx ; load the current shape x,y
shld tmpptr ; and save
call kickshape ; try and kick the shape into a
call setarenaptr ; non-colliding position
call collischeck ; Does it still collide after kick?
jnc rldraw ; if still no collision, then draw kicked version
lda tmp ; no kicks work, so restore old orientation
sta shapo
lhld tmpptr ; restore the old x,y
shld shapx
call prepshape
call setarenaptr ; if the shape was kicked, reset the
; position in arena
rldraw: call drawshape ; redraw the shape
ret
;
; moveleft - Move the shape 1 char to the left
movleft:
call eraseshape ; OPT: could erase right col of shape
lxi h,shapx ; decrease shape's x position
dcr m
call setarenaptr ; update position in arena
call collischeck ; does it collide?
jnc mlnocl ; if no, jump to no collision
lxi h,shapx ; otherwise move back to original pos
inr m
call setarenaptr ; reset arena position
mlnocl: call drawshape
mlclsn: ret
;
; moveright - Move the shape one character to the right
movright:
call eraseshape ; OPT: could erase left col of shape
lxi h,shapx ; increase shape's x position
inr m
call setarenaptr ; recalc arena position
call collischeck ; does it collide
jnc mrnocl ; if no then jump to no collision
lxi h,shapx ; otherwise move back to original pos
dcr m
call setarenaptr ; OPT: we could optimize just to
; inc/dec arena ptr here?
mrnocl: call drawshape
ret
;
; movdown - Move the shape down one line
; and check if it collides with anything
; If it collides, then don't move it and
; lock it in position
; RETURNS: CY = 0 shape not locked. 1 = shape locked
movdown:
call eraseshapetop ; erase the current shape (top row only)
lxi h,shapy ; increment y position by 1
inr m
call setarenaptr ; calculate the new arena pointer
call collischeck ; does this shape collide with anything
; in the arena?
jnc mdnocl ; if no then next
lxi h,shapy ; else restore position if collision
dcr m
call setarenaptr ; recalculate the arena pointer
call drawshape ; redraw the shape
; return that a collision occurred
xra a
cmc ; set carry = collision
ret
mdnocl: ; no collision
call drawshape ; draw shape in new position
xra a ; clear carry = no collision
ret
;
; collischeck - Check if the current shape/position collides with anything
; in the arena
; OUTPUTS: CY = 0 if no collision, 1 if collision
;
; DE contains a pointer to the current shape
; HL contains a pointer to the arena
; * Check current shape character until find a non-space
; * Check the corresponding arena char. If also not a space, then a collision
; * Otherwise increment to next shape position and next arena position
; * And repeat until all shape characters are checked.
;
collischeck:
lhld shape ; load current shape into HL
xchg ; and copy to DE
lhld araptr ; load the shape's position in the arena into HL
mvi b,4 ; B is col counter - each shape is 4 chars wide
mov c,b ; C is row counter - each shape is 4 lines tall
jmp ccchk ; don't increment pointers on first check
ccinc: inx h ; move to next arena pos
inx d ; move to next shape char
ccchk: ldax d ; get the first shape char
cpi ' ' ; if it is a space char, then ignore
jz ccnxt
; otherwise there is a shape character at this location
; check there is not an arena character also at this location
mov a,m ; load the arena character
cpi ' ' ; if no arena char, then ignore
jz ccnxt
jmp cccln ; otherwise here is a collision.
; exit function
ccnxt: dcr b ; decrement column counter
jnz ccinc ; and check next char
mvi b,4 ; at end of column, reset
dcr c ; and move to next row
jz ccdon ; if no more rows to check, then no collision
; done
; need to move to next line of arena
mov a,c ; save the value of the row counter in A
lxi b,ARENADN-3 ; move to next arena line minus the 4 shape cols
; + 1 to increment to next character
dad b ; move HL to next arena row
mvi b,4 ; reset the col counter
mov c,a ; restore the row counter
inx d ; move to the next shape character
jmp ccchk ; check the next row.
; Collision detected - set carry and return
cccln:
mov a,b ; save which part of the shape had a collision
sta clsnx ; in case shape needs to be wall kicked
mov a,c
sta clsny
xra a ; clear carry
cmc ; set carry to indicate collision
ret
; No collision - clear carry and return
ccdon: xra a ; clear carry to indicate no collision
ret
;;;;;;;;;;;;;;;;;;;;;;;
;; Arena Subroutines ;;
;;;;;;;;;;;;;;;;;;;;;;;
;
; setarenaptr - Set HL to point to the arena.
; Store in araptr
; Loads the start of arena into HL
; Then advances pointer to the row and col co-ordinates stored
; in shapy and shapx
; The result is that araptr points to the position in the arena
; that is the top-left position of that shape in the arena.
; araptr is used when drawing or erasing shapes so that the "background"
; arena character can be output for blank areas of the shape.
; RETURNS: HL points to arena location
; HL stored to araptr
; OPT: This recalculates from top of arena each time.
; Instead it would be quicker to move the pointer left/right/up/down
; based on the direction the shape moves
setarenaptr:
lxi h,arena ; load the start of arena into HL
lxi b,ARENADN ; Set BC to num chars to move down to next row
; find the row in the arena that matches the shape y pos
lda shapy ; get shape row
adi MINUSARENAY ; convert screen-coords to arena co-ords
; note use ADI as SBI includes carry
sarow: jz sacol ; have we finished processing the row position?
dad b ; move to next arena row
dcr a ; decrement row count
jmp sarow ; keep adding until find correct row
; find the column in the arena that maches the shape x position
sacol: lda shapx ; load shape x position
adi MINUSARENAX ; convert screen co-ords to arena co-ords.
mov c,a ; and store in C. B will already be 0
dad b ; add the column offset.
; HL will now contain a pointer to the top-left char in the arena
; that matches the top-left position of the current shape
shld araptr ; save in araptr, so don't need to always
; recalculate
ret
;
; drawarena - Draw the current arena on screen
;
drawarena:
mvi d,ARENAY
mvi e,ARENAX ; Set cursor to arena display pos d = y, e = x
call csrpos
mov a,d ; a = top left col of arena (ARENAY)
sta tmp ; store current row nr in tmp
lxi h,arena ; load the first line of the arena display
daloop: call outstr ; output the current arena line
lda tmp
inr a
cpi ARENAH+ARENAY+2 ; have we drawn all the lines? ARENA height + y
; screen offset
jz dadone ; if so, done?
sta tmp ; store current line nr to tmp
inx h ; relies on outstr leaving HL are the string
; null char
mov d,a ; set row to current screen row
mvi e,ARENAX ; set col to ARENAX
push h ; save string position
call csrpos ; move to next line
pop h ; restore string position
jmp daloop
dadone: ret
;
; drawarenaline - draw a single line of the arena
; INPUTS: HL = pointer to start of line to draw
; D = arena row number to draw
; Note: Converts arena co-ords to screen co-ords
drawarenaline:
mvi e,ARENAX ; E = column (ARENAX), D already contains row
mov a,d
adi ARENAY ; add the arena screen top position
mov d,a ; to convert to screen co-ords
push h
call csrpos ; set cursor to correct row/col
pop h
call outstr
ret
;
; cleararena - Reset the arena.
; Remove all shapes and return to initial state
;
cleararena:
lxi h,arena+2 ; HL = first shape char in arena
mvi c,ARENAH ; c = row counter
mvi b,ARENAW ; d = col counter
mvi a,' ' ; a = blank
lxi d,5 ; DE chars to skip are end of arena line
; 2x side + 2x space + null
caloop: mov m,a ; blank out the arena char
dcr b
inx h ; move to next char
jnz caloop ; for each char in row
dad d ; increment h to start of next row
mvi b,ARENAW ; reset width
dcr c ; dec row counter
jnz caloop
ret
;
; checkrows - Check each row to see if it is filled.
; Change the the filled rows to '-' and count the number
; of cleared rows
; OUTPUT: nrclr = number of full rows found
checkrows:
xra a ; a = 0
mov d,a ; d = 0
sta nrclr ; mark number of cleared rows as 0
lxi h,arena+2 ; load pointer to the first arena shape char
crckrw: lxi b,ARENAW ; BC is loop counter for each char in row
mvi a,' '
crchk: cmp m ; is this arena char a space?
jz crnxrw ; If so, not a full line, so check next row
inx h
dcr c ; Note: dcr (not dcx) to set Z flag
jnz crchk ; if more chars, check next char
; If ran out of chars to check on this line, then it is a full line
; mark it will '-'s
crfuln: lda nrclr ; nrclr++
inr a
sta nrclr
lxi b,-ARENAW ; jump back to start of line
dad b
lxi b,ARENAW ; restore the BC column counter
mvi a,'-' ; '-' is the char displayed when line is full
; now loop aver each char in row and replace with '-'
crmark: mov m,a ; copy '-' into arena
inx h
dcr c ; are we at end of row?
jnz crmark ; if no, keep copying
; Now we want to redisplay this line of the arena
; so the '-' are output to the user
push h ; save registers
push b
push d
lxi b,-(ARENAW+2) ; go back to start of arena line
dad b
call drawarenaline ; redraw the arena line
pop d ; restore registers
pop b
pop h
; fall through to crnxrw
; row count in BC should be 0
crnxrw: dad b ; add what is remaining of the row count
lxi b,5 ;side,spc,null,spc,side = 5. TODO: Make const
dad b
inr d ; row counter ++
mvi a,'=' ; '-' means we are at bottom of arena
cmp m ; if at bottom, then done
jz crscor
jmp crckrw ; otherwise check the next row
; calculate the score
crscor: lda nrclr ; how many lines were cleared?
ora a ; set status flags
jz crdone ; no rows cleared?, then done
call calcscore ; otherwise calculate the score
crdone:
ret
;
; dropwrows - Erase full rows and drop down existing rows
; Note: Only call this if at least 1 row needs to be dropped.
;
; Starts on last arena row and move upwards
; rowsrc is used as pointer to source row to copy from
; rowdst is used as pointer to dest row to copy to
; Starts at the last arena row.
; If that row is '-' then keep dst row the same,
; move src row one line up and don't copy anything.
; Otherwise check if we're previously found a '-' row.
; If we have then copy from src to dst
; If not then move src and dst up one row.
; This basically means that dst stays on the '-' line until it is copied into
; and src is always decremented one row
;
droprows:
lxi h,arena+ARENALST; move to first shape col on last arena line
shld rowdst ; init copy dst to last row
shld rowsrc ; init copy src to last row
mvi b,ARENAH ; keep track of the arena row number
mvi a,0
sta nrclr ; zero out number of cleared lines
drcmp: mvi a,'-'
cmp m ; if first char is a '-' then just dec the src ptr
jnz drnxt
lxi h,nrclr ; increment number of 'cleared' lines
inr m
jmp drdecs ; until find a non-'-' row
drnxt: lda nrclr ; check if we have already found cleared lines
ora a ; if nrclr > 0 then need to copy src to dst
jz drdecd ; otherwise skip the copy and dec src and dst
call copyrow
drdecd: lxi d,-ARENADN ; move src and dst back one row each
lhld rowdst
dad d ; position at first char 1 line up
shld rowdst
drdecs: lxi d,-ARENADN ; need to set this again in case only decing src
lhld rowsrc
dad d
shld rowsrc ; LH now set to the first char of the next row
; to check
dcr b ; dec the row number
jz drtop ; finish process top lines of arena
jmp drcmp ; compare the next line
drtop: ; reached the top of the arena, so no more src rows to copy
; set the src to an all blank row
lxi h,nrclr
mov b,m ; nrclr = number of cleared out rows
; so we need to blank the top nrclr rows
; to fill in the 'missing' rows at the top
lxi h,blnkrw ; row contains all blanks
shld rowsrc
; rowdst has already already points to correct dst row
; when we get here.
drtopl: call copyrow ; copy blank into destination
lxi d,-ARENADN
lhld rowdst ; move dst to prev arena row
dad d
shld rowdst
dcr b