.include "m16def.inc"


.def  adr_1  =r16
.def  adr_0  =r17
.def  cmd_1  =r18
.def  cmd_0  =r19
.def  temp   =r20
.def  t_cnt  =r21
.def  b_cnt  =r22
.def  flags  =r23
.def  temp1  =r24   ; флаг принят комд,

.equ kodonem =0x28     ;    40
.equ kodonen =0x14      ;   20
.equ kodzerom =0x14      ; 20
.equ kodzeron =0x05   ;      5
.equ kodstm =0x5A     ;     90
.equ kodstn =0x3C     ;     60
.equ kodpm =0x3C      ;    60
.equ kodpn =0x28       ;  40
.equ codT1L =0x4A      ;


  jmp reset
  jmp   EXT_INT0	   ; Addr $02
  reti		 			; Addr $04
  reti			      ; Addr $06
  reti			      ; Addr $08
  reti    				; Addr $0A
  reti					; Addr $0C
  reti      		 	; Addr $0E
  reti    				; Addr $10
   jmp 	TIMER0_OVF     ;  $0012  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  reti    				;$0014
  reti  				   ;$0016
  reti   				;$0018
  reti   				;$ОО1А
  reti    				;$ОО1С
  jmp 	TIMER0_OVF               ;$ОО1Е          !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  jmp 	TIMER0_OVF	;$0020                !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  reti   				;$0022
  reti  				   ;$0024
  reti    				;$0026
  reti    				;$0028
;-------------------------------------
TIMER0_OVF:
	clr temp
	cbr flags, 0b01111111
	clr t_cnt
	clr b_cnt
	out TCNT0, temp
	out 	TCCR0, temp
   reti
EXT_INT0:
   ldi temp, (0<< INT1)|(0<< INT0)|(0<< INT2)|(0<< IVSEL)|(0<<IVCE)
  	out GICR, temp
	in t_cnt,  TCNT0
	clr 	temp
	out 	TCNT0, temp
	ldi 	temp, 0x05
	out 	TCCR0, temp
	cpi		t_cnt, kodzeron
	brsh	prov0
	jmp    ent	
prov0: 	
	cpi t_cnt, kodonen           ; флаг 0
	brsh prov1
	cbr flags, 0b00000011
	sbr flags, 0b00000001
	jmp ili01

prov1:
	cpi t_cnt, kodpn            ;флаг 1
	brsh provp
	cbr flags, 0b00000011
	sbr flags, 0b00000010
	jmp ili01

provp:
	cpi t_cnt, kodstn       ;флаг повтора
	brsh provst
	cbr flags, 0b00001100
	sbr flags, 0b00000100
	sbrc flags,6
	jmp deistvie
	jmp sbrosvsego

provst:
	cpi t_cnt, kodstm            ;флаг старт
	brsh ent
	cbr flags, 0b00001100
	sbr flags, 0b00001000
	clr adr_1
	clr adr_0
	clr cmd_1
	clr cmd_0
	cbr flags, 0b01110111
	clr t_cnt
	clr b_cnt
	jmp ent


ili01:	
	clr temp
	mov temp, flags
	andi temp, 0b00001111
	cpi temp, 0b00001001
	brne zap1
	inc b_cnt
	rol cmd_1
	rol cmd_0
	rol adr_1
	rol adr_0
	jmp prov32

zap1: clr temp
   mov temp, flags
	andi temp, 0b00001111
	cpi temp, 0b00001010
	brne sbrosvsego
	inc b_cnt
	rol cmd_1
	rol cmd_0
	rol adr_1
	rol adr_0
	sbr cmd_1, 0b00000001
   jmp prov32

prov32:
	clr temp
	clr t_cnt
	ldi temp, 0x20
	cpi b_cnt, 0x20
	brne ent
	sbrs flags, 7
	jmp zapeep

	jmp deistvie
	;сравнение с eep
	;если нет jmp sbrosvsego
	;если да jmp deistvie




deistvie:
	; выполняем действие
	cbr flags, 0b01111111
	sbr flags, 0b01000000
	clr t_cnt
	clr b_cnt
	jmp ent

