;+---------------------------------------------------------------------------------+
;|       This is the game Tic-Tac-Toe 3x3. It is a two player game and             |
;|         the goal is to make "three in a row". To play it you need               |
;|      3 x (STK500 + ATmega8515). Every kit is a row of the 3x3 matrix.           |
;|   You have to make a special cabel to connect the three kits in a circle.       |
;|    RS232:TXD1->RXD2; TXD2->RXD3; TXD3->RXD1; GROUND1->GROUND2->GROUND3.         |
;|         You also need to connect LEDS to PORTB, Switches to PORTA,              |
;| RS232 Spare: RXD->PD0; TXD->PD1 and, of course, to program the target devices:) |
;|           Game controls: Buttons 7,5,3; Button 0 - restart;                     |
;|     LEDS 1 and 0 show the ID of each kit (so you can put them in order).        |
;|                                   Enjoy it!                                     |
;|                                                                                 |
;|                   Made by Ivaylo Belev   kink_alias@yahoo.com                   |
;+---------------------------------------------------------------------------------+

.include "8515def.inc"

.equ UCSRC = 0x20

.undef XL
.undef XH
.undef YL

.def tmp = r16 			;temporary registers
.def tmp2 = r17	
.def tmp3 = r28

.def cnt = r18 			;move counter
.def id = r19			;id number

.def u1 = r20 			;upper layer mask 1
.def l1 = r21 			;lower layer mask 2
.def u2 = r22 			
.def l2 = r23			
.def u3 = r24 
.def l3 = r25 

.def lul = r26			;local upper layer mask
.def lll = r27			;local lower layer mask

;--------------------------------------------------------------------


;**** Initialization

		ser		tmp
		out		DDRB,tmp					;set PORTB to output
		
		;USART initialization

		ldi		tmp,23
		out		UBRR,tmp					;set Baud Rate Register=23 (9600 bps)


		ldi 	tmp, (1<<RXEN)|(1<<TXEN) 	;enable receiver and transmitter
		out 	UCR,tmp


		ldi 	tmp, 0b10000110				; Set frame format: 8 data, 1 stop bit
		out 	UCSRC,tmp

;--------------------------------------------------------------------


;**** Connection establishing

		ldi		tmp,1			;send "online" signal
		out		UDR,tmp

Synch:							;wait all hosts to come online		
		sbis 	USR,RXC
		rjmp 	Synch

		in 		tmp,UDR
			
		cpi 	tmp,4
		brsh 	case45			;transmit increasing number
								;reaching 3, 4 or 5 = all are connected
		mov 	id,tmp			;and each one enters the game
		inc 	tmp
		out 	UDR,tmp
		
		cpi 	tmp,4
		breq 	INIT
		rjmp 	Synch

	
case45:	cpi 	tmp,4
		brne 	INIT

		inc 	tmp
		out 	UDR,tmp
	
;--------------------------------------------------------------------


;**** Loading initial state

INIT:	clr		u1
		clr		l1
		clr		u2
		clr		l2
		clr		u3
		clr		l3

		ldi		cnt,9
		mov		lul,id			;load initial values
		com		lul
		mov 	lll,lul
				
;--------------------------------------------------------------------


;**** Game start

MAIN:	
		out		PORTB,lul		;update LEDs layer1
									
		out		PORTB,lll		;update LEDs layer2
			
		ldi		tmp,7
	mdly:						;make some LEDs to look darker
		dec		tmp
		brne 	mdly

;*** Test input

		sbis	PINA,3			;if button3 is pressed
		rjmp 	Check3			;check if move is legal
		
		sbis	PINA,5
		rjmp 	Check5
		
		sbis	PINA,7		
		rjmp 	Check7

		sbis	PINA,0			;if button0 is pressed
		rjmp	Reset			;game restart

		sbic 	USR,RXC			;check for received data
		rjmp 	Dcode
		
		cpi		cnt,0			;check for a game end with no win
		brne	MAIN
		rjmp	Stuck
	
Reset: 	out 	UDR,id			;send reset signal
		rjmp 	INIT

;--------------------------------------------------------------------
;--------------------------------------------------------------------


Check3: cpi		id,1
		breq	id10
		rjmp	id20

