-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHandler_PAWN.asm
257 lines (181 loc) · 7.33 KB
/
Handler_PAWN.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
; Copyright (C)2020 Andrew Davie
; Pawn move handlers
;---------------------------------------------------------------------------------------------------
; WHITE PAWN
;---------------------------------------------------------------------------------------------------
WHITE_HOME_ROW = 40 ; < this, on home row
BLACK_HOME_ROW = 82 ; >= this, on home row
;---------------------------------------------------------------------------------------------------
MAC EN_PASSANT
SUBROUTINE
; {1} = _LEFT or _RIGHT
ldx currentSquare
ldy ValidSquare+{1},x
cpy enPassantPawn
bne .invalid
ldy ValidSquare+{1}+{2},x ; en-passant endpoint must be blank
lda Board,y
bne .invalid
jsr AddMove ; the MOVE will need to deal with the details of en-passant??
.invalid
ENDM
;---------------------------------------------------------------------------------------------------
MAC PROMOTE_PAwN
;SUBROUTINE
;{1} = BLACK or WHITE
sty __temp
lda #{1}|QUEEN
sta currentPiece
jsr AddMove
lda #{1}|ROOK
sta currentPiece
ldy __temp
jsr AddMove
lda #{1}|BISHOP
sta currentPiece
ldy __temp
jsr AddMove
lda #{1}|KNIGHT
sta currentPiece
ldy __temp
jsr AddMove
IF {1} = WHITE
lda #WHITE|WP
ENDIF
IF {1} = BLACK
lda #BLACK|BP
ENDIF
sta currentPiece
ENDM
;---------------------------------------------------------------------------------------------------
MAC MOVE_OR_PROMOTE_PAWN
;SUBROUTINE
; {1} = BLACK or WHITE
IF {1} = WHITE
cpy #90 ; last rank?
bcc .standard
jsr PromoteWhitePawn
jmp .pMoved
ENDIF
IF {1} = BLACK
cpy #30 ; last rank?
bcs .standard
jsr PromoteBlackPawn
jmp .pMoved
ENDIF
.standard jsr AddMove ; add +1UP move
.pMoved
ENDM
;---------------------------------------------------------------------------------------------------
MAC TAKE
;SUBROUTINE
; {1} = capture square offset
ldx currentSquare
ldy ValidSquare+{1},x
bmi .invalid2
lda Board,y
beq .invalid2 ; square empty
sta capture
eor currentPiece
bpl .invalid ; same colour
MOVE_OR_PROMOTE_PAWN {2}
jmp .invalid2
.invalid inc protecting
.invalid2
ENDM
;---------------------------------------------------------------------------------------------------
DEF PromoteWhitePawn
SUBROUTINE
REFER Handle_WHITE_PAWN
VAR __temp, 1
VEND PromoteWhitePawn
PROMOTE_PAWN WHITE
rts
;---------------------------------------------------------------------------------------------------
DEF Handle_WHITE_PAWN
SUBROUTINE
REFER GenerateAllMoves
VEND Handle_WHITE_PAWN
ldy ValidSquare+_UP,x ; square above must be blank (WILL NOT EVER be off-board!)
lda Board,y
bne .pMoved ; occupied
sta capture
; we may need to promote the pawn
; All possibilites (Q/R/B/N) are added as individual moves
MOVE_OR_PROMOTE_PAWN WHITE
; the +2 move off the home rank...
ldx currentSquare
cpx #WHITE_HOME_ROW
bcs .pMoved ; pawn has moved so can't do +2
ldy ValidSquare+_UP+_UP,x ; WILL be a valid square
lda Board,y
bne .pMoved ; destination square occupied
lda currentPiece
ora #FLAG_ENPASSANT
sta currentPiece ; GENERATE en-passant opportunity
jsr AddMove ; add the +2UP move off home row
.pMoved
; regular captures...
TAKE _UP+_LEFT, WHITE
TAKE _UP+_RIGHT, WHITE
IF ENPASSANT_ENABLED
; en-passant captures...
lda enPassantPawn
beq .noEnPassant ; previous move (opponent) enpassant square?
lda currentPiece
ora #FLAG_ENPASSANT
sta currentPiece ; CONSUME en-passant opportunity
EN_PASSANT _LEFT, _UP
EN_PASSANT _RIGHT, _UP
.noEnPassant
ENDIF
jmp MoveReturn
;---------------------------------------------------------------------------------------------------
; BLACK PAWN
;---------------------------------------------------------------------------------------------------
DEF PromoteBlackPawn
SUBROUTINE
REFER Handle_BLACK_PAWN
VAR __temp, 1
VEND PromoteBlackPawn
PROMOTE_PAWN BLACK
rts
DEF Handle_BLACK_PAWN
SUBROUTINE
REFER GenerateAllMoves
VEND Handle_BLACK_PAWN
ldy ValidSquare+_DOWN,x ; square below must be blank (WILL NOT EVER be off-board!)
lda Board,y
bne .pMoved ; occupied
sta capture
; we may need to promote the pawn
; All possibilites (Q/R/B/N) are added as individual moves
MOVE_OR_PROMOTE_PAWN BLACK
; the +2 move off the home rank...
ldx currentSquare
cpx #BLACK_HOME_ROW
bcc .pMoved ; pawn has moved so can't do +2
ldy ValidSquare+_DOWN+_DOWN,x ; WILL be a valid square
lda Board,y
bne .pMoved ; destination square occupied
lda currentPiece
ora #FLAG_ENPASSANT
sta currentPiece ; CAN en-passant
jsr AddMove ; add the +2DOWN move off home row
.pMoved
; regular captures... (with promotion)
TAKE _DOWN+_LEFT, BLACK
TAKE _DOWN+_RIGHT, BLACK
IF ENPASSANT_ENABLED
; en-passant captures...
lda enPassantPawn
beq .noEnPassant ; was last move en-passantable?
lda currentPiece
ora #FLAG_ENPASSANT
sta currentPiece ; any en-passant move added will have flag set
EN_PASSANT _LEFT, _DOWN
EN_PASSANT _RIGHT, _DOWN
.noEnPassant
ENDIF
jmp MoveReturn
; EOF