;********************************************
;*E_dice									*
;*Vicsys									*
;*2007										*
;*											*
;********************************************


	list	P=PIC12F629
	include  "P12F629.INC"

	__CONFIG _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

beept	equ	20h			
time1	equ	21h			
time2	equ	22h			
time3	equ	23h			
count1	equ	24h			
count2	equ	25h			
count3	equ	26h			
count4	equ	27h			
saime	equ	28h			
scnt	equ	29h			


	org	0
	goto	intset

intset
	bsf		STATUS, RP0	
	call	3ffh		
	movwf	OSCCAL		
	movlw	b'00001000'
	movwf	TRISIO
	movlw	b'00000000'
	movwf	OPTION_REG
	movlw	b'00001000'	
	movwf	IOC
	bcf		STATUS, RP0	;
	clrf	ANSEL
	movlw	b'00000111'
	movwf	CMCON		
	movlw	b'00001000'	;
	movwf	INTCON		;

	movlw	0x06
	movwf	saime

start
	call	ondisp		;
	call	t100ms		;
	clrf	GPIO		;
	call	t10ms		;
	call	ondisp		;
	call	beep		;
	call	t10ms
	clrf	GPIO
	call	t10ms
	call	ondisp
	call	beep
	call	t10ms
	clrf	GPIO
	call	t10ms
	call	ondisp
	call	beep

game					
	btfss	GPIO,3
	goto	game
	call	t50ms

game1					
	movlw	0x1e		
	movwf	count2
game1a
	movlw	0x64		;
	movwf	count3
game1b
	movlw	0x9b		
	movwf	count4
game1c
	movlw	0x0c		
	movwf	count1		;
game1d
	btfss	GPIO,3
	goto	game2		
	decfsz	count1,f
	goto	game1d

	decfsz	count4,f
	goto	game1c
	decfsz	count3,f
	goto	game1b
	decfsz	count2,f
	goto	game1a
	call	beep		;
	call	t20ms
	call	beep
	clrf	GPIO		;
	bcf		INTCON,GPIF	;
	sleep
	nop					
	goto	start		;

game2					
	movlw	0x06		
	addwf	count1,f
game2a
	movf	count1,w	
	movwf	scnt

game3					
	call	disp
	call	t5ms
	btfsc	GPIO,3		
	goto	game4
	goto	game3

game4					
	call	disp
	call	t50ms
	btfss	GPIO,3		;
	goto	game2a
	decfsz	scnt		
	goto	game4

	call	t300ms
	call	beep5		
	goto	game

beep
	movlw	0x30
	movwf	beept
beep1
	bsf		GPIO,5
	call	t50us
	bcf		GPIO,5
	call	t50us
	decfsz	beept,f
	goto	beep1
	return

t50us
	movlw	0x30
	movwf	time1
t50us1
	decfsz	time1,f
	goto	t50us1
	return

t1ms
	movlw	0x08
	movwf	time2
t1ms1
	movlw	0x7d
	movwf	time3
t1ms2
	decfsz	time3,f
	goto	t1ms2			
	decfsz	time2,f
	goto	t1ms1
	return

t5ms
	call	t1ms
	call	t1ms
	call	t1ms
	call	t1ms
	call	t1ms
	return

t10ms
	call	t5ms
	call	t5ms
	return

t20ms
	call	t10ms
	call	t10ms
	return

t50ms
	call	t10ms
	call	t20ms
	call	t20ms
	return

t100ms
	call	t50ms
	call	t50ms
	return

t300ms
	call	t100ms
	call	t100ms
	call	t100ms
	return

disp
	movf	saime,w			;
	call	digit
	movwf	GPIO			;
	call	beep			
	decfsz	saime,f			;
	return
	movlw	0x06			;
	movwf	saime
	return

digit
	addwf	PCL,f
	retlw	b'00011111'		
	retlw	b'00001111'		;
	retlw	b'00011101'		;
	retlw	b'00001101'		;
	retlw	b'00011100'		;
	retlw	b'00001100'		;
	retlw	b'00011000'		;

ondisp
	movlw	b'00010111'		;
	movwf	GPIO
	return

beep5
	call	beep
	call	t20ms
	call	beep
	call	t20ms
	call	beep
	call	t20ms
	call	beep
	call	t20ms
	call	beep
	return


	org		0x3ff		
	retlw	0x38		

	end
