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 ;----------------------------------------------------------------------------
77 ; Global variables. The bounding box values are sorted so that they can be
78 ; written with the least effort in the SETBOX and GETBOX routines, so don't
84 YPos: .res 2 ; Current mouse position, Y
85 XPos: .res 2 ; Current mouse position, X
86 XMin: .res 2 ; X1 value of bounding box
87 YMin: .res 2 ; Y1 value of bounding box
88 XMax: .res 2 ; X2 value of bounding box
89 YMax: .res 2 ; Y2 value of bounding box
90 Buttons: .res 1 ; Button mask
94 ; Temporary value used in the int handler
98 ; Keyboard buffer fill level at start of interrupt
100 old_key_count: .res 1
102 ; original IRQ vector
108 ; Default values for above variables
109 ; (We use ".proc" because we want to define both a label and a scope.)
112 .word SCREEN_HEIGHT/2 ; YPos
113 .word SCREEN_WIDTH/2 ; XPos
116 .word SCREEN_WIDTH - 1 ; XMax
117 .word SCREEN_HEIGHT - 1 ; YMax
123 ;----------------------------------------------------------------------------
124 ; INSTALL routine. Is called after the driver is loaded into memory. If
125 ; possible, check if the hardware is present.
126 ; Must return an MOUSE_ERR_xx code in a/x.
130 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
131 ; That allows direct access to the VIC-IIe's sprite registers.
138 ; Initialize variables. Just copy the default stuff over
140 ldx #.sizeof(DefVars)-1
146 ; Be sure the mouse cursor is invisible and at the default location. We
147 ; need to do that here, because our mouse interrupt handler doesn't set the
148 ; mouse position if it hasn't changed.
159 ; Initialize our IRQ magic
161 ; remember ROM IRQ continuation address
172 ; set ROM IRQ continuation address to point to the provided routine
180 ; set address of our IRQ callback routine
181 ; since it's called via "rts" we have to use "address-1"
190 ; set ROM entry point vector
191 ; since it's called via "rts" we have to decrement it by one
202 ; Done, return zero (= MOUSE_ERR_OK)
208 ;----------------------------------------------------------------------------
209 ; UNINSTALL routine. Is called before the driver is removed from memory.
210 ; No return code required (the driver is removed from memory on return).
220 jsr HIDE ; Hide cursor on exit
225 ;----------------------------------------------------------------------------
226 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
227 ; a counter for calls to show/hide, and the driver entry point is only called
228 ; if the mouse is currently visible and should get hidden. For most drivers,
229 ; no special action is required besides hiding the mouse cursor.
230 ; No return code required.
237 ;----------------------------------------------------------------------------
238 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
239 ; a counter for calls to show/hide, and the driver entry point is only called
240 ; if the mouse is currently hidden and should become visible. For most drivers,
241 ; no special action is required besides enabling the mouse cursor.
242 ; No return code required.
249 ;----------------------------------------------------------------------------
250 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
251 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
252 ; No checks are done if the mouse is currently inside the box, this is the job
253 ; of the caller. It is not necessary to validate the parameters, trust the
254 ; caller and save some code here. No return code required.
257 stx ptr1+1 ; Save data pointer
259 ldy #.sizeof (MOUSE_BOX)-1
270 ;----------------------------------------------------------------------------
271 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
272 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
275 stx ptr1+1 ; Save data pointer
277 ldy #.sizeof (MOUSE_BOX)-1
288 ;----------------------------------------------------------------------------
289 ; MOVE: Move the mouse to a new position. The position is passed as it comes
290 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
291 ; remove the parameter from the stack on return.
292 ; No checks are done if the new position is valid (within the bounding box or
293 ; the screen). No return code required.
296 MOVE: sei ; No interrupts
299 stx YPos+1 ; New Y position
308 sta XPos ; New X position
310 jsr CMOVEX ; Move the cursor
312 cli ; Allow interrupts
315 ;----------------------------------------------------------------------------
316 ; BUTTONS: Return the button mask in a/x.
323 ;----------------------------------------------------------------------------
324 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
325 ; No return code required.
327 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
329 sei ; Disable interrupts
330 lda XPos ; Transfer the position
339 cli ; Enable interrupts
342 sta (ptr1),y ; Store last byte
346 ;----------------------------------------------------------------------------
347 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
348 ; struct pointed to by ptr1. No return code required.
350 ; We're cheating here to keep the code smaller: The first fields of the
351 ; mouse_info struct are identical to the mouse_pos struct, so we will just
352 ; call _mouse_pos to initialize the struct pointer and fill the position
357 ; Fill in the button state
360 ldy #MOUSE_INFO::BUTTONS
365 ;----------------------------------------------------------------------------
366 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
367 ; specific data in ptr1, and the ioctl code in A.
368 ; Must return an error code in a/x.
371 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
372 ldx #>MOUSE_ERR_INV_IOCTL
375 ;----------------------------------------------------------------------------
376 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
377 ; (so be careful). The routine MUST return carry set if the interrupt has been
378 ; 'handled' - which means that the interrupt source is gone. Otherwise it
379 ; MUST return carry clear.
387 lda CIA1_PRB ; Read joystick #0
389 eor #$1F ; Make all bits active high
392 ; Check for a pressed button and place the result into Buttons
394 ldx #$00 ; Assume no button pressed
395 and #JOY::FIRE ; Check fire button
396 beq @L0 ; Jump if not pressed
397 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
402 lda Temp ; Read joystick #0
403 and #(JOY::LEFT | JOY::RIGHT)
406 ; We will cheat here and rely on the fact that either the left, OR the right
409 and #JOY::RIGHT ; Check RIGHT bit
413 bne @AddX ; Branch always
417 ; Calculate the new X coordinate (--> a/y)
420 tay ; Remember low byte
425 ; Limit the X coordinate to the bounding box
443 ; Move the mouse pointer to the new X pos
448 ; Calculate the Y movement vector
450 @SkipX: lda Temp ; Read joystick #0
451 and #(JOY::UP | JOY::DOWN) ; Check up/down
454 ; We will cheat here and rely on the fact that either the up, OR the down
465 ; Calculate the new Y coordinate (--> a/y)
468 tay ; Remember low byte
473 ; Limit the Y coordinate to the bounding box
491 ; Move the mouse pointer to the new X pos
499 clc ; Interrupt not "handled"
502 .define OLD_BUTTONS Temp ; tells callback.inc where the old port status is stored
503 .include "callback.inc"