-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path@3 GENERIC #2.asm
More file actions
367 lines (246 loc) · 9.09 KB
/
@3 GENERIC #2.asm
File metadata and controls
367 lines (246 loc) · 9.09 KB
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
;---------------------------------------------------------------------------------------------------
; @3 GENERIC #2.asm
; Atari 2600 Chess
; Copyright (c) 2019-2020 Andrew Davie
; andrew@taswegian.com
;---------------------------------------------------------------------------------------------------
SLOT 3
ROMBANK THREE
DEF Breaker
brk
;---------------------------------------------------------------------------------------------------
DEF GetPiece
SUBROUTINE
REF aiSelectDestinationSquare ;✅
REF aiQuiescent ;✅
VEND GetPiece
; Retrieve the piece+flags from the movelist, given from/to squares
; Required as moves have different flags but same origin squares (e.g., castling)
lda #RAMBANK_PLY+1 ;currentPly
;lda currentPly
sta SET_BANK_RAM;@2
; returns piece in A+fromPiece
; or Y=-1 if not found
; We need to get the piece from the movelist because it contains flags (e.g., castling) about
; the move. We need to do from/to checks because moves can have multiple origin/desinations.
; This fixes the move with/without castle flag
ldy@PLY moveIndex
bmi .fail ; shouldn't happen
.scan lda fromX12
cmp@PLY MoveFrom,y
bne .next
lda toX12
cmp@PLY MoveTo,y
beq .found
.next dey
bpl .scan
.fail rts
.found lda@PLY MovePiece,y
sta fromPiece
rts
;---------------------------------------------------------------------------------------------------
DEF GenCastleMoveForRook
SUBROUTINE
REF MakeMove ;✅
REF CastleFixupDraw ;✅
VEND GenCastleMoveForRook
; Generate secondary move for the rook, involved in a castling move
; Returns:
; CC --> not a castle/secondary
; CS --> secondary move valid
clc
lda fromPiece
and #FLAG_CASTLE
beq .exit ; NOT involved in castle!
ldx #4
lda toX12 ; *destination*
.findCast clc
dex
bmi .exit
cmp KSquare,x
bne .findCast
lda RSquareEnd,x
sta toX12
sta@PLY secondaryBlank
ldy RSquareStart,x
sty fromX12
sty originX12
sty@PLY secondarySquare
lda fromPiece
and #128 ; colour bit
ora #ROOK ; preserve colour
sta fromPiece
sta __originalPiece
sta@PLY secondaryPiece
sec
.exit rts
KSquare .byte 24,28,94,98
RSquareStart .byte 22,29,92,99
RSquareEnd .byte 25,27,95,97
virtualSquare1 .byte 25,27,95,97
;virtualSquare2 .byte 26,26,96,96
;---------------------------------------------------------------------------------------------------
; DEF GenEnPassantMove
; SUBROUTINE
; REF EnPassantCheck
; REF MakeMove
; VEND GenEnPassantMove
; rts
; The reset vectors
; these must live in the fixed bank (bank 0 in 3E+ format)
; ORG $63FC
; RORG $FFFC
; .word StartCartridge ; RESET
; .word StartCartridge ; IRQ (not used)
;---------------------------------------------------------------------------------------------------
DEF InterlaceFrame
; START OF FRAME
IF 0
lda SWCHB
bmi .normal ; RIGHT difficulty switches on/off
dec framenum
lda framenum
lsr
bcs .normal
SLEEP 36
.normal lda #2
sta WSYNC
sta VSYNC
sta WSYNC ; line 1 of VSYNC
sta WSYNC ; line 2 of VSYNC
lda #0
sta WSYNC ; line 3 of VSYNC
sta VSYNC ; @0
sta VBLANK
ENDIF
IF 1
frame
;Vertical sync
;dec framenum
;lda interlaced ;see if we are in interlaced mode
;beq non_interlaced
lda SWCHB
bmi even_sync
dec framenum
lda framenum
and #1
beq even_sync
non_interlaced ;entry point for non-interlaced
;this is the vertical sync for the first field of an interlaced frame
;or just a normal non-interlaced vertical sync
lda #2
sta WSYNC
sta VSYNC ; Begin vertical sync.
sta WSYNC ; First line of VSYNC
sta WSYNC ; Second line of VSYNC.
lda #0
sta WSYNC ; Third line of VSYNC.
sta VSYNC ; (0)
jmp done_sync
even_sync
;this is the vertical sync for the second field of an interlaced fram
sta WSYNC
;need 40 cycles until the start of vertical sync
SLEEP 36
lda #2 ;40
sta VSYNC ; Begin vertical sync.
sta WSYNC ; First line of VSYNC
sta WSYNC ; Second line of VSYNC.
sta WSYNC ; Third line of VSYNC.
;need 33 cycles until the end of VSYNC
;SLEEP 10
lda #0 ;33
sta VSYNC
done_sync
; LDA #40 ;timer for 34 lines of blanking
; STA TIM64T
ENDIF
; lda #%1110 ; VSYNC ON
;.loopVSync3 sta WSYNC
; sta VSYNC
; lsr
; bne .loopVSync3 ; branch until VYSNC has been reset
; sta VBLANK
rts
;---------------------------------------------------------------------------------------------------
DEF BubbleSort
SUBROUTINE
rts ;tmp
IF 1
;{
; This MUST be called at the start of a new ply
; It initialises the movelist to empty
; x must be preserved
; note that 'alpha' and 'beta' are set externally!!
lda #-1
sta@PLY bestMove
lda enPassantPawn ; flag/square from last actual move made
sta@PLY enPassantSquare ; used for backtracking, to reset the flag
lda vkSquare
sta@PLY virtualKingSquare
lda vkSquare+1
sta@PLY virtualKingSquare+1 ; traversal squares of king for castling
; The value of the material (signed, 16-bit) is restored to the saved value at the reversion
; of a move. It's quicker to restore than to re-sum. So we save the current evaluation at the
; start of each new ply.
lda Evaluation
sta@PLY savedEvaluation
lda Evaluation+1
sta@PLY savedEvaluation+1
;}
ENDIF
clc
lda currentPly
adc #RAMBANK_SORT-RAMBANK_PLY
sta __bank2
lda@PLY moveIndex
bpl .start ; no moves - no sorting
rts
.start
sta __n
inc __n ; n := length(A)
.bubble lda #0
sta __newn
lda #1
sta __i
.bubbleLoop lda __i
cmp __n
beq .exitBubble
tax ; A
tay
dey ; B i-1
lda __bank2
sta SET_BANK_RAM;@2
sec
lda MoveValueLO,x
sbc MoveValueLO,y
lda MoveValueHI,x
sbc MoveValueHI,y
bvc .cmp16bit
eor #$80
.cmp16bit bmi .lessThan ; sort small to large
; swap!
XCHG MoveValueLO
XCHG MoveValueHI
lda currentPly
sta SET_BANK_RAM;@2
XCHG MoveFrom
XCHG MoveTo
XCHG MovePiece
XCHG MoveCapture
lda __i
sta __newn
.lessThan inc __i
jmp .bubbleLoop
.exitBubble lda __newn
sta __n
cmp #2
bcc .exitLoop
jmp .bubble
.exitLoop lda currentPly
sta SET_BANK_RAM
rts
;---------------------------------------------------------------------------------------------------
END_BANK
;---------------------------------------------------------------------------------------------------
; EOF