2 ; Driver for a "joystick mouse".
4 ; 2009-09-26, Ullrich von Bassewitz
5 ; 2014-04-26, Christian Groessler
6 ; 2014-05-01, Greg King
9 .include "zeropage.inc"
10 .include "mouse-kernel.inc"
17 ; ------------------------------------------------------------------------
18 ; Header. Includes jump table
20 module_header _c128_joy_mou
26 .byte $6d, $6f, $75 ; "mou"
27 .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 ; Keyboard buffer fill level at start of interrupt
104 ; Default values for above variables
105 ; (We use ".proc" because we want to define both a label and a scope.)
108 .word SCREEN_HEIGHT/2 ; YPos
109 .word SCREEN_WIDTH/2 ; XPos
112 .word SCREEN_WIDTH - 1 ; XMax
113 .word SCREEN_HEIGHT - 1 ; YMax
119 ;----------------------------------------------------------------------------
120 ; INSTALL routine. Is called after the driver is loaded into memory. If
121 ; possible, check if the hardware is present.
122 ; Must return an MOUSE_ERR_xx code in a/x.
126 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
127 ; That allows direct access to the VIC-IIe's sprite registers.
134 ; Initialize variables. Just copy the default stuff over
136 ldx #.sizeof(DefVars)-1
142 ; Be sure the mouse cursor is invisible and at the default location. We
143 ; need to do that here, because our mouse interrupt handler doesn't set the
144 ; mouse position if it hasn't changed.
155 ; Initialize our IRQ magic
157 ; remember ROM IRQ continuation address
168 ; set ROM IRQ continuation address to point to the provided routine
176 ; set address of our IRQ callback routine
177 ; since it's called via "rts" we have to use "address-1"
186 ; set ROM entry point vector
187 ; since it's called via "rts" we have to decrement it by one
198 ; Done, return zero (= MOUSE_ERR_OK)
204 ;----------------------------------------------------------------------------
205 ; UNINSTALL routine. Is called before the driver is removed from memory.
206 ; No return code required (the driver is removed from memory on return).
214 ;cli ; This will be done at end of HIDE
216 jsr HIDE ; Hide cursor on exit
221 ;----------------------------------------------------------------------------
222 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
223 ; a counter for calls to show/hide, and the driver entry point is only called
224 ; if the mouse is currently visible and should get hidden. For most drivers,
225 ; no special action is required besides hiding the mouse cursor.
226 ; No return code required.
233 ;----------------------------------------------------------------------------
234 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
235 ; a counter for calls to show/hide, and the driver entry point is only called
236 ; if the mouse is currently hidden and should become visible. For most drivers,
237 ; no special action is required besides enabling the mouse cursor.
238 ; No return code required.
245 ;----------------------------------------------------------------------------
246 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
247 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
248 ; No checks are done if the mouse is currently inside the box, this is the job
249 ; of the caller. It is not necessary to validate the parameters, trust the
250 ; caller and save some code here. No return code required.
253 stx ptr1+1 ; Save data pointer
255 ldy #.sizeof (MOUSE_BOX)-1
266 ;----------------------------------------------------------------------------
267 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
268 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
271 stx ptr1+1 ; Save data pointer
273 ldy #.sizeof (MOUSE_BOX)-1
284 ;----------------------------------------------------------------------------
285 ; MOVE: Move the mouse to a new position. The position is passed as it comes
286 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
287 ; remove the parameter from the stack on return.
288 ; No checks are done if the new position is valid (within the bounding box or
289 ; the screen). No return code required.
292 MOVE: sei ; No interrupts
295 stx YPos+1 ; New Y position
304 sta XPos ; New X position
306 jsr CMOVEX ; Move the cursor
308 cli ; Allow interrupts
311 ;----------------------------------------------------------------------------
312 ; BUTTONS: Return the button mask in a/x.
317 and #MOUSE_BTN_LEFT ; Left button -- same as JOY::FIRE
320 ;----------------------------------------------------------------------------
321 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
322 ; No return code required.
324 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
326 sei ; Disable interrupts
327 lda XPos ; Transfer the position
336 cli ; Enable interrupts
339 sta (ptr1),y ; Store last byte
343 ;----------------------------------------------------------------------------
344 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
345 ; struct pointed to by ptr1. No return code required.
347 ; We're cheating here to keep the code smaller: The first fields of the
348 ; mouse_info struct are identical to the mouse_pos struct, so we will just
349 ; call _mouse_pos to initialize the struct pointer and fill the position
354 ; Fill in the button state
357 ldy #MOUSE_INFO::BUTTONS
362 ;----------------------------------------------------------------------------
363 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
364 ; specific data in ptr1, and the ioctl code in A.
365 ; Must return an error code in a/x.
368 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls for now
369 ldx #>MOUSE_ERR_INV_IOCTL
372 ;----------------------------------------------------------------------------
373 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
374 ; (so be careful). The routine MUST return carry set if the interrupt has been
375 ; 'handled' - which means that the interrupt source is gone. Otherwise it
376 ; MUST return carry clear.
384 lda CIA1_PRB ; Read joystick #0
386 eor #$1F ; Make all bits active high
391 and #(JOY::LEFT | JOY::RIGHT)
394 ; We will cheat here and rely on the fact that either the left, OR the right
397 and #JOY::RIGHT ; Check RIGHT bit
401 bne @AddX ; Branch always
405 ; Calculate the new X coordinate (--> a/y)
408 tay ; Remember low byte
413 ; Limit the X coordinate to the bounding box
431 ; Move the mouse pointer to the new X pos
436 ; Calculate the Y movement vector
438 @SkipX: lda Buttons ; Read joystick #0
439 and #(JOY::UP | JOY::DOWN) ; Check up/down
442 ; We will cheat here and rely on the fact that either the up, OR the down
453 ; Calculate the new Y coordinate (--> a/y)
456 tay ; Remember low byte
461 ; Limit the Y coordinate to the bounding box
479 ; Move the mouse pointer to the new X pos
487 clc ; Interrupt not "handled"
490 .define OLD_BUTTONS Buttons ; tells callback.inc where the old port status is stored
491 .include "callback.inc"