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. We
174 ; need to do that here, because our mouse interrupt handler doesn't set the
175 ; mouse position if it hasn't changed.
191 ; Install timer irq routine to poll mouse.
195 ; Setup pointer to wrapper install/deinstall function.
197 sta set_VTIMR1_handler+1
199 sta set_VTIMR1_handler+2
201 ; Install my handler.
205 jsr set_VTIMR1_handler
235 ora #%00000001 ; timer 1 enable
239 ; Done, return zero (= MOUSE_ERR_OK)
245 ;----------------------------------------------------------------------------
246 ; UNINSTALL routine. Is called before the driver is removed from memory.
247 ; No return code required (the driver is removed from memory on return).
251 ; uninstall timer irq routine
254 and #%11111110 ; timer 1 disable
261 jsr set_VTIMR1_handler
276 ;----------------------------------------------------------------------------
277 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
278 ; a counter for calls to show/hide, and the driver entry point is only called
279 ; if the mouse is currently visible and should get hidden. For most drivers,
280 ; no special action is required besides hiding the mouse cursor.
281 ; No return code required.
290 ;----------------------------------------------------------------------------
291 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
292 ; a counter for calls to show/hide, and the driver entry point is only called
293 ; if the mouse is currently hidden and should become visible. For most drivers,
294 ; no special action is required besides enabling the mouse cursor.
295 ; No return code required.
304 ;----------------------------------------------------------------------------
305 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
306 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
307 ; No checks are done if the mouse is currently inside the box, this is the job
308 ; of the caller. It is not necessary to validate the parameters, trust the
309 ; caller and save some code here. No return code required.
312 stx ptr1+1 ; Save data pointer
314 ldy #.sizeof (MOUSE_BOX)-1
326 ;----------------------------------------------------------------------------
327 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
328 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
331 stx ptr1+1 ; Save data pointer
333 ldy #.sizeof (MOUSE_BOX)-1
345 ;----------------------------------------------------------------------------
346 ; MOVE: Move the mouse to a new position. The position is passed as it comes
347 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
348 ; remove the parameter from the stack on return.
349 ; No checks are done if the new position is valid (within the bounding box or
350 ; the screen). No return code required.
371 stx YPos+1 ; New Y position
382 sta XPos ; New X position
385 jsr CMOVEX ; Move the cursor
392 @Ret: plp ; Restore interrupt flag
395 ;----------------------------------------------------------------------------
396 ; BUTTONS: Return the button mask in a/x.
403 ;----------------------------------------------------------------------------
404 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
405 ; No return code required.
407 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
410 sei ; Disable interrupts
411 lda XPos ; Transfer the position
420 plp ; Restore interrupt flag
423 sta (ptr1),y ; Store last byte
427 ;----------------------------------------------------------------------------
428 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
429 ; struct pointed to by ptr1. No return code required.
431 ; We're cheating here to keep the code smaller: The first fields of the
432 ; mouse_info struct are identical to the mouse_pos struct, so we will just
433 ; call _mouse_pos to initialize the struct pointer and fill the position
438 ; Fill in the button state
441 ldy #MOUSE_INFO::BUTTONS
446 ;----------------------------------------------------------------------------
447 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
448 ; specific data in ptr1, and the ioctl code in A.
449 ; Must return an error code in a/x.
452 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
453 ldx #>MOUSE_ERR_INV_IOCTL
456 ;----------------------------------------------------------------------------
457 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
458 ; (so be careful). The routine MUST return carry set if the interrupt has been
459 ; 'handled' - which means that the interrupt source is gone. Otherwise it
460 ; MUST return carry clear.
465 ; Check for a pressed button and place the result into Buttons
468 lda TRIG0 ; joystick #0 trigger
469 bne @L0 ; not pressed
473 ; Update coordinates if needed
492 ; Limit the X coordinate to the bounding box
516 ; Limit the Y coordinate to the bounding box
548 ;----------------------------------------------------------------------------
549 ; T1Han: Local IRQ routine to poll mouse
621 .elseif .defined (AMIGA_MOUSE)
623 ; Amiga mouse version
676 .elseif .defined (TRAK_MOUSE)