;/* Title	: Butterfly LCD display as PANEL */
;/* Author	: Jan Skalicky, 2005 */


.include	"m169def.inc"
.include	"s_macro.inc"

.dseg
.org 0x100
msg_ram:	.byte	64		/* message buffer */
msg_pos:	.byte	1		/* position in message */
msg_len:	.byte	1		/* message length (counted) */

/* reset interrupt vector */
.cseg
.org 0x0000
	jmp		init

/* hw (2 joystick buttons) interrupt vector */
.cseg 
.org 0x0006
	reti

/* timer/counter 2 interrupt vector */
.cseg
.org 0x000A
	jmp		int_tim2
	
.cseg
.include	"init.asm"
.include	"muldiv.asm"
.include	"display.asm"
.include	"string.asm"

.cseg
msg_rom:
.db			"      TENTO VLAK KONCI JIZDU VE STANICI SMICHOVSKE NADRAZI", 0, 0


/* stack init */
init:
	ldi		r20, high(RAMEND)
	out		sph, r20
	ldi		r20, low(RAMEND)
	out		spl, r20
/* start */
start:
	call	init_disp
	call	init_joy
/* initialization of used variables */
	ldi		XL, low (msg_ram)
	ldi		XH, high(msg_ram)
	ldi		ZL, low (msg_rom<<1)
	ldi		ZH, high(msg_rom<<1)
	call	strcpy
	call	strlen
	sts		msg_len, r2
	clr		r0
	sts		msg_pos, r0

	call	init_tim2
	
joy_refresh:
/* wait for some key pressed */
	lds		r6, PINE
	lds		r7, PINB
	and		r6, r7
	inc		r6
	breq	joy_refresh

/* CASE: when key is pressed, perform appropriate action */
joy_tstRight:
	sbic	PINE, 3
	rjmp	joy_tstLeft
	ldi		XL, low (msg_ram)
	ldi		XH, high(msg_ram)
	lds		r0, msg_pos
	dec		r0
	ser		r20
	cpse	r0, r20
	rjmp	PC+5
	lds		r1, msg_len
	mov		r0, r1
	dec		r0
	sts		msg_pos, r0
	rcall	print_msg
joy_tstLeft:
	sbic	PINE, 2
	rjmp	joy_tstDown
	ldi		XL, low (msg_ram)
	ldi		XH, high(msg_ram)
	lds		r0, msg_pos
	inc		r0
	lds		r1, msg_len
	cp		r0, r1
	brlo	PC+2
	clr		r0
	sts		msg_pos, r0
	rcall	print_msg
joy_tstDown:
	sbic	PINB, 7
	rjmp	joy_tstUp
	lds		r15, TIMSK2
	ldi		r16, 0b11111110
	and		r16, r15
	sts		TIMSK2, r16
joy_tstUp:
	sbic	PINB, 6
	rjmp	joy_tstEnter
	lds		r15, TIMSK2
	ldi		r16, 0b00000001
	or		r16, r15
	sts		TIMSK2, r16
joy_tstEnter:
	sbic	PINB, 4
	rjmp	joy_tstEnd
	clr		r0
	sts		msg_pos, r0
	rcall	print_msg
joy_tstEnd:
/* wait some time before processing another keystroke (repeat-rate, noise prevention) */
	ldi		r24, 2
	mov		r3, r24
	rcall	wait64k
	
	rjmp	joy_refresh

/* legacy purposes */
looop:
	rjmp	PC

/* timer/counter 2 initialization */
int_tim2:
	push	r0
	lds		r0, SREG
	push	r0
	push	XL
	push	XH

	rcall	print_msg
	ldi		XL, low (msg_ram)
	ldi		XH, high(msg_ram)
	lds		r0, msg_pos
	inc		r0
	lds		r1, msg_len
	cp		r0, r1
	brlo	PC+2
	clr		r0
	sts		msg_pos, r0

	pop		XH
	pop		XL
	pop		r0
	sts		SREG, r0
	pop		r0	
	reti

/* print message, starting with actual position */
print_msg:
	push	r17
	push	r20
	push	XL
	push	XH

	ldi		XL, low (msg_ram)
	ldi		XH, high(msg_ram)
	lds		r0, msg_pos
	ADD16	XL, XH, r0
	call	disp_clear
	ldi		r17, 2	
	ser		r20
	call	printStr

	pop		XH
	pop		XL
	pop		r20
	pop		r17
	ret

/* wait for r3 * 64kInstructions */
wait64k:
	push	r1
	push	r2
	
	tst		r3
	breq	wait64k_bypass
	clr		r1
	clr		r2
	dec		r1
	brne	PC-1
	dec		r2
	brne	PC-3
	dec		r3
	brne	PC-5
wait64k_bypass:
	pop		r2
	pop		r1
	ret