-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBANK_StateMachine@1#1.asm
903 lines (601 loc) · 24.1 KB
/
BANK_StateMachine@1#1.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
SLOT 1
NEWBANK STATEMACHINE
; Banks holding data (ply 0 doubles as WHITE, and ply 1 as BLACK)
CURSOR_MOVE_SPEED = 16
CAP_SPEED = 20
HOLD_DELAY = 40
;---------------------------------------------------------------------------------------------------
;---------------------------------------------------------------------------------------------------
DEF aiStartMoveGen
SUBROUTINE
REFER AiStateMachine
VEND aiStartMoveGen
; To assist with castling, generate the moves for the opponent, giving us effectively
; a list of squares that are being attacked. The castling can't happen if the king is
; in check or if the squares it would have to move over are in check
; we don't need to worry about this if K has moved, or relevant R has moved or if
; the squares between are occupied. We can tell THAT by examining the movelist to see
; if there are K-moves marked "FLAG_CASTLE" - and the relevant squares
;inc currentPly
;jsr InitialiseMoveGeneration
PHASE AI_StepMoveGen
rts
;---------------------------------------------------------------------------------------------------
DEF aiInCheckBackup
SUBROUTINE
REFER AiStateMachine
VEND aiInCheckBackup
; We're about to draw some large text on the screen
; Make a backup copy of all of the row bitmaps, so that we can restore once text is done
dec drawCount
bmi .exit ; done all rows
ldy drawCount
IF 0
jmp SAFE_BackupBitmaps
ENDIF
.exit PHASE AI_InCheckDelay
rts
;---------------------------------------------------------------------------------------------------
DEF aiInCheckDelay
SUBROUTINE
REFER AiStateMachine
VEND aiInCheckDelay
dec mdelay
bne .exit
lda #0
sta COLUBK
PHASE AI_BeginSelectMovePhase
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiBeginSelectMovePhase
SUBROUTINE
REFER AiStateMachine
VEND aiBeginSelectMovePhase
lda #$2
sta COLUP0
ldx #%100
stx CTRLPF ; under
lda #0
sta mdelay ;?
sta aiFlashPhase ; odd/even for flashing pieces
lda #CAP_SPEED*2
sta aiFlashDelay
lda #-1
sta fromX12
sta toX12
;lsr randomness
PHASE AI_FlashComputerMove
rts
;---------------------------------------------------------------------------------------------------
DEF aiFlashComputerMove
SUBROUTINE
lda squareToDraw
bmi .initial ; startup - no computer move to show
; "squareToDraw" is the piece that should flash while human waits
lda SWCHA
and #$F0
cmp #$F0
beq .nodir
lda #1
sta aiFlashDelay
and aiFlashPhase
beq .initial
.nodir dec aiFlashDelay
bne .exit ; don't flash
lda #CAP_SPEED*2
sta aiFlashDelay
inc aiFlashPhase
jsr CopySinglePiece;@0
rts
.initial PHASE AI_SelectStartSquare
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiSelectStartSquare
SUBROUTINE
REFER AiStateMachine
VEND aiSelectStartSquare
NEXT_RANDOM
jsr moveCursor
jsr IsValidMoveFromSquare
dec ccur ; pulse colour for valid squares
jsr setCursorColours
tya
ora INPT4
bmi .exit ; illegal square or no button press
PHASE AI_StartSquareSelected
.exit rts
;---------------------------------------------------------------------------------------------------
DEF setCursorPriority
SUBROUTINE
REFER moveCursor
VEND setCursorPriority
tya
pha
ldx #%100
ldy cursorX12
bmi .under
jsr GetBoard
cmp #0
bne .under
ldx #0
.under stx CTRLPF ; UNDER
pla
tay
rts
;---------------------------------------------------------------------------------------------------
DEF setCursorColours
SUBROUTINE
REFER aiSelectStartSquare
REFER aiDrawMoves
REFER aiUnDrawTargetSquares
REFER aiShowMoveCaptures
REFER aiSlowFlash
REFER aiSelectDestinationSquare
VEND setCursorColours
; pass y=-1 if move is NOT in the movelist
; preserve y
lda #$40
cpy #-1
beq .writeCursorCol ; NOT in the movelist
lda ccur
lsr
lsr
lsr
and #3
clc
adc #$D0 ;COLOUR_LINE_1
.writeCursorCol sta COLUP0
rts
;---------------------------------------------------------------------------------------------------
; RLDU RLD RL U RL R DU R D R U R LDU LD L U L DU D U
; 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
ALLOCATE JoyCombined, 16
.byte 0, 0, 0, 0, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1, -1, 0
ALLOCATE JoyMoveCursor, 16
.byte 0, 0, 0, 0, 0, -9, 11, 1, 0, -11, 9, -1, 0, -10, 10, 0
;---------------------------------------------------------------------------------------------------
DEF aiStartSquareSelected
SUBROUTINE
REFER AiStateMachine
VEND aiStartSquareSelected
; Mark all the valid moves for the selected piece on the board
; and then start pulsing the piece
; AND start choosing for selection of TO square
; Iterate the movelist and for all from squares which = drawPieceNumber
; then draw a BLANK at that square
; do 1 by one, when none found then increment state
lda cursorX12
sta squareToDraw
lda #10
sta aiFlashDelay
lda #0
sta toX12 ;aiToSquareX12
sta aiFlashPhase ; for debounce exit timing
lda #-1
sta aiMoveIndex
lda #HOLD_DELAY
sta mdelay ; hold-down delay before moves are shown
PHASE AI_DrawMoves
rts
;---------------------------------------------------------------------------------------------------
DEF aiDrawMoves
SUBROUTINE
REFER AiStateMachine
VEND aiDrawMoves
dec ccur
jsr setCursorColours
dec mdelay
bne .exit
lda #1 ; larger number will slow the draw of available moves
sta mdelay ; once triggered, runs always
lda aiMoveIndex
bpl .valid
jsr SAFE_getMoveIndex
sta aiMoveIndex
.valid
jsr SAFE_showMoveOptions ; draw potential moves one at a time
lda aiMoveIndex
bpl .unsure ; still drawing in this phase
lda #CAP_SPEED
sta mdelay
lda #0
sta aiFlashPhase ; controls odd/even exit of flashing
PHASE AI_ShowMoveCaptures
rts
.exit
; Initial piece selection has happened, but the button hasn't been released yet
; AND we're still in the waiting phase to see if the button was held long enough for move show
lda INPT4
bpl .unsure ; button still pressed, so still unsure what to do
; Aha! Button released, so we know the selected piece and can start flashing it
; and allowing movement of the selector to a destination square...
lda #6*4
sta ccur ; bright green square for selection
PHASE AI_SelectDestinationSquare
.unsure rts
;---------------------------------------------------------------------------------------------------
DEF SAFE_showMoveOptions
SUBROUTINE
REFER aiDrawMoves
REFER aiUnDrawTargetSquares
VAR __saveIdx, 1
VAR __piece, 1
VEND SAFE_showMoveOptions
; place a marker on the board for any square matching the piece
; EXCEPT for squares which are occupied (we'll flash those later)
.next ldx aiMoveIndex
stx __saveIdx
bmi .skip
lda INTIM
cmp #2+SPEEDOF_COPYSINGLEPIECE
bcc .skip
dec aiMoveIndex
jsr GetP_MoveFrom
cmp fromX12
bne .next
jsr GetP_MoveTo
sta squareToDraw
jsr GetP_MovePiece
sta __piece
; If it's a pawn promote (duplicate "to" AND piece different (TODO) then skip others)
; TODO this could/will fail on sorted lists. MMh.
.sk dex
bmi .prom
jsr GetP_MoveTo
cmp squareToDraw
bne .prom
jsr GetP_MovePiece
eor __piece
and #PIECE_MASK
beq .prom ; same piece type so not a promote
dec aiMoveIndex
dec aiMoveIndex
dec aiMoveIndex
.prom
ldy squareToDraw
jsr GetBoard
and #PIECE_MASK
bne .next ; don't draw dots on captures - they are flashed later
;lda INTIM
;cmp #SPEEDOF_COPYSINGLEPIECE
;bcc .skip
;lda aiMoveIndex
;sta __saveIdx
jsr markerDraw
rts
.skip lda __saveIdx
sta aiMoveIndex
rts
;---------------------------------------------------------------------------------------------------
DEF aiUnDrawTargetSquares
SUBROUTINE
REFER AiStateMachine
VEND aiUnDrawTargetSquares
dec ccur
jsr setCursorColours
dec mdelay
bne .exit
lda #1
sta mdelay ; once triggered, runs always
lda aiMoveIndex
bpl .valid
jsr SAFE_getMoveIndex
sta aiMoveIndex
.valid
jsr SAFE_showMoveOptions ; draw potential moves one at a time
lda aiMoveIndex
bpl .exit ; still drawing in this phase
PHASE AI_SelectStartSquare
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiShowMoveCaptures
SUBROUTINE
REFER AiStateMachine
VEND aiShowMoveCaptures
; draw/undraw ALL captured pieces
; we should do this an even number of times so that pieces don't disappEOR
dec ccur
jsr setCursorColours
dec mdelay ; flash speed UNVARYING despite draw happening
lda aiMoveIndex
bpl .valid ; guaranteed -1 on 1st call
jsr SAFE_getMoveIndex
sta aiMoveIndex
.valid
jsr SAFE_showMoveCaptures
lda aiMoveIndex
bpl .exit
inc aiFlashPhase
PHASE AI_SlowFlash
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiSlowFlash
SUBROUTINE
REFER AiStateMachine
VEND aiSlowFlash
; Joystick button is held down, so we're displaying the available moves
; They have all been drawn, so now we "slow" flash any pieces that can be captures
dec ccur
jsr setCursorColours
lda aiFlashPhase
and #1
bne .notEven ; only exit after even # EOR-draws
lda INPT4
bmi .butpress ; exit on button release
.notEven
; Wait for delay to expire then back and flash 'em again
dec mdelay
bpl .slowWait
lda #CAP_SPEED
sta mdelay
PHASE AI_ShowMoveCaptures ; go back and rEORdraw all captures again
.slowWait rts
.butpress lda #1
sta mdelay
PHASE AI_UnDrawTargetSquares
rts
;---------------------------------------------------------------------------------------------------
DEF moveCursor
SUBROUTINE
REFER aiSelectStartSquare
REFER aiSelectDestinationSquare
VAR __newCursor, 1
VEND moveCursor
; Part (a) move cursor around the board waiting for joystick press
ldx #0 ; delay
lda SWCHA
lsr
lsr
lsr
lsr
tay
cmp #15
beq .cursor ; nothing pressed - skip delays
dec mdelay
bpl .delaym
clc
lda cursorX12
adc JoyMoveCursor,y
sta __newCursor
tay
jsr GetValid
cmp #-1
beq .invalid
lda __newCursor
sta cursorX12
.invalid
ldx #CURSOR_MOVE_SPEED
.cursor stx mdelay
jsr setCursorPriority
.delaym rts
;---------------------------------------------------------------------------------------------------
DEF aiSelectDestinationSquare
SUBROUTINE
REFER AiStateMachine
VEND aiSelectDestinationSquare
; Piece is selected and now we're looking for a button press on a destination square
; we flash the piece on-and-off while we're doing that
; Flash the selected piece
lda INTIM
cmp #ONCEPERFRAME
bcc .exit
dec aiFlashDelay
bne .exit ; don't flash
lda #CAP_SPEED
sta aiFlashDelay
inc aiFlashPhase
jsr CopySinglePiece;@0
rts
.exit
jsr moveCursor
lda INTIM
cmp #20
bcc .noButton
ldy cursorX12
sty toX12
jsr GetPiece
jsr setCursorColours
; y = valid square
lda INPT4
bmi .noButton
lda toX12
cmp fromX12
beq .cancel
cpy #-1
beq .noButton ; not a valid square
lda aiFlashPhase
and #1
beq .done
sta aiFlashDelay ; EOR-phase incorrect - force quick fix to allow next-frame button detect
rts
.cancel
lda aiFlashPhase
and #1
beq .doCancel
; EOR-phase incorrect - force quick fix to allow next-frame button detect
lda #1
sta aiFlashDelay
rts
.doCancel PHASE AI_ReselectDebounce
rts
.done PHASE AI_Quiescent ; destination selected!
.noButton rts
;---------------------------------------------------------------------------------------------------
DEF aiReselectDebounce
SUBROUTINE
REFER AiStateMachine
VEND aiReselectDebounce
; We've just cancelled the move. Wait for the button to be released
; and then go back to selecting a piece to move
lda INPT4
bpl .exit ; button still pressed, so wait
PHASE AI_SelectStartSquare
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiQuiescent
SUBROUTINE
REFER AiStateMachine
VEND aiQuiescent
; Move has been selected
lda #-1
sta cursorX12
lda fromX12
sta originX12
jsr GetPiece ; from the movelist
ldy fromX12
jsr GetBoard ; get the piece from the board itself
eor fromPiece
and #PIECE_MASK ; if not the same piece board/movelist...
bne .promote ; promote a pawn
PHASE AI_MoveIsSelected
rts
.promote PHASE AI_PromotePawnStart
rts
;---------------------------------------------------------------------------------------------------
DEF aiRollPromotionPiece
SUBROUTINE
REFER AiStateMachine
VEND aiRollPromotionPiece
; Flash the '?' and wait for an UDLR move
lda INTIM
cmp #SPEEDOF_COPYSINGLEPIECE
bcc .exit
lda SWCHA
and #$F0
cmp #$F0
beq .nojoy
lda #0
sta aiFlashDelay
lda aiFlashPhase
and #1
beq .even
.nojoy dec aiFlashDelay
bpl .exit
lda #10
sta aiFlashDelay
ldx #INDEX_WHITE_PROMOTE_on_WHITE_SQUARE_0
lda sideToMove
bpl .wtm
ldx #INDEX_BLACK_PROMOTE_on_WHITE_SQUARE_0
.wtm
jsr showPromoteOptions
inc aiFlashPhase
.exit rts
.even lda #3 ; QUEEN
sta fromPiece ; cycles as index to NBRQ
inc aiFlashPhase
ldx #INDEX_WHITE_QUEEN_on_WHITE_SQUARE_0 ;TODO: fix for colour
lda sideToMove
bpl .whiteToMove
ldx #INDEX_BLACK_QUEEN_on_WHITE_SQUARE_0
.whiteToMove
jsr showPromoteOptions
PHASE AI_ChooseDebounce
rts
;---------------------------------------------------------------------------------------------------
DEF aiMarchToTargetA
SUBROUTINE
REFER AiStateMachine
VAR __fromRow, 1
VAR __boardIndex, 1
VAR __fromCol, 1
VAR __toCol, 1
VEND aiMarchToTargetA
; Now we calculate move to new square
lda fromX12
cmp toX12
beq .unmovedx
sta lastSquareX12
sec
ldx #-3
.sub10 sbc #10
inx
bcs .sub10
adc #8
sta __fromCol
stx __fromRow
lda toX12
sec
ldx #-3
.sub10b sbc #10
inx
bcs .sub10b
adc #8
sta __toCol
cpx __fromRow
beq .rowDone
bcs .incRow
sec
lda fromX12
sbc #10
sta fromX12
jmp .rowDone
.incRow clc
lda fromX12
adc #10
sta fromX12
.rowDone
lda __toCol
cmp __fromCol
beq .colDone
bcs .incCol
dec fromX12
jmp .colDone
.incCol inc fromX12
.colDone
.unmovedx
PHASE AI_MarchA2
rts
;---------------------------------------------------------------------------------------------------
DEF aiMarchA2
SUBROUTINE
; erase object in new sqare --> blank
ldy fromX12
sty squareToDraw
jsr GetBoard
cmp #0
beq .skipbl
jsr CopySinglePiece;@0 ; erase next square along --> blank
.skipbl
ldy fromX12
sty __boardIndex
jsr GetBoard
sta lastPiece ; what we are overwriting
lda fromPiece
ora #FLAG_MOVED ; prevents usage in castling for K/R
and #~FLAG_ENPASSANT
ldy __boardIndex
jsr PutBoard
PHASE AI_MarchB
rts
;---------------------------------------------------------------------------------------------------
DEF aiMarchB2
SUBROUTINE
REFER AiStateMachine
VEND aiMarchB2
ldy lastSquareX12
sty squareToDraw
jsr GetBoard
cmp #0
beq .skipbl2
jsr CopySinglePiece;@0 ; draw previous piece back in old position
.skipbl2
lda fromX12
cmp toX12
beq xhalt
lda #0 ; inter-move segment speed (can be 0)
sta drawDelay
PHASE AI_MarchToTargetA
rts
xhalt
;??? jsr FinaliseMove
lda #4 ; on/off count
sta drawCount ; flashing for piece about to move
lda #0
sta drawDelay
PHASE AI_FinalFlash
rts
;---------------------------------------------------------------------------------------------------
CHECK_BANK_SIZE "BANK_StateMachine"
;---------------------------------------------------------------------------------------------------
; EOF