2 ; Driver for a "joystick mouse".
4 ; Ullrich von Bassewitz, 2004-04-05, 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
46 .byte MOUSE_FLAG_LATE_IRQ
48 ; Callback table, set by the kernel before INSTALL is called
50 CHIDE: jmp $0000 ; Hide the cursor
51 CSHOW: jmp $0000 ; Show the cursor
52 CPREP: jmp $0000 ; Prepare to move the cursor
53 CDRAW: jmp $0000 ; Draw the cursor
54 CMOVEX: jmp $0000 ; Move the cursor to X coord
55 CMOVEY: jmp $0000 ; Move the cursor to Y coord
58 ;----------------------------------------------------------------------------
72 ;----------------------------------------------------------------------------
73 ; Global variables. The bounding box values are sorted so that they can be
74 ; written with the least effort in the SETBOX and GETBOX routines, so don't
80 YPos: .res 2 ; Current mouse position, Y
81 XPos: .res 2 ; Current mouse position, X
82 XMin: .res 2 ; X1 value of bounding box
83 YMin: .res 2 ; Y1 value of bounding box
84 XMax: .res 2 ; X2 value of bounding box
85 YMax: .res 2 ; Y2 value of bounding box
86 Buttons: .res 1 ; Button mask
88 ; Temporary value used in the int handler
94 ; Default values for above variables
95 ; (We use ".proc" because we want to define both a label and a scope.)
98 .word SCREEN_HEIGHT/2 ; YPos
99 .word SCREEN_WIDTH/2 ; XPos
102 .word SCREEN_WIDTH ; XMax
103 .word SCREEN_HEIGHT ; YMax
109 ;----------------------------------------------------------------------------
110 ; INSTALL routine. Is called after the driver is loaded into memory. If
111 ; possible, check if the hardware is present.
112 ; Must return an MOUSE_ERR_xx code in a/x.
116 ; Initialize variables. Just copy the default stuff over
118 ldx #.sizeof(DefVars)-1
124 ; Be sure the mouse cursor is invisible and at the default location. We
125 ; need to do that here, because our mouse interrupt handler doesn't set the
126 ; mouse position if it hasn't changed.
138 ; Done, return zero (= MOUSE_ERR_OK)
144 ;----------------------------------------------------------------------------
145 ; UNINSTALL routine. Is called before the driver is removed from memory.
146 ; No return code required (the driver is removed from memory on return).
148 UNINSTALL = HIDE ; Hide cursor on exit
150 ;----------------------------------------------------------------------------
151 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
152 ; a counter for calls to show/hide, and the driver entry point is only called
153 ; if the mouse is currently visible and should get hidden. For most drivers,
154 ; no special action is required besides hiding the mouse cursor.
155 ; No return code required.
162 ;----------------------------------------------------------------------------
163 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
164 ; a counter for calls to show/hide, and the driver entry point is only called
165 ; if the mouse is currently hidden and should become visible. For most drivers,
166 ; no special action is required besides enabling the mouse cursor.
167 ; No return code required.
174 ;----------------------------------------------------------------------------
175 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
176 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
177 ; No checks are done if the mouse is currently inside the box, this is the job
178 ; of the caller. It is not necessary to validate the parameters, trust the
179 ; caller and save some code here. No return code required.
182 stx ptr1+1 ; Save data pointer
184 ldy #.sizeof (MOUSE_BOX)-1
195 ;----------------------------------------------------------------------------
196 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
197 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
200 stx ptr1+1 ; Save data pointer
202 ldy #.sizeof (MOUSE_BOX)-1
213 ;----------------------------------------------------------------------------
214 ; MOVE: Move the mouse to a new position. The position is passed as it comes
215 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
216 ; remove the parameter from the stack on return.
217 ; No checks are done if the new position is valid (within the bounding box or
218 ; the screen). No return code required.
221 MOVE: sei ; No interrupts
224 stx YPos+1 ; New Y position
233 sta XPos ; New X position
235 jsr CMOVEX ; Move the cursor
237 cli ; Allow interrupts
240 ;----------------------------------------------------------------------------
241 ; BUTTONS: Return the button mask in a/x.
248 ;----------------------------------------------------------------------------
249 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
250 ; No return code required.
252 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
254 sei ; Disable interrupts
255 lda XPos ; Transfer the position
264 cli ; Enable interrupts
267 sta (ptr1),y ; Store last byte
271 ;----------------------------------------------------------------------------
272 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
273 ; struct pointed to by ptr1. No return code required.
275 ; We're cheating here to keep the code smaller: The first fields of the
276 ; mouse_info struct are identical to the mouse_pos struct, so we will just
277 ; call _mouse_pos to initialize the struct pointer and fill the position
282 ; Fill in the button state
285 ldy #MOUSE_INFO::BUTTONS
290 ;----------------------------------------------------------------------------
291 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
292 ; specific data in ptr1, and the ioctl code in A.
293 ; Must return an error code in a/x.
296 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
297 ldx #>MOUSE_ERR_INV_IOCTL
300 ;----------------------------------------------------------------------------
301 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
302 ; (so be careful). The routine MUST return carry set if the interrupt has been
303 ; 'handled' - which means that the interrupt source is gone. Otherwise it
304 ; MUST return carry clear.
310 lda CIA1_PRB ; Read joystick #0
312 eor #$1F ; Make all bits active high
315 ; Check for a pressed button and place the result into Buttons
317 ldx #$00 ; Assume no button pressed
318 and #JOY::FIRE ; Check fire button
319 beq @L0 ; Jump if not pressed
320 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
325 lda Temp ; Read joystick #0
326 and #(JOY::LEFT | JOY::RIGHT)
329 ; We will cheat here and rely on the fact that either the left, OR the right
332 and #JOY::RIGHT ; Check RIGHT bit
336 bne @AddX ; Branch always
340 ; Calculate the new X coordinate (--> a/y)
343 tay ; Remember low byte
348 ; Limit the X coordinate to the bounding box
366 ; Move the mouse pointer to the new X pos
371 ; Calculate the Y movement vector
373 @SkipX: lda Temp ; Read joystick #0
374 and #(JOY::UP | JOY::DOWN) ; Check up/down
377 ; We will cheat here and rely on the fact that either the up, OR the down
388 ; Calculate the new Y coordinate (--> a/y)
391 tay ; Remember low byte
396 ; Limit the Y coordinate to the bounding box
414 ; Move the mouse pointer to the new X pos
422 clc ; Interrupt not "handled"