;*** Host 1, pressed 3

id10:	sbrc	u1,0			;check if empty
		rjmp 	MAIN

		sbr		u1,1
		cbr		lul,8

		bst		cnt,0
		bld		l1,0
		bld		lll,3

		cpi		cnt,6
		brsh	Tx10
		
;*** Check for win

	;horizontal

		cpi 	u1,21
		brne	V10

		cpi		l1,21
		brne	h10
		rjmp	w10h
		
h10:	cpi		l1,0
		brne	V10
		
w10h:	mov		tmp,id			;send win signal
		ori		tmp,0b00111100
		out		UDR,tmp
		ldi		cnt,1			;load code of win
		rjmp	Win

	;vertical	

V10:	sbrs	u2,0
		rjmp	D10

		sbrs	u3,0
		rjmp	D10
		
		sbrc	cnt,0			;check last move's color (i.e if counter odd or even)
		rjmp	co10v

		sbrc	l2,0			;counter even
		rjmp	D10

		sbrc	l3,0
		rjmp	D10
		
		rjmp 	w10v

co10v:	sbrs	l2,0			;counter odd
		rjmp	D10

		sbrs	l3,0
		rjmp	D10

w10v:	mov		tmp,id			;send win signal
		ori		tmp,0b10011100
		out		UDR,tmp
		ldi		cnt,4			;load code of win
		rjmp 	Win
		
	;diagonal

D10:	sbrs	u2,2
		rjmp	Tx10

		sbrs	u3,4
		rjmp	Tx10
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co10d

		sbrc	l2,2		;counter even
		rjmp	Tx10

		sbrc	l3,4
		rjmp	Tx10
		
		rjmp 	w10d

co10d:	sbrs	l2,2		;counter odd
		rjmp	Tx10

		sbrs	l3,4
		rjmp	Tx10
		
w10d:	mov		tmp,id			;send win signal
		ori		tmp,0b11111100
		out		UDR,tmp
		ldi		cnt,7			;load code of win
		rjmp 	Win	


;*** Transmit
		
Tx10:	dec 	cnt

		ldi		tmp,5					;001|01 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN
			
;--------------------------------------------------------------------

id20:	cpi		id,2
		brne	id30


;*** Host 2, pressed 3

		sbrc	u2,0				;check if empty
		rjmp 	MAIN

		sbr		u2,1
		cbr		lul,8

		bst		cnt,0
		bld		l2,0
		bld		lll,3

		cpi		cnt,6
		brsh	Tx20
		
;*** Check for win

	;horizontal

		cpi 	u2,21
		brne	V20

		cpi		l2,21
		brne	h20
		rjmp	w20h
		
h20:	cpi		l2,0
		brne	V20

w20h:	mov		tmp,id			;send win signal
		ori		tmp,0b01011100
		out		UDR,tmp
		ldi		cnt,2			;load code of win		
		rjmp	Win

	;vertical	

V20:	sbrs	u1,0
		rjmp	Tx20

		sbrs	u3,0
		rjmp	Tx20
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co20v

		sbrc	l1,0		;counter even
		rjmp	Tx20

		sbrc	l3,0
		rjmp	Tx20
		
		rjmp 	w10v

co20v:	sbrs	l1,0		;counter odd
		rjmp	Tx20

		sbrs	l3,0
		rjmp	Tx20
	
		rjmp 	w10v	


;*** Transmit
		
Tx20:	dec 	cnt

		ldi		tmp,6			;001|10 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN

;--------------------------------------------------------------------


;*** Host 3, pressed 3

id30:	sbrc	u3,0			;check if empty
		rjmp 	MAIN

		sbr		u3,1
		cbr		lul,8

		bst		cnt,0
		bld		l3,0
		bld		lll,3

		cpi		cnt,6
		brsh	Tx30
		
;*** Check for win

	;horizontal

		cpi 	u3,21
		brne	V30

		cpi		l3,21
		brne	h30
		rjmp	w30h
		
h30:	cpi		l3,0
		brne	V30

w30h:	mov		tmp,id			;send win signal
		ori		tmp,0b01111100
		out		UDR,tmp
		ldi		cnt,3			;load code of win
		rjmp	Win

	;vertical	

