;***************************************************************************
;* A P P L I C A T I O N   N O T E   F O R   T H E   A V R   F A M I L Y
;* 
;* Number           : AVR000
;* File Name        : "m164def.inc"
;* Title            : Register/Bit Definitions for the ATmega164
;* Date             : April 14th, 2004
;* Version          : 1.0
;* Support E-mail   : support@atmel.no
;* Target MCU       : ATmega164
;*
;* DESCRIPTION
;* When including this file in the assembly program file, all I/O register  
;* names and I/O register bit names appearing in the data book can be used.
;* In addition, the six registers forming the three data pointers X, Y and
;* Z have been assigned names XL - ZH. Highest RAM address for Internal 
;* SRAM is also defined 
;*
;* The Register names are represented by their hexadecimal address.
;* 
;* The Register Bit names are represented by their bit number (0-7).
;* 
;* Please observe the difference in using the bit names with instructions
;* such as "sbr"/"cbr" (set/clear bit in register) and "sbrs"/"sbrc" 
;* (skip if bit in register set/cleared). The following example illustrates
;* this:
;* 
;* in   r16,PORTB       ;read PORTB latch
;* sbr  r16,(1<<PB6)+(1<<PB5)   ;set PB6 and PB5 (use masks, not bit#)
;* out  PORTB,r16       ;output to PORTB
;*
;* in   r16,TIFR        ;read the Timer Interrupt Flag Register
;* sbrc r16,TOV0        ;test the overflow flag (use bit#)
;* rjmp TOV0_is_set     ;jump if set
;* ...              ;otherwise do something else
;***************************************************************************

;**** Specify Device ****
.device ATmega164

;*****************************************************************************
; I/O Register Definitions
;*****************************************************************************

.IODEF ;Embedded Pascal specific extention

;**** Memory Mapped I/O Register Definitions  (0xFF-0x60) ****
.equ  UDR1	= $CE
.equ  UBRR1H	= $CD
.equ  UBRR1L	= $CC
.equ  UCSR1C	= $CA
.equ  UCSR1B	= $C9
.equ  UCSR1A	= $C8
.equ  UDR0      = $C6  
.equ  UDR	= $C6
.equ  UBRR0H    = $C5
.equ  UBRRH	= $C5  
.equ  UBRR0L    = $C4
.equ  UBRRL	= $C4  
.equ  UCSR0C    = $C2
.equ  UCSRC	= $C2
.equ  UCSR0B    = $C1
.equ  UCSRB	= $C1
.equ  UCSR0A    = $C0
.equ  UCSRA	= $C0
.equ  TWAMR	= $BD
.equ  TWCR	= $BC
.equ  TWDR	= $BB
.equ  TWAR	= $BA
.equ  TWSR	= $B9
.equ  TWBR	= $B8
.equ  ASSR      = $B6
.equ  OCR2B	= $B4
.equ  OCR2A     = $B3  
.equ  TCNT2     = $B2
.equ  TCCR2B	= $B1  
.equ  TCCR2A    = $B0
.equ  OCR1BH    = $8B 
.equ  OCR1BL    = $8A 
.equ  OCR1AH    = $89 
.equ  OCR1AL    = $88 
.equ  ICR1H     = $87 
.equ  ICR1L     = $86 
.equ  TCNT1H    = $85 
.equ  TCNT1L    = $84 
.equ  TCCR1C    = $82 
.equ  TCCR1B    = $81
.equ  TCCR1A    = $80
.equ  DIDR1     = $7F
.equ  DIDR0     = $7E
.equ  ADMUX     = $7C 
.equ  ADCSRB    = $7B
.equ  ADCSRA    = $7A
.equ  ADCH      = $79 
.equ  ADCL      = $78
.equ  PCMSK3	= $73
.equ  TIMSK2    = $70
.equ  TIMSK1    = $6F
.equ  TIMSK0    = $6E
.equ  PCMSK2	= $6D
.equ  PCMSK1    = $6C
.equ  PCMSK0    = $6B 
.equ  EICRA     = $69 
.equ  OSCCAL    = $66
.equ  PRR       = $64
.equ  CLKPR     = $61
.equ  WDTCSR    = $60

;**** I/O Register Definitions (0x3F-0x00) ****
.equ  SREG      = $3F
.equ  SPH       = $3E
.equ  SPL       = $3D
.equ  RAMPZ	= $3B
.equ  SPMCSR    = $37
.equ  MCUCR     = $35
.equ  MCUSR     = $34
.equ  SMCR      = $33
.equ  OCDR      = $31
.equ  ACSR      = $30
.equ  SPDR      = $2E 
.equ  SPSR      = $2D
.equ  SPCR      = $2C
.equ  GPIOR2    = $2B 
.equ  GPIOR1    = $2A 
.equ  OCR0B	= $28
.equ  OCR0A     = $27
.equ  TCNT0     = $26
.equ  TCCR0B	= $25 
.equ  TCCR0A    = $24
.equ  GTCCR     = $23
.equ  EEARH     = $22 
.equ  EEARL     = $21 
.equ  EEDR      = $20
.equ  EECR      = $1F
.equ  GPIOR0    = $1E 
.equ  EIMSK     = $1D
.equ  EIFR      = $1C
.equ  PCIFR	= $1B
.equ  TIFR2     = $17
.equ  TIFR1     = $16
.equ  TIFR0     = $15
.equ  PORTD     = $0B
.equ  DDRD      = $0A
.equ  PIND      = $09
.equ  PORTC     = $08
.equ  DDRC      = $07
.equ  PINC      = $06
.equ  PORTB     = $05
.equ  DDRB      = $04
.equ  PINB      = $03
.equ  PORTA     = $02
.equ  DDRA      = $01
.equ  PINA      = $00

;*****************************************************************************
; Bit Definitions
;*****************************************************************************

.BITDEF ;Embedded Pascal specific extention

; *** UCSRC ***
.equ UMSEL      = 6
.equ UPM1       = 5
.equ UPM0       = 4
.equ USBS       = 3
.equ UCSZ1      = 2
.equ UCSZ0      = 1
.equ UCPOL      = 0

; *** UCSRB ***
.equ RXCIE      = 7
.equ TXCIE      = 6
.equ UDRIE      = 5
.equ RXEN       = 4
.equ TXEN       = 3
.equ UCSZ2      = 2
.equ RXB8       = 1
.equ TXB8       = 0

; *** UCSRA ***
.equ RXC        = 7
.equ TXC        = 6
.equ UDRE       = 5
.equ FE         = 4
.equ DOR        = 3
.equ UPE        = 2
.equ U2X        = 1
.equ MPCM       = 0

; *** USISR ***
.equ USISIF     = 7  
.equ USIOIF     = 6
.equ USIPF      = 5
.equ USIDC      = 4
.equ USICNT3    = 3
.equ USICNT2    = 2
.equ USICNT1    = 1
.equ USICNT0    = 0

; *** USICR ***
.equ USISIE     = 7  
.equ USIOIE     = 6
.equ USIWM1     = 5
.equ USIWM0     = 4
.equ USICS1     = 3
.equ USICS0     = 2
.equ USICLK     = 1
.equ USITC      = 0
    
; *** ASSR ***
.equ EXCLK      = 4
.equ AS2        = 3  
.equ TCN2UB     = 2
.equ OCR2UB     = 1
.equ TCR2UB     = 0

; *** TCCR2A ***
.equ FOC2       = 7  
.equ WGM20      = 6
.equ COM2A1     = 5
.equ COM2A0     = 4
.equ WGM21      = 3
.equ CS22       = 2
.equ CS21       = 1
.equ CS20       = 0

; *** TCCR1C ***
.equ FOC1A      = 7  
.equ FOC1B      = 6

; *** TCCR1B ***
.equ ICNC1      = 7  
.equ ICES1      = 6
.equ WGM13      = 4
.equ WGM12      = 3
.equ CS12       = 2
.equ CS11       = 1
.equ CS10       = 0

; *** TCCR1A ***
.equ COM1A1     = 7  
.equ COM1A0     = 6
.equ COM1B1     = 5
.equ COM1B0     = 4
.equ WGM11      = 1
.equ WGM10      = 0

; *** DIDR1 ***
.equ ADC7D      = 7  
.equ ADC6D      = 6
.equ ADC5D      = 5
.equ ADC4D      = 4
.equ ADC3D      = 3
.equ ADC2D      = 2
.equ ADC1D      = 1
.equ ADC0D      = 0

; *** DIDR0 ***
.equ AIN1D      = 1  
.equ AIN0D      = 0

; *** ADMUX ***
.equ REFS1      = 7  
.equ REFS0      = 6
.equ ADLAR      = 5
.equ MUX4       = 4
.equ MUX3       = 3
.equ MUX2       = 2
.equ MUX1       = 1
.equ MUX0       = 0

; *** ADCSRB ***
.equ ACME       = 6
.equ ADTS2      = 2
.equ ADTS1      = 1
.equ ADTS0      = 0

; *** ADCSRA ***
.equ ADEN       = 7  
.equ ADSC       = 6
.equ ADATE      = 5
.equ ADIF       = 4
.equ ADIE       = 3
.equ ADPS2      = 2
.equ ADPS1      = 1
.equ ADPS0      = 0

; *** TIMSK2 ***
.equ OCIE2A     = 1
.equ TOIE2      = 0

; *** TIMSK1 ***
.equ ICIE1      = 5  
.equ OCIE1B     = 2
.equ OCIE1A     = 1
.equ TOIE1      = 0

; *** TIMSK0 ***
.equ OCIE0A     = 1
.equ TOIE0      = 0

; *** PCMSK1 ***
.equ PCINT15    = 7  
.equ PCINT14    = 6
.equ PCINT13    = 5
.equ PCINT12    = 4
.equ PCINT11    = 3
.equ PCINT10    = 2
.equ PCINT9     = 1
.equ PCINT8     = 0

; *** PCMSK0 ***
.equ PCINT7     = 7  
.equ PCINT6     = 6
.equ PCINT5     = 5
.equ PCINT4     = 4
.equ PCINT3     = 3
.equ PCINT2     = 2
.equ PCINT1     = 1
.equ PCINT0     = 0

; *** EICRA ***
.equ ISC01      = 1
.equ ISC00      = 0

; *** PRR ***
.equ PRTIM1     = 3
.equ PRSPI      = 2
.equ PRUSART0   = 1
.equ PRADC      = 0

; *** CLKPR ***
.equ CLKPCE     = 7  
.equ CLKPS3     = 3
.equ CLKPS2     = 2
.equ CLKPS1     = 1
.equ CLKPS0     = 0

; *** WDTCR ***
.equ WDCE       = 4  
.equ WDE        = 3
.equ WDP2       = 2
.equ WDP1       = 1
.equ WDP0       = 0

; *** SREG ***
.equ I          = 7  
.equ T          = 6
.equ H          = 5
.equ S          = 4
.equ V          = 3
.equ N          = 2
.equ Z          = 1
.equ C          = 0

; *** SPH ***
.equ SP15       = 7  
.equ SP14       = 6
.equ SP13       = 5
.equ SP12       = 4
.equ SP11       = 3
.equ SP10       = 2
.equ SP9        = 1
.equ SP8        = 0

; *** SPL ***
.equ SP7        = 7  
.equ SP6        = 6
.equ SP5        = 5
.equ SP4        = 4
.equ SP3        = 3
.equ SP2        = 2
.equ SP1        = 1
.equ SP0        = 0

; *** SPMCSR ***
.equ SPMIE      = 7  
.equ RWWSB      = 6
.equ RWWSRE     = 4
.equ BLBSET     = 3
.equ PGWRT      = 2
.equ PGERS      = 1
.equ SPMEN      = 0

; *** MCUCR ***
.equ JTD        = 7  
.equ PUD        = 4
.equ IVSEL      = 1
.equ IVCE       = 0

; *** MCUSR ***
.equ JTRF       = 4  
.equ WDRF       = 3
.equ BORF       = 2
.equ EXTRF      = 1
.equ PORF       = 0

; *** SMCR ***
.equ SM2        = 3  
.equ SM1        = 2
.equ SM0        = 1
.equ SE         = 0

; *** OCDR ***
.equ IDRD       = 7  
.equ OCD        = 7
.equ OCDR6      = 6
.equ OCDR5      = 5
.equ OCDR4      = 4
.equ OCDR3      = 3
.equ OCDR2      = 2
.equ OCDR1      = 1
.equ OCDR0      = 0

; *** ACSR ***
.equ ACD        = 7  
.equ ACBG       = 6
.equ ACO        = 5
.equ ACI        = 4
.equ ACIE       = 3
.equ ACIC       = 2
.equ ACIS1      = 1
.equ ACIS0      = 0

; *** SPSR ***
.equ SPIF       = 7  
.equ WCOL       = 6
.equ SPI2X      = 0

; *** SPCR ***
.equ SPIE       = 7 
.equ SPE        = 6
.equ DORD       = 5
.equ MSTR       = 4
.equ CPOL       = 3
.equ CPHA       = 2
.equ SPR1       = 1
.equ SPR0       = 0

; *** TCCR0A ***
.equ FOC0A      = 7 
.equ WGM00      = 6
.equ COM0A1     = 5
.equ COM0A0     = 4
.equ WGM01      = 3
.equ CS02       = 2
.equ CS01       = 1
.equ CS00       = 0

; *** GTCCR ***
.equ TSM        = 7  
.equ PSR2       = 1
.equ PSR10      = 0
                     
; *** EECR ***
.equ EERIE      = 3  
.equ EEMWE      = 2
.equ EEWE       = 1
.equ EERE       = 0

; *** EIMSK ***
.equ PCIE1      = 7
.equ PCIE0      = 6
.equ INT0       = 0

; *** EIFR ***
.equ PCIF1      = 7
.equ PCIF0      = 6
.equ INTF0      = 0

; *** TIFR2 ***
.equ OCF2A      = 1
.equ TOV2       = 0

; *** TIFR1 ***
.equ ICF1       = 5  
.equ OCF1B      = 2
.equ OCF1A      = 1
.equ TOV1       = 0

; *** TIFR0 ***
.equ OCF0A      = 1
.equ TOV0       = 0


; *** PORTD ***
.equ PORTD7     = 7  
.equ PORTD6     = 6
.equ PORTD5     = 5
.equ PORTD4     = 4
.equ PORTD3     = 3
.equ PORTD2     = 2
.equ PORTD1     = 1
.equ PORTD0     = 0
.equ PD7        = 7  
.equ PD6        = 6
.equ PD5        = 5
.equ PD4        = 4
.equ PD3        = 3
.equ PD2        = 2
.equ PD1        = 1
.equ PD0        = 0
  
; *** DDRD ***
.equ DDD7       = 7  
.equ DDD6       = 6
.equ DDD5       = 5
.equ DDD4       = 4
.equ DDD3       = 3
.equ DDD2       = 2
.equ DDD1       = 1
.equ DDD0       = 0

; *** PIND ***
.equ PIND7      = 7  
.equ PIND6      = 6
.equ PIND5      = 5
.equ PIND4      = 4
.equ PIND3      = 3
.equ PIND2      = 2
.equ PIND1      = 1
.equ PIND0      = 0

; *** PORTC ***
.equ PORTC7     = 7  
.equ PORTC6     = 6
.equ PORTC5     = 5
.equ PORTC4     = 4
.equ PORTC3     = 3
.equ PORTC2     = 2
.equ PORTC1     = 1
.equ PORTC0     = 0
.equ PC7        = 7  
.equ PC6        = 6
.equ PC5        = 5
.equ PC4        = 4
.equ PC3        = 3
.equ PC2        = 2
.equ PC1        = 1
.equ PC0        = 0

; *** DDRC ***
.equ DDC7       = 7  
.equ DDC6       = 6
.equ DDC5       = 5
.equ DDC4       = 4
.equ DDC3       = 3
.equ DDC2       = 2
.equ DDC1       = 1
.equ DDC0       = 0

; *** PINC ***
.equ PINC7      = 7  
.equ PINC6      = 6
.equ PINC5      = 5
.equ PINC4      = 4
.equ PINC3      = 3
.equ PINC2      = 2
.equ PINC1      = 1
.equ PINC0      = 0

; *** PORTB ***
.equ PORTB7     = 7  
.equ PORTB6     = 6
.equ PORTB5     = 5
.equ PORTB4     = 4
.equ PORTB3     = 3
.equ PORTB2     = 2
.equ PORTB1     = 1
.equ PORTB0     = 0
.equ PB7        = 7  
.equ PB6        = 6
.equ PB5        = 5
.equ PB4        = 4
.equ PB3        = 3
.equ PB2        = 2
.equ PB1        = 1
.equ PB0        = 0

; *** DDRB ***
.equ DDB7       = 7  
.equ DDB6       = 6
.equ DDB5       = 5
.equ DDB4       = 4
.equ DDB3       = 3
.equ DDB2       = 2
.equ DDB1       = 1
.equ DDB0       = 0

; *** PINB ***
.equ PINB7      = 7  
.equ PINB6      = 6
.equ PINB5      = 5
.equ PINB4      = 4
.equ PINB3      = 3
.equ PINB2      = 2
.equ PINB1      = 1
.equ PINB0      = 0

; *** PORTA ***
.equ PORTA7     = 7  
.equ PORTA6     = 6
.equ PORTA5     = 5
.equ PORTA4     = 4
.equ PORTA3     = 3
.equ PORTA2     = 2
.equ PORTA1     = 1
.equ PORTA0     = 0
.equ PA7        = 7  
.equ PA6        = 6
.equ PA5        = 5
.equ PA4        = 4
.equ PA3        = 3
.equ PA2        = 2
.equ PA1        = 1
.equ PA0        = 0

; *** DDRA ***
.equ DDA7       = 7  
.equ DDA6       = 6
.equ DDA5       = 5
.equ DDA4       = 4
.equ DDA3       = 3
.equ DDA2       = 2
.equ DDA1       = 1
.equ DDA0       = 0

; *** PINA ***
.equ PINA7      = 7  
.equ PINA6      = 6
.equ PINA5      = 5
.equ PINA4      = 4
.equ PINA3      = 3
.equ PINA2      = 2
.equ PINA1      = 1
.equ PINA0      = 0

;*****************************************************************************
; CPU Register Declarations
;*****************************************************************************
.def    XL  = r26       ; X pointer low
.def    XH  = r27       ; X pointer high
.def    YL  = r28       ; Y pointer low
.def    YH  = r29       ; Y pointer high
.def    ZL  = r30       ; Z pointer low
.def    ZH  = r31       ; Z pointer high


;*****************************************************************************
; Data Memory Declarations
;*****************************************************************************
//.CODEDEF ;Embedded Pascal specific extension

.CPUDEF

;.equ	AdvancedLPM = 1		;CPU has LPM Rx,Z and LPM Rx,Z+ instructions


.equ    RAMEND    = $08FF  ; Highest internal data memory (SRAM) address.
                            ;(1k RAM + IO + REG)
.equ	XRAMEND	  = $04FF;
.equ    E2END = $01FF  ; Highest EEPROM address.
                            ;(512 byte)
;*****************************************************************************
; Program Memory Declarations
;*****************************************************************************


.equ    FLASHEND  = $1FFF  ;  Highest program memory (flash) address
                            ; (When addressed as 16 bit words)
                            ; ( 8k words , 16k byte ) 
        
;**** Boot Vectors ****
                        ;   byte groups
                        ;  /--\/--\/--\/--\ 
.equ    SMALLBOOTSTART  = $1F80; smallest boot block is 128W
.equ    SECONDBOOTSTART = $1F00; second boot block size is 256W
.equ    THIRDBOOTSTART  = $1E00; third boot block size is 512W
.equ    LARGEBOOTSTART  = $1C00; largest boot block is 1024W

;**** Page Size ****
.equ    PAGESIZE    = 64    ;number of WORDS in a page

;*****************************************************************************
;**** Interrupt Vectors ****        
;*****************************************************************************

.VECTORS

.equ    INT0addr      = $02   ;External Interrupt 0 Vector
.equ    PCINT0addr    = $04   ;Pin Change 0 Interrupt Vector 
.equ    PCINT1addr    = $06   ;Pin Change 1 Interrupt Vector
.equ    OC2addr       = $08   ;Timer/Counter2 Compare Match Interrupt Vector
.equ    OVF2addr      = $0A   ;Timer/Counter2 Overflow Interrupt Vector
.equ    ICP1addr      = $0C   ;Timer/Counter1 Capture Event Interrupt Vector
.equ    OC1Aaddr      = $0E   ;Timer/Counter1 Compare Match A Interrupt Vector
.equ    OC1Baddr      = $10   ;Timer/Counter1 Compare Match B Interrupt Vector 
.equ    OVF1addr      = $12   ;Timer/Counter1 Overflow Interrupt Vector
.equ    OC0addr       = $14   ;Timer/Counter0 Compare Match Interrupt Vector
.equ    OVF0addr      = $16   ;Timer/Counter0 Overflow Interrupt Vector
.equ    SPIaddr       = $18   ;SPI Serial Transfer Complete Interrupt Vector
.equ    URXC0addr     = $1A   ;USART, Rx Complete Interrupt Vector
.equ    UDRE0addr     = $1C   ;USART, Data Register Empty Interrupt Vector
.equ    UTXC0addr     = $1E   ;USART, Tx Complete Interrupt Vector
.equ    USI_STARTaddr = $20   ;USI Start Condition Interrupt Vector   
.equ    USI_OVFaddr   = $22   ;USI Overflow Interrupt Vector  
.equ    ACIaddr       = $24   ;Analog Comparator Interrupt Vector
.equ    ADCCaddr      = $26   ;ADC Conversion Complete Interrupt Vector
.equ    ERDYaddr      = $28   ;EEPROM Ready Interrupt Vector
.equ    SPMRaddr      = $2A   ;Store Program Memory Ready Interrupt Vector
.equ	ConstSectionaddr = $74 ;Start of typed constant section unless overridden by "PlaceConst" 


