-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBANK_StateMachine@1#2.asm
349 lines (231 loc) · 9.63 KB
/
BANK_StateMachine@1#2.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
SLOT 1
NEWBANK STATEMACHINE2
;---------------------------------------------------------------------------------------------------
DEF aiChoosePromotePiece
SUBROUTINE
REFER AiStateMachine
VEND aiChoosePromotePiece
; Question-mark phase has exited via joystick direction
; Now we cycle through the selectable pieces
lda INTIM
cmp #SPEEDOF_COPYSINGLEPIECE
bcc .exit
lda INPT4
bmi .nobut ; no press
; button pressed but make sure phase is correct for exit
lda #0
sta aiFlashDelay
lda aiFlashPhase
and #1
beq .chosen ; button pressed --> selection made
.nobut
lda SWCHA
and #$F0
cmp #$F0
beq .odd ; no direction pressed
lsr
lsr
lsr
lsr
tay
; joystick but make sure phase is correct
lda aiFlashPhase
lsr
bcs .odd ; must wait until piece undrawn
; cycle to the next promotable piece (N/B/R/Q)
; TODO; use joy table for mod instead of just incrementing all the time
;clc
lda fromPiece
adc JoyCombined,y
and #3
sta fromPiece
PHASE AI_ChooseDebounce ; wait for release
.odd dec aiFlashDelay
bpl .exit
.force lda #10
sta aiFlashDelay
inc aiFlashPhase
ldy fromPiece
ldx promotePiece,y
jsr showPromoteOptions
.exit rts
.chosen
lda fromPiece
and #PIECE_MASK
tax
lda promoteType,x
sta fromPiece
ldy toX12
jsr GetBoard
and #PIECE_MASK
beq .nothing
jsr CopySinglePiece;@0 ; put back whatever was there to start
.nothing PHASE AI_MoveIsSelected
rts
ALLOCATE promotePiece, 4
.byte INDEX_WHITE_KNIGHT_on_WHITE_SQUARE_0
.byte INDEX_WHITE_BISHOP_on_WHITE_SQUARE_0
.byte INDEX_WHITE_ROOK_on_WHITE_SQUARE_0
.byte INDEX_WHITE_QUEEN_on_WHITE_SQUARE_0
ALLOCATE promoteType,4
.byte KNIGHT, BISHOP, ROOK, QUEEN
;---------------------------------------------------------------------------------------------------
DEF aiChooseDebounce
SUBROUTINE
REFER AiStateMachine
VEND aiChooseDebounce
; We've changed promotion piece, but wait for joystick to be released
lda SWCHA
and #$F0
cmp #$F0
bne .exit ; wait while joystick still pressed
lda #1
sta aiFlashDelay
PHASE AI_ChoosePromotePiece
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiDelayAfterMove
SUBROUTINE
VEND aiDelayAfterMove
lda #50
sta aiFlashDelay
PHASE AI_DelayAfterMove2
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiDelayAfterMove2
SUBROUTINE
VEND aiDelayAfterMove
dec aiFlashDelay
bne .exit
PHASE AI_MoveIsSelected
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiDelayAfterPlaced
SUBROUTINE
VEND aiDelayAfterPlaced
ldx #75 ; delay after human move
lda sideToMove
bmi .computer
ldx #1 ; delay after computer move
.computer stx aiFlashDelay
PHASE AI_DelayAfterPlaced2
rts
;---------------------------------------------------------------------------------------------------
DEF aiDelayAfterPlaced2
SUBROUTINE
;jsr debug
dec aiFlashDelay
bne .exit
PHASE AI_GenerateMoves
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiMarchToTargetB
SUBROUTINE
REFER AiStateMachine
VEND aiMarchToTargetB
; now we want to undraw the piece in the old square
lda lastSquareX12
sta squareToDraw
jsr CopySinglePiece;@0 ; erase whatever was on the previous square (completely blank)
ldy lastSquareX12
lda previousPiece
jsr PutBoard
lda lastPiece
sta previousPiece
PHASE AI_MarchB2
rts
;---------------------------------------------------------------------------------------------------
DEF aiPromotePawnStart
SUBROUTINE
REFER AiStateMachine
VEND aiPromotePawnStart
lda INTIM
cmp #SPEEDOF_COPYSINGLEPIECE
bcc .exit
lda #0
sta aiFlashPhase
sta aiFlashDelay
ldy toX12
sty squareToDraw
jsr GetBoard
and #PIECE_MASK
beq .empty
jsr CopySinglePiece;@0 ; remove any capturable piece for display purposes
.empty PHASE AI_RollPromotionPiece
.exit rts
;---------------------------------------------------------------------------------------------------
DEF aiGenerateMoves
SUBROUTINE
REFER AiStateMachine
VEND aiGenerateMoves
; Player comes here at the start of making a move
; This generates a valid movelist by calling 'negaMax' (removing illegal moves)
rts ;tmp
lda toX12
sta squareToDraw ; for showing move (display square)
ldx sideToMove
bpl .player
.computer PHASE AI_ComputerMove ; computer select move
rts
.player PHASE AI_StartMoveGen
rts
;---------------------------------------------------------------------------------------------------
DEF aiStepMoveGen
SUBROUTINE
REFER AiStateMachine
VEND aiStepMoveGen
lda originX12 ; location of cursor (show move)
sta cursorX12
PHASE AI_BeginSelectMovePhase
rts
;---------------------------------------------------------------------------------------------------
align 256
DEF PositionSprites
SUBROUTINE
REFER StartupBankReset
VEND PositionSprites
lda cursorX12
sec
.sub10 sbc #10
bcs .sub10
adc #8
tay
sta WSYNC ; 00 Sync to start of scanline.
lda colToPixel,y
sec ; 02 Set the carry flag so no borrow will be applied during the division.
.divideby15 sbc #15 ; 04 Waste the necessary amount of time dividing X-pos by 15!
bcs .divideby15 ; 06/07 11/16/21/26/31/36/41/46/51/56/61/66
tay
lda fineAdjustTable,y ; 13 -> Consume 5 cycles by guaranteeing we cross a page boundary
sta HMP0
sta RESP0 ; 21/ 26/31/36/41/46/51/56/61/66/71 - Set the rough position.
sta WSYNC
sta HMOVE
rts
; This table converts the "remainder" of the division by 15 (-1 to -15) to the correct
; fine adjustment value. This table is on a page boundary to guarantee the processor
; will cross a page boundary and waste a cycle in order to be at the precise position
; for a RESP0,x write
fineAdjustBegin
DC.B %01110000; Left 7
DC.B %01100000; Left 6
DC.B %01010000; Left 5
DC.B %01000000; Left 4
DC.B %00110000; Left 3
DC.B %00100000; Left 2
DC.B %00010000; Left 1
DC.B %00000000; No movement.
DC.B %11110000; Right 1
DC.B %11100000; Right 2
DC.B %11010000; Right 3
DC.B %11000000; Right 4
DC.B %10110000; Right 5
DC.B %10100000; Right 6
DC.B %10010000; Right 7
fineAdjustTable EQU fineAdjustBegin - %11110001; NOTE: %11110001 = -15
ALLOCATE colToPixel, 8
.byte 0,20,40,60,80,100,120,140
;---------------------------------------------------------------------------------------------------
CHECK_BANK_SIZE "BANK_StateMachine2"
;---------------------------------------------------------------------------------------------------
; EOF