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 CPREP: jmp $0000 ; Prepare to move the cursor
64 CDRAW: jmp $0000 ; Draw the cursor
65 CMOVEX: jmp $0000 ; Move the cursor to X coord
66 CMOVEY: jmp $0000 ; Move the cursor to Y coord
69 ;----------------------------------------------------------------------------
82 ;----------------------------------------------------------------------------
83 ; Global variables. The bounding box values are sorted so that they can be
84 ; written with the least effort in the SETBOX and GETBOX routines, so don't
90 YPos: .res 2 ; Current mouse position, Y
91 XPos: .res 2 ; Current mouse position, X
92 XMin: .res 2 ; X1 value of bounding box
93 YMin: .res 2 ; Y1 value of bounding box
94 XMax: .res 2 ; X2 value of bounding box
95 YMax: .res 2 ; Y2 value of bounding box
96 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 ; Make sure the mouse cursor is at the default location.
186 ; Install timer irq routine to poll mouse.
190 ; Setup pointer to wrapper install/deinstall function.
192 sta set_VTIMR1_handler+1
194 sta set_VTIMR1_handler+2
196 ; Install my handler.
200 jsr set_VTIMR1_handler
230 ora #%00000001 ; timer 1 enable
234 ; Done, return zero (= MOUSE_ERR_OK)
240 ;----------------------------------------------------------------------------
241 ; UNINSTALL routine. Is called before the driver is removed from memory.
242 ; No return code required (the driver is removed from memory on return).
246 ; uninstall timer irq routine
249 and #%11111110 ; timer 1 disable
256 jsr set_VTIMR1_handler
271 ;----------------------------------------------------------------------------
272 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
273 ; a counter for calls to show/hide, and the driver entry point is only called
274 ; if the mouse is currently visible and should get hidden. For most drivers,
275 ; no special action is required besides hiding the mouse cursor.
276 ; No return code required.
284 ;----------------------------------------------------------------------------
285 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
286 ; a counter for calls to show/hide, and the driver entry point is only called
287 ; if the mouse is currently hidden and should become visible. For most drivers,
288 ; no special action is required besides enabling the mouse cursor.
289 ; No return code required.
297 ;----------------------------------------------------------------------------
298 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
299 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
300 ; No checks are done if the mouse is currently inside the box, this is the job
301 ; of the caller. It is not necessary to validate the parameters, trust the
302 ; caller and save some code here. No return code required.
305 stx ptr1+1 ; Save data pointer
307 ldy #.sizeof (MOUSE_BOX)-1
319 ;----------------------------------------------------------------------------
320 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
321 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
324 stx ptr1+1 ; Save data pointer
326 ldy #.sizeof (MOUSE_BOX)-1
338 ;----------------------------------------------------------------------------
339 ; MOVE: Move the mouse to a new position. The position is passed as it comes
340 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
341 ; remove the parameter from the stack on return.
342 ; No checks are done if the new position is valid (within the bounding box or
343 ; the screen). No return code required.
359 stx YPos+1 ; New Y position
370 sta XPos ; New X position
372 jsr CMOVEX ; Move the cursor
376 plp ; Restore interrupt flag
379 ;----------------------------------------------------------------------------
380 ; BUTTONS: Return the button mask in a/x.
387 ;----------------------------------------------------------------------------
388 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
389 ; No return code required.
391 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
394 sei ; Disable interrupts
395 lda XPos ; Transfer the position
404 plp ; Restore interrupt flag
407 sta (ptr1),y ; Store last byte
411 ;----------------------------------------------------------------------------
412 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
413 ; struct pointed to by ptr1. No return code required.
415 ; We're cheating here to keep the code smaller: The first fields of the
416 ; mouse_info struct are identical to the mouse_pos struct, so we will just
417 ; call _mouse_pos to initialize the struct pointer and fill the position
422 ; Fill in the button state
425 ldy #MOUSE_INFO::BUTTONS
430 ;----------------------------------------------------------------------------
431 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
432 ; specific data in ptr1, and the ioctl code in A.
433 ; Must return an error code in a/x.
436 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
437 ldx #>MOUSE_ERR_INV_IOCTL
440 ;----------------------------------------------------------------------------
441 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
442 ; (so be careful). The routine MUST return carry set if the interrupt has been
443 ; 'handled' - which means that the interrupt source is gone. Otherwise it
444 ; MUST return carry clear.
449 ; Check for a pressed button and place the result into Buttons
452 lda TRIG0 ; joystick #0 trigger
453 bne @L0 ; not pressed
459 ; Limit the X coordinate to the bounding box
483 ; Limit the Y coordinate to the bounding box
513 ;----------------------------------------------------------------------------
514 ; T1Han: Local IRQ routine to poll mouse
586 .elseif .defined (AMIGA_MOUSE)
588 ; Amiga mouse version
641 .elseif .defined (TRAK_MOUSE)