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-05-05, Greg King
9 .include "zeropage.inc"
10 .include "mouse-kernel.inc"
15 ; ------------------------------------------------------------------------
16 ; Header. Includes jump table
24 .byte $6d, $6f, $75 ; "mou"
25 .byte MOUSE_API_VERSION ; Mouse driver API version number
48 .byte MOUSE_FLAG_LATE_IRQ
50 ; Callback table, set by the kernel before INSTALL is called
52 CHIDE: jmp $0000 ; Hide the cursor
53 CSHOW: jmp $0000 ; Show the cursor
54 CPREP: jmp $0000 ; Prepare to move the cursor
55 CDRAW: jmp $0000 ; Draw the cursor
56 CMOVEX: jmp $0000 ; Move the cursor to X coord
57 CMOVEY: jmp $0000 ; Move the cursor to Y coord
60 ;----------------------------------------------------------------------------
74 ;----------------------------------------------------------------------------
75 ; Global variables. The bounding box values are sorted so that they can be
76 ; written with the least effort in the SETBOX and GETBOX routines, so don't
82 YPos: .res 2 ; Current mouse position, Y
83 XPos: .res 2 ; Current mouse position, X
84 XMin: .res 2 ; X1 value of bounding box
85 YMin: .res 2 ; Y1 value of bounding box
86 XMax: .res 2 ; X2 value of bounding box
87 YMax: .res 2 ; Y2 value of bounding box
88 Buttons: .res 1 ; Button mask
90 ; Temporary value used in the int handler
96 ; Default values for above variables
97 ; (We use ".proc" because we want to define both a label and a scope.)
100 .word SCREEN_HEIGHT/2 ; YPos
101 .word SCREEN_WIDTH/2 ; XPos
104 .word SCREEN_WIDTH - 1 ; XMax
105 .word SCREEN_HEIGHT - 1 ; YMax
111 ;----------------------------------------------------------------------------
112 ; INSTALL routine. Is called after the driver is loaded into memory. If
113 ; possible, check if the hardware is present.
114 ; Must return an MOUSE_ERR_xx code in a/x.
118 ; Initialize variables. Just copy the default stuff over
120 ldx #.sizeof(DefVars)-1
126 ; Be sure the mouse cursor is invisible and at the default location. We
127 ; need to do that here, because our mouse interrupt handler doesn't set the
128 ; mouse position if it hasn't changed.
140 ; Done, return zero (= MOUSE_ERR_OK)
146 ;----------------------------------------------------------------------------
147 ; UNINSTALL routine. Is called before the driver is removed from memory.
148 ; No return code required (the driver is removed from memory on return).
150 UNINSTALL = HIDE ; Hide cursor on exit
152 ;----------------------------------------------------------------------------
153 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
154 ; a counter for calls to show/hide, and the driver entry point is only called
155 ; if the mouse is currently visible and should get hidden. For most drivers,
156 ; no special action is required besides hiding the mouse cursor.
157 ; No return code required.
164 ;----------------------------------------------------------------------------
165 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
166 ; a counter for calls to show/hide, and the driver entry point is only called
167 ; if the mouse is currently hidden and should become visible. For most drivers,
168 ; no special action is required besides enabling the mouse cursor.
169 ; No return code required.
176 ;----------------------------------------------------------------------------
177 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
178 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
179 ; No checks are done if the mouse is currently inside the box, this is the job
180 ; of the caller. It is not necessary to validate the parameters, trust the
181 ; caller and save some code here. No return code required.
184 stx ptr1+1 ; Save data pointer
186 ldy #.sizeof (MOUSE_BOX)-1
197 ;----------------------------------------------------------------------------
198 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
199 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
202 stx ptr1+1 ; Save data pointer
204 ldy #.sizeof (MOUSE_BOX)-1
215 ;----------------------------------------------------------------------------
216 ; MOVE: Move the mouse to a new position. The position is passed as it comes
217 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
218 ; remove the parameter from the stack on return.
219 ; No checks are done if the new position is valid (within the bounding box or
220 ; the screen). No return code required.
223 MOVE: sei ; No interrupts
226 stx YPos+1 ; New Y position
235 sta XPos ; New X position
237 jsr CMOVEX ; Move the cursor
239 cli ; Allow interrupts
242 ;----------------------------------------------------------------------------
243 ; BUTTONS: Return the button mask in a/x.
250 ;----------------------------------------------------------------------------
251 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
252 ; No return code required.
254 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
256 sei ; Disable interrupts
257 lda XPos ; Transfer the position
266 cli ; Enable interrupts
269 sta (ptr1),y ; Store last byte
273 ;----------------------------------------------------------------------------
274 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
275 ; struct pointed to by ptr1. No return code required.
277 ; We're cheating here to keep the code smaller: The first fields of the
278 ; mouse_info struct are identical to the mouse_pos struct, so we will just
279 ; call _mouse_pos to initialize the struct pointer and fill the position
284 ; Fill in the button state
287 ldy #MOUSE_INFO::BUTTONS
292 ;----------------------------------------------------------------------------
293 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
294 ; specific data in ptr1, and the ioctl code in A.
295 ; Must return an error code in a/x.
298 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
299 ldx #>MOUSE_ERR_INV_IOCTL
302 ;----------------------------------------------------------------------------
303 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
310 lda CIA1_PRB ; Read port #1
312 eor #%00001100 ; Make all bits active high
333 bne @AddX ; Branch always
337 ; Calculate the new X coordinate (--> a/y)
340 tay ; Remember low byte
345 ; Limit the X coordinate to the bounding box
363 ; Move the mouse pointer to the new X pos
374 ; Calculate the new Y coordinate (--> a/y)
377 tay ; Remember low byte
382 ; Limit the Y coordinate to the bounding box
400 ; Move the mouse pointer to the new X pos
405 clc ; Interrupt not "handled"