;
;
;
;===========================================
;      1351 mouse basic-compatible driver
;               for the c128
;===========================================
;
;
;
iirq    = $0314
vic     = $d000
sid     = $d400
potx    = sid+$19
poty    = sid+$1a
;
;
active  = $117e         ; if zero, then move sprite
;
vicdata = $11d6         ; 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
;
;
        *=$18f0
;
;
iirq2   *=*+2
opotx   *=*+1
opoty   *=*+1
newvalue        *=*+1
oldvalue        *=*+1
;
;
        * = $1800
;
;
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 active      ; if    basic is moveing sprite
        bne 90$         ;       let basic have it ( why not ? )
        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 increase 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
