;-----------------------------------------------------------------
.nolist
.include "m128def.inc"
.list
; Interrupt service vectors

.org $0000
	rjmp Reset						; Reset vector
	
;-----------------------------------------------------------------
; Register defines for main loop
;-----------------------------------------------------------------


.def		mpr		=	r16			; defines multipurpose register
.def		rbin1L 	= 	r17			; Left 90 degrees upper byte
.def		rbin1H	= 	r18
.def		rbin2L	= 	r19
.def		rbin2H	=	r20



;-----------------------------------------------------------------
; Reset vector
;-----------------------------------------------------------------

Reset:
;-----Setting Stackpointer----------------------------------------
	ldi		MPR,low(RAMEND)			; Set stackptr to ram end
	out		SPL,MPR
	ldi 	MPR, high(RAMEND)
	out 	SPH, MPR

ldi	rbin1L,$ff
ldi	rbin1H,$01

ldi ZH,high(result)
ldi	ZL,low(result)

call Bin2ToHex4
mainloop:

	lpm	r21,Z+
	lpm r22,Z+
	lpm r23,Z+
	lpm R24,Z
	rjmp	mainloop
; **************************************************
;
; Package III: From binary to Hex-ASCII
;
; Bin2ToHex4
; ==========
; converts a 16-bit-binary to uppercase Hex-ASCII
; In: 16-bit-binary in rBin1H:L, Z points to first
;   position of the four-character Hex-ASCII
; Out: Z points to the first digit of the four-character
;   Hex-ASCII, ASCII digits A..F in capital letters
; Used registers: rBin1H:L (unchanged), mpr
; Called subroutines: Bin1ToHex2, Bin1ToHex1
;
Bin2ToHex4:
	mov mpr,rBin1H ; load MSB
	rcall Bin1ToHex2 ; convert byte
	mov mpr,rBin1L
	rcall Bin1ToHex2
	sbiw ZL,4 ; Set Z to start
	ret
;
; Bin1ToHex2 converts an 8-bit-binary to uppercase hex
; Called by: Bin2ToHex4
;
Bin1ToHex2:
	push mpr ; Save byte
	swap mpr ; upper to lower nibble
	rcall Bin1ToHex1
	pop mpr ; Restore byte
Bin1ToHex1:
	andi mpr,$0F ; mask upper nibble
	subi mpr,-'0' ; add 0 to convert to ASCII
	cpi mpr,'9'+1 ; A..F?
	brcs Bin1ToHex1a
	subi mpr,-7 ; add 7 for A..F
Bin1ToHex1a:
	spm z+,mpr ; store in target
	ret ; and return
;
; *******************************************

.org $500

.dseg

result:	.byte 2