V30:	sbrs	u1,0
		rjmp	D30

		sbrs	u2,0
		rjmp	D30
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co30v

		sbrc	l1,0		;counter even
		rjmp	D30

		sbrc	l2,0
		rjmp	D30
		
		rjmp 	w10v

co30v:	sbrs	l1,0		;counter odd
		rjmp	D30

		sbrs	l2,0
		rjmp	D30
		
		rjmp 	w10v	

	;diagonal

D30:	sbrs	u2,2
		rjmp	Tx30

		sbrs	u1,4
		rjmp	Tx30
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co30d

		sbrc	l2,2		;counter even
		rjmp	Tx30

		sbrc	l1,4
		rjmp	Tx30
		
		rjmp 	w14d

co30d:	sbrs	l2,2		;counter odd
		rjmp	Tx30

		sbrs	l1,4
		rjmp	Tx30

		rjmp 	w14d	


;*** Transmit
		
Tx30:	dec 	cnt

		ldi		tmp,7					;001|11 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN
		
;--------------------------------------------------------------------
;--------------------------------------------------------------------


Check5:	cpi		id,1
		brne	id22
		

;*** Host 1, pressed 5

		sbrc	u1,2			;check if empty
		rjmp 	MAIN

		sbr		u1,4
		cbr		lul,32

		bst		cnt,0
		bld		l1,2
		bld		lll,5

		cpi		cnt,6
		brsh	Tx12
		
;*** Check for win

	;horizontal

		cpi 	u1,21
		brne	V12

		cpi		l1,21
		brne	h12
		rjmp	w10h
		
h12:	cpi		l1,0
		brne	V12

		rjmp	w10h

	;vertical	

V12:	sbrs	u2,2
		rjmp	Tx12

		sbrs	u3,2
		rjmp	Tx12
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co12v

		sbrc	l2,2		;counter even
		rjmp	Tx12

		sbrc	l3,2
		rjmp	Tx12
		
		rjmp 	w12v

co12v:	sbrs	l2,2		;counter odd
		rjmp	Tx12

		sbrs	l3,2
		rjmp	Tx12
		
w12v:	mov		tmp,id			;send win signal
		ori		tmp,0b10111100
		out		UDR,tmp
		ldi		cnt,5			;load code of win
		rjmp 	Win	


;*** Transmit
		
Tx12:	dec 	cnt

		ldi		tmp,9			;010|01 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN
			
;--------------------------------------------------------------------


id22:	cpi		id,2
		breq	id222
		rjmp	id32

;*** Host 2, pressed 5

id222:	sbrc	u2,2				;check if empty
		rjmp 	MAIN

		sbr		u2,4
		cbr		lul,32

		bst		cnt,0
		bld		l2,2
		bld		lll,5

		cpi		cnt,6
		brlo	cfw
		rjmp	Tx22
		
;*** Check for win

	;horizontal

cfw:	cpi 	u2,21
		brne	V22

		cpi		l2,21
		brne	h22
		rjmp	w20h
		
h22:	cpi		l2,0
		brne	V22

		rjmp	w20h

	;vertical	

V22:	sbrs	u1,2
		rjmp	D22m

		sbrs	u3,2
		rjmp	D22m
		
		sbrc	cnt,0			;check last move's color (i.e if counter odd or even)
		rjmp	co22v

		sbrc	l1,2			;counter even
		rjmp	D22m

		sbrc	l3,2
		rjmp	D22m
		
		rjmp 	w12v

co22v:	sbrs	l1,2			;counter odd
		rjmp	D22m

		sbrs	l3,2
		rjmp	D22m

		rjmp 	w12v	


	;main diagonal

D22m:	sbrs	u3,4
		rjmp	D22s

		sbrs	u1,0
		rjmp	D22s
		
		sbrc	cnt,0			;check last move's color (i.e if counter odd or even)
		rjmp	co22dm

		sbrc	l3,4			;counter even
		rjmp	D22s

		sbrc	l1,0
		rjmp	D22s
		
		rjmp 	w10d

co22dm:	sbrs	l3,4			;counter odd
		rjmp	D22s

		sbrs	l1,0
		rjmp	D22s

		rjmp 	w10d


	;secondary diagonal

