;/* Title	: Butterfly Joystick & LCD display experiments */
;/* Author	: Jan Skalicky, 2005 */


.include	"m169def.inc"

.dseg
.org 0x100
sekundy:	.byte	1
minuty:		.byte	1

.cseg
.org 0x0000
	jmp		init

.cseg
.org 0x000A
	jmp		int_tim2
	
.cseg
asctable:
.include	"znaky.inc"
.include	"display.asm"
.include	"muldiv.asm"

/* stack init */
init:
	ldi		r20, high(RAMEND)
	out		sph, r20
	ldi		r20, low (RAMEND)
	out		spl, r20

/* start */
	call	init_disp
	call	init_joy
	clr		r0
	sts		sekundy, r0
	sts		minuty, r0
	call	init_tim2

joy_refresh:
	ldi		r17, 2
	ldi		r16, 'R'
	sbis	PINE, 3
	call	printChar
	ldi		r16, 'L'
	sbis	PINE, 2
	call	printChar
	ldi		r16, 'D'
	sbis	PINB, 7
	call	printChar
	ldi		r16, 'U'
	sbis	PINB, 6
	call	printChar
	ldi		r16, 'X'
	sbis	PINB, 4
	call	printChar
	rjmp	joy_refresh

looop:
	rjmp	PC

init_tim2:
	push	r16

	cli
	ldi		r16, 0b00001000
	sts		ASSR, r16
	ldi		r16, 0b00000001
	sts		TIMSK2, r16
	ldi		r16, 0b00000101
	sts		TCCR2A, r16
	sei

	pop		r16
	ret

init_joy:
	push	r16
	push	r17

	in		r17, DDRE
	andi	r17, 0b11110011
	in		r16, PORTE
	ori		r16, 0b00001100
	out		DDRE, r17
	out		PORTE, r16
	ldi		r16, 0b00000000
	sts		DIDR1, r16
	in		r17, DDRB
	andi	r17, 0b00101111
	in		r16, PORTB
	ori		r16, 0b11010000
	out		DDRB, r17
	out		PORTB, r16

	pop		r17
	pop		r16
	ret

int_tim2:
	push	r16
	push	r17
	push	r19
	push	r20
	lds		r16, SREG
	push	r16

	lds		r16, sekundy
	inc		r16
	cpi		r16, 60
	brne	int_tim2_nomin

	lds		r16, minuty
	inc		r16
	cpi		r16, 10
	brne	PC+2
	clr		r16
	sts		minuty, r16
	clr		r16

int_tim2_nomin:
	sts		sekundy, r16
	ldi		r19, 6
	ldi		r20, 2
	call	printDec
	sbrc	r16, 0
	rjmp	PC+3
	ldi		r16, '-'
	rjmp	PC+2
	ldi		r16, '+'
	ldi		r17, 4
	call	printChar

	lds		r16, minuty
	ldi		r19, 3
	ldi		r20, 1
	call	printDec	

	pop		r16
	sts		SREG, r16
	pop		r20	
	pop		r19
	pop		r17
	pop		r16
	reti