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
41 ; Callback table, set by the kernel before INSTALL is called
43 CHIDE: jmp $0000 ; Hide the cursor
44 CSHOW: jmp $0000 ; Show the cursor
45 CMOVEX: jmp $0000 ; Move the cursor to X coord
46 CMOVEY: jmp $0000 ; Move the cursor to Y coord
49 ;----------------------------------------------------------------------------
63 ;----------------------------------------------------------------------------
64 ; Global variables. The bounding box values are sorted so that they can be
65 ; written with the least effort in the SETBOX and GETBOX routines, so don't
71 YPos: .res 2 ; Current mouse position, Y
72 XPos: .res 2 ; Current mouse position, X
73 XMin: .res 2 ; X1 value of bounding box
74 YMin: .res 2 ; Y1 value of bounding box
75 XMax: .res 2 ; X2 value of bounding box
76 YMax: .res 2 ; Y2 value of bounding box
77 Buttons: .res 1 ; Button mask
79 ; Temporary value used in the int handler
83 ; Default values for above variables
88 .word SCREEN_HEIGHT/2 ; YPos
89 .word SCREEN_WIDTH/2 ; XPos
92 .word SCREEN_WIDTH ; XMax
93 .word SCREEN_HEIGHT ; YMax
99 ;----------------------------------------------------------------------------
100 ; INSTALL routine. Is called after the driver is loaded into memory. If
101 ; possible, check if the hardware is present.
102 ; Must return an MOUSE_ERR_xx code in a/x.
106 ; Initialize variables. Just copy the default stuff over
108 ldx #.sizeof(DefVars)-1
114 ; Be sure the mouse cursor is invisible and at the default location. We
115 ; need to do that here, because our mouse interrupt handler doesn't set the
116 ; mouse position if it hasn't changed.
128 ; Done, return zero (= MOUSE_ERR_OK)
134 ;----------------------------------------------------------------------------
135 ; UNINSTALL routine. Is called before the driver is removed from memory.
136 ; No return code required (the driver is removed from memory on return).
138 UNINSTALL = HIDE ; Hide cursor on exit
140 ;----------------------------------------------------------------------------
141 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
142 ; a counter for calls to show/hide, and the driver entry point is only called
143 ; if the mouse is currently visible and should get hidden. For most drivers,
144 ; no special action is required besides hiding the mouse cursor.
145 ; No return code required.
152 ;----------------------------------------------------------------------------
153 ; SHOW routine. Is called to show 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 hidden and should become visible. For most drivers,
156 ; no special action is required besides enabling the mouse cursor.
157 ; No return code required.
164 ;----------------------------------------------------------------------------
165 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
166 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
167 ; No checks are done if the mouse is currently inside the box, this is the job
168 ; of the caller. It is not necessary to validate the parameters, trust the
169 ; caller and save some code here. No return code required.
172 stx ptr1+1 ; Save data pointer
174 ldy #.sizeof (MOUSE_BOX)-1
185 ;----------------------------------------------------------------------------
186 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
187 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
190 stx ptr1+1 ; Save data pointer
192 ldy #.sizeof (MOUSE_BOX)-1
203 ;----------------------------------------------------------------------------
204 ; MOVE: Move the mouse to a new position. The position is passed as it comes
205 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
206 ; remove the parameter from the stack on return.
207 ; No checks are done if the new position is valid (within the bounding box or
208 ; the screen). No return code required.
211 MOVE: sei ; No interrupts
214 stx YPos+1 ; New Y position
223 sta XPos ; New X position
225 jsr CMOVEX ; Move the cursor
227 cli ; Allow interrupts
230 ;----------------------------------------------------------------------------
231 ; BUTTONS: Return the button mask in a/x.
238 ;----------------------------------------------------------------------------
239 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
240 ; No return code required.
242 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
244 sei ; Disable interrupts
245 lda XPos ; Transfer the position
254 cli ; Enable interrupts
257 sta (ptr1),y ; Store last byte
261 ;----------------------------------------------------------------------------
262 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
263 ; struct pointed to by ptr1. No return code required.
265 ; We're cheating here to keep the code smaller: The first fields of the
266 ; mouse_info struct are identical to the mouse_pos struct, so we will just
267 ; call _mouse_pos to initialize the struct pointer and fill the position
272 ; Fill in the button state
275 ldy #MOUSE_INFO::BUTTONS
280 ;----------------------------------------------------------------------------
281 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
282 ; specific data in ptr1, and the ioctl code in A.
283 ; Must return an error code in a/x.
286 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
287 ldx #>MOUSE_ERR_INV_IOCTL
290 ;----------------------------------------------------------------------------
291 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
297 lda CIA1_PRB ; Read port #1
299 eor #%00001100 ; Make all bits active high
320 bne @AddX ; Branch always
324 ; Calculate the new X coordinate (--> a/y)
327 tay ; Remember low byte
332 ; Limit the X coordinate to the bounding box
350 ; Move the mouse pointer to the new X pos
361 ; Calculate the new Y coordinate (--> a/y)
364 tay ; Remember low byte
369 ; Limit the Y coordinate to the bounding box
387 ; Move the mouse pointer to the new X pos