*A/D Converter Addresses
PORT       EQU $6      Analog Port Number
ADR1       EQU $1031   Result of Analog reading
ADCTL      EQU $1030   A/D Control Status
OPTION     EQU $1039   OPTION register

*Array Addresses
INDEX      EQU $A800       Retina sample count
ARRAY      EQU $A801       Retina samples

*Interrupt vectors
IRQ_VECT   EQU $FFF2
XIRQ_VECT  EQU $FFF4



*************************************************************************************************
*IRQ - Interrupt SubRotine initialization
*************************************************************************************************

           ORG $9000

           SEI             Disable maskable interrupts
           LDD #NULL_IRQ   Initialize interrupt vectors to NULL
           STD IRQ_VECT
           LDD #NULL_XIRQ
           STD XIRQ_VECT
           LDAA #$10       Enable XIRQ interrupt
           TAP
           RTI



*************************************************************************************************
*IRQ - Interrupt SubRotine to fill the array
*array index in INDEX
*array starts at ARRAY
*************************************************************************************************

           ORG $9800

IRQ_INTR   SEI             Disable maskable interrupts
           LDAA INDEX      If INDEX>39 then exit
           LDAB #39
           CBA
           BGT NULL_IRQ

*Get analog from port 6 and store in acumulator A
           LDAA #$80       enable ADC converter in OPTION
           STAA OPTION
           LDAA #PORT      Load ADC PORT number
           STAA ADCTL      Store PORT in A/D Control/Status register
READADCTL  LDAA ADCTL      Read ADCTL
           ANDA #128
           BEQ  READADCTL  Test if result Valid
           LDAA ADR1       Get ADC result


*Get Array Index and store in B, final address in X
           LDX  #ARRAY     X=ARRAY
           LDAB INDEX      B=(INDEX)
           ABX             X=X+B
           STAA 0,X        (X)=A

           INC INDEX      Increment (INDEX)
NULL_IRQ   RTI



*************************************************************************************************
*XIRQ - Interrupt SubRotine to reset array index at $A000
*************************************************************************************************

           ORG $A000

XIRQ_INTR  LDD IRQ_VECT    Is IRQ already enabled?
           CPD #IRQ_INTR
           BEQ DISAB_INTR  If yes, then disable IRQ and XIRQ
           LDAA #0         If no, then enable IRQ and reset INDEX=0
           STAA INDEX
           LDD #IRQ_INTR
           STD IRQ_VECT
           RTI
DISAB_INTR LDD #NULL_IRQ   Disable IRQ and XIRQ
           STD IRQ_VECT
           LDD #NULL_XIRQ
           STD XIRQ_VECT
NULL_XIRQ  RTI
