       opt    b,c,f,w,x
       nam    Micro Mouse Firmware
       spc    4
************************************************
*                                              *
*      I/O register addresses                  *
*                                              *
************************************************
cntout equ    $2000
dra    equ    $4000    PIA port A data
cra    equ    $4001    PIA port A control
drb    equ    $4002    PIA port B data
crb    equ    $4003    PIA port B control
cr13   equ    $8000    PTM Channel 1/3 control
cr2    equ    $8001    PTM Status/Chan 2 Control
tc1h   equ    $8002    PTM Channel 1 counter
tc1l   equ    $8003
tc2h   equ    $8004    PTM Channel 2 counter
tc2l   equ    $8005
tc3h   equ    $8006    PTM Channel 3 counter
tc3l   equ    $8007
       spc
************************************************
*                                              *
*      Static Variables                        *
*                                              *
************************************************
       org    0
tc     rmb    1
sem    rmb    1
pxrate rmb    1
pyrate rmb    1
xrate  rmb    1
yrate  rmb    1
xcnt   rmb    1
ycnt   rmb    1
timx   rmb    1
timy   rmb    1
normx  rmb    1
normy  rmb    1
xzdel  rmb    1
yzdel  rmb    1
xnzdel rmb    1
ynzdel rmb    1
pulsim rmb    1
ximage rmb    1
yimage rmb    1
xcross rmb    1
ycross rmb    1
savex  rmb    2
       spc
************************************************
*                                              *
*      Interrupt vectors                       *
*                                              *
************************************************
       org    $FFF8
       fdb    timout   Maskable
       fdb    start    Software
       fdb    start    Non-maskable
       fdb    start    Reset
       page
************************************************
*                                              *
*      Start of program                        *
*                                              *
************************************************
       org    $FC00
start  lds    #$7F     Initialise stack pointer
       spc
************************************************
*                                              *
*      Initialise PIA                          *
*                                              *
************************************************
       clr a           Access both Data
       sta a  cra      Direction regs
       sta a  crb
       sta a  sem
       sta a  timx
       sta a  timy
       sta a  normx
       sta a  normy
       sta a  xcnt
       sta a  ycnt
       sta a  xrate
       sta a  yrate
       sta a  pxrate
       sta a  pyrate
       sta a  pulsim
       sta a  cntout
       sta a  xcross
       sta a  ycross
       lda a  #$FF     Set all bits to output
       sta a  dra
       sta a  drb
       lda a  #%100    Access data regs
       sta a  cra
       sta a  crb
       lda a  #$80
       sta a  dra
       sta a  drb
       sta a  ximage
       sta a  yimage
       spc    2
************************************************
*                                              *
*      Initialise Counter and Display buffer   *
*                                              *
************************************************
       lda a  #10
       sta a  tc
       clr b
       spc
************************************************
*                                              *
*      Initialise PTM (channel 3)              *
*                                              *
************************************************
       lda a  #1
       sta a  cr2      Access control reg 1
       sta a  cr13     Reset all counters
       sta b  cr2      Access control reg 3
       ldx    #124     Load counter 3 with
       stx    tc3h     1 ms time constant
       lda a  #$43     Load control reg 3 for
       sta a  cr13     continuous, internal
*                      prescaled by 8
       ldx    #$FF00
       stx    tc2h
       lda a  #1
       sta a  cr2      Access control reg 1
       stx    tc1h
       sta b  cr13     Enable counters
       cli
       lda a  #200
       sta a  xzdel
iloop  tst    sem
       ble    iloop
       dec    sem
       lda a  timx
       add a  normx
       ror a
       sta a  normx
       lda a  timy
       add a  normy
       ror a
       sta a  normy
       dec    xzdel
       bne    iloop
zloope lda a  #10
       sta a  xnzdel
       sta a  ynzdel
zloop  jsr    wait
       dec    xnzdel
       dec    ynzdel
       lda a  #10
       tst    pxrate
       bne    zl1
       sta a  xnzdel
zl1    tst    pyrate
       bne    zl2
       sta a  ynzdel
zl2    tst    xnzdel
       bne    zl4
       tst    ynzdel
       bne    zl3
       jsr    pulsex
       jsr    pulsey
       jmp    bloop
zl3    jmp    xloope
zl4    tst    ynzdel
       bne    zloop
       jmp    yloope
bloop  jsr    wait
       dec    xzdel
       dec    xnzdel
       dec    yzdel
       dec    ynzdel
       lda a  #50
       lda b  #40
       tst    pxrate
       bne    bl1
       sta b  xnzdel
       bra    bl2
bl1    sta a  xzdel
bl2    tst    pyrate
       bne    bl3
       sta b  ynzdel
       bra    bl4
bl3    sta a  yzdel
bl4    lda a  #10
       tst    xnzdel
       bne    bl5
       jmp    rloope
bl5    tst    ynzdel
       bne    bl6
       jmp    rloope
bl6    tst    xzdel
       bne    bl8
       sta a  xnzdel
       tst    yzdel
       bne    bl7
       sta a  ynzdel
       jmp    zloop
bl7    jmp    yloop
bl8    tst    yzdel
       bne    bloop
       sta a  ynzdel
       jmp    xloop
xloope jsr    pulsex
xloop  jsr    wait
       dec    xzdel
       dec    xnzdel
       dec    ynzdel
       lda a  #50
       lda b  #40
       tst    pxrate
       bne    xl1
       sta b  xnzdel
       bra    xl2
xl1    sta a  xzdel
xl2    tst    pyrate
       bne    xl3
       sta a  ynzdel
xl3    lda a  #10
       tst    xnzdel
       bne    xl4
       jmp    rloope
