2 ; Driver for a "joystick mouse".
4 ; 2009-09-26, Ullrich von Bassewitz
5 ; 2014-03-15, Greg King
8 .include "zeropage.inc"
9 .include "mouse-kernel.inc"
14 ; ------------------------------------------------------------------------
15 ; Header. Includes jump table
23 .byte $6d, $6f, $75 ; "mou"
24 .byte MOUSE_API_VERSION ; Mouse driver API version number
47 .byte MOUSE_FLAG_LATE_IRQ
49 ; Callback table, set by the kernel before INSTALL is called
51 CHIDE: jmp $0000 ; Hide the cursor
52 CSHOW: jmp $0000 ; Show the cursor
53 CPREP: jmp $0000 ; Prepare to move the cursor
54 CDRAW: jmp $0000 ; Draw the cursor
55 CMOVEX: jmp $0000 ; Move the cursor to X coord
56 CMOVEY: jmp $0000 ; Move the cursor to Y coord
59 ;----------------------------------------------------------------------------
73 ;----------------------------------------------------------------------------
74 ; Global variables. The bounding box values are sorted so that they can be
75 ; written with the least effort in the SETBOX and GETBOX routines, so don't
81 YPos: .res 2 ; Current mouse position, Y
82 XPos: .res 2 ; Current mouse position, X
83 XMin: .res 2 ; X1 value of bounding box
84 YMin: .res 2 ; Y1 value of bounding box
85 XMax: .res 2 ; X2 value of bounding box
86 YMax: .res 2 ; Y2 value of bounding box
87 Buttons: .res 1 ; Button mask
91 ; Temporary value used in the int handler
97 ; Default values for above variables
98 ; (We use ".proc" because we want to define both a label and a scope.)
101 .word SCREEN_HEIGHT/2 ; YPos
102 .word SCREEN_WIDTH/2 ; XPos
105 .word SCREEN_WIDTH ; XMax
106 .word SCREEN_HEIGHT ; YMax
112 ;----------------------------------------------------------------------------
113 ; INSTALL routine. Is called after the driver is loaded into memory. If
114 ; possible, check if the hardware is present.
115 ; Must return an MOUSE_ERR_xx code in a/x.
119 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
120 ; That allows direct access to the VIC-IIe's sprite registers.
127 ; Initialize variables. Just copy the default stuff over
129 ldx #.sizeof(DefVars)-1
135 ; Be sure the mouse cursor is invisible and at the default location. We
136 ; need to do that here, because our mouse interrupt handler doesn't set the
137 ; mouse position if it hasn't changed.
149 ; Done, return zero (= MOUSE_ERR_OK)
155 ;----------------------------------------------------------------------------
156 ; UNINSTALL routine. Is called before the driver is removed from memory.
157 ; No return code required (the driver is removed from memory on return).
160 jsr HIDE ; Hide cursor on exit
165 ;----------------------------------------------------------------------------
166 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
167 ; a counter for calls to show/hide, and the driver entry point is only called
168 ; if the mouse is currently visible and should get hidden. For most drivers,
169 ; no special action is required besides hiding the mouse cursor.
170 ; No return code required.
177 ;----------------------------------------------------------------------------
178 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
179 ; a counter for calls to show/hide, and the driver entry point is only called
180 ; if the mouse is currently hidden and should become visible. For most drivers,
181 ; no special action is required besides enabling the mouse cursor.
182 ; No return code required.
189 ;----------------------------------------------------------------------------
190 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
191 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
192 ; No checks are done if the mouse is currently inside the box, this is the job
193 ; of the caller. It is not necessary to validate the parameters, trust the
194 ; caller and save some code here. No return code required.
197 stx ptr1+1 ; Save data pointer
199 ldy #.sizeof (MOUSE_BOX)-1
210 ;----------------------------------------------------------------------------
211 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
212 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
215 stx ptr1+1 ; Save data pointer
217 ldy #.sizeof (MOUSE_BOX)-1
228 ;----------------------------------------------------------------------------
229 ; MOVE: Move the mouse to a new position. The position is passed as it comes
230 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
231 ; remove the parameter from the stack on return.
232 ; No checks are done if the new position is valid (within the bounding box or
233 ; the screen). No return code required.
236 MOVE: sei ; No interrupts
239 stx YPos+1 ; New Y position
248 sta XPos ; New X position
250 jsr CMOVEX ; Move the cursor
252 cli ; Allow interrupts
255 ;----------------------------------------------------------------------------
256 ; BUTTONS: Return the button mask in a/x.
263 ;----------------------------------------------------------------------------
264 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
265 ; No return code required.
267 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
269 sei ; Disable interrupts
270 lda XPos ; Transfer the position
279 cli ; Enable interrupts
282 sta (ptr1),y ; Store last byte
286 ;----------------------------------------------------------------------------
287 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
288 ; struct pointed to by ptr1. No return code required.
290 ; We're cheating here to keep the code smaller: The first fields of the
291 ; mouse_info struct are identical to the mouse_pos struct, so we will just
292 ; call _mouse_pos to initialize the struct pointer and fill the position
297 ; Fill in the button state
300 ldy #MOUSE_INFO::BUTTONS
305 ;----------------------------------------------------------------------------
306 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
307 ; specific data in ptr1, and the ioctl code in A.
308 ; Must return an error code in a/x.
311 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
312 ldx #>MOUSE_ERR_INV_IOCTL
315 ;----------------------------------------------------------------------------
316 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
317 ; (so be careful). The routine MUST return carry set if the interrupt has been
318 ; 'handled' - which means that the interrupt source is gone. Otherwise it
319 ; MUST return carry clear.
325 lda CIA1_PRB ; Read joystick #0
327 eor #$1F ; Make all bits active high
330 ; Check for a pressed button and place the result into Buttons
332 ldx #$00 ; Assume no button pressed
333 and #JOY::FIRE ; Check fire button
334 beq @L0 ; Jump if not pressed
335 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
340 lda Temp ; Read joystick #0
341 and #(JOY::LEFT | JOY::RIGHT)
344 ; We will cheat here and rely on the fact that either the left, OR the right
347 and #JOY::RIGHT ; Check RIGHT bit
351 bne @AddX ; Branch always
355 ; Calculate the new X coordinate (--> a/y)
358 tay ; Remember low byte
363 ; Limit the X coordinate to the bounding box
381 ; Move the mouse pointer to the new X pos
386 ; Calculate the Y movement vector
388 @SkipX: lda Temp ; Read joystick #0
389 and #(JOY::UP | JOY::DOWN) ; Check up/down
392 ; We will cheat here and rely on the fact that either the up, OR the down
403 ; Calculate the new Y coordinate (--> a/y)
406 tay ; Remember low byte
411 ; Limit the Y coordinate to the bounding box
429 ; Move the mouse pointer to the new X pos
437 clc ; Interrupt not "handled"