; ******************************************************
; 
; 	RDY/BYSY 	PD1
;	OE			PD2
;	WR			PD3
;	BS1			PD4
;	XA0			PD5
;	XA1			PD6
;	PAGEL		PD7
;	+12V		RESET
;	BS2			PC2
;	XTAL1		
;	GND
; ******************************************************

..include "m8def.inc"

..def	zero	=	r1
..def	tmp	=	r16
..def	tmp2 =	r17
..def	data=	r21
..def	_WR_=	r22

..equ	bysy =	pd1
..equ	OE =	pd2
..equ	wr	=	pd3
..equ	bs1=	pd4
..equ	xa0	=	pd5
..equ	xa1	=	pd6
..equ	PAGEL=	pd7
..equ	bs2	=	pc2

..equ	xtal1=	pb6



..equ	pitanie_podanno =	pc5



..org 0
        rjmp reset     		  ; Обработка сброса
        rjmp reset    		  ; Обработка IRQ0 EXT_INT0
        rjmp reset			  ; Обработка IRQ1 EXT_INT1
        rjmp reset		      ; Обработка захвата таймера 1
        rjmp reset		      ; Обработка совпадения таймера 1
        rjmp reset		      ; Обработка переполнения таймера 1
        rjmp overflow_timer0  ; Обработка переполнения таймера 0
        rjmp reset		      ; Обработка приема байта
        rjmp reset		      ; Обработка освобождения UDR
        rjmp reset		      ; Обработка передачи байта
        rjmp reset		      ; Обработка аналогового компаратора

;****************************
overflow_timer0:
		reti

;****************************
reset:
		nop;
		ldi tmp, 	low(ramend)
		ldi tmp2, 	High(ramend)
		out SPH, tmp2	; настроили стек
		out SPL, tmp	; настроили стек

		ldi tmp, 0xff
		out ddrb, tmp	; порт данных на выход
		
		ldi tmp, (1<<xa0)|(1<<xa1)|(1<<bs1)|(1<<wr)|(1<<PAGEL)	;
		out ddrd, tmp

		ldi tmp, (1<<bs2)|(1<<pc1)|(pc0)
		out ddrc, tmp
		
		ldi _WR_, 1<<wr
		
		clr zero
		
		cbi	ddrc, pitanie_podanno

;----------------------------------------------
main:
		;sbis pinc, pitanie_podanno
		;rjmp main
		
		;начало программирования
		rcall delay
		sbi portd, wr

		;1a
		;1. Set XA1, XA0 to “10”. This enables command loading.
		;2. Set BS1 to ”0”.
		;3. Set DATA to “XXXX XXXX”. This is the command for XXX.
		;4. Give XTAL1 a positive pulse. This loads the command.
		sbi	portd, xa1
		cbi portd, xa0
		cbi portd, bs1

		// 0b01000000	;команда  Write Fuse Bits
		out portb, zero
		cbi portc, pc1
		sbi portc, pc0

		rcall xtal_pulse

		;2c
		;C. Load Data Low byte
		;1. Set XA1, XA0 to “01”. This enables data loading.
		;2. Set DATA = Data Low byte (0x00 - 0xFF).
		;3. Give XTAL1 a positive pulse. This loads the data byte.
		cbi portd, xa1
		sbi portd, xa0

		; 0b11100001 ;BODLEVEL BODEN SUT1 SUT0 CKSEL3 CKSEL2 CKSEL1 CKSEL0
		out portb, zero
		sbi portb, pb0
		sbi portb, pb5
		sbi portc, pc0
		sbi portc, pc1

		rcall xtal_pulse

		;3 
		;(Low byte 	- Set BS1 and BS2 to “0”) 
		;(High byte	- Set BS1 to “1” and BS2 to “0”. This selects high data byte.)
		cbi portd, bs1
		cbi portc, bs2
		
		;4. Give WR a negative pulse and wait for RDY/BSY to go high.
		cbi portd, wr
		rcall delay;
		out portd, _WR_ ;все выводы =0 wr=1
	

_bysy_:	sbis pind, bysy
		rjmp _bysy_

		;1a
		sbi	portd, xa1
		cbi portd, xa0
		cbi portd, bs1

		; 0b01000000	;команда  Write Fuse Bits
		out portb, zero
		cbi portc, pc1
		sbi portc, pc0

		rcall xtal_pulse

		;2c
		cbi portd, xa1
		sbi portd, xa0

		;data, 0b11011001 ;(RSTDISBL WDTON SPIEN CKOPT EESAVE BOOTSZ1 BOOTSZ0 BOOTRST)
		out portb, zero
		sbi portb, pb0
		sbi portb, pb3
		sbi portb, pb4
		sbi portc, pc0
		sbi portc, pc1		

		rcall xtal_pulse

		;3 
		;(High byte	- Set BS1 to “1” and BS2 to “0”. This selects high data byte.)
		sbi portd, bs1
		cbi portc, bs2
		
		;4. Give WR a negative pulse and wait for RDY/BSY to go high.
		cbi portd, wr

	sbi ddrc, pc4
	sbi portc, pc4

end:	rjmp end;


		rjmp main

;****************************
delay:
		ldi tmp, 70
	loop1:
		dec tmp
		brne	loop1
		ret

;****************************

xtal_pulse:
		rcall delay
		sbi portb, xtal1
		rcall delay
		cbi portb, xtal1
		out portd, _WR_ ;все выводы =0 wr=1
		rcall delay
		ret		
;****************************


