/*
 * Extended_Addr.asm
 *
 *  Modified: 8 Sept 2021
 *  Authors: Dr. Schwartz, Ivan Bukreyev
 *
 *	Extended addressing mode
 */ 

.include "ATxmega128A1Udef.inc"

.org 0x9123
DATA_FAR:	.dw 0x4744

.dseg
.org 0x2000
OutDat1:	.byte 1

.cseg 
.org 0x0000
	jmp MAIN	

.org 0x200
MAIN:
	;Address = RAMPZ:(Z15..Z1), Z(0) is byte select
	ldi r16, BYTE3(DATA_FAR<<1)	
	out CPU_RAMPZ, r16			;Set up RAMPZ
	ldi ZL, BYTE1(DATA_FAR<<1)	;Set up Z register
	ldi ZH, BYTE2(DATA_FAR<<1)	;
	elpm XL, Z+
	elpm XH, Z

	ldi r16, byte3(FAR_AWAY)	;Address = EIND:(Z15..Z0)
	out CPU_EIND,r16			;Set up EIND (PC(21:16) <- EIND)

;No need for << here, Z0 is part of the address
	ldi ZL,low(FAR_AWAY)
	ldi ZH,high(FAR_AWAY)		
	eijmp						;PC(21:16) <- EIND; PC(15:0) <- Z(15:0) 

;In ATxmega128A1U, 0x10000 - 0x10FFF is boot section
.org 0x10FF0		
FAR_AWAY:
	rjmp FAR_AWAY				;This is relative jump, address offset is between -2048 to 2047.




