        .nolist

        #include "ion.inc"		;tells TASM--the compiler--what file to read from to define rom call memory addresses and such
        #include "keys.inc"		; includes file with key equates
        .list
counter = saferam3
#ifdef TI83P			;a check for TASM to see whether it is making an 83 program--if so, do the next two commands
        .org    progstart-2
        .db     $BB,$6D
#else				;if it isn't an 83 program, then do something [#]else--the next line
        .org    progstart
#endif				;simply ends the #ifdef command
        ret				

        jr      nc,begin 		;Jumps to the beginning of the program (this line and the below three will be 
        .db     "Dice Roller by Cole South",0		;The title displayed by ION--anything you want
begin: 				;defines where label begin is, program code follows this label
	ld a,1
	ld (counter),a
	call clear
	ld	b,20				;Numbers of Rows in Sprite
	ld	a,2				;X coordinate
	ld	l,2				;Y coordinate
	ld	c,2
	ld	ix,lastdice			; These three lines display 
	call	IonLargeSprite	      	
	ld	b,20				;Numbers of Rows in Sprite
	ld	a,78				;X coordinate
	ld	l,2				;Y coordinate
	ld	c,2
	ld	ix,lastdice			; These three lines display 
	call	IonLargeSprite	
	call	IonFastCopy	
	ld	a,23
	ld	(PENCOL),a	; x coordinate			
	ld	a,20
	ld	(PENROW),a	; y coordinate
	ld	hl,diceroller
	bcall(_vputs)          ; notice, this is vputs, not puts
	ld	a,29
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,bycole
	bcall(_vputs)          ; notice, this is vputs, not puts
	ld	a,18
	ld	(PENCOL),a	; x coordinate			
	ld	a,47
	ld	(PENROW),a	; y coordinate
	ld	hl,onedice
	bcall(_vputs)          ; notice, this is vputs, not puts
	ld	a,18
	ld	(PENCOL),a	; x coordinate			
	ld	a,56
	ld	(PENROW),a	; y coordinate
	ld	hl,twodice
	bcall(_vputs)          ; notice, this is vputs, not puts
GkLoop:
	bcall(_getkey)
	cp Gk1
	jr z,rolldice
	cp Gk2
	jp z,firstdiceroll
	cp GkClear
	ret z
	jp GkLoop


CoolDisplay:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,spdice1			; These three lines display 
	call IonLargeSprite	      	
	call IonFastCopy
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,spdice2			; These three lines display 
	call IonLargeSprite	      	
	call IonFastCopy				;ROUTINE BY JOE PEMBERTON(GRAPH INVERSE)
gInverse:
	ld	bc,768		;the graph buffer is 768 bytes
	ld	hl,plotsscreen	;load the address for the graph buffer into hl
gInverseLoop:
	ld	a,(hl)		;load what is at (hl) (the graph buffer) into a
	cpl			;find the inverse of a
	ld	(hl),a		;load a back into (hl)
	inc	hl		;increment the current address in hl
	dec	bc		;decrement the counter
	ld	a,c		;load the lsb of the counter into a
	or	b		;or a with the msb of the counter
	jr	nz,gInverseLoop	;if a doesn't equal 0, goto gInverseLoop
	call IonFastcopy
	ld a,(counter)
	inc a
	cp 15
	ret z
	ld (counter),a
	jp cooldisplay
	ret

rolldice:
	call CoolDisplay
	ld b,6					;set the upper bound
	call ionRandom				;call the library
	cp 0					;since the result is already put in register a...
	jp z,dice1				;jump to that label if it is 0
	cp 1					;check if it was one
	jp z,dice2				;jump to that label if so
	cp 2					;check if it was two
	jp z,dice3				;jump to that label if so
	cp 3					;since the result is already put in register a...
	jp z,dice4				;jump to that label if it is 0
	cp 4					;check if it was one
	jp z,dice5				;jump to that label if so
	cp 5					;check if it was two
	jp z,dice6				;jump to that label if so

dice1:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,48
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,onerolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin
dice2:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,48
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,tworolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin
dice3:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,48
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,threerolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin
dice4:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,48
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,fourrolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin
dice5:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,48
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,fiverolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin
dice6:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,40				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,48
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,sixrolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin
firstdiceroll:
	call CoolDisplay
	ld b,6					;set the upper bound
	call ionRandom				;call the library
	cp 0					;since the result is already put in register a...
	jp z,firstdice1				;jump to that label if it is 0
	cp 1					;check if it was one
	jp z,firstdice2				;jump to that label if so
	cp 2					;check if it was two
	jp z,firstdice3				;jump to that label if so
	cp 3					;since the result is already put in register a...
	jp z,firstdice4				;jump to that label if it is 0
	cp 4					;check if it was one
	jp z,firstdice5				;jump to that label if so
	cp 5					;check if it was two
	jp z,firstdice6				;jump to that label if so

firstdice1:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,30				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	ld b,20				;Numbers of Rows in Sprite
	ld a,55				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,38
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,onerolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	jp seconddiceroll

firstdice2:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,30				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	ld b,20				;Numbers of Rows in Sprite
	ld a,55				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,38
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,tworolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	jp seconddiceroll
firstdice3:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,30				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	ld b,20				;Numbers of Rows in Sprite
	ld a,55				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,38
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,threerolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	jp seconddiceroll

firstdice4:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,30				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	ld b,20				;Numbers of Rows in Sprite
	ld a,55				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,38
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,fourrolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	jp seconddiceroll

firstdice5:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,30				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	ld b,20				;Numbers of Rows in Sprite
	ld a,55				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,38
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,fiverolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	jp seconddiceroll

firstdice6:
	call clear
	ld b,20				;Numbers of Rows in Sprite
	ld a,30				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	ld b,20				;Numbers of Rows in Sprite
	ld a,55				;X coordinate
	ld l,30				;Y coordinate
	ld c,2
	ld ix,lastdice			; These three lines display 
	call IonLargeSprite
	call IonFastCopy
	ld	a,38
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,sixrolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	jp seconddiceroll

seconddiceroll:
	ld b,6					;set the upper bound
	call ionRandom				;call the library
	cp 0					;since the result is already put in register a...
	jp z,seconddice1				;jump to that label if it is 0
	cp 1					;check if it was one
	jp z,seconddice2				;jump to that label if so
	cp 2					;check if it was two
	jp z,seconddice3				;jump to that label if so
	cp 3					;since the result is already put in register a...
	jp z,seconddice4				;jump to that label if it is 0
	cp 4					;check if it was one
	jp z,seconddice5				;jump to that label if so
	cp 5					;check if it was two
	jp z,seconddice6				;jump to that label if so

seconddice1:
	ld	a,63
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,onerolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin

seconddice2:
	ld	a,63
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,tworolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin

seconddice3:
	ld	a,63
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,threerolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	jp begin

seconddice4:
	ld	a,63
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,fourrolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	bcall(_getkey)
	jp begin

seconddice5:
	ld	a,63
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,fiverolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	bcall(_getkey)
	jp begin

seconddice6:
	ld	a,63
	ld	(PENCOL),a	; x coordinate			
	ld	a,35
	ld	(PENROW),a	; y coordinate
	ld	hl,sixrolled
	bcall(_vputs)          ; notice, this is vputs, not puts
	bcall(_getkey)
	bcall(_getkey)
	jp begin
clear:
	bcall(_clrlcdf)
	bcall(_cleargbuf)
	ret


onedice:
	.db "Press 1 for One Dice",0

twodice:
	.db "Press 2 for Two Dice",0

diceroller:
	.db "Dice Roller v1.0",0

bycole:
	.db "By Cole South",0

onerolled:			;these all tell what to display when a certain number is rolled
	.db "1",0

tworolled:
	.db "2",0

threerolled:
	.db "3",0

fourrolled:
	.db "4",0

fiverolled:
	.db "5",0

sixrolled:
	.db "6",0

spdice1:
	.db %11100000,%00000000			;dice one and dice two are alternated repeatedly while
	.db %00010000,%00000000			;the screen is constantly inverted, producing a cool
	.db %00001000,%01000000			;effect for a second or two until the result of the dice
	.db %00001000,%10100000			;roll is shown
	.db %00010001,%00010000
	.db %00100010,%00001000
	.db %01000100,%00000100
	.db %01001000,%00000010
	.db %01010000,%00000001
	.db %01011000,%00000011
	.db %00010100,%00000111
	.db %01010010,%00001111
	.db %00010001,%00011111
	.db %00010000,%10111111
	.db %00010000,%01111110
	.db %00010000,%01111100
	.db %00001000,%01111000
	.db %00000100,%01110000
	.db %00000010,%01100000
	.db %00000001,%11000000

spdice2:
	.db %11100000,%00000000
	.db %00010000,%00000000
	.db %00001000,%01000000
	.db %00001000,%10100000
	.db %00010001,%00010000
	.db %00100010,%00001000
	.db %01000100,%00000100
	.db %01001000,%00000010
	.db %01010000,%00000001
	.db %01011000,%00000011
	.db %00011100,%00000101
	.db %01011110,%00001001
	.db %00011111,%00010001
	.db %00011111,%10100001
	.db %00011111,%11000010
	.db %00011111,%11000100
	.db %00001111,%11001000
	.db %00000111,%11010000
	.db %00000011,%11100000
	.db %00000001,%11000000

lastdice:
	.db %00000000,%00000000			;this last dice is where the outcome is displayed
	.db %00000000,%00000000
	.db %00000000,%01000000
	.db %00000000,%10100000
	.db %00000001,%00010000
	.db %00000010,%00001000
	.db %00000100,%00000100
	.db %00001000,%00000010
	.db %00010000,%00000001
	.db %00011000,%00000011
	.db %00010100,%00000101
	.db %00010010,%00001001
	.db %00010001,%00010001
	.db %00010000,%11100001
	.db %00010000,%11000010
	.db %00010000,%11000100
	.db %00001000,%11001000
	.db %00000100,%11010000
	.db %00000010,%11100000
	.db %00000001,%11000000

.end
END