	[const pinb $23][const ddrb $24][const portb $25]
	  [const run-led 5][const bootmode 4]
	[const ddrd $2a][const portd $2b]

	[const udr0 $c6][const ubrr0h $c5][const ubrr0l $c4]
	[const ucsr0c $c2][const ucsr0b $c1]
	[const ucsr0a $c0]
	  [const rxc0 7][const udre0  5]

	[const eearl $41][const eearh $42][const eedr $40]
	[const eecr $3f]
	  [const eere 0][const eepe 1][const eempe 2]

;---------------
;
; monitor
;

start	[ldi d0 16][sts d0 ubrr0l]
	[eor d0 d0][sts d0 ubrr0h]
	[ldi d0 2][sts d0 ucsr0a]		; u2x
	[ldi d0 $18][sts d0 ucsr0b]		; rxen, txen
	[ldi d0 6][sts d0 ucsr0c]		; 8N1

loop	[rcall getc]
	[inc d0][breq i'm-here]
	[inc d0][breq read-data-byte]
	[inc d0][breq write-data-bytes]
	[inc d0][breq read-flash-word]
	[inc d0][breq loop]
	[inc d0][breq loop]
	[inc d0][breq read-ee-byte]
	[inc d0][breq write-ee-byte]
	[rjmp loop]

i'm-here
	[ldi d0 $23][rcall putc]
	[rjmp loop]

read-data-byte
	[rcall getc][mov x d0]
	[rcall getc][mov xh d0]
	[ld@x d0]
	[rcall putc]
	[rjmp loop]

write-data-bytes
	[rcall getc][mov x d0]
	[rcall getc][mov xh d0]
	[rcall getc][mov t0 d0]
wdb20	[rcall getc][st@x+ d0]
	[dec t0][brne wdb20]
	[rjmp loop]

read-flash-word
	[rcall getc][mov z d0]
	[rcall getc][mov zh d0]
	[add z z][adc zh zh]
	[lpm][mov d0 r0][rcall putc]
	[inc z][lpm][mov d0 r0][rcall putc]
	[rjmp loop]

read-ee-byte
	[rcall getc][sts d0 eearl]
	[rcall getc][sts d0 eearh]
	[sbi eecr eere]
	[lds d0 eedr]
	[rcall putc]
	[rjmp loop]

write-ee-byte
	[rcall getc][sts d0 eearl]
	[rcall getc][sts d0 eearh]
	[rcall getc][sts d0 eedr]
	[sbi eecr eempe][sbi eecr eepe]
web20	[sbic eecr eepe][rjmp web20]
	[ldi d0 $8f][rcall putc]
	[rjmp loop]

getc	[lds d1 ucsr0a]
	[sbrs d1 rxc0][rjmp getc]
	[lds d0 udr0]
	[ret]

putc	[lds d1 ucsr0a]
	[sbrs d1 udre0][rjmp putc]
	[sts d0 udr0]
	[ret]


end
