2 ; Driver for a potentiometer "mouse", e.g. Koala Pad
4 ; 2006-08-20, Stefan Haubenthal
5 ; 2009-09-26, Ullrich von Bassewitz
6 ; 2014-04-26, Christian Groessler
7 ; 2014-05-05, Greg King
10 .include "zeropage.inc"
11 .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
49 .byte MOUSE_FLAG_LATE_IRQ
51 ; Callback table, set by the kernel before INSTALL is called
53 CHIDE: jmp $0000 ; Hide the cursor
54 CSHOW: jmp $0000 ; Show the cursor
55 CPREP: jmp $0000 ; Prepare to move the cursor
56 CDRAW: jmp $0000 ; Draw the cursor
57 CMOVEX: jmp $0000 ; Move the cursor to X coord
58 CMOVEY: jmp $0000 ; Move the cursor to Y coord
61 ;----------------------------------------------------------------------------
75 ;----------------------------------------------------------------------------
76 ; Global variables. The bounding box values are sorted so that they can be
77 ; written with the least effort in the SETBOX and GETBOX routines, so don't
83 YPos: .res 2 ; Current mouse position, Y
84 XPos: .res 2 ; Current mouse position, X
85 XMin: .res 2 ; X1 value of bounding box
86 YMin: .res 2 ; Y1 value of bounding box
87 XMax: .res 2 ; X2 value of bounding box
88 YMax: .res 2 ; Y2 value of bounding box
89 Buttons: .res 1 ; Button mask
93 ; Keyboard buffer fill level at start of interrupt
103 ; Default values for above variables
104 ; (We use ".proc" because we want to define both a label and a scope.)
107 .word SCREEN_HEIGHT/2 ; YPos
108 .word SCREEN_WIDTH/2 ; XPos
111 .word SCREEN_WIDTH - 1 ; XMax
112 .word SCREEN_HEIGHT - 1 ; YMax
118 ;----------------------------------------------------------------------------
119 ; INSTALL routine. Is called after the driver is loaded into memory. If
120 ; possible, check if the hardware is present.
121 ; Must return an MOUSE_ERR_xx code in a/x.
125 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
126 ; That allows direct access to the VIC-IIe's sprite registers.
133 ; Initialize variables. Just copy the default stuff over
135 ldx #.sizeof(DefVars)-1
141 ; Be sure the mouse cursor is invisible and at the default location. We
142 ; need to do that here, because our mouse interrupt handler doesn't set the
143 ; mouse position if it hasn't changed.
154 ; Initiate our IRQ magic.
156 ; Remember the ROM IRQ continuation address.
167 ; Set the ROM IRQ continuation address to point to the provided routine.
175 ; Set the address of our IRQ callback routine.
176 ; Because it's called via "rts", we must use "address-1".
184 ; Set the ROM entry-point vector.
185 ; Because it's called via "rts", we must decrement it by one.
196 ; Done, return zero (= MOUSE_ERR_OK)
202 ;----------------------------------------------------------------------------
203 ; UNINSTALL routine. Is called before the driver is removed from memory.
204 ; No return code required (the driver is removed from memory on return).
214 jsr HIDE ; Hide cursor on exit
219 ;----------------------------------------------------------------------------
220 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
221 ; a counter for calls to show/hide, and the driver entry point is only called
222 ; if the mouse is currently visible and should get hidden. For most drivers,
223 ; no special action is required besides hiding the mouse cursor.
224 ; No return code required.
231 ;----------------------------------------------------------------------------
232 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
233 ; a counter for calls to show/hide, and the driver entry point is only called
234 ; if the mouse is currently hidden and should become visible. For most drivers,
235 ; no special action is required besides enabling the mouse cursor.
236 ; No return code required.
243 ;----------------------------------------------------------------------------
244 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
245 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
246 ; No checks are done if the mouse is currently inside the box, this is the job
247 ; of the caller. It is not necessary to validate the parameters, trust the
248 ; caller and save some code here. No return code required.
251 stx ptr1+1 ; Save data pointer
253 ldy #.sizeof (MOUSE_BOX)-1
264 ;----------------------------------------------------------------------------
265 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
266 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
269 stx ptr1+1 ; Save data pointer
271 ldy #.sizeof (MOUSE_BOX)-1
282 ;----------------------------------------------------------------------------
283 ; MOVE: Move the mouse to a new position. The position is passed as it comes
284 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
285 ; remove the parameter from the stack on return.
286 ; No checks are done if the new position is valid (within the bounding box or
287 ; the screen). No return code required.
290 MOVE: sei ; No interrupts
293 stx YPos+1 ; New Y position
302 sta XPos ; New X position
304 jsr CMOVEX ; Move the cursor
306 cli ; Allow interrupts
309 ;----------------------------------------------------------------------------
310 ; BUTTONS: Return the button mask in a/x.
316 ; Make the buttons look like a 1351 mouse.
318 and #JOY::LEFT | JOY::RIGHT
323 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
326 ;----------------------------------------------------------------------------
327 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
328 ; No return code required.
330 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
332 sei ; Disable interrupts
333 lda XPos ; Transfer the position
342 cli ; Enable interrupts
345 sta (ptr1),y ; Store last byte
349 ;----------------------------------------------------------------------------
350 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
351 ; struct pointed to by ptr1. No return code required.
353 ; We're cheating here to keep the code smaller: The first fields of the
354 ; mouse_info struct are identical to the mouse_pos struct, so we will just
355 ; call _mouse_pos to initialize the struct pointer and fill the position
360 ; Fill in the button state
363 ldy #MOUSE_INFO::BUTTONS
368 ;----------------------------------------------------------------------------
369 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
370 ; specific data in ptr1, and the ioctl code in A.
371 ; Must return an error code in a/x.
374 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls for now
375 ldx #>MOUSE_ERR_INV_IOCTL
378 ;----------------------------------------------------------------------------
379 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
388 lda CIA1_PRB ; Read port #1
389 eor #%11111111 ; Make all bits active high
392 ldx #%01000000 ; Read port 1 paddles
404 bne @AddX ; Branch always
408 ; Calculate the new X coordinate (--> a/y)
411 tay ; Remember low byte
416 ; Limit the X coordinate to the bounding box
434 ; Move the mouse pointer to the new X pos
445 ; Calculate the new Y coordinate (--> a/y)
448 tay ; Remember low byte
453 ; Limit the Y coordinate to the bounding box
471 ; Move the mouse pointer to the new X pos
476 clc ; Interrupt not "handled"
479 .define OLD_BUTTONS Buttons ; Tells callback.inc where the old port status is stored
480 .include "callback.inc"