eris2010

Documentation: http://frombelow.net/projects/eris2010/
Clone: git clone https://git.frombelow.net/eris2010.git
Log | Files | Refs | README | LICENSE

microchess.asm (24218B)


      1 ;***********************************************************************
      2 ;
      3 ;  Kim-1 MicroChess (c) 1976-2005 Peter Jennings, www.benlo.com 
      4 ;
      5 ;***********************************************************************
      6 
      7 ; All rights reserved.
      8 
      9 ; Redistribution and use in source and binary forms, with or without
     10 ; modification, are permitted provided that the following conditions
     11 ; are met:
     12 ; 1. Redistributions of source code must retain the above copyright
     13 ;    notice, this list of conditions and the following disclaimer.
     14 ; 2. Redistributions in binary form must reproduce the above copyright
     15 ;    notice, this list of conditions and the following disclaimer in the
     16 ;    documentation and/or other materials provided with the distribution.
     17 ; 3. The name of the author may not be used to endorse or promote products
     18 ;    derived from this software without specific prior written permission.
     19 
     20 ; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
     21 ; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22 ; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23 ; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24 ; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25 ; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29 ; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 ;
     32 ; modified by Daryl Rictor to work over a 
     33 ; serial terminal connection, August 2002.
     34 ;
     35 ; Updated with corrections to earlier OCR errors by Bill Forster, August 2005.
     36 ;
     37 ;
     38 ; Adapted to work on the Eris 2010 8-Bit System with 64tass by Gerd Beuster,
     39 ; January 2021. All changes by Gerd Beuster toggled by VERSION_ERIS2010.
     40 ;
     41 VERSION_ERIS2010 = true
     42 .if !VERSION_ERIS2010
     43    cpu 65c02
     44    page 0,132
     45 .endif
     46 ;
     47 ; 6551 I/O Port Addresses
     48 ;
     49 .if VERSION_ERIS2010
     50 .if SYMON
     51 ACIADat	= 	$8800
     52 .else
     53 ACIADat	= 	$c000
     54 .endif
     55 ACIASta	=	ACIADat + 1
     56 ACIACmd	=	ACIADat + 2
     57 ACIACtl	=	ACIADat + 3
     58 .else
     59 ACIADat	= 	$7F70
     60 ACIASta	=	$7F71
     61 ACIACmd	=	$7F72
     62 ACIACtl	=	$7F73
     63 .endif
     64 ;
     65 ; page zero variables
     66 ;
     67 BOARD   =	$50 
     68 BK      =	$60 
     69 PIECE   =	$B0 
     70 SQUARE  =	$B1 
     71 SP2     =	$B2 
     72 SP1     =	$B3 
     73 INCHEK  =	$B4 
     74 STATE   =	$B5 
     75 MOVEN   =	$B6 
     76 REV	=       $B7
     77 OMOVE   =	$DC 
     78 WCAP0   =	$DD 
     79 COUNT   =	$DE 
     80 BCAP2   =	$DE 
     81 WCAP2   =	$DF 
     82 BCAP1   =	$E0 
     83 WCAP1   =	$E1 
     84 BCAP0   =	$E2 
     85 MOB     =	$E3 
     86 MAXC    =	$E4 
     87 CC      =	$E5 
     88 PCAP    =	$E6 
     89 BMOB    =	$E3 
     90 BMAXC   =	$E4 
     91 BMCC    =	$E5 		; was BCC (TASS doesn't like it as a label)
     92 BMAXP   =	$E6 
     93 XMAXC   =	$E8 
     94 WMOB    =	$EB 
     95 WMAXC   =	$EC 
     96 WCC     =	$ED 
     97 WMAXP   =	$EE 
     98 PMOB    =	$EF 
     99 PMAXC   =	$F0 
    100 PCC     =	$F1 
    101 PCP     =	$F2 
    102 OLDKY   =	$F3 
    103 BESTP   =	$FB 
    104 BESTV   =	$FA 
    105 BESTM   =	$F9 
    106 DIS1    =	$FB 
    107 DIS2    =	$FA 
    108 DIS3    =	$F9 
    109 temp    =       $FC
    110 ;
    111 ;
    112 ;
    113 .if VERSION_ERIS2010
    114 	.if SYMON
    115 		* = $0300
    116 	.else
    117 		* = $0200
    118 	.endif
    119 .else	
    120 		*=$1000			; load into RAM @ $1000-$15FF
    121 .endif
    122 
    123 		LDA     #$00		; REVERSE TOGGLE
    124 		STA     REV
    125                 JSR     Init_6551
    126 CHESS		CLD			; INITIALIZE
    127 		LDX	#$FF		; TWO STACKS
    128 		TXS	
    129 		LDX	#$C8
    130 		STX	SP2
    131 ;		
    132 ;       ROUTINES TO LIGHT LED
    133 ;       DISPLAY AND GET KEY
    134 ;       FROM KEYBOARD
    135 ;		
    136 OUT		JSR	pout		; DISPLAY AND
    137 		JSR	KIN		; GET INPUT   *** my routine waits for a keypress
    138 ;		CMP	OLDKY		; KEY IN ACC  *** no need to debounce
    139 ;		BEQ	OUT		; (DEBOUNCE)
    140 ;		STA	OLDKY
    141 ;		
    142 		cmp	#$43		; [C]
    143 		BNE	NOSET		; SET UP
    144 		LDX	#$1F		; BOARD
    145 WHSET		LDA	SETW,X		; FROM
    146 		STA	BOARD,X		; SETW
    147 		DEX	
    148 		BPL	WHSET
    149 		LDX	#$1B		; *ADDED
    150 		STX	OMOVE		; INITS TO $FF
    151 		LDA	#$CC		; Display CCC
    152 		BNE	CLDSP
    153 ;		
    154 NOSET		cmp	#$45		; [E]
    155 		BNE	NOREV		; REVERSE
    156 		JSR	REVERSE        	; BOARD IS
    157 		SEC
    158 		LDA	#$01
    159 		SBC	REV
    160 		STA	REV		; TOGGLE REV FLAG
    161 		LDA	#$EE            ; IS
    162 		BNE	CLDSP
    163 ;		
    164 NOREV		cmp	#$40			; [P]
    165 		BNE	NOGO           	; PLAY CHESS
    166 		JSR	GO
    167 CLDSP		STA	DIS1          	; DISPLAY
    168 		STA	DIS2         	; ACROSS
    169 		STA	DIS3          	; DISPLAY
    170 		BNE	CHESS
    171 ;		
    172 NOGO		CMP	#$0D            ; [Enter]
    173 		BNE	NOMV          	; MOVE MAN
    174 		JSR	MOVE          	; AS ENTERED
    175 		JMP	DISP
    176 NOMV		cmp     #$41		; [Q] ***Added to allow game exit***
    177 		BEQ     DONE		; quit the game, exit back to system.  
    178 		JMP	INPUT		; process move
    179 DONE		JMP     ($FFFC)		; *** MUST set this to YOUR OS starting address
    180 ;		
    181 ;       THE ROUTINE JANUS DIRECTS THE
    182 ;       ANALYSIS BY DETERMINING WHAT
    183 ;       SHOULD OCCUR AFTER EACH MOVE
    184 ;       GENERATED BY GNM
    185 ;		
    186 ;		
    187 ;
    188 JANUS		LDX	STATE
    189 		BMI	NOCOUNT
    190 ;		
    191 ;       THIS ROUTINE COUNTS OCCURRENCES
    192 ;       IT DEPENDS UPON STATE TO INDEX
    193 ;       THE CORRECT COUNTERS
    194 ;		
    195 COUNTS		LDA	PIECE
    196 		BEQ	OVER           	; IF STATE=8
    197 		CPX 	#$08           	; DO NOT COUNT
    198 		BNE	OVER          	; BLK MAX CAP
    199 		CMP	BMAXP        	; MOVES FOR
    200 		BEQ	XRT           	; WHITE
    201 ; 		
    202 OVER		INC	MOB,X          	; MOBILITY
    203 		CMP 	#$01           	;  + QUEEN
    204 		BNE	NOQ           	; FOR TWO
    205 		INC	MOB,X
    206 ;		
    207 NOQ		BVC	NOCAP
    208 		LDY	#$0F           	; CALCULATE
    209 		LDA	SQUARE       	; POINTS
    210 ELOOP		CMP	BK,Y           	; CAPTURED
    211 		BEQ	FOUN          	; BY THIS
    212 		DEY			; MOVE
    213 		BPL	ELOOP
    214 FOUN		LDA	POINTS,Y
    215 		CMP	MAXC,X
    216 		BCC	LESS          	; SAVE IF
    217 		STY	PCAP,X         	; BEST THIS
    218 		STA	MAXC,X         	; STATE
    219 ;		
    220 LESS		CLC	
    221 		PHP			; ADD TO
    222 		ADC	CC,X           	; CAPTURE
    223 		STA	CC,X           	; COUNTS
    224 		PLP	
    225 ;		
    226 NOCAP		CPX	#$04
    227 		BEQ	ON4
    228 		BMI	TREE          	;(=00 ONLY)
    229 XRT		RTS	
    230 ;		
    231 ;      GENERATE FURTHER MOVES FOR COUNT
    232 ;      AND ANALYSIS	
    233 ;		
    234 ON4		LDA	XMAXC        	; SAVE ACTUAL 
    235 		STA	WCAP0         	; CAPTURE
    236 		LDA	#$00           	; STATE=0
    237 		STA	STATE
    238 		JSR	MOVE          	; GENERATE
    239 		JSR	REVERSE       	; IMMEDIATE
    240 		JSR	GNMZ         	; REPLY MOVES  
    241 		JSR	REVERSE
    242 ;		
    243 		LDA	#$08       	; STATE=8
    244 		STA	STATE        	; GENERATE
    245 		JSR	GNM          	; CONTINUATION
    246 		JSR	UMOVE         	; MOVES
    247 ;		
    248 		JMP	STRATGY       	; FINAL EVALUATION
    249 NOCOUNT	CPX	#$F9
    250 		BNE	TREE
    251 ;		
    252 ;      DETERMINE IF THE KING CAN BE
    253 ;      TAKEN, USED BY CHKCHK
    254 ;		
    255 		LDA	BK           	; IS KING
    256 		CMP	SQUARE       	; IN CHECK?
    257 		BNE	RETJ          	; SET INCHEK=0
    258 		LDA	#$00           	; IF IT IS
    259 		STA	INCHEK
    260 RETJ		RTS	
    261 ;		
    262 ;      IF A PIECE HAS BEEN CAPTURED BY
    263 ;      A TRIAL MOVE, GENERATE REPLIES &
    264 ;      EVALUATE THE EXCHANGE GAIN/LOSS
    265 ;		
    266 TREE		BVC	RETJ          	; NO CAP
    267 		LDY	#$07           	; (PIECES)
    268 		LDA	SQUARE
    269 LOOPX		CMP	BK,Y
    270 		BEQ	FOUNX
    271 		DEY	
    272 		BEQ	RETJ          	; (KING)
    273 		BPL	LOOPX         	; SAVE
    274 FOUNX		LDA	POINTS,Y       	; BEST CAP
    275 		CMP	BCAP0,X        	; AT THIS
    276 		BCC	NOMAX         	; LEVEL
    277 		STA	BCAP0,X
    278 NOMAX		DEC	STATE
    279 		LDA	#$FB           	; IF STATE=FB
    280 		CMP	STATE        	; TIME TO TURN
    281 		BEQ	UPTREE        	; AROUND
    282 		JSR	GENRM         	; GENERATE FURTHER
    283 UPTREE		INC	STATE        	; CAPTURES
    284 		RTS	
    285 ;		
    286 ;      THE PLAYER'S MOVE IS INPUT
    287 ;		
    288 INPUT		CMP	#$08           	; NOT A LEGAL
    289 		BCS	ERROR      	; SQUARE #
    290 		JSR	DISMV
    291 DISP		LDX	#$1F
    292 SEARCH		LDA	BOARD,X
    293 		CMP	DIS2
    294 		BEQ	HERE          	; DISPLAY
    295 		DEX			; PIECE AT    
    296 		BPL	SEARCH        	; FROM
    297 HERE		STX	DIS1         	; SQUARE
    298 		STX	PIECE
    299 ERROR		JMP	CHESS
    300 ;		
    301 ;      GENERATE ALL MOVES FOR ONE
    302 ;      SIDE, CALL JANUS AFTER EACH
    303 ;      ONE FOR NEXT STEP
    304 ;		
    305 ;
    306 GNMZ		LDX	#$10            ; CLEAR
    307 GNMX		LDA	#$00            ; COUNTERS
    308 CLEAR		STA	COUNT,X
    309 		DEX	
    310 		BPL	CLEAR
    311 ;		
    312 GNM		LDA	#$10            ; SET UP
    313 		STA	PIECE        	; PIECE
    314 NEWP		DEC	PIECE        	; NEW PIECE
    315 		BPL	NEX           	; ALL DONE?
    316 		RTS			;    -YES
    317 ;		
    318 NEX		JSR	RESET        	; READY
    319 		LDY	PIECE        	; GET PIECE
    320 		LDX	#$08
    321 		STX	MOVEN        	; COMMON START
    322 		CPY	#$08            ; WHAT IS IT?
    323 		BPL	PAWN          	; PAWN
    324 		CPY	#$06
    325 		BPL	KNIGHT        	; KNIGHT
    326 		CPY	#$04
    327 		BPL	BISHOP       	; BISHOP
    328 		CPY	#$01
    329 		BEQ	QUEEN         	; QUEEN
    330 		BPL	ROOK          	; ROOK
    331 ;		
    332 KING		JSR	SNGMV         	; MUST BE KING!
    333 		BNE	KING          	; MOVES
    334 		BEQ	NEWP          	; 8 TO 1
    335 QUEEN		JSR	LINE
    336 		BNE	QUEEN         	; MOVES
    337 		BEQ	NEWP          	; 8 TO 1
    338 ;		
    339 ROOK		LDX	#$04
    340 		STX	MOVEN        	; MOVES
    341 AGNR		JSR	LINE          	; 4 TO 1
    342 		BNE	AGNR
    343 		BEQ	NEWP
    344 ;		
    345 BISHOP		JSR	LINE
    346 		LDA	MOVEN        	; MOVES
    347 		CMP	#$04           	; 8 TO 5
    348 		BNE	BISHOP
    349 		BEQ	NEWP
    350 ;		
    351 KNIGHT		LDX	#$10
    352 		STX	MOVEN        	; MOVES
    353 AGNN		JSR	SNGMV         	; 16 TO 9
    354 		LDA	MOVEN
    355 		CMP	#$08
    356 		BNE	AGNN
    357 		BEQ	NEWP
    358 ;		
    359 PAWN		LDX	#$06
    360 		STX	MOVEN
    361 P1		JSR	CMOVE         	; RIGHT CAP?
    362 		BVC	P2
    363 		BMI	P2
    364 		JSR	JANUS         	; YES
    365 P2		JSR	RESET
    366 		DEC	MOVEN        	; LEFT CAP?
    367 		LDA	MOVEN
    368 		CMP	#$05
    369 		BEQ	P1
    370 P3		JSR	CMOVE         	; AHEAD
    371 		BVS	NEWP          	; ILLEGAL
    372 		BMI	NEWP
    373 		JSR	JANUS
    374 		LDA	SQUARE       	; GETS TO
    375 		AND	#$F0           	; 3RD RANK?
    376 		CMP	#$20
    377 		BEQ	P3            	; DO DOUBLE
    378 		JMP	NEWP
    379 ;		
    380 ;      CALCULATE SINGLE STEP MOVES
    381 ;      FOR K,N	
    382 ;		
    383 SNGMV		JSR	CMOVE        	; CALC MOVE
    384 		BMI	ILL1           	; -IF LEGAL
    385 		JSR	JANUS           ; -EVALUATE
    386 ILL1		JSR	RESET
    387 		DEC	MOVEN
    388 		RTS	
    389 ;		
    390 ;     CALCULATE ALL MOVES DOWN A
    391 ;     STRAIGHT LINE FOR Q,B,R
    392 ;		
    393 LINE		JSR	CMOVE         	; CALC MOVE
    394 		BCC	OVL            	; NO CHK
    395 		BVC	LINE		; NOCAP       
    396 OVL		BMI	ILL             ; RETURN
    397 		PHP	
    398 		JSR	JANUS         	; EVALUATE POSN
    399 		PLP	
    400 		BVC	LINE          	; NOT A CAP
    401 ILL		JSR	RESET         	; LINE STOPPED
    402 		DEC	MOVEN         	; NEXT DIR
    403 		RTS	
    404 ;		
    405 ;      EXCHANGE SIDES FOR REPLY
    406 ;      ANALYSIS	
    407 ;		
    408 REVERSE		LDX	#$0F
    409 ETC		SEC	
    410 		LDY	BK,X           	; SUBTRACT
    411 		LDA 	#$77           	; POSITION
    412 		SBC  	BOARD,X        	; FROM 77
    413 		STA  	BK,X
    414 		STY  	BOARD,X         ; AND
    415 		SEC	
    416 		LDA	#$77           	; EXCHANGE
    417 		SBC 	BOARD,X        	; PIECES
    418 		STA  	BOARD,X
    419 		DEX	
    420 		BPL   	ETC
    421 		RTS	
    422 ;		
    423 ;        CMOVE CALCULATES THE TO SQUARE
    424 ;        USING SQUARE AND THE MOVE
    425 ;       TABLE,  FLAGS SET AS FOLLOWS:
    426 ;       N - ILLEGAL MOVE
    427 ;       V - CAPTURE (LEGAL UNLESS IN CH)
    428 ;       C - ILLEGAL BECAUSE OF CHECK
    429 ;       [MY THANKS TO JIM BUTTERFIELD
    430 ;        WHO WROTE THIS MORE EFFICIENT
    431 ;        VERSION OF CMOVE]
    432 ;		
    433 CMOVE		LDA	SQUARE       	; GET SQUARE
    434 		LDX	MOVEN       	; MOVE POINTER
    435 		CLC	
    436 		ADC	MOVEX,X        	; MOVE LIST
    437 		STA	SQUARE       	; NEW POS'N
    438 		AND	#$88
    439 		BNE	ILLEGAL       	; OFF BOARD
    440 		LDA	SQUARE
    441 ;			
    442 		LDX	#$20
    443 LOOP		DEX			; IS TO
    444 		BMI	NO            	; SQUARE
    445 		CMP  	BOARD,X        	; OCCUPIED?
    446 		BNE   	LOOP
    447 ;			
    448 		CPX	#$10            ; BY SELF?
    449 		BMI   	ILLEGAL
    450 ;			
    451 		LDA	#$7F		; MUST BE CAP!
    452 		ADC	#$01            ; SET V FLAG
    453 		BVS   	SPX 	        ; (JMP)
    454 ;			
    455 NO		CLV			; NO CAPTURE
    456 ;			
    457 SPX		LDA  	STATE         	; SHOULD WE
    458 		BMI   	RETL           	; DO THE
    459 		CMP	#$08 	        ; CHECK CHECK?
    460 		BPL  	RETL
    461 ;			
    462 ;        CHKCHK REVERSES SIDES
    463 ;       AND LOOKS FOR A KING
    464 ;       CAPTURE TO INDICATE
    465 ;       ILLEGAL MOVE BECAUSE OF
    466 ;       CHECK  SINCE THIS IS
    467 ;       TIME CONSUMING, IT IS NOT
    468 ;       ALWAYS DONE	
    469 ;		
    470 CHKCHK	        PHA			; STATE
    471 		PHP	
    472 		LDA	#$F9
    473 		STA	STATE         	; GENERATE
    474 		STA	INCHEK        	; ALL REPLY
    475 		JSR	MOVE          	; MOVES TO
    476 		JSR	REVERSE       	; SEE IF KING
    477 		JSR	GNM           	; IS IN
    478 		JSR	RUM           	; CHECK
    479 		PLP	
    480 		PLA	
    481 		STA	STATE
    482 		LDA	INCHEK
    483 		BMI	RETL           	; NO - SAFE
    484 		SEC			; YES - IN CHK
    485 		LDA	#$FF
    486 		RTS	
    487 ;		
    488 RETL		CLC			; LEGAL
    489 		LDA	#$00            ; RETURN
    490 		RTS	
    491 ;		
    492 ILLEGAL	LDA	#$FF
    493 		CLC			; ILLEGAL
    494 		CLV			; RETURN
    495 		RTS	
    496 ;		
    497 ;       REPLACE PIECE ON CORRECT SQUARE
    498 ;		
    499 RESET		LDX	PIECE      	; GET LOGAT
    500 		LDA	BOARD,X        	; FOR PIECE
    501 		STA	SQUARE       	; FROM BOARD
    502 		RTS	
    503 ;		
    504 ;		
    505 ;		
    506 GENRM		JSR	MOVE          	; MAKE MOVE
    507 GENR2		JSR	REVERSE      	; REVERSE BOARD
    508 		JSR	GNM          	; GENERATE MOVES
    509 RUM		JSR	REVERSE   	; REVERSE BACK
    510 ;		
    511 ;       ROUTINE TO UNMAKE A MOVE MADE BY
    512 ;	  MOVE
    513 ;		
    514 UMOVE		TSX			; UNMAKE MOVE
    515 		STX	SP1
    516 		LDX	SP2           	; EXCHANGE
    517 		TXS			; STACKS
    518 		PLA			; MOVEN
    519 		STA	MOVEN
    520 		PLA			; CAPTURED
    521 		STA	PIECE        	; PIECE
    522 		TAX	
    523 		PLA			; FROM SQUARE
    524 		STA	BOARD,X
    525 		PLA			; PIECE
    526 		TAX	
    527 		PLA			; TO SOUARE
    528 		STA	SQUARE
    529 		STA	BOARD,X
    530 		JMP	STRV
    531 ;		
    532 ;       THIS ROUTINE MOVES PIECE
    533 ;       TO SQUARE, PARAMETERS
    534 ;       ARE SAVED IN A STACK TO UNMAKE
    535 ;       THE MOVE LATER
    536 ;		
    537 MOVE		TSX	
    538 		STX	SP1          	; SWITCH
    539 		LDX	SP2          	; STACKS
    540 		TXS	
    541 		LDA	SQUARE
    542 		PHA			; TO SQUARE
    543 		TAY	
    544 		LDX	#$1F
    545 CHECK		CMP	BOARD,X        	; CHECK FOR
    546 		BEQ	TAKE          	; CAPTURE
    547 		DEX	
    548 		BPL	CHECK
    549 TAKE		LDA	#$CC
    550 		STA	BOARD,X
    551 		TXA			; CAPTURED
    552 		PHA			; PIECE
    553 		LDX	PIECE
    554 		LDA	BOARD,X
    555 		STY	BOARD,X        	; FROM
    556 		PHA			; SQUARE
    557 		TXA	
    558 		PHA			; PIECE
    559 		LDA	MOVEN
    560 		PHA			; MOVEN
    561 STRV		TSX	
    562 		STX	SP2           	; SWITCH
    563 		LDX	SP1           	; STACKS
    564 		TXS			; BACK
    565 		RTS	
    566 ;			
    567 ;       CONTINUATION OF SUB STRATGY
    568 ;       -CHECKS FOR CHECK OR CHECKMATE
    569 ;       AND ASSIGNS VALUE TO MOVE
    570 ;		
    571 CKMATE	        LDX	BMAXC         	; CAN BLK CAP
    572 		CPX	POINTS       	; MY KING?
    573 		BNE	NOCHEK	
    574 		LDA	#$00           	; GULP!
    575 		BEQ	RETV          	; DUMB MOVE!
    576 ;		
    577 NOCHEK	LDX	BMOB         		; IS BLACK
    578 		BNE	RETV          	; UNABLE TO
    579 		LDX	WMAXP        	; MOVE AND
    580 		BNE	RETV          	; KING IN CH?
    581 		LDA	#$FF           	; YES! MATE
    582 ;		
    583 RETV		LDX	#$04            ; RESTORE
    584 		STX	STATE        	; STATE=4
    585 ;		
    586 ;       THE VALUE OF THE MOVE (IN ACCU)
    587 ;       IS COMPARED TO THE BEST MOVE AND
    588 ;       REPLACES IT IF IT IS BETTER
    589 ;		
    590 PUSH		CMP	BESTV         	; IS THIS BEST
    591 		BCC	RETP          	; MOVE SO FAR?
    592 		BEQ	RETP
    593 		STA	BESTV        	; YES!
    594 		LDA	PIECE        	; SAVE IT
    595 		STA	BESTP
    596 		LDA	SQUARE
    597 		STA	BESTM        	; FLASH DISPLAY
    598 RETP		LDA	#"."		; print ... instead of flashing disp
    599 		Jmp	syschout	; print . and return
    600 ;		
    601 ;       MAIN PROGRAM TO PLAY CHESS
    602 ;       PLAY FROM OPENING OR THINK
    603 ;		
    604 GO		LDX	OMOVE        	; OPENING?
    605 		BMI	NOOPEN          ; -NO   *ADD CHANGE FROM BPL
    606 		LDA	DIS3         	; -YES WAS
    607 		CMP	OPNING,X        ; OPPONENT'S
    608 		BNE	END            	; MOVE OK?
    609 		DEX	
    610 		LDA	OPNING,X       	; GET NEXT
    611 		STA	DIS1         	; CANNED
    612 		DEX			; OPENING MOVE
    613 		LDA	OPNING,X
    614 		STA	DIS3         	; DISPLAY IT
    615 		DEX	
    616 		STX	OMOVE        	; MOVE IT
    617 		BNE	MV2            	; (JMP)
    618 ;			
    619 END		LDA     #$FF		; *ADD - STOP CANNED MOVES
    620 		STA	OMOVE        	; FLAG OPENING
    621 NOOPEN	LDX	#$0C            	; FINISHED
    622 		STX	STATE        	; STATE=C
    623 		STX	BESTV        	; CLEAR BESTV
    624 		LDX	#$14           	; GENERATE P
    625 		JSR	GNMX          	; MOVES
    626 ;		
    627 		LDX	#$04           	; STATE=4
    628 		STX	STATE        	; GENERATE AND
    629 		JSR	GNMZ          	; TEST AVAILABLE
    630 ;	MOVES
    631 ;		
    632 		LDX	BESTV        	; GET BEST MOVE
    633 		CPX	#$0F           	; IF NONE
    634 		BCC	MATE          	; OH OH!
    635 ;		
    636 MV2		LDX	BESTP        	; MOVE
    637 		LDA	BOARD,X         ; THE
    638 		STA	BESTV        	; BEST
    639 		STX	PIECE        	; MOVE
    640 		LDA	BESTM
    641 		STA	SQUARE       	; AND DISPLAY
    642 		JSR	MOVE           	; IT
    643 		JMP	CHESS
    644 ;		
    645 MATE		LDA	#$FF           	; RESIGN
    646 		RTS			; OR STALEMATE
    647 ;		
    648 ;       SUBROUTINE TO ENTER THE
    649 ;       PLAYER'S MOVE
    650 ;		
    651 DISMV		LDX	#$04           	; ROTATE
    652 DROL		ASL	DIS3          	; KEY
    653 		ROL	DIS2         	; INTO
    654 		DEX			; DISPLAY
    655 		BNE	DROL		;
    656 		ORA	DIS3
    657 		STA	DIS3
    658 		STA	SQUARE
    659 		RTS	
    660 ;		
    661 ;       THE FOLLOWING SUBROUTINE ASSIGNS
    662 ;       A VALUE TO THE MOVE UNDER
    663 ;       CONSIDERATION AND RETURNS IT IN
    664 ;	THE ACCUMULATOR
    665 ;		
    666 
    667 STRATGY	CLC	
    668 		LDA	#$80
    669 		ADC	WMOB         	; PARAMETERS
    670 		ADC	WMAXC        	; WITH WEIGHT
    671 		ADC	WCC          	; OF 0.25
    672 		ADC	WCAP1
    673 		ADC	WCAP2
    674 		SEC	
    675 		SBC	PMAXC
    676 		SBC	PCC
    677 		SBC	BCAP0
    678 		SBC	BCAP1
    679 		SBC	BCAP2
    680 		SBC	PMOB
    681 		SBC	BMOB
    682 		BCS	POS           	; UNDERFLOW
    683 		LDA	#$00           	; PREVENTION
    684 POS		LSR	
    685 		CLC			; **************
    686 		ADC	#$40
    687 		ADC	WMAXC       	; PARAMETERS
    688 		ADC	WCC         	; WITH WEIGHT
    689 		SEC			; OF 0.5
    690 		SBC	BMAXC
    691 		LSR			; **************
    692 		CLC	
    693 		ADC	#$90
    694 		ADC	WCAP0       	; PARAMETERS
    695 		ADC	WCAP0       	; WITH WEIGHT
    696 		ADC	WCAP0       	; OF 1.0
    697 		ADC	WCAP0
    698 		ADC	WCAP1
    699 		SEC			; [UNDER OR OVER-
    700 		SBC	BMAXC        	; FLOW MAY OCCUR
    701 		SBC	BMAXC        	; FROM THIS
    702 		SBC	BMCC          	; SECTION]
    703 		SBC	BMCC
    704 		SBC	BCAP1
    705 		LDX	SQUARE      	; ***************
    706 		CPX	#$33
    707 		BEQ	POSN         	; POSITION
    708 		CPX	#$34           	; BONUS FOR
    709 		BEQ	POSN         	; MOVE TO
    710 		CPX	#$22           	; CENTRE
    711 		BEQ	POSN           	; OR
    712 		CPX	#$25           	; OUT OF
    713 		BEQ	POSN         	; BACK RANK
    714 		LDX	PIECE
    715 		BEQ	NOPOSN
    716 		LDY	BOARD,X
    717 		CPY	#$10
    718 		BPL	NOPOSN
    719 POSN		CLC
    720 		ADC	#$02
    721 NOPOSN	JMP	CKMATE       		; CONTINUE
    722 
    723 
    724 ;-----------------------------------------------------------------
    725 ; The following routines were added to allow text-based board
    726 ; display over a standard RS-232 port.
    727 ;
    728 POUT        	jsr 	pout9		; print CRLF
    729 		jsr     pout13		; print copyright
    730 		JSR	POUT10		; print column labels
    731 		LDY   	#$00		; init board location
    732 		JSR	POUT5		; print board horz edge
    733 POUT1		lDA   	#"|"		; print vert edge
    734 		JSR   	syschout	; PRINT ONE ASCII CHR - SPACE
    735 		LDX   	#$1F
    736 POUT2		TYA			; scan the pieces for a location match
    737             	CMP	BOARD,X		; match found?
    738             	BEQ   	POUT4		; yes; print the piece's color and type
    739             	DEX			; no
    740             	BPL	POUT2		; if not the last piece, try again
    741 		tya			; empty square	
    742 		and	#$01		; odd or even column?				
    743 		sta   	temp		; save it
    744 		tya  			; is the row odd or even
    745 		lsr			; shift column right 4 spaces 
    746 		lsr			;
    747 		lsr			;
    748 		lsr			;
    749 		and   	#$01		; strip LSB  
    750 		clc			; 
    751 		adc   	temp		; combine row & col to determine square color  
    752 		and   	#$01		; is board square white or blk?
    753 		beq	pout25 		; white, print space
    754 		lda   	#"*"		; black, print *
    755 .if VERSION_ERIS2010
    756 		.byte	$2c		; used to skip over LDA #$20
    757 .else
    758 		db	$2c		; used to skip over LDA #$20
    759 .endif
    760 POUT25		LDA   	#$20		; ASCII space
    761 		JSR   	syschout	; PRINT ONE ASCII CHR - SPACE
    762 		JSR   	syschout	; PRINT ONE ASCII CHR - SPACE
    763 POUT3		INY			; 
    764 	          TYA			; get row number
    765             	AND   	#$08		; have we completed the row?	 
    766             	BEQ   	POUT1		; no, do next column
    767 		LDA   	#"|"		; yes, put the right edge on
    768 		JSR   	syschout	; PRINT ONE ASCII CHR - |             
    769 		jsr	pout12		; print row number
    770 		JSR   	POUT9		; print CRLF
    771             	JSR   	POUT5		; print bottom edge of board
    772 		CLC			; 
    773 		TYA			; 
    774 		ADC	#$08		; point y to beginning of next row
    775 		TAY			;
    776 		CPY   	#$80		; was that the last row?
    777 		BEQ   	POUT8		; yes, print the LED values
    778 		BNE   	POUT1		; no, do new row
    779 
    780 POUT4		LDA   	REV		; print piece's color & type
    781 		BEQ   	POUT41		;
    782 		LDA	cpl+16,X	;
    783 		BNE	POUT42		;
    784 POUT41		LDA   	cpl,x		;
    785 POUT42		JSR	syschout	;
    786 		lda	cph,x		;
    787 		jsr   	syschout	; 
    788 		BNE	POUT3		; branch always
    789 
    790 POUT5       	TXA			; print "-----...-----<crlf>"
    791 		PHA
    792 		LDX	#$19
    793 		LDA	#"-"
    794 POUT6		JSR   	syschout	; PRINT ONE ASCII CHR - "-"
    795 		DEX
    796 		BNE	POUT6
    797 		PLA
    798 		TAX
    799 		JSR	POUT9
    800 		RTS	 		
    801 
    802 POUT8		jsr	pout10		; 
    803 		LDA   	$FB
    804 		JSR   	syshexout	; PRINT 1 BYTE AS 2 HEX CHRS	
    805         	LDA   	#$20
    806 		JSR   	syschout	; PRINT ONE ASCII CHR - SPACE
    807         	LDA   	$FA
    808 		JSR   	syshexout	; PRINT 1 BYTE AS 2 HEX CHRS	
    809         	LDA   	#$20
    810 		JSR   	syschout	; PRINT ONE ASCII CHR - SPACE
    811         	LDA   	$F9
    812 		JSR   	syshexout	; PRINT 1 BYTE AS 2 HEX CHRS	
    813 
    814 POUT9      	LDA   	#$0D
    815 		JSR   	syschout	; PRINT ONE ASCII CHR - CR
    816         	LDA   	#$0A
    817 		JSR   	syschout	; PRINT ONE ASCII CHR - LF
    818                 RTS 
    819 
    820 pout10		ldx   	#$00		; print the column labels
    821 POUT11		lda	#$20		; 00 01 02 03 ... 07 <CRLF>
    822 		jsr   	syschout
    823 		txa
    824 		jsr	syshexout
    825 		INX
    826 		CPX   	#$08
    827 		BNE	POUT11
    828 		BEQ	POUT9
    829 POUT12		TYA
    830 		and 	#$70
    831 		JSR 	syshexout
    832 		rts
    833 
    834 Pout13		ldx   	#$00		; Print the copyright banner
    835 Pout14		lda   	banner,x
    836 		beq   	POUT15
    837 		jsr   	syschout
    838 		inx
    839 		bne   	POUT14
    840 POUT15		rts         
    841 
    842 KIN        	LDA   	#"?"
    843 		JSR   	syschout	; PRINT ONE ASCII CHR - ?
    844 		JSR   	syskin		; GET A KEYSTROKE FROM SYSTEM
    845             	AND   	#$4F            ; MASK 0-7, AND ALPHA'S
    846             	RTS
    847 ;
    848 ; 6551 I/O Support Routines
    849 ;
    850 ;
    851 Init_6551      lda   #$1F               ; 19.2K/8/1
    852                sta   ACIActl            ; control reg 
    853 .if VERSION_ERIS2010
    854                lda #$CB                 ; N parity/echo off/rx int off/ dtr ready
    855 .else
    856                lda   #$0B               ; N parity/echo off/rx int off/ dtr active low
    857 .endif
    858                sta   ACIAcmd            ; command reg 
    859 .if VERSION_ERIS2010
    860 	;; Terminal program on PC needs some time to start up
    861 	ldy #$ff
    862 	ldx #$ff
    863 io_setup_delay_delay:	
    864 	dex
    865 	bne io_setup_delay_delay
    866 	dey
    867 	bne io_setup_delay_delay
    868 .endif
    869                rts                      ; done
    870 ;
    871 ; input chr from ACIA1 (waiting)
    872 ;
    873 syskin         lda   ACIASta            ; Serial port status             
    874                and   #$08               ; is recvr full
    875                beq   syskin             ; no char to get
    876                Lda   ACIAdat            ; get chr
    877                RTS                      ;
    878 ;
    879 ; output to OutPut Port
    880 ;
    881 .if VERSION_ERIS2010
    882 CLOCK_SPEED = 4
    883 syschout:
    884 	.block
    885 	pha
    886 	phx
    887 	phy
    888 	;; Send character.
    889         ;; Due to bugs in the register and interrupt
    890 	;; handling of the WDC 65C02, we have to use
    891 	;; a manual delay.
    892         sta ACIADat
    893 .switch	CLOCK_SPEED
    894 .case  4			; 4 Mhz Clock
    895 	SERIAL_SEND_DELAY_X = $c3
    896 	SERIAL_SEND_DELAY_Y = $02
    897 .case 2				; 2 Mhz Clock
    898 	SERIAL_SEND_DELAY_X = $c2
    899 	SERIAL_SEND_DELAY_Y = $01
    900 .default			; 1 Mhz Clock
    901 	SERIAL_SEND_DELAY_X = $88
    902 	SERIAL_SEND_DELAY_Y = $01
    903 .endswitch
    904 	ldy #SERIAL_SEND_DELAY_Y
    905 outer_loop:	
    906 	ldx #SERIAL_SEND_DELAY_X
    907 inner_loop:
    908 	dex
    909         bne inner_loop
    910 	dey
    911 	bne outer_loop
    912 	ply
    913 	plx
    914 	pla
    915 	rts
    916 	.bend
    917 .else
    918 syschout       PHA                      ; save registers
    919 ACIA_Out1      lda   ACIASta            ; serial port status
    920                and   #$10               ; is tx buffer empty
    921                beq   ACIA_Out1          ; no
    922                PLA                      ; get chr
    923                sta   ACIAdat            ; put character to Port
    924                RTS                      ; done
    925 .endif
    926 
    927 syshexout      PHA                     ;  prints AA hex digits
    928                LSR                     ;  MOVE UPPER NIBBLE TO LOWER
    929                LSR                     ;
    930                LSR                     ;
    931                LSR                     ;
    932                JSR   PrintDig          ;
    933                PLA                     ;
    934 PrintDig       AND   #$0F              ;  prints A hex nibble (low 4 bits)
    935                PHY
    936                TAY                     ;
    937                LDA   Hexdigdata,Y      ;
    938                PLY
    939                jmp   syschout          ;
    940 
    941 .if VERSION_ERIS2010
    942 Hexdigdata	.text	"0123456789ABCDEF"
    943 banner
    944 .if VERSION_ERIS2010
    945 	.text x"1b" .. "[2J"	; Clear screen
    946 	.text x"1b" .. "[1;1H"	; Set cursor to top of screen
    947 .endif
    948 			.text	"MicroChess (c) 1996-2005 Peter Jennings, www.benlo.com"
    949 		.byte	$0d, $0a, $00
    950 cpl		.text	"WWWWWWWWWWWWWWWWBBBBBBBBBBBBBBBBWWWWWWWWWWWWWWWW"
    951 cph		.text	"KQRRBBNNPPPPPPPPKQRRBBNNPPPPPPPP"
    952 		.byte	$00
    953 ;
    954 ; end of added code
    955 ;
    956 ; BLOCK DATA
    957 SETW		.byte 	$03, $04, $00, $07, $02, $05, $01, $06
    958         	.byte 	$10, $17, $11, $16, $12, $15, $14, $13
    959         	.byte 	$73, $74, $70, $77, $72, $75, $71, $76
    960 	 	.byte	$60, $67, $61, $66, $62, $65, $64, $63
    961 
    962 MOVEX   	.byte 	$00, $F0, $FF, $01, $10, $11, $0F, $EF, $F1
    963 		.byte	$DF, $E1, $EE, $F2, $12, $0E, $1F, $21
    964 
    965 POINTS  	.byte 	$0B, $0A, $06, $06, $04, $04, $04, $04
    966 		.byte 	$02, $02, $02, $02, $02, $02, $02, $02
    967 
    968 OPNING  	.byte 	$99, $25, $0B, $25, $01, $00, $33, $25
    969 		.byte	$07, $36, $34, $0D, $34, $34, $0E, $52
    970         	.byte 	$25, $0D, $45, $35, $04, $55, $22, $06
    971 		.byte	$43, $33, $0F, $CC
    972 .else
    973 Hexdigdata	asc	"0123456789ABCDEF"
    974 banner		asc	"MicroChess (c) 1996-2005 Peter Jennings, www.benlo.com"
    975 		db	$0d, $0a, $00
    976 cpl		asc	"WWWWWWWWWWWWWWWWBBBBBBBBBBBBBBBBWWWWWWWWWWWWWWWW"
    977 cph		asc	"KQRRBBNNPPPPPPPPKQRRBBNNPPPPPPPP"
    978 		db	$00
    979 ;
    980 ; end of added code
    981 ;
    982 ; BLOCK DATA
    983 		*= $1580
    984 SETW		db 	$03, $04, $00, $07, $02, $05, $01, $06
    985         	db 	$10, $17, $11, $16, $12, $15, $14, $13
    986         	db 	$73, $74, $70, $77, $72, $75, $71, $76
    987 	 	db	$60, $67, $61, $66, $62, $65, $64, $63
    988 
    989 MOVEX   	db 	$00, $F0, $FF, $01, $10, $11, $0F, $EF, $F1
    990 		db	$DF, $E1, $EE, $F2, $12, $0E, $1F, $21
    991 
    992 POINTS  	db 	$0B, $0A, $06, $06, $04, $04, $04, $04
    993 		db 	$02, $02, $02, $02, $02, $02, $02, $02
    994 
    995 OPNING  	db 	$99, $25, $0B, $25, $01, $00, $33, $25
    996 		db	$07, $36, $34, $0D, $34, $34, $0E, $52
    997         	db 	$25, $0D, $45, $35, $04, $55, $22, $06
    998 		db	$43, $33, $0F, $CC
    999 .endif
   1000 	
   1001 ;
   1002 ;
   1003 ; end of file
   1004 ;