2 ; Mouse driver for ST & Amiga mouses and Atari trakball.
4 ; Original access routines: 05/07/2000 Freddy Offenga
5 ; Converted to driver: Christian Groessler, 2014-01-04
8 ; AMIGA_MOUSE - builds Amiga mouse version
9 ; TRAK_MOUSE - builds trakball version
10 ; If none of these defines are active, the ST mouse version
14 .include "zeropage.inc"
15 .include "mouse-kernel.inc"
20 .if .not ( .defined (AMIGA_MOUSE) .or .defined (TRAK_MOUSE))
24 ; ------------------------------------------------------------------------
25 ; Header. Includes jump table
33 .byte $6d, $6f, $75 ; "mou"
34 .byte MOUSE_API_VERSION ; Mouse driver API version number
57 .byte MOUSE_FLAG_LATE_IRQ
59 ; Callback table, set by the kernel before INSTALL is called
61 CHIDE: jmp $0000 ; Hide the cursor
62 CSHOW: jmp $0000 ; Show the cursor
63 CMOVEX: jmp $0000 ; Move the cursor to X coord
64 CMOVEY: jmp $0000 ; Move the cursor to Y coord
67 ;----------------------------------------------------------------------------
80 ;----------------------------------------------------------------------------
81 ; Global variables. The bounding box values are sorted so that they can be
82 ; written with the least effort in the SETBOX and GETBOX routines, so don't
88 YPos: .res 2 ; Current mouse position, Y
89 XPos: .res 2 ; Current mouse position, X
90 XMin: .res 2 ; X1 value of bounding box
91 YMin: .res 2 ; Y1 value of bounding box
92 XMax: .res 2 ; X2 value of bounding box
93 YMax: .res 2 ; Y2 value of bounding box
94 Buttons: .res 1 ; Button mask
101 .if .defined (AMIGA_MOUSE) .or .defined (ST_MOUSE)
121 ; Default values for some of the above variables
122 ; (We use ".proc" because we want to define both a label and a scope.)
125 .word (SCREEN_HEIGHT+1)/2 ; YPos
126 .word (SCREEN_WIDTH+1)/2 ; XPos
129 .word SCREEN_WIDTH ; XMax
130 .word SCREEN_HEIGHT ; YMax
136 ; ST mouse lookup table
138 STTab: .byte $FF,$01,$00,$01
139 .byte $00,$FF,$00,$01
140 .byte $01,$00,$FF,$00
141 .byte $01,$00,$01,$FF
147 ; Amiga mouse lookup table
149 AmiTab: .byte $FF,$01,$00,$FF
150 .byte $00,$FF,$FF,$01
151 .byte $01,$FF,$FF,$00
152 .byte $FF,$00,$01,$FF
158 ;----------------------------------------------------------------------------
159 ; INSTALL routine. Is called after the driver is loaded into memory. If
160 ; possible, check if the hardware is present.
161 ; Must return an MOUSE_ERR_xx code in a/x.
165 ; Initialize variables. Just copy the default stuff over
167 ldx #.sizeof(DefVars)-1
173 ; Be sure the mouse cursor is invisible and at the default location.
187 ; Install timer irq routine to poll mouse.
191 ; Setup pointer to wrapper install/deinstall function.
193 sta set_VTIMR1_handler+1
195 sta set_VTIMR1_handler+2
197 ; Install my handler.
201 jsr set_VTIMR1_handler
231 ora #%00000001 ; timer 1 enable
235 ; Done, return zero (= MOUSE_ERR_OK)
241 ;----------------------------------------------------------------------------
242 ; UNINSTALL routine. Is called before the driver is removed from memory.
243 ; No return code required (the driver is removed from memory on return).
247 ; uninstall timer irq routine
250 and #%11111110 ; timer 1 disable
257 jsr set_VTIMR1_handler
272 ;----------------------------------------------------------------------------
273 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
274 ; a counter for calls to show/hide, and the driver entry point is only called
275 ; if the mouse is currently visible and should get hidden. For most drivers,
276 ; no special action is required besides hiding the mouse cursor.
277 ; No return code required.
286 ;----------------------------------------------------------------------------
287 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
288 ; a counter for calls to show/hide, and the driver entry point is only called
289 ; if the mouse is currently hidden and should become visible. For most drivers,
290 ; no special action is required besides enabling the mouse cursor.
291 ; No return code required.
300 ;----------------------------------------------------------------------------
301 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
302 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
303 ; No checks are done if the mouse is currently inside the box, this is the job
304 ; of the caller. It is not necessary to validate the parameters, trust the
305 ; caller and save some code here. No return code required.
308 stx ptr1+1 ; Save data pointer
310 ldy #.sizeof (MOUSE_BOX)-1
322 ;----------------------------------------------------------------------------
323 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
324 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
327 stx ptr1+1 ; Save data pointer
329 ldy #.sizeof (MOUSE_BOX)-1
341 ;----------------------------------------------------------------------------
342 ; MOVE: Move the mouse to a new position. The position is passed as it comes
343 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
344 ; remove the parameter from the stack on return.
345 ; No checks are done if the new position is valid (within the bounding box or
346 ; the screen). No return code required.
367 stx YPos+1 ; New Y position
378 sta XPos ; New X position
381 jsr CMOVEX ; Move the cursor
388 @Ret: plp ; Restore interrupt flag
391 ;----------------------------------------------------------------------------
392 ; BUTTONS: Return the button mask in a/x.
399 ;----------------------------------------------------------------------------
400 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
401 ; No return code required.
403 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
406 sei ; Disable interrupts
407 lda XPos ; Transfer the position
416 plp ; Restore interrupt flag
419 sta (ptr1),y ; Store last byte
423 ;----------------------------------------------------------------------------
424 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
425 ; struct pointed to by ptr1. No return code required.
427 ; We're cheating here to keep the code smaller: The first fields of the
428 ; mouse_info struct are identical to the mouse_pos struct, so we will just
429 ; call _mouse_pos to initialize the struct pointer and fill the position
434 ; Fill in the button state
437 ldy #MOUSE_INFO::BUTTONS
442 ;----------------------------------------------------------------------------
443 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
444 ; specific data in ptr1, and the ioctl code in A.
445 ; Must return an error code in a/x.
448 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
449 ldx #>MOUSE_ERR_INV_IOCTL
452 ;----------------------------------------------------------------------------
453 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
454 ; (so be careful). The routine MUST return carry set if the interrupt has been
455 ; 'handled' - which means that the interrupt source is gone. Otherwise it
456 ; MUST return carry clear.
461 ; Check for a pressed button and place the result into Buttons
464 lda TRIG0 ; joystick #0 trigger
465 bne @L0 ; not pressed
473 ; Limit the X coordinate to the bounding box
497 ; Limit the Y coordinate to the bounding box
529 ;----------------------------------------------------------------------------
530 ; T1Han: Local IRQ routine to poll mouse
602 .elseif .defined (AMIGA_MOUSE)
604 ; Amiga mouse version
657 .elseif .defined (TRAK_MOUSE)