2 ; Driver for a "joystick mouse".
4 ; Ullrich von Bassewitz, 2004-03-29
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
41 .byte MOUSE_FLAG_LATE_IRQ
43 ; Callback table, set by the kernel before INSTALL is called
45 CHIDE: jmp $0000 ; Hide the cursor
46 CSHOW: jmp $0000 ; Show the cursor
47 CMOVEX: jmp $0000 ; Move the cursor to X coord
48 CMOVEY: jmp $0000 ; Move the cursor to Y coord
51 ;----------------------------------------------------------------------------
65 ;----------------------------------------------------------------------------
66 ; Global variables. The bounding box values are sorted so that they can be
67 ; written with the least effort in the BOX routine, so don't reorder them.
72 YPos: .res 2 ; Current mouse position, Y
73 XPos: .res 2 ; Current mouse position, X
74 YMax: .res 2 ; Y2 value of bounding box
75 XMax: .res 2 ; X2 value of bounding box
76 YMin: .res 2 ; Y1 value of bounding box
77 XMin: .res 2 ; X1 value of bounding box
78 Buttons: .res 1 ; Button mask
80 ; Temporary value used in the int handler
84 ; Default values for above variables
89 .word SCREEN_HEIGHT/2 ; YPos
90 .word SCREEN_WIDTH/2 ; XPos
91 .word SCREEN_HEIGHT ; YMax
92 .word SCREEN_WIDTH ; XMax
100 ;----------------------------------------------------------------------------
101 ; INSTALL routine. Is called after the driver is loaded into memory. If
102 ; possible, check if the hardware is present.
103 ; Must return an MOUSE_ERR_xx code in a/x.
107 ; Initialize variables. Just copy the default stuff over
109 ldx #.sizeof(DefVars)-1
115 ; Be sure the mouse cursor is invisible and at the default location. We
116 ; need to do that here, because our mouse interrupt handler doesn't set the
117 ; mouse position if it hasn't changed.
129 ; Done, return zero (= MOUSE_ERR_OK)
135 ;----------------------------------------------------------------------------
136 ; UNINSTALL routine. Is called before the driver is removed from memory.
137 ; No return code required (the driver is removed from memory on return).
139 UNINSTALL = HIDE ; Hide cursor on exit
141 ;----------------------------------------------------------------------------
142 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
143 ; a counter for calls to show/hide, and the driver entry point is only called
144 ; if the mouse is currently visible and should get hidden. For most drivers,
145 ; no special action is required besides hiding the mouse cursor.
146 ; No return code required.
153 ;----------------------------------------------------------------------------
154 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
155 ; a counter for calls to show/hide, and the driver entry point is only called
156 ; if the mouse is currently hidden and should become visible. For most drivers,
157 ; no special action is required besides enabling the mouse cursor.
158 ; No return code required.
165 ;----------------------------------------------------------------------------
166 ; BOX: Set the mouse bounding box. The parameters are passed as they come from
167 ; the C program, that is, maxy in a/x and the other parameters on the stack.
168 ; The C wrapper will remove the parameters from the stack when the driver
170 ; No checks are done if the mouse is currently inside the box, this is the job
171 ; of the caller. It is not necessary to validate the parameters, trust the
172 ; caller and save some code here. No return code required.
187 ;----------------------------------------------------------------------------
188 ; MOVE: Move the mouse to a new position. The position is passed as it comes
189 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
190 ; remove the parameter from the stack on return.
191 ; No checks are done if the new position is valid (within the bounding box or
192 ; the screen). No return code required.
195 MOVE: sei ; No interrupts
198 stx YPos+1 ; New Y position
207 sta XPos ; New X position
209 jsr CMOVEX ; Move the cursor
211 cli ; Allow interrupts
214 ;----------------------------------------------------------------------------
215 ; BUTTONS: Return the button mask in a/x.
222 ;----------------------------------------------------------------------------
223 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
224 ; No return code required.
226 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
228 sei ; Disable interrupts
229 lda XPos ; Transfer the position
238 cli ; Enable interrupts
241 sta (ptr1),y ; Store last byte
245 ;----------------------------------------------------------------------------
246 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
247 ; struct pointed to by ptr1. No return code required.
249 ; We're cheating here to keep the code smaller: The first fields of the
250 ; mouse_info struct are identical to the mouse_pos struct, so we will just
251 ; call _mouse_pos to initialize the struct pointer and fill the position
256 ; Fill in the button state
259 ldy #MOUSE_INFO::BUTTONS
264 ;----------------------------------------------------------------------------
265 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
266 ; specific data in ptr1, and the ioctl code in A.
267 ; Must return an error code in a/x.
270 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
271 ldx #>MOUSE_ERR_INV_IOCTL
274 ;----------------------------------------------------------------------------
275 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
276 ; (so be careful). The routine MUST return carry set if the interrupt has been
277 ; 'handled' - which means that the interrupt source is gone. Otherwise it
278 ; MUST return carry clear.
283 lda CIA1_PRB ; Read joystick #0
285 eor #$1F ; Make all bits active high
288 ; Check for a pressed button and place the result into Buttons
290 ldx #$00 ; Assume no button pressed
291 and #JOY::FIRE ; Check fire button
292 beq @L0 ; Jump if not pressed
293 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
298 lda Temp ; Read joystick #0
299 and #(JOY::LEFT | JOY::RIGHT)
302 ; We will cheat here and rely on the fact that either the left, OR the right
305 and #JOY::RIGHT ; Check RIGHT bit
309 bne @AddX ; Branch always
313 ; Calculate the new X coordinate (--> a/y)
316 tay ; Remember low byte
321 ; Limit the X coordinate to the bounding box
339 ; Move the mouse pointer to the new X pos
344 ; Calculate the Y movement vector
346 @SkipX: lda Temp ; Read joystick #0
347 and #(JOY::UP | JOY::DOWN) ; Check up/down
350 ; We will cheat here and rely on the fact that either the up, OR the down
361 ; Calculate the new Y coordinate (--> a/y)
364 tay ; Remember low byte
369 ; Limit the Y coordinate to the bounding box
387 ; Move the mouse pointer to the new X pos
394 @SkipY: clc ; Interrupt not handled