2 ; Driver for a potentiometer "mouse" e.g. Koala Pad
4 ; Ullrich von Bassewitz, 2004-03-29
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
40 ; Callback table, set by the kernel before INSTALL is called
42 CHIDE: jmp $0000 ; Hide the cursor
43 CSHOW: jmp $0000 ; Show the cursor
44 CMOVEX: jmp $0000 ; Move the cursor to X coord
45 CMOVEY: jmp $0000 ; Move the cursor to Y coord
48 ;----------------------------------------------------------------------------
62 ;----------------------------------------------------------------------------
63 ; Global variables. The bounding box values are sorted so that they can be
64 ; written with the least effort in the BOX routine, so don't reorder them.
69 YPos: .res 2 ; Current mouse position, Y
70 XPos: .res 2 ; Current mouse position, X
71 YMax: .res 2 ; Y2 value of bounding box
72 XMax: .res 2 ; X2 value of bounding box
73 YMin: .res 2 ; Y1 value of bounding box
74 XMin: .res 2 ; X1 value of bounding box
75 Buttons: .res 1 ; Button mask
77 ; Temporary value used in the int handler
81 ; Default values for above variables
86 .word SCREEN_HEIGHT/2 ; YPos
87 .word SCREEN_WIDTH/2 ; XPos
88 .word SCREEN_HEIGHT ; YMax
89 .word SCREEN_WIDTH ; XMax
97 ;----------------------------------------------------------------------------
98 ; INSTALL routine. Is called after the driver is loaded into memory. If
99 ; possible, check if the hardware is present.
100 ; Must return an MOUSE_ERR_xx code in a/x.
104 ; Initialize variables. Just copy the default stuff over
106 ldx #.sizeof(DefVars)-1
112 ; Be sure the mouse cursor is invisible and at the default location. We
113 ; need to do that here, because our mouse interrupt handler doesn't set the
114 ; mouse position if it hasn't changed.
126 ; Done, return zero (= MOUSE_ERR_OK)
132 ;----------------------------------------------------------------------------
133 ; UNINSTALL routine. Is called before the driver is removed from memory.
134 ; No return code required (the driver is removed from memory on return).
136 UNINSTALL = HIDE ; Hide cursor on exit
138 ;----------------------------------------------------------------------------
139 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
140 ; a counter for calls to show/hide, and the driver entry point is only called
141 ; if the mouse is currently visible and should get hidden. For most drivers,
142 ; no special action is required besides hiding the mouse cursor.
143 ; No return code required.
150 ;----------------------------------------------------------------------------
151 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
152 ; a counter for calls to show/hide, and the driver entry point is only called
153 ; if the mouse is currently hidden and should become visible. For most drivers,
154 ; no special action is required besides enabling the mouse cursor.
155 ; No return code required.
162 ;----------------------------------------------------------------------------
163 ; BOX: Set the mouse bounding box. The parameters are passed as they come from
164 ; the C program, that is, maxy in a/x and the other parameters on the stack.
165 ; The C wrapper will remove the parameters from the stack when the driver
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.
184 ;----------------------------------------------------------------------------
185 ; MOVE: Move the mouse to a new position. The position is passed as it comes
186 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
187 ; remove the parameter from the stack on return.
188 ; No checks are done if the new position is valid (within the bounding box or
189 ; the screen). No return code required.
192 MOVE: sei ; No interrupts
195 stx YPos+1 ; New Y position
204 sta XPos ; New X position
206 jsr CMOVEX ; Move the cursor
208 cli ; Allow interrupts
211 ;----------------------------------------------------------------------------
212 ; BUTTONS: Return the button mask in a/x.
219 ;----------------------------------------------------------------------------
220 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
221 ; No return code required.
223 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
225 sei ; Disable interrupts
226 lda XPos ; Transfer the position
235 cli ; Enable interrupts
238 sta (ptr1),y ; Store last byte
242 ;----------------------------------------------------------------------------
243 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
244 ; struct pointed to by ptr1. No return code required.
246 ; We're cheating here to keep the code smaller: The first fields of the
247 ; mouse_info struct are identical to the mouse_pos struct, so we will just
248 ; call _mouse_pos to initialize the struct pointer and fill the position
253 ; Fill in the button state
256 ldy #MOUSE_INFO::BUTTONS
261 ;----------------------------------------------------------------------------
262 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
263 ; specific data in ptr1, and the ioctl code in A.
264 ; Must return an error code in a/x.
267 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
268 ldx #>MOUSE_ERR_INV_IOCTL
271 ;----------------------------------------------------------------------------
272 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
278 lda CIA1_PRB ; Read port #1
280 eor #%00001100 ; Make all bits active high
301 bne @AddX ; Branch always
305 ; Calculate the new X coordinate (--> a/y)
308 tay ; Remember low byte
313 ; Limit the X coordinate to the bounding box
331 ; Move the mouse pointer to the new X pos
342 ; Calculate the new Y coordinate (--> a/y)
345 tay ; Remember low byte
350 ; Limit the Y coordinate to the bounding box
368 ; Move the mouse pointer to the new X pos