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.
164 ; 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).
212 jsr HIDE ; Hide cursor on exit
217 ;----------------------------------------------------------------------------
218 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
219 ; a counter for calls to show/hide, and the driver entry point is only called
220 ; if the mouse is currently visible and should get hidden. For most drivers,
221 ; no special action is required besides hiding the mouse cursor.
222 ; No return code required.
229 ;----------------------------------------------------------------------------
230 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
231 ; a counter for calls to show/hide, and the driver entry point is only called
232 ; if the mouse is currently hidden and should become visible. For most drivers,
233 ; no special action is required besides enabling the mouse cursor.
234 ; No return code required.
241 ;----------------------------------------------------------------------------
242 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
243 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
244 ; No checks are done if the mouse is currently inside the box, this is the job
245 ; of the caller. It is not necessary to validate the parameters, trust the
246 ; caller and save some code here. No return code required.
249 stx ptr1+1 ; Save data pointer
251 ldy #.sizeof (MOUSE_BOX)-1
262 ;----------------------------------------------------------------------------
263 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
264 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
267 stx ptr1+1 ; Save data pointer
269 ldy #.sizeof (MOUSE_BOX)-1
280 ;----------------------------------------------------------------------------
281 ; MOVE: Move the mouse to a new position. The position is passed as it comes
282 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
283 ; remove the parameter from the stack on return.
284 ; No checks are done if the new position is valid (within the bounding box or
285 ; the screen). No return code required.
288 MOVE: sei ; No interrupts
291 stx YPos+1 ; New Y position
300 sta XPos ; New X position
302 jsr CMOVEX ; Move the cursor
304 cli ; Allow interrupts
307 ;----------------------------------------------------------------------------
308 ; BUTTONS: Return the button mask in a/x.
315 ;----------------------------------------------------------------------------
316 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
317 ; No return code required.
319 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
321 sei ; Disable interrupts
322 lda XPos ; Transfer the position
331 cli ; Enable interrupts
334 sta (ptr1),y ; Store last byte
338 ;----------------------------------------------------------------------------
339 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
340 ; struct pointed to by ptr1. No return code required.
342 ; We're cheating here to keep the code smaller: The first fields of the
343 ; mouse_info struct are identical to the mouse_pos struct, so we will just
344 ; call _mouse_pos to initialize the struct pointer and fill the position
349 ; Fill in the button state
352 ldy #MOUSE_INFO::BUTTONS
357 ;----------------------------------------------------------------------------
358 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
359 ; specific data in ptr1, and the ioctl code in A.
360 ; Must return an error code in a/x.
363 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
364 ldx #>MOUSE_ERR_INV_IOCTL
367 ;----------------------------------------------------------------------------
368 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
369 ; (so be careful). The routine MUST return carry set if the interrupt has been
370 ; 'handled' - which means that the interrupt source is gone. Otherwise it
371 ; MUST return carry clear.
379 lda CIA1_PRB ; Read joystick #0
381 eor #$1F ; Make all bits active high
384 ; Check for a pressed button and place the result into Buttons
386 ldx #$00 ; Assume no button pressed
387 and #JOY::FIRE ; Check fire button
388 beq @L0 ; Jump if not pressed
389 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
394 lda Temp ; Read joystick #0
395 and #(JOY::LEFT | JOY::RIGHT)
398 ; We will cheat here and rely on the fact that either the left, OR the right
401 and #JOY::RIGHT ; Check RIGHT bit
405 bne @AddX ; Branch always
409 ; Calculate the new X coordinate (--> a/y)
412 tay ; Remember low byte
417 ; Limit the X coordinate to the bounding box
435 ; Move the mouse pointer to the new X pos
440 ; Calculate the Y movement vector
442 @SkipX: lda Temp ; Read joystick #0
443 and #(JOY::UP | JOY::DOWN) ; Check up/down
446 ; We will cheat here and rely on the fact that either the up, OR the down
457 ; Calculate the new Y coordinate (--> a/y)
460 tay ; Remember low byte
465 ; Limit the Y coordinate to the bounding box
483 ; Move the mouse pointer to the new X pos
491 clc ; Interrupt not "handled"
494 ;----------------------------------------------------------------------------
495 ; Called after ROM IRQ handler has been run.
496 ; Check if there was joystick activity before and/or after the ROM handler.
497 ; If there was activity, discard the key presses since they are most
498 ; probably "phantom" key presses.
505 lda Temp ; keypress before?
506 bne @discard_key ; yes, discard key
510 lda CIA1_PRB ; Read joystick #0
512 eor #$1F ; keypress after
513 beq @nokey ; no, probably a real key press
516 stx KEY_COUNT ; set old keyboard buffer fill level