;
;
;
;=========================================
;      1351 mouse basic-compatible driver
;              for the c64
;=========================================
;
;
;
iirq    = $0314
vic     = $d000
sid     = $d400
potx    = sid+$19
poty    = sid+$1a
;
;
vicdata = $d000         ; basics copy of vic register image
xpos    = vicdata+$00   ; low order xposition
ypos    = vicdata+$01   ; y position
xposmsb = vicdata+$10   ; bit 0 is high order x position
;
;
        *=$c000
;
;
iirq2   *=*+2
opotx   *=*+1
opoty   *=*+1
newvalue        *=*+1
oldvalue        *=*+1
;
;
        * = $c100
;
;
install lda iirq+1
        cmp #>mirq
        beq 90$
        php
        sei
        lda iirq
        sta iirq2
        lda iirq+1
        sta iirq2+1
;
        lda #<mirq
        sta iirq
        lda #>mirq
        sta iirq+1
;
        plp
90$     rts
;
;











mirq    cld             ; just in case.....
        lda potx        ; get delta values for x
        ldy opotx
        jsr movchk
        sty opotx
;
        clc             ; modify low order xposition
        adc xpos
        sta xpos
        txa
        adc #$00
        and #%00000001
        eor xposmsb
        sta xposmsb
;
        lda poty        ; get delta value for y
        ldy opoty
        jsr movchk
        sty opoty
;
        sec             ; modify y position ( decrease y for increse in pot )
        eor #$ff
        adc ypos
        sta ypos
;
90$     jmp (iirq2)     ; continue w/ irq operation
;
;==================================================
; movchk
;       entry   y = old value of pot register
;               a = currrent value of pot register
;       exit    y = value to use for old value
;               x,a = delta value for position
;==================================================
;
movchk  sty oldvalue    ;save old &  new values
        sta newvalue
        ldx #0          ;preload x w/ 0
;
        sec             ;a <=  mod64( new-old )
        sbc oldvalue
        and #%01111111	
        cmp #%01000000  ;if   > 0
        bcs 50$
        lsr a           ;       a <= a/2
        beq 80$         ;       if      <> 0
        ldy newvalue    ;               y <= newvalue
        rts             ;               return
;
50$     ora #%11000000  ;else   or in high order bits
        cmp #$ff        ;       if      <> -1
        beq 80$
        sec             ;               a <= a/2
        ror a
        ldx #$ff        ;               x <= -1
        ldy newvalue    ;               y <= newvalue
        rts             ;               return
;
80$     lda #0          ;a <= 0
        rts             ;return w/ y = old value
;
	.end

