2 ; Driver for a "joystick mouse".
4 ; 2009-09-26, Ullrich von Bassewitz
5 ; 2014-09-10, Greg King
8 .include "zeropage.inc"
9 .include "../extzp.inc"
11 .include "mouse-kernel.inc"
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table
21 module_header _cbm510_joy_mou
27 .byte $6d, $6f, $75 ; ASCII "mou"
28 .byte MOUSE_API_VERSION ; Mouse driver API version number
51 .byte MOUSE_FLAG_LATE_IRQ
53 ; Callback table, set by the kernel before INSTALL is called.
55 CHIDE: jmp $0000 ; Hide the cursor
56 CSHOW: jmp $0000 ; Show the cursor
57 CPREP: jmp $0000 ; Prepare to move the cursor
58 CDRAW: jmp $0000 ; Draw the cursor
59 CMOVEX: jmp $0000 ; Move the cursor to x co-ord.
60 CMOVEY: jmp $0000 ; Move the cursor to y co-ord.
63 ;----------------------------------------------------------------------------
66 SCREEN_HEIGHT = YSIZE * 8
67 SCREEN_WIDTH = XSIZE * 8
77 ;----------------------------------------------------------------------------
78 ; Global variables. The bounding box values are sorted so that they can be
79 ; written with the least effort in the SETBOX and GETBOX routines; so, don't
85 YPos: .res 2 ; Current mouse position, y
86 XPos: .res 2 ; Current mouse position, x
87 XMin: .res 2 ; X1 value of bounding box
88 YMin: .res 2 ; Y1 value of bounding box
89 XMax: .res 2 ; X2 value of bounding box
90 YMax: .res 2 ; Y2 value of bounding box
92 ; Temporary value used in the interrupt handler
98 ; Default values for above variables
99 ; (We use ".proc" because we want to define both a label and a scope.)
102 .word SCREEN_HEIGHT / 2 ; YPos
103 .word SCREEN_WIDTH / 2 ; XPos
106 .word SCREEN_WIDTH - 1 ; XMax
107 .word SCREEN_HEIGHT - 1 ; YMax
112 ;----------------------------------------------------------------------------
113 ; INSTALL routine. Is called after the driver is loaded into memory. If
114 ; possible, check if the hardware is present.
115 ; Must return a MOUSE_ERR_xx code in .XA.
119 ; Initiate variables; just copy the default stuff over.
121 ldx #.sizeof(DefVars) - 1
127 ; Be sure the mouse cursor is invisible and at the default location. We
128 ; need to do that here because our mouse interrupt handler doesn't set the
129 ; mouse position if it hasn't changed.
147 ;----------------------------------------------------------------------------
148 ; UNINSTALL routine. Is called before the driver is removed from memory.
149 ; No return code required (the driver is removed from memory, on return).
151 UNINSTALL := HIDE ; Hide cursor, on exit
153 ;----------------------------------------------------------------------------
154 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
155 ; a counter for calls to show/hide; and, the driver entry point is called only
156 ; if the mouse currently is visible and should get hidden. For most drivers,
157 ; no special action is required besides hiding the mouse cursor.
158 ; No return code required.
165 ;----------------------------------------------------------------------------
166 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
167 ; a counter for calls to show/hide; and, the driver entry point is called only
168 ; if the mouse currently is hidden and should become visible. For most drivers,
169 ; no special action is required besides enabling the mouse cursor.
170 ; No return code required.
177 ;----------------------------------------------------------------------------
178 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
179 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
180 ; No checks are done if the mouse currently is inside the box; that is the job
181 ; of the caller. It is not necessary to validate the parameters; trust the
182 ; caller, and save some code here. No return code required.
185 stx ptr1+1 ; Save data pointer
187 ldy #.sizeof (MOUSE_BOX) - 1
198 ;----------------------------------------------------------------------------
199 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
200 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
203 stx ptr1+1 ; Save data pointer
205 ldy #.sizeof (MOUSE_BOX) - 1
214 ;----------------------------------------------------------------------------
215 ; MOVE: Move the mouse to a new position. The position is passed as it comes
216 ; from the C program, that is: x on the stack and y in .XA. The C wrapper will
217 ; remove the parameter from the stack, on return.
218 ; No checks are done if the new position is valid (within the bounding box or
219 ; the screen). No return code required.
222 MOVE: sei ; No interrupts
224 jsr MoveY ; Set new y position
232 jsr MoveX ; Move the pointer
234 cli ; Allow interrupts
237 ;----------------------------------------------------------------------------
238 ; BUTTONS: Return the button mask in .XA.
239 ; Joystick 2's fire button is the left mouse button.
240 ; Joystick 1's fire button is the right mouse button.
243 ldx #15 ; Switch to the system bank
246 ; Get the fire-button bits
251 ; Switch back to the execution bank.
256 ; Joystick 2, fire button is in bit 7.
257 ; Joystick 1, fire button is in bit 6.
260 asl a ; Move bits 7,6 to bits 1,0
263 adc #%00001110 ; Move bit 1 to bit 4
264 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
266 ; Bits go up when buttons go down.
268 eor #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
272 ;----------------------------------------------------------------------------
273 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
274 ; struct pointed to by ptr1. No return code required.
276 ; We're cheating here to keep the code smaller: The first fields of the
277 ; mouse_info struct are identical to the mouse_pos struct; so, we just will
278 ; use _mouse_pos to fill the position fields.
282 ; Fill in the button state.
284 ldy #MOUSE_INFO::BUTTONS
287 ; jmp POS ; Fall through
289 ;----------------------------------------------------------------------------
290 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
291 ; No return code required.
293 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
295 sei ; Disable interrupts
296 lda XPos ; Transfer the position
305 cli ; Enable interrupts
308 sta (ptr1),y ; Store last byte
312 ;----------------------------------------------------------------------------
313 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
314 ; specific data in ptr1, and the ioctl code in .A.
315 ; Must return an error code in .XA.
318 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
319 ldx #>MOUSE_ERR_INV_IOCTL
322 ;----------------------------------------------------------------------------
323 ; IRQ: Irq.-handler entry point. Called as a subroutine, but in the IRQ context
324 ; (so, be careful). The routine MUST return carry set if the interrupt has been
325 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
326 ; MUST return carry clear.
331 ldy #15 ; Switch to the system bank
334 ; Get the direction bits.
337 lda (cia2),y ; Read joystick inputs
339 ; Switch back to the execution bank.
344 ; Joystick 2, directions in bits 7-4.
345 ; Mask the relevant bits.
348 eor #$F0 ; All bits are inverted
353 and #(JOY::LEFT | JOY::RIGHT) << 4
356 ; We will cheat here, and rely on the fact that either the left OR the right
359 and #JOY::RIGHT << 4 ; Check RIGHT bit
363 bnz @AddX ; Branch always
367 ; Calculate the new x co-ordinate (--> .YA).
370 tay ; Remember low byte
375 ; Limit the x co-ordinate to the bounding box.
393 ; Calculate the y movement vector.
395 @SkipX: lda Temp ; Get joystick again
396 and #(JOY::UP | JOY::DOWN) << 4 ; Check up/down
399 ; We will cheat here, and rely on the fact that either the up OR the down
402 and #JOY::UP << 4 ; Check UP bit
410 ; Calculate the new y co-ordinate (--> .YA).
413 tay ; Remember low byte
418 ; Limit the y co-ordinate to the bounding box.
439 clc ; Interrupt not "handled"
442 ; Move the mouse pointer to the new x pos.
448 ; Move the mouse pointer to the new y pos.