;/// CCLOCK.Z80 -- Chess Tournament Clock /////////////////////////////
;/// By Chris Hiszpanski, 01/02/1998 //////////////////////////////////
;/// Ion Port (10/07/2000) ////////////////////////////////////////////
	.nolist		
	#include "ion.inc"
	.list		
#ifdef TI83P		
	.org progstart-2
	.db $BB,$6D
TEXT_MEM  = $8508
#else		
	.org progstart
TEXT_MEM  = $80c9
#endif		
	ret
	jr nc,setup
	.db "Chess Clock v1.0",0

;/// VARIBLES /////////////////////////////////////////////////////////

inittime = TEXT_MEM
count = TEXT_MEM+1
m1one = TEXT_MEM+2
m1two = TEXT_MEM+3
m2one = TEXT_MEM+4
m2two = TEXT_MEM+5
s1one = TEXT_MEM+6
s1two = TEXT_MEM+8
s2one = TEXT_MEM+9
s2two = TEXT_MEM+11
temp  = TEXT_MEM+12

;/// SETUP ////////////////////////////////////////////////////////////

setup:
     bcall(_clrlcdf)			;
     bcall(_cleargbuf)			;
     bcall(_homeup)			;
     set textInverse,(iy+textflags)	;Reverse video
     ld hl,title			;Displays title
     bcall(_puts)		        ;
     res textInverse,(iy+textflags)	;
     ld hl,9*256+1			;Displays my name
     ld (pencol),hl			;
     ld hl,name				;
     bcall(_vputs)			;
     ld hl,17*256+1			;aka (Man in the Moon)
     ld (pencol),hl			;
     ld hl,aka				;
     bcall(_vputs)			;
     ld hl,35*256+1			;directions
     ld (pencol),hl			;
     ld hl,dir_one			;
     bcall(_vputs)			;
     ld hl,45*256+1			;
     ld (pencol),hl			;
     ld hl,dir_two			;
     bcall(_vputs)			;
     ld hl,55*256+35			;press mode
     ld (pencol),hl			;
     ld hl,press_mode			;
     bcall(_vputs)			;
     					;
     bcall(_getkey)			;pause (line 50)
     bcall(_clrlcdf)			;

;/// MAIN /////////////////////////////////////////////////////////////

main:
     bcall(_homeup)			;Display "How much time?"
     ld hl,settime			;
     bcall(_puts)			;
     ld a,2				;Display "Minutes:"
     ld (currow),a			;
     ld a,0				;
     ld (curcol),a			;
     ld hl,mstring			;
     bcall(_puts)			;
     ld a,5				;Load default time
     ld (inittime),a			;
     jp getkey				;

begin:
     call setclock			; Set the two timers
     call display			; Display the timers
     bcall(_getkey)			; Clocks ready. Press any key (except 2nd, Alpha, or ON)

run_one:
     ld a,(s2one)			; Decreament the seconds
     or a				;
     jr z,decd1				;
     dec a				;
     ld (s2one),a			;
     jp continued			;   
     
decd1:
     ld a,9				; If player ones second have come to a number like 10
     ld (s2one),a			;
     ld a,(s1one)			;
     or a				;
     jr z,decc1				;
     dec a				;
     ld (s1one),a			;
     jp continued			;

decc1:
     ld a,5				; If 0 seconds load 59 into seconds. Dec minute
     ld (s1one),a			;
     ld a,9				;
     ld (s2one),a			;
     ld a,(m2one)			;
     or a				;
     jr z,decb1				;
     dec a				;
     ld (m2one),a			;
     jp continued			;

decb1:
     ld a,9				; If player ones minutes have come to a number like 10
     ld (m2one),a			;
     ld a,(m1one)			;
     or a				;
     jp z,win_two			;
     dec a				;
     ld (m1one),a			;
     jp continued			;

win_two:
     xor a				; If no time left player 2 wins
     ld (m1one),a			;
     ld (m2one),a			;
     ld (s1one),a			;
     ld (s2one),a			;
     call display			;
     ld hl,10*256+15			;
     ld (pencol),hl			;
     ld hl,wontwo			;
     bcall(_vputs)			; Display Win
     bcall(_getkey)			;
     ret

continued:
     call delay				; Delay to actual time
     bcall(_getk)			; Check for end of turn and exit
     bcall(_op2toop1)			;
     bcall(_convop1)			;
     cp 11				;
     jr z,run_two			;
     cp 23				;
     ret z				;
     call display			; Display updated time
     jp run_one				; Repeat player one's counting

