; ATmega8A 1MHz (bez Xtalu)
; po stlaceni buttonu (INT0) sa zvysi jas LED (OC1A)

.nolist
.include "m8def.inc"
.list

.cseg
.org 0x0000
	rjmp RESET
.org INT0addr
	rjmp INT0_vect

RESET:
	ldi R16, high(ramend)
	out SPH, R16
	ldi R16, low(ramend)
	out SPH, R16

	; PB1 LED output (OC1A)
	sbi DDRB, DDB1
	
	; PD2 Button input (INT0) pull-up
	cbi DDRD, DDD2
	sbi PORTD, PD2

	; PWM (Fast 8-bit, Non inverting, No prescaling)
	ldi R16, 100
	out OCR1AL, R16  ; LOW Byte of OCR1A
	ldi R16, 1<<WGM10 | 1<<COM1A1
	out TCCR1A, R16
	ldi R16, 1<<WGM12 | 1<<CS10
	out TCCR1B, R16

	; External Interrupt (Falling Edge)
	ldi R16, 1<<ISC01
	out MCUCR, R16
	ldi R16, 1<<INT0
	out GICR, R16
	sei

LOOP:
	rjmp LOOP

; rutina prerusenia
INT0_vect:
	push R16
	in R16, SREG
	push R16

	in R16, OCR1AL
	ldi R17, 25
	add R16, R17
	out OCR1AL, R16

	pop R16
	out SREG, R16
	pop R16
	reti
