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
39 ; Callback table, set by the kernel before INSTALL is called
41 CHIDE: jmp $0000 ; Hide the cursor
42 CSHOW: jmp $0000 ; Show the cursor
43 CMOVEX: jmp $0000 ; Move the cursor to X coord
44 CMOVEY: jmp $0000 ; Move the cursor to Y coord
47 ;----------------------------------------------------------------------------
61 ;----------------------------------------------------------------------------
62 ; Global variables. The bounding box values are sorted so that they can be
63 ; written with the least effort in the BOX routine, so don't reorder them.
68 YPos: .res 2 ; Current mouse position, Y
69 XPos: .res 2 ; Current mouse position, X
70 YMax: .res 2 ; Y2 value of bounding box
71 XMax: .res 2 ; X2 value of bounding box
72 YMin: .res 2 ; Y1 value of bounding box
73 XMin: .res 2 ; X1 value of bounding box
74 Buttons: .res 1 ; Button mask
76 ; Temporary value used in the int handler
80 ; Default values for above variables
85 .word SCREEN_HEIGHT/2 ; YPos
86 .word SCREEN_WIDTH/2 ; XPos
87 .word SCREEN_HEIGHT ; YMax
88 .word SCREEN_WIDTH ; XMax
96 ;----------------------------------------------------------------------------
97 ; INSTALL routine. Is called after the driver is loaded into memory. If
98 ; possible, check if the hardware is present.
99 ; Must return an MOUSE_ERR_xx code in a/x.
103 ; Initialize variables. Just copy the default stuff over
105 ldx #.sizeof(DefVars)-1
111 ; Be sure the mouse cursor is invisible and at the default location. We
112 ; need to do that here, because our mouse interrupt handler doesn't set the
113 ; mouse position if it hasn't changed.
125 ; Done, return zero (= MOUSE_ERR_OK)
131 ;----------------------------------------------------------------------------
132 ; UNINSTALL routine. Is called before the driver is removed from memory.
133 ; No return code required (the driver is removed from memory on return).
135 UNINSTALL = HIDE ; Hide cursor on exit
137 ;----------------------------------------------------------------------------
138 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
139 ; a counter for calls to show/hide, and the driver entry point is only called
140 ; if the mouse is currently visible and should get hidden. For most drivers,
141 ; no special action is required besides hiding the mouse cursor.
142 ; No return code required.
149 ;----------------------------------------------------------------------------
150 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
151 ; a counter for calls to show/hide, and the driver entry point is only called
152 ; if the mouse is currently hidden and should become visible. For most drivers,
153 ; no special action is required besides enabling the mouse cursor.
154 ; No return code required.
161 ;----------------------------------------------------------------------------
162 ; BOX: Set the mouse bounding box. The parameters are passed as they come from
163 ; the C program, that is, maxy in a/x and the other parameters on the stack.
164 ; The C wrapper will remove the parameters from the stack when the driver
166 ; No checks are done if the mouse is currently inside the box, this is the job
167 ; of the caller. It is not necessary to validate the parameters, trust the
168 ; caller and save some code here. No return code required.
183 ;----------------------------------------------------------------------------
184 ; MOVE: Move the mouse to a new position. The position is passed as it comes
185 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
186 ; remove the parameter from the stack on return.
187 ; No checks are done if the new position is valid (within the bounding box or
188 ; the screen). No return code required.
191 MOVE: sei ; No interrupts
194 stx YPos+1 ; New Y position
203 sta XPos ; New X position
205 jsr CMOVEX ; Move the cursor
207 cli ; Allow interrupts
210 ;----------------------------------------------------------------------------
211 ; BUTTONS: Return the button mask in a/x.
218 ;----------------------------------------------------------------------------
219 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
220 ; No return code required.
222 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
224 sei ; Disable interrupts
225 lda XPos ; Transfer the position
234 cli ; Enable interrupts
237 sta (ptr1),y ; Store last byte
241 ;----------------------------------------------------------------------------
242 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
243 ; struct pointed to by ptr1. No return code required.
245 ; We're cheating here to keep the code smaller: The first fields of the
246 ; mouse_info struct are identical to the mouse_pos struct, so we will just
247 ; call _mouse_pos to initialize the struct pointer and fill the position
252 ; Fill in the button state
255 ldy #MOUSE_INFO::BUTTONS
260 ;----------------------------------------------------------------------------
261 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
262 ; specific data in ptr1, and the ioctl code in A.
263 ; Must return an error code in a/x.
266 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
267 ldx #>MOUSE_ERR_INV_IOCTL
270 ;----------------------------------------------------------------------------
271 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
277 lda CIA1_PRB ; Read joystick #0
279 eor #$1F ; Make all bits active high
282 ; Check for a pressed button and place the result into Buttons
284 ldx #$00 ; Assume no button pressed
285 and #JOY::FIRE ; Check fire button
286 beq @L0 ; Jump if not pressed
287 ldx #MOUSE_BTN_LEFT ; Left (only) button is pressed
292 lda Temp ; Read joystick #0
293 and #(JOY::LEFT | JOY::RIGHT)
296 ; We will cheat here and rely on the fact that either the left, OR the right
299 and #JOY::RIGHT ; Check RIGHT bit
303 bne @AddX ; Branch always
307 ; Calculate the new X coordinate (--> a/y)
310 tay ; Remember low byte
315 ; Limit the X coordinate to the bounding box
333 ; Move the mouse pointer to the new X pos
338 ; Calculate the Y movement vector
340 @SkipX: lda Temp ; Read joystick #0
341 and #(JOY::UP | JOY::DOWN) ; Check up/down
344 ; We will cheat here and rely on the fact that either the up, OR the down
355 ; Calculate the new Y coordinate (--> a/y)
358 tay ; Remember low byte
363 ; Limit the Y coordinate to the bounding box
381 ; Move the mouse pointer to the new X pos