run_two:
     ld a,(s2two)			; Decreament the seconds
     or a				;
     jr z,decd2				;
     dec a				;
     ld (s2two),a			;
     jp continued2			;   
     
decd2:
     ld a,9				; If player ones second have come to a number like 10
     ld (s2two),a			;
     ld a,(s1two)			;
     or a				;
     jr z,decc2				;
     dec a				;
     ld (s1two),a			;
     jp continued2			;

decc2:
     ld a,5				; If 0 seconds load 59 into seconds. Dec minute
     ld (s1two),a			;
     ld a,9				;
     ld (s2two),a			;
     ld a,(m2two)			;
     or a				;
     jr z,decb2				;
     dec a				;
     ld (m2two),a			;
     jp continued2			;

decb2:
     ld a,9				; If player ones minutes have come to a number like 10
     ld (m2two),a			;
     ld a,(m1two)			;
     or a				;
     jr z,win_one			;
     dec a				;
     ld (m1two),a			;
     jp continued2			;

win_one:
     xor a				; If no time left player 2 wins
     ld (m1two),a			;
     ld (m2two),a			;
     ld (s1two),a			;
     ld (s2two),a			;
     call display			;
     ld hl,10*256+30			;
     ld (pencol),hl			;
     ld hl,wonone			;
     bcall(_vputs)			; Display Win
     bcall(_getkey)			;
     ret

continued2:
     call delay				; Delay to actual time
     bcall(_getk)			; Check for end of turn and exit
     bcall(_op2toop1)			;
     bcall(_convop1)			;
     cp 15				;
     jp z,run_one			;
     cp 23				;
     ret z
     call display			; Display updated time
     jp run_two				; Repeat player one's counting

;/// ROUTINES /////////////////////////////////////////////////////////

delay:					; Delay to actual time
     ld b,110				;
delay1:
     halt				;
     djnz delay1			;
     ret				;
     
getkey:
     ld a,(inittime)			;
     ld l,a				;
     ld a,2				; Display the new time
     ld (currow),a			;
     ld a,9				;
     ld (curcol),a			;
     ld h,0				;
     bcall(_dispHL)			;
     bcall(_getk)			;
     bcall(_op2toop1)			;
     bcall(_convop1)			;
     cp 25				;
     jp z,less				;
     cp 34				;
     jp z,more				;
     cp 23				;
     ret z 				; ...quit.
     cp 21				;
     jp z,begin				; If want this time, start clock
     jp getkey 				; Repeat.

more:
     ld a,(inittime)			; Increase the starting time 
     cp 60				; to no more than 60 minutes
     jp z,getkey			;
     inc a				;
     ld (inittime),a			;     
     jp getkey				;

less:
     ld a,(inittime)			;Decrease the starting time
     cp 1				;to no less than 1 minute
     jp z,getkey			;
     dec a				;
     ld (inittime),a			;
     jp getkey				;

setclock:
     xor a				;
     ld (count),a			;
     ld a,(inittime)			;Load the chosen time into  OP1 for test
     bcall(_setxxop1)			;       
tenloop:				;loop that divides by ten & checks for positive
     ld a,(count)			;count how many times subtracted ten
     inc a				;
     ld (count),a			;
     ld a,10				;set the subtraction factor
     bcall(_setxxop2)			;
     bcall(_fpsub)			;subtract 10 and check if still positive
     bcall(_ckop1pos)			;
     jr z,tenloop			; if still positive, repeat process

     ld b,10				; Now for actual time setting
     ld a,(count)			;
     dec a				; Off-by-one error
     ld (temp),a			;
tenloop2:
     ld a,(count)			; Count how many times to subtract
     dec a				;
     ld (count),a			;
     or a				;
     jr z,loadtime			;
     ld a,(inittime)			;
     sub b				; Subtract ten
     ld (inittime),a			;           
     jr nz,tenloop2			;

loadtime:
     ld a,(temp)			;
     ld (m1one),a			; Set the first digit of the minutes for both players
     ld (m1two),a			;
  
     ld a,(inittime)			; Set the second digit of the minutes for both players
     ld (m2one),a			;
     ld (m2two),a			;

     xor a				; Set both digits of the seconds for both players
     ld (s1one),a			;
     ld (s1two),a			;
     ld (s2one),a			;
     ld (s2two),a			;
     ret				; Time set. Return to main body

