[comp.sys.atari.8bit] Mouse/Trakball Driver

slackey@bbn.com (Stan Lackey) (12/12/89)

OK you hackers, here are a few of the routines I did for the various mouse
trakball etc. fun stuff.

(1) Routine called in with the timer interrupt.  This tracks the Commodore
mouse, a joystick, and possibly the trakball in joystick mode, although
I never tested the last case.

;
OLDVAL = $CB   ;
HORIZ  = $CD   ;
VERT   = $CE   ;
JOYST  = 54016
;
;
        ORG $0600
;
; TIMER ROUTINE
;
TRAKVB:
        TYA
        PHA
        LDA JOYST
        TAY
        AND #1
        BNE X1
        DEC VERT
X1:     TYA
        AND #2
        BNE X2
        INC VERT
X2:     TYA
        AND #4
        BNE X3
        DEC HORIZ
X3:     TYA
        AND #8
        BNE X4
        INC HORIZ
X4:     PLA
        TAY
        PLA
        RTI     ;
        END

This one just tests the various bits from the joystick inputs and
incr/decr the vert/horiz position values.  They go from 0 to 255 then
wrap around; other versions I made allocated limit values in memory
which would be tested before an inc/dec would be done.

(2) Routine for the trakball/ST mouse, on a timer interrupt.  Actually,
the routine is the same as a timer or VBI/DLI; it's just which vectors
get set up that's different.  Except for the VBI, of course, if you
want to invoke the regular Atari VBI after the custom routine.

;
; Definitions
;
OLDVAL = $CB   ;
HORIZ  = $CD   ;
VERT   = $CE   ;
JOYST  = 54016
;
;
        ORG $0600
;
; TIMER ROUTINE
;
TRAKVB:
        TYA
        PHA
        LDA JOYST
        TAY
        EOR OLDVAL
        AND #2
        BEQ TSTVER
        TYA
        AND #1
        BNE RIGHT
        DEC HORIZ
        CLC
        BCC TSTVER
RIGHT:  INC HORIZ

TSTVER: TYA
        EOR OLDVAL
        AND #8
        BEQ EXIT3
        TYA
        AND #4
        BEQ UP
        INC VERT
        CLC
        BCC EXIT3
UP:     DEC VERT
EXIT3:  STY OLDVAL
EXIT2:  PLA
        TAY
        PLA
        RTI     ;
        END

The trakball has 4 outputs: bit1 is the horiz motion clock, with bit0
telling left vs right; bit3 is the vert clock, with bit2 up vs down.

The routine gets input and saves it in y.  It exor's the oldvalue to
see if there was a change in the clock, then rereads the newvalue to
tell which direction.

(3) The BASIC program illustrates how you might load it.

10 OPEN #4,4,0,"D:FTRAK.OBJ"
20 GET #4,X:GET #4,X:GET #4,X
30 GET #4,X:GET #4,X:GET #4,Y
40 ADDR=256*6
50 FOR I=ADDR TO ADDR+X
60 GET #4,X
70 POKE I,X
80 NEXT I
110 CLOSE #4

It just gets the object (assemble the one you need for trakball vs mouse
and stick it in FTRAK.OBJ).  The routine eats the binary format header
(you can check for validity if you're fussy) and saves the low byte of the
length.  It just sticks it in page6.

If you prefer, you could load it from DATA statements.

(4) This one is a simple demo/test program.

5 F=100:REM TIMER FREQ
115 POKE 528,0
120 POKE 529,6:REM TMRINT1 VECTOR
130 POKE 53761,0:REM VOLUME TO ZERO
140 POKE 53760,F:REM FREQ OF TMR1
150 POKE 53769,255:REM TURN ON TIMERS
160 POKE 16,193:REM SET TMINT1
201 INPUT G
204 TRAP 204
205 GRAPHICS G+16
206 COLOR 1
207 H=12*16+13
208 V=12*16+14
209 POKE H,20:POKE V,20
210 PLOT PEEK(H),PEEK(V)
215 DRAWTO PEEK(H),PEEK(V)
220 GOTO 215

Line 5 is to make experimenting with the timer interrupt freq. easy.
(Lower values make the interrupt rate faster.)
Lines 115/120 set the timer interrupt vector to ^X600 .
130/140 set up the timer.
150 turns the timer interrupt on.
160 sets up the interrupt mask.

201 lets you select graphics mode.  Reply 1 thru 8.
207/208 are just the addresses of the H and V coordinates.
209 initializes the coords.
215/220 are the drawing loop.

As you wiggle the trakball etc. you will leave a line behind.  This
was useful in setting the interrupt rate; too slow and you start
aliasing, too fast and the Atari gets slow.

I have come a long way from these routines; I wrote them well over
a year ago.  They probably work.  Write me if you have any problems.

Note: The interrupt routines don't handle the pointer on the screen,
or test the triggers.  My applications do that part themselves; I
thought it was more appropriate that way, as that reduces overhead.
You can't do "mouse-ahead" (as opposed to "type-ahead") like my Sun,
but hey.

Have fun. -Stan