* memory segments
RAM     equ     0
STACK   equ     $ff
DEVICES equ     $1000
EEPROM  equ     $f800

* constants for configuration register
OPTION  equ     $39
IRQE    equ     %00100000

* interrupt vectors
IRQ     equ     $fff2
RESET   equ     $fffe

* constants for motors
MOTORS  equ     $04

MOT1EN  equ     %00010000
MOT2EN  equ     %00100000
MOT3EN  equ     %01000000
MOT4EN  equ     %10000000

MOT1DIR equ     %00000001
MOT2DIR equ     %00000010
MOT3DIR equ     %00000100
MOT4DIR equ     %00001000

* data area
        org     RAM

* code area
        org     EEPROM

start
* initialization code
        ldx     #DEVICES
        bset    OPTION,x #IRQE
    
        lds     #STACK

* Enable all the motors so we'll be able to watch the IRQ results
        ldaa    #MOT1EN|MOT2EN|MOT3EN|MOT4EN
        staa    MOTORS,x

* Enable interrupts.  Note IRQ has no local mask
        cli

* main loop
mloop
        bra     mloop

* IRQ interrupt handler
irqhan  ldaa    MOTORS,x

* If I don't have all the direction bits set, add one to the motor port.
* If I do, clear them all (so we don't get a carry into the enable bits)

        brset   MOTORS,x #MOT1DIR|MOT2DIR|MOT3DIR|MOT4DIR clearem
	inca
        staa    MOTORS,x
        bra     retcode

clearem bclr    MOTORS,x #MOT1DIR|MOT2DIR|MOT3DIR|MOT4DIR

retcode rti

* interrupt vectors
        org     IRQ
        fdb     irqhan

        org     RESET
        fdb     start