D22s:	sbrs	u1,4
		rjmp	Tx22

		sbrs	u3,0
		rjmp	Tx22
		
		sbrc	cnt,0			;check last move's color (i.e if counter odd or even)
		rjmp	co22ds

		sbrc	l1,4			;counter even
		rjmp	Tx22

		sbrc	l3,0
		rjmp	Tx22
		
		rjmp 	w14d

co22ds:	sbrs	l1,4			;counter odd
		rjmp	Tx22

		sbrs	l3,0
		rjmp	Tx22
		
		rjmp 	w14d	


;*** Transmit
		
Tx22:	dec 	cnt

		ldi		tmp,0xA			;010|10 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN

;--------------------------------------------------------------------


;*** Host 3, pressed 5

id32:	sbrc	u3,2			;check if empty
		rjmp 	MAIN

		sbr		u3,4
		cbr		lul,32

		bst		cnt,0
		bld		l3,2
		bld		lll,5

		cpi		cnt,6
		brsh	Tx32
		
;*** Check for win

	;horizontal

		cpi 	u3,21
		brne	V32

		cpi		l3,21
		brne	h32
		rjmp	w30h
		
h32:	cpi		l3,0
		brne	V32

		rjmp	w30h

	;vertical	

V32:	sbrs	u1,2
		rjmp	Tx32

		sbrs	u2,2
		rjmp	Tx32
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co32v

		sbrc	l1,2		;counter even
		rjmp	Tx32

		sbrc	l2,2
		rjmp	Tx32
		
		rjmp 	w12v

co32v:	sbrs	l1,2		;counter odd
		rjmp	Tx32

		sbrs	l2,2
		rjmp	Tx32
		
		rjmp 	w12v	


;*** Transmit
		
Tx32:	dec 	cnt

		ldi		tmp,0xB			;010|11 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN

;--------------------------------------------------------------------
;--------------------------------------------------------------------


Check7: cpi		id,1
		breq	id14
		rjmp	id24
		

;*** Host 1, pressed 7

id14:	sbrc	u1,4			;check if empty
		rjmp 	MAIN

		sbr		u1,16
		cbr		lul,128

		bst		cnt,0
		bld		l1,4
		bld		lll,7

		cpi		cnt,6
		brsh	Tx14
		
;*** Check for win

	;horizontal

		cpi 	u1,21
		brne	V14

		cpi		l1,21
		brne	h14
		rjmp	w10h
		
h14:	cpi		l1,0
		brne	V14

		rjmp	w10h

	;vertical	

V14:	sbrs	u2,4
		rjmp	D14

		sbrs	u3,4
		rjmp	D14
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co14v

		sbrc	l2,4		;counter even
		rjmp	D14

		sbrc	l3,4
		rjmp	D14
		
		rjmp 	w14v

co14v:	sbrs	l2,4		;counter odd
		rjmp	D14

		sbrs	l3,4
		rjmp	D14
		
w14v:	mov		tmp,id			;send win signal
		ori		tmp,0b11011100
		out		UDR,tmp
		ldi		cnt,6			;load code of win
		rjmp 	Win	

	;diagonal

D14:	sbrs	u2,2
		rjmp	Tx14

		sbrs	u3,0
		rjmp	Tx14
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co14d

		sbrc	l2,2		;counter even
		rjmp	Tx14

		sbrc	l3,0
		rjmp	Tx14
		
		rjmp 	w14d

co14d:	sbrs	l2,2		;counter odd
		rjmp	Tx14

		sbrs	l3,0
		rjmp	Tx14
		
w14d:	mov		tmp,id			;send win signal
		ori		tmp,0b00011100
		out		UDR,tmp
		ldi		cnt,0			;load code of win
		rjmp 	Win	


;*** Transmit
		
Tx14:	dec 	cnt

		ldi		tmp,0x11				;100|01 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN
			
;--------------------------------------------------------------------


id24:	cpi		id,2
		brne	id34


;*** Host 2, pressed 7

		sbrc	u2,4				;check if empty
		rjmp 	MAIN

		sbr		u2,16
		cbr		lul,128

		bst		cnt,0
		bld		l2,4
		bld		lll,7

		cpi		cnt,6
		brsh	Tx24
		
