2 ; Driver for a potentiometer "mouse" e.g. Koala Pad
4 ; Ullrich von Bassewitz, 2004-03-29, 2009-09-26
5 ; Stefan Haubenthal, 2006-08-20
8 .include "zeropage.inc"
9 .include "mouse-kernel.inc"
14 ; ------------------------------------------------------------------------
15 ; Header. Includes jump table
23 .byte $6d, $6f, $75 ; "mou"
24 .byte MOUSE_API_VERSION ; Mouse driver API version number
45 ; Callback table, set by the kernel before INSTALL is called
47 CHIDE: jmp $0000 ; Hide the cursor
48 CSHOW: jmp $0000 ; Show the cursor
49 CMOVEX: jmp $0000 ; Move the cursor to X coord
50 CMOVEY: jmp $0000 ; Move the cursor to Y coord
53 ;----------------------------------------------------------------------------
67 ;----------------------------------------------------------------------------
68 ; Global variables. The bounding box values are sorted so that they can be
69 ; written with the least effort in the SETBOX and GETBOX routines, so don't
75 YPos: .res 2 ; Current mouse position, Y
76 XPos: .res 2 ; Current mouse position, X
77 XMin: .res 2 ; X1 value of bounding box
78 YMin: .res 2 ; Y1 value of bounding box
79 XMax: .res 2 ; X2 value of bounding box
80 YMax: .res 2 ; Y2 value of bounding box
81 Buttons: .res 1 ; Button mask
83 ; Temporary value used in the int handler
89 ; Default values for below variables
90 ; (We use ".proc" because we want to define both a label and a scope.)
93 .word SCREEN_HEIGHT/2 ; YPos
94 .word SCREEN_WIDTH/2 ; XPos
97 .word SCREEN_WIDTH ; XMax
98 .word SCREEN_HEIGHT ; YMax
104 ;----------------------------------------------------------------------------
105 ; INSTALL routine. Is called after the driver is loaded into memory. If
106 ; possible, check if the hardware is present.
107 ; Must return an MOUSE_ERR_xx code in a/x.
111 ; Initialize variables. Just copy the default stuff over
113 ldx #.sizeof(DefVars)-1
119 ; Be sure the mouse cursor is invisible and at the default location. We
120 ; need to do that here, because our mouse interrupt handler doesn't set the
121 ; mouse position if it hasn't changed.
133 ; Done, return zero (= MOUSE_ERR_OK)
139 ;----------------------------------------------------------------------------
140 ; UNINSTALL routine. Is called before the driver is removed from memory.
141 ; No return code required (the driver is removed from memory on return).
143 UNINSTALL = HIDE ; Hide cursor on exit
145 ;----------------------------------------------------------------------------
146 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
147 ; a counter for calls to show/hide, and the driver entry point is only called
148 ; if the mouse is currently visible and should get hidden. For most drivers,
149 ; no special action is required besides hiding the mouse cursor.
150 ; No return code required.
157 ;----------------------------------------------------------------------------
158 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
159 ; a counter for calls to show/hide, and the driver entry point is only called
160 ; if the mouse is currently hidden and should become visible. For most drivers,
161 ; no special action is required besides enabling the mouse cursor.
162 ; No return code required.
169 ;----------------------------------------------------------------------------
170 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
171 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
172 ; No checks are done if the mouse is currently inside the box, this is the job
173 ; of the caller. It is not necessary to validate the parameters, trust the
174 ; caller and save some code here. No return code required.
177 stx ptr1+1 ; Save data pointer
179 ldy #.sizeof (MOUSE_BOX)-1
190 ;----------------------------------------------------------------------------
191 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
192 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
195 stx ptr1+1 ; Save data pointer
197 ldy #.sizeof (MOUSE_BOX)-1
208 ;----------------------------------------------------------------------------
209 ; MOVE: Move the mouse to a new position. The position is passed as it comes
210 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
211 ; remove the parameter from the stack on return.
212 ; No checks are done if the new position is valid (within the bounding box or
213 ; the screen). No return code required.
216 MOVE: sei ; No interrupts
219 stx YPos+1 ; New Y position
228 sta XPos ; New X position
230 jsr CMOVEX ; Move the cursor
232 cli ; Allow interrupts
235 ;----------------------------------------------------------------------------
236 ; BUTTONS: Return the button mask in a/x.
243 ;----------------------------------------------------------------------------
244 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
245 ; No return code required.
247 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
249 sei ; Disable interrupts
250 lda XPos ; Transfer the position
259 cli ; Enable interrupts
262 sta (ptr1),y ; Store last byte
266 ;----------------------------------------------------------------------------
267 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
268 ; struct pointed to by ptr1. No return code required.
270 ; We're cheating here to keep the code smaller: The first fields of the
271 ; mouse_info struct are identical to the mouse_pos struct, so we will just
272 ; call _mouse_pos to initialize the struct pointer and fill the position
277 ; Fill in the button state
280 ldy #MOUSE_INFO::BUTTONS
285 ;----------------------------------------------------------------------------
286 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
287 ; specific data in ptr1, and the ioctl code in A.
288 ; Must return an error code in a/x.
291 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
292 ldx #>MOUSE_ERR_INV_IOCTL
295 ;----------------------------------------------------------------------------
296 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
302 lda CIA1_PRB ; Read port #1
304 eor #%00001100 ; Make all bits active high
325 bne @AddX ; Branch always
329 ; Calculate the new X coordinate (--> a/y)
332 tay ; Remember low byte
337 ; Limit the X coordinate to the bounding box
355 ; Move the mouse pointer to the new X pos
366 ; Calculate the new Y coordinate (--> a/y)
369 tay ; Remember low byte
374 ; Limit the Y coordinate to the bounding box
392 ; Move the mouse pointer to the new X pos