xl4    tst    xzdel
       bne    xl6
       sta a  xnzdel
       tst    ynzdel
       bne    xl5
       jmp    yloope
xl5    jmp    zloop
xl6    tst    ynzdel
       bne    xloop
       jsr    pulsey
       jmp    bloop
yloope jsr    pulsey
yloop  jsr    wait
       dec    xnzdel
       dec    yzdel
       dec    ynzdel
       lda a  #50
       lda b  #40
       tst    pxrate
       bne    yl1
       sta a  xnzdel
yl1    tst    pyrate
       bne    yl2
       sta b  ynzdel
       bra    yl3
yl2    sta b  yzdel
yl3    tst    ynzdel
       bne    yl4
       jmp    rloope
yl4    lda a  #10
       tst    yzdel
       bne    yl6
       sta a  ynzdel
       tst    xnzdel
       bne    yl5
       jmp    xloope
yl5    jmp    zloop
yl6    tst    xnzdel
       bne    yloop
       jsr    pulsex
       jmp    bloop
rloope lda a  #50
       sta a  xzdel
       sta a  xzdel
rloop  lda a  pxrate
       sta a  xrate
       lda a  pyrate
       sta a  yrate
       jsr    wait
       dec    xzdel
       dec    yzdel
       lda a  #50
       tst    pxrate
       bne    rl1
       sta a  xzdel
rl1    tst    pyrate
       bne    rl2
       sta a  yzdel
rl2    tst    xzdel
       bne    rloop
       tst    yzdel
       bne    rloop
       jmp    zloope
pulsex lda a  #50
       sta a  xzdel
       lda a  #40
       sta a  xnzdel
       tst    pxrate
       bmi    pulxl1
       ldx    #incx
       bra    pulxl2
pulxl1 ldx    #decx
pulxl2 sei
       jsr    pulse
       cli
       rts
pulsey lda a  #50
       sta a  yzdel
       lda a  #40
       sta a  ynzdel
       tst    pyrate
       bmi    pulyl1
       ldx    #incy
       bra    pulyl2
pulyl1 ldx    #decy
pulyl2 sei
       jsr    pulse
       cli
       rts
gtrate asr a
       add a  0,x
       bpl    ok1
       clr a
ok1    cmp a  1,x
       ble    ok2
       lda a  1,x
ok2    inx
       inx
       stx    savex
       add a  savex+1
       sta a  savex+1
       bcc    skpinc
       inc    savex
skpinc ldx    savex
       lda a  0,x
       rts
xclass fcb    midx-2-*
       fcb    endx-2-*
       fcb    2,3,4,5,6,7,9,12,15,23,31,47,63,95,127
       fcb    0,0,0,0
midx   fcb    0,0,0,0,0
       fcb    129,161,193,209,225,233,241,244,247,249,250,251,252,253,254
endx   equ    *
yclass fcb    midy-2-*
       fcb    endy-2-*
       fcb    254,253,252,251,250,249,247,244,241,233,225,209,193,161,129
       fcb    0,0,0,0
midy   fcb    0,0,0,0,0
       fcb    127,95,63,47,31,23,15,12,9,7,6,5,4,3,2
endy   equ    *
wait   tst    sem
       ble    wait
       dec    sem
       lda a  normx
       sub a  timx
       ldx    #xclass
       jsr    gtrate
       sta a  pxrate
       lda a  normy
       sub a  timy
       ldx    #yclass
       jsr    gtrate
       sta a  pyrate
       rts
       page
************************************************
*                                              *
*      PTM timeout interrupt handler           *
*                                              *
************************************************
       spc    2
timout lda a  cr2      Read PTM status
       lda a  tc
       dec a
       bne    notnth
       ldx    #$FF00
       lda a  tc1h
       lda a  tc1l
       sta a  timx
       stx    tc1h
       lda a  tc2h
       lda a  tc2l
       sta a  timy
       stx    tc2h
       inc    sem
       lda a  #10
notnth sta a  tc
       ldx    tc3h     Read counter to clear
*                      interrupts
       spc
       lda a  xcnt
       beq    restx
       blt    negx
       dec a
       bne    nochgx
       ldx    #incx
       jsr    pulse
       bra    restx
negx   inc a
       bne    nochgx
       ldx    #decx
       jsr    pulse
restx  lda a  xrate
nochgx sta a  xcnt
       lda a  ycnt
       beq    resty
       blt    negy
       dec a
       bne    nochgy
       ldx    #incy
       jsr    pulse
       bra    resty
negy   inc a
       bne    nochgy
       ldx    #decy
       jsr    pulse
resty  lda a  yrate
nochgy sta a  ycnt
       spc
       lda a  xcross
       cmp a  #3
       beq    notx
       inc a
       sta a  xcross
       cmp a  #3
       bne    notoy
       lda b  #-4&255
       sta b  ycross
notoy  cmp a  #-3&255
       bne    nofrmy
       lda b  yimage
       sta b  drb
nofrmy add a  ximage
       sta a  dra
       rti
notx   lda a  ycross
       inc a
       sta a  ycross
       cmp a  #3
       bne    notox
       lda b  #-4&255
       sta b  xcross
notox  cmp a  #-3&255
       bne    nofrmx
       lda b  ximage
       sta b  dra
nofrmx add a  yimage
       sta a  drb
       rti
       spc    2
pulse  lda a  ximage
       add a  0,x
       sta a  ximage
       lda a  yimage
       add a  1,x
       sta a  yimage
       rts
       spc
incx   fcb    1
       fcb    0
decx   fcb    255
       fcb    0
incy   fcb    0
       fcb    1
decy   fcb    0
       fcb    255
       end
