-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchess.asm
executable file
·658 lines (483 loc) · 17.8 KB
/
chess.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
; Chess
; Atari 2600 Chess display system
; Copyright (c) 2019-2020 Andrew Davie
TIA_BASE_ADDRESS = $40
processor 6502
include "vcs.h"
include "macro.h"
include "piece_defines.h"
VERBOSE = 0 ; set to 1 for compile messages
ORIGIN SET 0
ORIGIN_RAM SET 0
include "segtime.asm"
_FIRST_BANK = 0 ; 3E+ 1st bank holds reset vectors
;FIXED_BANK = 3 * 2048 ;--> 8K ROM tested OK
;FIXED_BANK = 7 * 2048 ;--> 16K ROM tested OK
;FIXED_BANK = 15 * 2048 ; ->> 32K
;FIXED_BANK = 31 * 2048 ; ->> 64K
;FIXED_BANK = 239 * 2048 ;--> 480K ROM tested OK (KK/CC2 compatibility)
;FIXED_BANK = 127 * 2048 ;--> 256K ROM tested OK
;FIXED_BANK = 255 * 2048 ;--> 512K ROM tested OK (CC2 can't handle this)
YES = 1
NO = 0
INFINITY = $7000 ;32767
; assemble diagnostics. Remove for release.
TEST_POSITION = 0 ; 0=normal, 1 = setup test position
DIAGNOSTICS = 0
QUIESCENCE = 1
ASSERTS = 0
PVSP = 0 ; player versus player =1
ENPASSANT_ENABLED = 1
CASTLING_ENABLED = 1
SEARCH_DEPTH = 3
QUIESCE_EXTRA_DEPTH = 4
PLY_BANKS = SEARCH_DEPTH + QUIESCE_EXTRA_DEPTH
MAX_PLY_DEPTH_BANK = PLY_BANKS ;TODO -- RAMBANK_PLY + PLY_BANKS
;IF RAMBANK_PLY + MAX_PLY_DEPTH_BANK > 31
; ERR "Not enough RAM for PLY banks"
;ENDIF
SWAP_SIDE = 128 ;TODO + (RAMBANK_PLY ^ (RAMBANK_PLY+1))
; DELAYS
READY_TO_MOVE_FLASH = 10
;===================================
FINAL_VERSION = NO ; this OVERRIDES any selections below and sets everything correct for a final release
;===================================
;-------------------------------------------------------------------------------
; The following are optional YES/NO depending on phase of the moon
L276 SET YES ; use 276 line display for NTSC
;-------------------------------------------------------------------------------
; DO NOT MODIFY THE BELOW SETTINGS -- USE THE ONES ABOVE!
; Here we make sure everyting is OK based on the single switch -- less chance for accidents
IF FINAL_VERSION = YES
L276 SET YES ; use 276 line display for NTSC
ENDIF
;-------------------------------------------------------------------------------
COMPILE_ILLEGALOPCODES = 1
DIRECTION_BITS = %111 ; for ManLastDirection
;------------------------------------------------------------------------------
PLUSCART = YES
;------------------------------------------------------------------------------
CHESSBOARD_ROWS = 8 ; number of ROWS of chessboard
LINES_PER_CHAR = 24 ; MULTIPLE OF 3 SO RGB INTERFACES CHARS OK
PIECE_SHAPE_SIZE = 72 ; 3 PF bytes x 24 scanlines
SET_BANK = $3F ; write address to switch ROM banks
SET_BANK_RAM = $3E ; write address to switch RAM banks
RAM_SIZE = $200
RAM_WRITE = $200 ; add this to RAM address when doing writes
RAM = RAM_WRITE
; Platform constants:
PAL = %10
PAL_50 = PAL|0
PAL_60 = PAL|1
IF L276
VBLANK_TIM_NTSC = 48 ; NTSC 276 (Desert Falcon does 280, so this should be pretty safe)
ELSE
VBLANK_TIM_NTSC = 50 ; NTSC 262
ENDIF
VBLANK_TIM_PAL = 85 ;85 ; PAL 312 (we could increase this too, if we want to, but I suppose the used vertical screen size would become very small then)
IF L276
OVERSCAN_TIM_NTSC = 35 ;24 ;51 ; NTSC 276 (Desert Falcon does 280, so this should be pretty safe)
ELSE
OVERSCAN_TIM_NTSC = 8 ;51 ; NTSC 262
ENDIF
OVERSCAN_TIM_PAL = 41 ; PAL 312 (we could increase this too, if we want to, but I suppose the used vertical screen size would become very small then)
IF L276
SCANLINES_NTSC = 276 ; NTSC 276 (Desert Falcon does 280, so this should be pretty safe)
ELSE
SCANLINES_NTSC = 262 ; NTSC 262
ENDIF
SCANLINES_PAL = 312
TIME_PART_2 = 46
TIME_PART_1 = 46
SLOT0 = 0
SLOT1 = 64
SLOT2 = 128
SLOT3 = 192
;------------------------------------------------------------------------------
; MACRO definitions
_ROM_BANK_SIZE = $400
_RAM_BANK_SIZE = $200
MAC NEWBANK ; bank name
SEG {1}
ORG _ORIGIN
RORG _BANK_ADDRESS_ORIGIN
_BANK_START SET *
{1}_START SET *
_CURRENT_BANK SET _ORIGIN/1024
{1} SET _BANK_SLOT + _CURRENT_BANK
_ORIGIN SET _ORIGIN + 1024
ENDM
; MAC DEFINE_1K_SEGMENT ; {seg name}
; ALIGN $400
;SEGMENT_{1} SET *
;BANK_{1} SET _CURRENT_BANK
; ENDM
MAC CHECK_BANK_SIZE ; name
.TEMP = * - _BANK_START
ECHO {1}, "(1K) SIZE = ", .TEMP, ", FREE=", _ROM_BANK_SIZE - .TEMP
IF ( .TEMP ) > _ROM_BANK_SIZE
ECHO "BANK OVERFLOW @ ", {1}, " size=", * - ORIGIN
ERR
ENDIF
ENDM
MAC CHECK_RAM_BANK_SIZE ; name
.TEMP = * - _BANK_START
ECHO {1}, "(512 byte) SIZE = ", .TEMP, ", FREE=", _RAM_BANK_SIZE - .TEMP
IF ( .TEMP ) > _RAM_BANK_SIZE
ECHO "BANK OVERFLOW @ ", {1}, " size=", * - ORIGIN
ERR
ENDIF
ENDM
;---------------------------------------------------------------------------------------------------
; Macro inserts a page break if the object would overlap a page
MAC OPTIONAL_PAGEBREAK ; { string, size }
LIST OFF
IF (>( * + {2} -1 )) > ( >* )
EARLY_LOCATION SET *
ALIGN 256
IF VERBOSE=1
ECHO "PAGE BREAK INSERTED FOR", {1}
ECHO "REQUESTED SIZE =", {2}
ECHO "WASTED SPACE =", *-EARLY_LOCATION
ECHO "PAGEBREAK LOCATION =", *
ENDIF
ENDIF
LIST ON
ENDM
MAC CHECK_PAGE_CROSSING
LIST OFF
IF ( >BLOCK_END != >BLOCK_START )
ECHO "PAGE CROSSING @ ", BLOCK_START
ENDIF
LIST ON
ENDM
MAC CHECKPAGE
LIST OFF
IF >. != >{1}
ECHO ""
ECHO "ERROR: different pages! (", {1}, ",", ., ")"
ECHO ""
ERR
ENDIF
LIST ON
ENDM
MAC CHECKPAGEX
LIST OFF
IF >. != >{1}
ECHO ""
ECHO "ERROR: different pages! (", {1}, ",", ., ") @ {0}"
ECHO {2}
ECHO ""
ERR
ENDIF
LIST ON
ENDM
;---------------------------------------------------------------------------------------------------
; Defines a variable of the given size, making sure it doesn't cross a page
MAC VARIABLE ; {name, size}
OPTIONAL_PAGEBREAK "Variable", {2}
{1} ds {2}
ENDM
;---------------------------------------------------------------------------------------------------
MAC DEF ; name of subroutine
SLOT_{1} SET _BANK_SLOT
BANK_{1} SET SLOT_{1} + _CURRENT_BANK ; bank in which this subroutine resides
{1} ; entry point
TEMPORARY_VAR SET Overlay
TEMPORARY_OFFSET SET 0
VAR_BOUNDARY_{1} SET TEMPORARY_OFFSET
FUNCTION_NAME SET {1}
ENDM
;---------------------------------------------------------------------------------------------------
MAC ALLOCATE
OPTIONAL_PAGEBREAK "Table", {2}
DEF {1}
ENDM
;---------------------------------------------------------------------------------------------------
MAC SLOT ; {1}
IF ({1} < 0) || ({1} > 3)
ECHO "Illegal bank address/segment location", {1}
ERR
ENDIF
_BANK_ADDRESS_ORIGIN SET $F000 + ({1} * _ROM_BANK_SIZE)
_BANK_SLOT SET {1} * 64 ; D7/D6 selector
ENDM
;---------------------------------------------------------------------------------------------------
MAC NEGEVAL
sec
lda #0
sbc Evaluation
sta Evaluation
lda #0
sbc Evaluation+1
sta Evaluation+1
ENDM
MAC SWAP
lda sideToMove
eor #SWAP_SIDE
sta sideToMove
ENDM
;---------------------------------------------------------------------------------------------------
TEMPORARY_OFFSET SET 0
MAC VEND ; {1}
IFNCONST {1}
ECHO "Incorrect VEND label", {1}
ERR
ENDIF
VAREND_{1} = TEMPORARY_VAR
ENDM
MAC REFER ; {1}
IF VAREND_{1} > TEMPORARY_VAR
TEMPORARY_VAR SET VAREND_{1}
ENDIF
ENDM
; Define a temporary variable for use in a subroutine
; Will allocate appropriate bytes, and also check for overflow of the available overlay buffer
MAC VAR ; { name, size }
{1} = TEMPORARY_VAR
TEMPORARY_VAR SET TEMPORARY_VAR + TEMPORARY_OFFSET + {2}
OVERLAY_DELTA SET TEMPORARY_VAR - Overlay
IF OVERLAY_DELTA > MAXIMUM_REQUIRED_OVERLAY_SIZE
MAXIMUM_REQUIRED_OVERLAY_SIZE SET OVERLAY_DELTA
ENDIF
IF OVERLAY_DELTA > OVERLAY_SIZE
ECHO "Temporary Variable", {1}, "overflow!"
ERR
ENDIF
LIST ON
ENDM
;---------------------------------------------------------------------------------------------------
MAC TAG ; {ident/tag}
; {0}
ENDM
;---------------------------------------------------------------------------------------------------
MAC sta@RAM ;{}
sta [RAM]+{0}
ENDM
MAC stx@RAM
stx [RAM]+{0}
ENDM
MAC sty@RAM
sty [RAM]+{0}
ENDM
MAC sta@PLY ;{}
sta [RAM]+{0}
ENDM
MAC stx@PLY
stx [RAM]+{0}
ENDM
MAC sty@PLY
sty [RAM]+{0}
ENDM
MAC lda@RAM ;{}
lda {0}
ENDM
MAC ldx@RAM ;{}
ldx {0}
ENDM
MAC ldy@RAM ;{}
ldy {0}
ENDM
MAC lda@PLY ;{}
lda {0}
ENDM
MAC ldx@PLY ;{}
ldx {0}
ENDM
MAC ldy@PLY ;{}
ldy {0}
ENDM
MAC adc@PLY ;{}
adc {0}
ENDM
MAC sbc@PLY ;{}
sbc {0}
ENDM
MAC cmp@PLY ;{}
cmp {0}
ENDM
;---------------------------------------------------------------------------------------------------
MAC NEWRAMBANK ; bank name
; {1} bank name
; {2} RAM bank number
SEG.U {1}
ORG ORIGIN_RAM
RORG _BANK_ADDRESS_ORIGIN
_BANK_START SET *
RAMBANK_{1} SET _BANK_SLOT + (ORIGIN_RAM / RAM_SIZE)
_CURRENT_RAMBANK SET RAMBANK_{1}
ORIGIN_RAM SET ORIGIN_RAM + RAM_SIZE
ENDM
;---------------------------------------------------------------------------------------------------
MAC RESYNC
; resync screen, X and Y == 0 afterwards
lda #%10 ; make sure VBLANK is ON
sta VBLANK
ldx #8 ; 5 or more RESYNC_FRAMES
.loopResync
VERTICAL_SYNC
ldy #SCANLINES_NTSC/2 - 2
lda Platform
eor #PAL_50 ; PAL-50?
bne .ntsc
ldy #SCANLINES_PAL/2 - 2
.ntsc
.loopWait
sta WSYNC
sta WSYNC
dey
bne .loopWait
dex
bne .loopResync
ENDM
MAC SET_PLATFORM
; 00 = NTSC
; 01 = NTSC
; 10 = PAL-50
; 11 = PAL-60
lda SWCHB
rol
rol
rol
and #%11
eor #PAL
sta Platform ; P1 difficulty --> TV system (0=NTSC, 1=PAL)
ENDM
;---------------------------------------------------------------------------------------------------
; MAC JSROM_SAFE ; {routine}
; ; Saves bank of routine to variable for later restore.
; ; Switches to the bank and does a JSR to the routine.
; lda #BANK_{1}
; sta savedBank
; sta SET_BANK
; jsr {1}
; ENDM
; MAC JSROM ; {routine}
; lda #BANK_{1}
; sta SET_BANK
; jsr {1}
; ENDM
; MAC JSRAM
; lda #BANK_{1}
; sta SET_BANK_RAM
; jsr {1}
; ENDM
MAC TIMECHECK ; {ident}, {branch if out of time}
lda INTIM
cmp #SPEEDOF_{1}
bcc {2}
ENDM
MAC TIMING ; {label}, {cycles}
SPEEDOF_{1} = ({2}/64) + 1
ENDM
;---------------------------------------------------------------------------------------------------
; Failsafe call of function in another bank
; This will check the slot #s for current, call to make sure they're not the same!
MAC CALL ; function name
IF SLOT_{1} == _BANK_SLOT
ECHO "ERROR: Incompatible call to function requiring same slot..."
ECHO "Cannot switch bank in use for", {0}
ERR
ENDIF
lda #BANK_{1}
sta SET_BANK
jsr {1}
ENDM
;---------------------------------------------------------------------------------------------------
#include "zeropage.asm"
#include "overlays.asm"
#include "stack.asm"
ECHO "FREE BYTES IN ZERO PAGE = ", $FF - *
IF * > $FF
ERR "Zero Page overflow!"
ENDIF
;------------------------------------------------------------------------------
;##############################################################################
;------------------------------------------------------------------------------
; NOW THE VERY INTERESTING '3E' RAM BANKS
; EACH BANK HAS A READ-ADDRESS AND A WRITE-ADDRESS, WITH 512 bytes TOTAL ACCESSIBLE
; IN A 1K MEMORY SPACE
SLOT 0
NEWRAMBANK CHESS_BOARD_ROW
REPEAT (CHESSBOARD_ROWS) - 1
NEWRAMBANK .DUMMY
REPEND
; NOTE: THIS BANK JUST *LOOKS* EMPTY.
; It actually contains everything copied from the ROM copy of the ROW RAM banks.
; The variable definitions are also in that ROM bank (even though they're RAM :)
; Now we have the actual graphics data for each of the rows. This consists of an
; actual bitmap (in exact PF-style format, 6 bytes per line) into which the
; character shapes are masked/copied. The depth of the character shapes may be
; changed by changing the #LINES_PER_CHAR value. Note that this depth should be
; a multiple of 3, so that the RGB scanlines match at character joins.
; We have one bank for each chessboard row. These banks are duplicates of the above,
; accessed via the above labels but with the appropriate bank switched in.
;---------------------------------------------------------------------------------------------------
RND_EOR_VAL = $FE ;B4
MAC NEXT_RANDOM
lda rnd
lsr
bcc .skipEOR
eor #RND_EOR_VAL
.skipEOR sta rnd
ENDM
;--------------------------------------------------------------------------------
MAC PHASE ;#
lda #{1}
sta aiState
ENDM
;--------------------------------------------------------------------------------
MAC COMMON_VARS_ALPHABETA
VAR __thinkbar, 1
VAR __toggle, 1
VAR __bestMove, 1
VAR __alpha, 2
VAR __beta, 2
VAR __negaMax, 2
VAR __value, 2
VAR __quiesceCapOnly, 1
ENDM
;--------------------------------------------------------------------------------
include "[email protected]" ; MUST be first in ROM - contains reset vectors
include "BANK_GENERIC@1#1.asm"
include "BANK_ROM_SHADOW_SCREEN.asm"
include "[email protected]"
include "BANK_PLY.asm"
include "SHADOW_PLY.asm"
include "SHADOW_BOARD.asm"
include "BANK_EVAL.asm"
include "BANK_StateMachine@1#1.asm"
include "BANK_StateMachine@1#2.asm"
include "BANK_RECON.asm"
include "piece_graphics.asm"
include "[email protected]"
include "BANK_GENERIC@1#3.asm"
include "GFX1.asm"
include "GFX2.asm"
include "GFX3.asm"
include "GFX4.asm"
include "NEGAMAX.asm"
include "Handler_MACROS.asm"
include "GENMOVE.asm"
include "GENMOVE2.asm"
include "BANK_GENERIC@1#2.asm"
include "BANK_3.asm"
;include "Handler_MACROS.asm"
;include "BANK_GENERIC.asm"
;include "BANK_GENERIC2.asm"
;include "BANK_ROM_SHADOW_SCREEN.asm"
;include "BANK_CHESS_INCLUDES.asm"
;include "BANK_StateMachine.asm"
;include "BANK_TEXT_OVERLAYS.asm"
;include "BANK_PLIST.asm"
;include "titleScreen.asm"
;include "BANK_RECON.asm"
; The handlers for piece move generation
;include "Handler_BANK1.asm"
;include "BANK_PLY.asm"
;include "BANK_EVAL.asm"
;include "BANK_SPEAK.asm"
; MUST BE LAST...
;include "BANK_FIXED.asm"
;END