;*** Check for win

	;horizontal

		cpi 	u2,21
		brne	V24

		cpi		l2,21
		brne	h24
		rjmp	w20h
		
h24:	cpi		l2,0
		brne	V24

		rjmp	w20h

	;vertical	

V24:	sbrs	u1,4
		rjmp	Tx24

		sbrs	u3,4
		rjmp	Tx24
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co24v

		sbrc	l1,4		;counter even
		rjmp	Tx24

		sbrc	l3,4
		rjmp	Tx24
		
		rjmp 	w14v

co24v:	sbrs	l1,4		;counter odd
		rjmp	Tx24

		sbrs	l3,4
		rjmp	Tx24

		rjmp 	w14v


;*** Transmit
		
Tx24:	dec 	cnt

		ldi		tmp,0x12			;100|10 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN

;--------------------------------------------------------------------


;*** Host 3, pressed 7

id34:	sbrc	u3,4			;check if empty
		rjmp 	MAIN

		sbr		u3,16
		cbr		lul,128

		bst		cnt,0
		bld		l3,4
		bld		lll,7

		cpi		cnt,6
		brsh	Tx34
		
;*** Check for win

	;horizontal

		cpi 	u3,21
		brne	V34

		cpi		l3,21
		brne	h34
		rjmp	w30h
		
h34:	cpi		l3,0
		brne	V34

		rjmp	w30h

	;vertical	

V34:	sbrs	u1,4
		rjmp	D34

		sbrs	u2,4
		rjmp	D34
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co34v

		sbrc	l1,4		;counter even
		rjmp	D34

		sbrc	l2,4
		rjmp	D34
		
		rjmp 	w14v

co34v:	sbrs	l1,4		;counter odd
		rjmp	D34

		sbrs	l2,4
		rjmp	D34
		
		rjmp 	w14v

	;diagonal

D34:	sbrs	u2,2
		rjmp	Tx34

		sbrs	u1,0
		rjmp	Tx34
		
		sbrc	cnt,0		;check last move's color (i.e if counter odd or even)
		rjmp	co34d

		sbrc	l2,2		;counter even
		rjmp	Tx34

		sbrc	l1,0
		rjmp	Tx34
		
		rjmp 	w10d

co34d:	sbrs	l2,2		;counter odd
		rjmp	Tx34

		sbrs	l1,0
		rjmp	Tx34
		
		rjmp 	w10d


;*** Transmit
		
Tx34:	dec 	cnt

		ldi		tmp,0x13		;100|11 bit_to_change|host_id
		out 	UDR,tmp

		rjmp 	MAIN

;--------------------------------------------------------------------
;--------------------------------------------------------------------

;**** Decoding received data

Dcode: 	in 		tmp,UDR
		
		mov		tmp2,tmp
		andi	tmp2,3			;isolate initial sender's id
		
		dec		tmp2			;check if next host is the sender
		cpi		tmp2,0			
		brne	cmp
		ldi		tmp2,3		

cmp:	cp		tmp2,id
		breq	CRSig			;if next is sender, don't resend
		
		out		UDR,tmp

CRSig:	lsr		tmp				;isolate bit_to_change
		lsr		tmp
		mov		tmp3,tmp
		andi	tmp,0b00000111

		cpi		tmp,0			;check if received data is "reset signal"
		brne	WinSig
		rjmp	INIT

WinSig:	cpi		tmp,7			;check if received data is "win signal"	
		brne	Update
		
		lsr		tmp3			;isolate win sequence
		lsr		tmp3
		lsr		tmp3
		mov		cnt,tmp3			
		rjmp	Win

Update:	cpi		tmp2,3			;Find & Update according to the received data
		brne	UID2
		
		sbrs	tmp,2			
		rjmp	uid12

		sbr		u1,16			;update left game bit on Host 1
		bst		cnt,0
		bld		l1,4
		dec		cnt
		rjmp	MAIN

uid12:	sbrs	tmp,1
		rjmp	uid13

		sbr		u1,4			;update middle game bit on Host 1
		bst		cnt,0
		bld		l1,2
		dec		cnt
		rjmp	MAIN

uid13:	sbr		u1,1			;update right game bit on Host 1
		bst		cnt,0
		bld		l1,0
		dec		cnt
		rjmp	MAIN

