2 ; Driver for a "joystick mouse".
4 ; Ullrich von Bassewitz, 2004-03-29, 2009-09-26
7 .include "zeropage.inc"
8 .include "mouse-kernel.inc"
13 ; ------------------------------------------------------------------------
14 ; Header. Includes jump table
22 .byte $6d, $6f, $75 ; "mou"
23 .byte MOUSE_API_VERSION ; Mouse driver API version number
42 .byte MOUSE_FLAG_LATE_IRQ
44 ; Callback table, set by the kernel before INSTALL is called
46 CHIDE: jmp $0000 ; Hide the cursor
47 CSHOW: jmp $0000 ; Show the cursor
48 CMOVEX: jmp $0000 ; Move the cursor to X coord
49 CMOVEY: jmp $0000 ; Move the cursor to Y coord
52 ;----------------------------------------------------------------------------
66 ;----------------------------------------------------------------------------
67 ; Global variables. The bounding box values are sorted so that they can be
68 ; written with the least effort in the SETBOX and GETBOX routines, so don't
74 YPos: .res 2 ; Current mouse position, Y
75 XPos: .res 2 ; Current mouse position, X
76 XMin: .res 2 ; X1 value of bounding box
77 YMin: .res 2 ; Y1 value of bounding box
78 XMax: .res 2 ; X2 value of bounding box
79 YMax: .res 2 ; Y2 value of bounding box
80 Buttons: .res 1 ; Button mask
82 ; Temporary value used in the int handler
86 ; Default values for above variables
91 .word SCREEN_HEIGHT/2 ; YPos
92 .word SCREEN_WIDTH/2 ; XPos
95 .word SCREEN_WIDTH ; XMax
96 .word SCREEN_HEIGHT ; YMax
102 ;----------------------------------------------------------------------------
103 ; INSTALL routine. Is called after the driver is loaded into memory. If
104 ; possible, check if the hardware is present.
105 ; Must return an MOUSE_ERR_xx code in a/x.
109 ; Initialize variables. Just copy the default stuff over
111 ldx #.sizeof(DefVars)-1
117 ; Be sure the mouse cursor is invisible and at the default location. We
118 ; need to do that here, because our mouse interrupt handler doesn't set the
119 ; mouse position if it hasn't changed.
131 ; Done, return zero (= MOUSE_ERR_OK)
137 ;----------------------------------------------------------------------------
138 ; UNINSTALL routine. Is called before the driver is removed from memory.
139 ; No return code required (the driver is removed from memory on return).
141 UNINSTALL = HIDE ; Hide cursor on exit
143 ;----------------------------------------------------------------------------
144 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
145 ; a counter for calls to show/hide, and the driver entry point is only called
146 ; if the mouse is currently visible and should get hidden. For most drivers,
147 ; no special action is required besides hiding the mouse cursor.
148 ; No return code required.
155 ;----------------------------------------------------------------------------
156 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
157 ; a counter for calls to show/hide, and the driver entry point is only called
158 ; if the mouse is currently hidden and should become visible. For most drivers,
159 ; no special action is required besides enabling the mouse cursor.
160 ; No return code required.
167 ;----------------------------------------------------------------------------
168 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
169 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
170 ; No checks are done if the mouse is currently inside the box, this is the job
171 ; of the caller. It is not necessary to validate the parameters, trust the
172 ; caller and save some code here. No return code required.
175 stx ptr1+1 ; Save data pointer
177 ldy #.sizeof (MOUSE_BOX)-1
188 ;----------------------------------------------------------------------------
189 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
190 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
193 stx ptr1+1 ; Save data pointer
195 ldy #.sizeof (MOUSE_BOX)-1
206 ;----------------------------------------------------------------------------
207 ; MOVE: Move the mouse to a new position. The position is passed as it comes
208 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
209 ; remove the parameter from the stack on return.
210 ; No checks are done if the new position is valid (within the bounding box or
211 ; the screen). No return code required.
214 MOVE: sei ; No interrupts
217 stx YPos+1 ; New Y position
226 sta XPos ; New X position
228 jsr CMOVEX ; Move the cursor
230 cli ; Allow interrupts
233 ;----------------------------------------------------------------------------
234 ; BUTTONS: Return the button mask in a/x.
241 ;----------------------------------------------------------------------------
242 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
243 ; No return code required.
245 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
247 sei ; Disable interrupts
248 lda XPos ; Transfer the position
257 cli ; Enable interrupts
260 sta (ptr1),y ; Store last byte
264 ;----------------------------------------------------------------------------
265 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
266 ; struct pointed to by ptr1. No return code required.
268 ; We're cheating here to keep the code smaller: The first fields of the
269 ; mouse_info struct are identical to the mouse_pos struct, so we will just
270 ; call _mouse_pos to initialize the struct pointer and fill the position
275 ; Fill in the button state
278 ldy #MOUSE_INFO::BUTTONS
283 ;----------------------------------------------------------------------------
284 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
285 ; specific data in ptr1, and the ioctl code in A.
286 ; Must return an error code in a/x.
289 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
290 ldx #>MOUSE_ERR_INV_IOCTL
293 ;----------------------------------------------------------------------------
294 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
295 ; (so be careful). The routine MUST return carry set if the interrupt has been
296 ; 'handled' - which means that the interrupt source is gone. Otherwise it
297 ; MUST return carry clear.
302 lda CIA1_PRB ; Read joystick #0
304 eor #$1F ; Make all bits active high
307 ; Check for a pressed button and place the result into Buttons
309 ldx #$00 ; Assume no button pressed
310 and #JOY::FIRE ; Check fire button
311 beq @L0 ; Jump if not pressed
312 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
317 lda Temp ; Read joystick #0
318 and #(JOY::LEFT | JOY::RIGHT)
321 ; We will cheat here and rely on the fact that either the left, OR the right
324 and #JOY::RIGHT ; Check RIGHT bit
328 bne @AddX ; Branch always
332 ; Calculate the new X coordinate (--> a/y)
335 tay ; Remember low byte
340 ; Limit the X coordinate to the bounding box
358 ; Move the mouse pointer to the new X pos
363 ; Calculate the Y movement vector
365 @SkipX: lda Temp ; Read joystick #0
366 and #(JOY::UP | JOY::DOWN) ; Check up/down
369 ; We will cheat here and rely on the fact that either the up, OR the down
380 ; Calculate the new Y coordinate (--> a/y)
383 tay ; Remember low byte
388 ; Limit the Y coordinate to the bounding box
406 ; Move the mouse pointer to the new X pos
413 @SkipY: clc ; Interrupt not handled