ent:	
	ldi temp, (0<< INT1)|(1<< INT0)|(0<< INT2)|(0<< IVSEL)|(0<<IVCE)
  	out GICR, temp
	reti
sbrosvsego:
	clr 	temp
	out 	TCNT0, temp
	out 	TCCR0, temp; останавливаем таймер
	cbr flags, 0b01111111
	clr adr_1
	clr adr_0
	clr cmd_1
	clr cmd_0
	clr t_cnt
	clr b_cnt
	jmp ent
	
	

zapeep:
		
w1:   sbic eecr,eewe
   	jmp w1
		ldi temp, 0x00
   	ldi temp1, 0x01
		out eearh, temp  ;записываем eep
		out eearl, temp1
		out eedr, adr_0
		sbi eecr, eemwe
		sbi eecr, eewe
w2:   sbic eecr,eewe
   	jmp w2
		ldi temp, 0x00
   	ldi temp1, 0x02
		out eearh, temp  ;записываем eep
		out eearl, temp1
		out eedr, adr_1
		sbi eecr, eemwe
		sbi eecr, eewe	
w3:   sbic eecr,eewe
   	jmp w3
		ldi temp, 0x00
   	ldi temp1, 0x03
		out eearh, temp  ;записываем eep
		out eearl, temp1
		out eedr, cmd_0
		sbi eecr, eemwe
		sbi eecr, eewe
w4:   sbic eecr,eewe
   	jmp w4
		ldi temp, 0x00
   	ldi temp1, 0x04
		out eearh, temp  ;записываем eep
		out eearl, temp1
		out eedr, cmd_1
		sbi eecr, eemwe
		sbi eecr, eewe	
	clr flags
	sbr flags, 0b10000000
	clr 	temp
	out 	TCNT0, temp
	out 	TCCR0, temp; останавливаем таймер
	clr t_cnt
	clr b_cnt
	jmp ent



;---------------------------------

reset:
  ldi temp, high(RAMEND)
  out SPH, temp
  ldi temp, low(RAMEND)
  out SPL, temp
  ldi temp, (0<< SE)|(0<< SM2)|(0<< SM1)|(0<< SM0)|(0<<ISC11)|(1<<ISC10)|(1<<ISC01)|(1<<ISC00)
  out MCUCR, temp
  ldi temp, 0b00000001
  out TIMSK, temp
  ldi temp, (0<< INT1)|(1<< INT0)|(0<< INT2)|(0<< IVSEL)|(0<<IVCE)
  out GICR, temp
  ldi temp, (1<< DDA0)|(1<< DDA1)|(1<< DDA2)|(1<< DDA3)|(1<< DDA4)|(1<< DDA5)|(1<< DDA6)|(1<< DDA7)
  out DDRA, temp
  ldi temp, (1<< DDB0)|(1<< DDB1)|(1<< DDB2)|(1<< DDB3)|(1<< DDB4)|(1<< DDB5)|(1<< DDB6)|(1<< DDB7)
  out DDRB, temp
  ldi temp, (1<< DDC0)|(1<< DDC1)|(1<< DDC2)|(1<< DDC3)|(1<< DDC4)|(1<< DDC5)|(1<< DDC6)
  out DDRC, temp
  ldi temp, (1<< DDD0)|(1<< DDD1)|(0<< DDD2)|(1<< DDD3)|(1<< DDD4)|(1<< DDD5)|(1<< DDD6)|(1<< DDD7)
  out DDRD, temp
  ldi temp, 0xFF
  out PORTA, temp
  out PORTB, temp	
  out PORTC, temp
  ldi temp, 0xFB
  out PORTD, temp
  ldi t_cnt, 0xFC
  ldi temp, 0x00
  out GIFR, temp
  	clr temp
	clr flags
	clr t_cnt
	clr b_cnt
	clr adr_1
	clr adr_0
	clr CMD_1
	clr CMD_0
	out TCNT0, temp
	out 	TCCR0, temp
  sei

cykle:
  nop
  nop
nop
      jmp cykle