display:
     bcall(_cleargbuf)			; Clear the buffer

     call load_first			; The minute for player 1
     ld a,(m1one)			;
     call load_digit			; load the right digit
     ld a,5				; x-coordinate
     call vector0			;

     call load_first			;
     ld a,(m2one)			;
     call load_digit			;
     ld a,14				;
     call vector0			;

     call load_first			; The ":"
     ld a,22				;
     ld ix,semi				;
     call vector0			;

     call load_first			; The seconds for player 1
     ld a,(s1one)			;
     call load_digit			;
     ld a,26				;
     call vector0			;

     call load_first			;
     ld a,(s2one)			;
     call load_digit			;
     ld a,35				;
     call vector0			;

     call load_first			;The minute for player 2
     ld a,(m1two)			;
     call load_digit			;
     ld a,51				;
     call vector0			;

     call load_first			;
     ld a,(m2two)			;
     call load_digit			;
     ld a,60				;
     call vector0			;

     call load_first			;The ":"
     ld a,68				;
     ld ix,semi				;
     call vector0			;

     call load_first			;The second for player 2
     ld a,(s1two)			;
     call load_digit			;
     ld a,72				;
     call vector0			;	

     call load_first			;
     ld a,(s2two)			;
     call load_digit			;
     ld a,81				;
     call vector0			;

     bcall(_copygbuf)			;Copy everything to display
     ret				;		

load_first:
     ld b,8				;
     ld a,20				;y-coordinate
     ld l,a				;
     ret				;      

load_digit:
     cp 1     
     jp z,ld1
     cp 2
     jp z,ld2
     cp 3
     jp z,ld3
     cp 4
     jp z,ld4
     cp 5
     jp z,ld5
     cp 6
     jp z,ld6
     cp 7
     jp z,ld7
     cp 8
     jp z,ld8
     cp 9
     jp z,ld9
     cp 0
     jp z,ld0

ld1:
     ld ix,one
     ret
ld2:
     ld ix,two
     ret
ld3:
     ld ix,three
     ret
ld4:
     ld ix,four
     ret
ld5:
     ld ix,five
     ret
ld6:
     ld ix,six
     ret
ld7:
     ld ix,seven
     ret
ld8:
     ld ix,eight
     ret
ld9:
     ld ix,nine
     ret
ld0:
     ld ix,zero
     ret

;/// TEXT DATA ////////////////////////////////////////////////////////

title:      .db "Chess Clock v1.0",0
name:       .db "By Chris Hiszpanski",0
aka:        .db "aka: Man in the Moon",0
dir_one:    .db "Y= is player ones key",0
dir_two:    .db "GRAPH for player two",0
press_mode: .db "Press MODE",0
settime:    .db "How much time?",0
mstring:    .db "Minutes:",0
wontwo:     .db "Player two wins",0
wonone:     .db "Player one wins",0
vector0 = ionPutSprite

;/// SPRITE DATA //////////////////////////////////////////////////////

zero:
     .db %00000000
     .db %01111110
     .db %10000001
     .db %10000001
     .db %10000001
     .db %10000001
     .db %10000001
     .db %01111110

one:
     .db %00000000
     .db %00000010
     .db %00000110
     .db %00000010
     .db %00000010
     .db %00000010
     .db %00000010
     .db %00000010

two:
     .db %00000000
     .db %11111110
     .db %00000001
     .db %00000001
     .db %01111110
     .db %10000000
     .db %10000000
     .db %01111111

three:
     .db %00000000
     .db %11111110
     .db %00000001
     .db %00000001
     .db %00011110
     .db %00000001
     .db %00000001
     .db %11111110

four:
     .db %10000001
     .db %10000001
     .db %10000001
     .db %10000001
     .db %01111110
     .db %00000001
     .db %00000001
     .db %00000001

five:
     .db %00000000
     .db %01111111
     .db %10000000
     .db %10000000
     .db %01111110
     .db %00000001
     .db %00000001
     .db %11111110

six:
     .db %00000000
     .db %01111111
     .db %10000000
     .db %10000000
     .db %01111110
     .db %10000001
     .db %10000001
     .db %01111110

seven:
     .db %00000000
     .db %11111110
     .db %00000100
     .db %00001000
     .db %00010000
     .db %00100000
     .db %01000000
     .db %10000000

eight:
     .db %00000000
     .db %01111110
     .db %10000001
     .db %10000001
     .db %01111110
     .db %10000001
     .db %10000001
     .db %01111110

nine:
     .db %00000000
     .db %01111110
     .db %10000001
     .db %10000001
     .db %01111110
     .db %00000001
     .db %00000001
     .db %11111110

semi:
     .db %00000000
     .db %00000000
     .db %01100000
     .db %01100000
     .db %00000000
     .db %01100000
     .db %01100000
     .db %00000000

;/// EXIT /////////////////////////////////////////////////////////////

.end
END 
