2 ; Driver for a "joystick mouse".
4 ; 2009-09-26, Ullrich von Bassewitz
5 ; 2014-03-17, Greg King
8 .include "zeropage.inc"
9 .include "mouse-kernel.inc"
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table
25 .byte $6d, $6f, $75 ; "mou"
26 .byte MOUSE_API_VERSION ; Mouse driver API version number
50 .byte MOUSE_FLAG_LATE_IRQ
52 ; Callback table, set by the kernel before INSTALL is called
54 CHIDE: jmp $0000 ; Hide the cursor
55 CSHOW: jmp $0000 ; Show the cursor
56 CPREP: jmp $0000 ; Prepare to move the cursor
57 CDRAW: jmp $0000 ; Draw the cursor
58 CMOVEX: jmp $0000 ; Move the cursor to X coord
59 CMOVEY: jmp $0000 ; Move the cursor to Y coord
62 ;----------------------------------------------------------------------------
76 ;----------------------------------------------------------------------------
82 .byte $4c ; JMP opcode
83 .word 0 ; pointer to ROM IRQ handler (will be set at runtime)
85 ;----------------------------------------------------------------------------
86 ; Global variables. The bounding box values are sorted so that they can be
87 ; written with the least effort in the SETBOX and GETBOX routines, so don't
93 YPos: .res 2 ; Current mouse position, Y
94 XPos: .res 2 ; Current mouse position, X
95 XMin: .res 2 ; X1 value of bounding box
96 YMin: .res 2 ; Y1 value of bounding box
97 XMax: .res 2 ; X2 value of bounding box
98 YMax: .res 2 ; Y2 value of bounding box
99 Buttons: .res 1 ; Button mask
103 ; Temporary value used in the int handler
107 ; Keyboard buffer fill level at start of interrupt
109 old_key_count: .res 1
113 ; Default values for above variables
114 ; (We use ".proc" because we want to define both a label and a scope.)
117 .word SCREEN_HEIGHT/2 ; YPos
118 .word SCREEN_WIDTH/2 ; XPos
121 .word SCREEN_WIDTH - 1 ; XMax
122 .word SCREEN_HEIGHT - 1 ; YMax
128 ;----------------------------------------------------------------------------
129 ; INSTALL routine. Is called after the driver is loaded into memory. If
130 ; possible, check if the hardware is present.
131 ; Must return an MOUSE_ERR_xx code in a/x.
135 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
136 ; That allows direct access to the VIC-IIe's sprite registers.
143 ; Initialize variables. Just copy the default stuff over
145 ldx #.sizeof(DefVars)-1
151 ; Be sure the mouse cursor is invisible and at the default location. We
152 ; need to do that here, because our mouse interrupt handler doesn't set the
153 ; mouse position if it hasn't changed.
165 ; Initialize our IRQ magic
194 ; Done, return zero (= MOUSE_ERR_OK)
200 ;----------------------------------------------------------------------------
201 ; UNINSTALL routine. Is called before the driver is removed from memory.
202 ; No return code required (the driver is removed from memory on return).
211 jsr HIDE ; Hide cursor on exit
216 ;----------------------------------------------------------------------------
217 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
218 ; a counter for calls to show/hide, and the driver entry point is only called
219 ; if the mouse is currently visible and should get hidden. For most drivers,
220 ; no special action is required besides hiding the mouse cursor.
221 ; No return code required.
228 ;----------------------------------------------------------------------------
229 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
230 ; a counter for calls to show/hide, and the driver entry point is only called
231 ; if the mouse is currently hidden and should become visible. For most drivers,
232 ; no special action is required besides enabling the mouse cursor.
233 ; No return code required.
240 ;----------------------------------------------------------------------------
241 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
242 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
243 ; No checks are done if the mouse is currently inside the box, this is the job
244 ; of the caller. It is not necessary to validate the parameters, trust the
245 ; caller and save some code here. No return code required.
248 stx ptr1+1 ; Save data pointer
250 ldy #.sizeof (MOUSE_BOX)-1
261 ;----------------------------------------------------------------------------
262 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
263 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
266 stx ptr1+1 ; Save data pointer
268 ldy #.sizeof (MOUSE_BOX)-1
279 ;----------------------------------------------------------------------------
280 ; MOVE: Move the mouse to a new position. The position is passed as it comes
281 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
282 ; remove the parameter from the stack on return.
283 ; No checks are done if the new position is valid (within the bounding box or
284 ; the screen). No return code required.
287 MOVE: sei ; No interrupts
290 stx YPos+1 ; New Y position
299 sta XPos ; New X position
301 jsr CMOVEX ; Move the cursor
303 cli ; Allow interrupts
306 ;----------------------------------------------------------------------------
307 ; BUTTONS: Return the button mask in a/x.
314 ;----------------------------------------------------------------------------
315 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
316 ; No return code required.
318 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
320 sei ; Disable interrupts
321 lda XPos ; Transfer the position
330 cli ; Enable interrupts
333 sta (ptr1),y ; Store last byte
337 ;----------------------------------------------------------------------------
338 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
339 ; struct pointed to by ptr1. No return code required.
341 ; We're cheating here to keep the code smaller: The first fields of the
342 ; mouse_info struct are identical to the mouse_pos struct, so we will just
343 ; call _mouse_pos to initialize the struct pointer and fill the position
348 ; Fill in the button state
351 ldy #MOUSE_INFO::BUTTONS
356 ;----------------------------------------------------------------------------
357 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
358 ; specific data in ptr1, and the ioctl code in A.
359 ; Must return an error code in a/x.
362 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
363 ldx #>MOUSE_ERR_INV_IOCTL
366 ;----------------------------------------------------------------------------
367 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
368 ; (so be careful). The routine MUST return carry set if the interrupt has been
369 ; 'handled' - which means that the interrupt source is gone. Otherwise it
370 ; MUST return carry clear.
378 lda CIA1_PRB ; Read joystick #0
380 eor #$1F ; Make all bits active high
383 ; Check for a pressed button and place the result into Buttons
385 ldx #$00 ; Assume no button pressed
386 and #JOY::FIRE ; Check fire button
387 beq @L0 ; Jump if not pressed
388 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
393 lda Temp ; Read joystick #0
394 and #(JOY::LEFT | JOY::RIGHT)
397 ; We will cheat here and rely on the fact that either the left, OR the right
400 and #JOY::RIGHT ; Check RIGHT bit
404 bne @AddX ; Branch always
408 ; Calculate the new X coordinate (--> a/y)
411 tay ; Remember low byte
416 ; Limit the X coordinate to the bounding box
434 ; Move the mouse pointer to the new X pos
439 ; Calculate the Y movement vector
441 @SkipX: lda Temp ; Read joystick #0
442 and #(JOY::UP | JOY::DOWN) ; Check up/down
445 ; We will cheat here and rely on the fact that either the up, OR the down
456 ; Calculate the new Y coordinate (--> a/y)
459 tay ; Remember low byte
464 ; Limit the Y coordinate to the bounding box
482 ; Move the mouse pointer to the new X pos
490 clc ; Interrupt not "handled"
493 ;----------------------------------------------------------------------------
494 ; Called after ROM IRQ handler has been run.
495 ; Check if there was joystick activity before and/or after the ROM handler.
496 ; If there was activity, discard the key presses since they are most
497 ; probably "phantom" key presses.
504 lda Temp ; keypress before?
505 bne @discard_key ; yes, discard key
509 lda CIA1_PRB ; Read joystick #0
511 eor #$1F ; keypress after
512 beq @nokey ; no, probably a real key press
515 stx KEY_COUNT ; set old keyboard buffer fill level