UID2:	cpi		tmp2,1
		brne	UID3

		sbrs	tmp,2
		rjmp	uid22

		sbr		u2,16			;update left game bit on Host 2
		bst		cnt,0
		bld		l2,4
		dec		cnt
		rjmp	MAIN

uid22:	sbrs	tmp,1
		rjmp	uid23

		sbr		u2,4			;update middle game bit on Host 2
		bst		cnt,0
		bld		l2,2
		dec		cnt
		rjmp	MAIN

uid23:	sbr		u2,1			;update right game bit on Host 2
		bst		cnt,0
		bld		l2,0
		dec		cnt
		rjmp	MAIN

UID3:	sbrs	tmp,2
		rjmp	uid32

		sbr		u3,16			;update left game bit on Host 3
		bst		cnt,0
		bld		l3,4
		dec		cnt
		rjmp	MAIN

uid32:	sbrs	tmp,1
		rjmp	uid33

		sbr		u3,4			;update middle game bit on Host 3
		bst		cnt,0
		bld		l3,2
		dec		cnt
		rjmp	MAIN

uid33:	sbr		u3,1			;update right game bit on Host 3
		bst		cnt,0
		bld		l3,0
		dec		cnt
		rjmp	MAIN

;--------------------------------------------------------------------
;--------------------------------------------------------------------

;*** STUCK

Stuck:			
		sbr		lul,2
		sbr		lll,2
		cbr		lul,1
		cbr		lll,1

cycl:
		out		PORTB,lul		;update LEDs layer1
										
		out		PORTB,lll		;update LEDs layer2

		ldi		tmp,7
	sdly:						;make some LEDs to look darker
		dec		tmp
		brne 	sdly

		sbis 	PINA,0			;if button0 pressed
		rjmp 	Reset			;restart
		
		sbic 	USR,RXC			;check for far reset signal
		rjmp 	Dcode

		rjmp 	cycl

;--------------------------------------------------------------------
;--------------------------------------------------------------------

;*** WIN!!!

Win:	ldi 	lll,0xFE

		cpi		id,1				;Identify win sequence and determine 
		brne	wid2				;which LED should flash on the local Host
		
			cpi		cnt,1
			breq	all

			cpi		cnt,2
			breq	none
				
			cpi		cnt,3
			breq	none

			cpi		cnt,4
			breq	third
				
			cpi		cnt,5
			breq	second

			cpi		cnt,6
			breq	first
				
			cpi		cnt,7
			breq	third

			cpi		cnt,0
			breq	first


wid2:	cpi		id,2
		brne	wid3
		
			cpi		cnt,1
			breq	none

			cpi		cnt,2
			breq	all
				
			cpi		cnt,3
			breq	none

			cpi		cnt,4
			breq	third
				
			cpi		cnt,5
			breq	second

			cpi		cnt,6
			breq	first
				
			cpi		cnt,7
			breq	second

			cpi		cnt,0
			breq	second


wid3:		cpi		cnt,1
			breq	none

			cpi		cnt,2
			breq	none
				
			cpi		cnt,3
			breq	all

			cpi		cnt,4
			breq	third
				
			cpi		cnt,5
			breq	second

			cpi		cnt,6
			breq	first
				
			cpi		cnt,7
			breq	first

			cpi		cnt,0
			breq	third


first:	ldi		lul,0b01111110
		rjmp	blink

second:	ldi		lul,0b11011110
		rjmp	blink

third:	ldi		lul,0b11110110
		rjmp	blink

all:	ldi		lul,0b01010110
		rjmp	blink

none:	ldi		lul,0b11111110
		rjmp	blink


blink:
		out		PORTB,lul		;update LEDs layer1

	delay:
		dec		tmp
		brne 	delay
		dec		tmp2
		brne 	delay

		sbis 	PINA,0			;if button0 pressed
		rjmp 	Reset			;restart
		
		sbic 	USR,RXC			;check for far reset signal
		rjmp 	Dcode
		
		out		PORTB,lll		;update LEDs layer2

	delay2:
		dec		tmp
		brne 	delay2
		dec		tmp2
		brne 	delay2
		
		rjmp 	blink
