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 CPREP: jmp $0000 ; Prepare to move the cursor
50 CDRAW: jmp $0000 ; Draw the cursor
51 CMOVEX: jmp $0000 ; Move the cursor to X coord
52 CMOVEY: jmp $0000 ; Move the cursor to Y coord
55 ;----------------------------------------------------------------------------
69 ;----------------------------------------------------------------------------
70 ; Global variables. The bounding box values are sorted so that they can be
71 ; written with the least effort in the SETBOX and GETBOX routines, so don't
77 YPos: .res 2 ; Current mouse position, Y
78 XPos: .res 2 ; Current mouse position, X
79 XMin: .res 2 ; X1 value of bounding box
80 YMin: .res 2 ; Y1 value of bounding box
81 XMax: .res 2 ; X2 value of bounding box
82 YMax: .res 2 ; Y2 value of bounding box
83 Buttons: .res 1 ; Button mask
85 ; Temporary value used in the int handler
91 ; Default values for above variables
92 ; (We use ".proc" because we want to define both a label and a scope.)
95 .word SCREEN_HEIGHT/2 ; YPos
96 .word SCREEN_WIDTH/2 ; XPos
99 .word SCREEN_WIDTH ; XMax
100 .word SCREEN_HEIGHT ; YMax
106 ;----------------------------------------------------------------------------
107 ; INSTALL routine. Is called after the driver is loaded into memory. If
108 ; possible, check if the hardware is present.
109 ; Must return an MOUSE_ERR_xx code in a/x.
113 ; Initialize variables. Just copy the default stuff over
115 ldx #.sizeof(DefVars)-1
121 ; Be sure the mouse cursor is invisible and at the default location. We
122 ; need to do that here, because our mouse interrupt handler doesn't set the
123 ; mouse position if it hasn't changed.
135 ; Done, return zero (= MOUSE_ERR_OK)
141 ;----------------------------------------------------------------------------
142 ; UNINSTALL routine. Is called before the driver is removed from memory.
143 ; No return code required (the driver is removed from memory on return).
145 UNINSTALL = HIDE ; Hide cursor on exit
147 ;----------------------------------------------------------------------------
148 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
149 ; a counter for calls to show/hide, and the driver entry point is only called
150 ; if the mouse is currently visible and should get hidden. For most drivers,
151 ; no special action is required besides hiding the mouse cursor.
152 ; No return code required.
159 ;----------------------------------------------------------------------------
160 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
161 ; a counter for calls to show/hide, and the driver entry point is only called
162 ; if the mouse is currently hidden and should become visible. For most drivers,
163 ; no special action is required besides enabling the mouse cursor.
164 ; No return code required.
171 ;----------------------------------------------------------------------------
172 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
173 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
174 ; No checks are done if the mouse is currently inside the box, this is the job
175 ; of the caller. It is not necessary to validate the parameters, trust the
176 ; caller and save some code here. No return code required.
179 stx ptr1+1 ; Save data pointer
181 ldy #.sizeof (MOUSE_BOX)-1
192 ;----------------------------------------------------------------------------
193 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
194 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
197 stx ptr1+1 ; Save data pointer
199 ldy #.sizeof (MOUSE_BOX)-1
210 ;----------------------------------------------------------------------------
211 ; MOVE: Move the mouse to a new position. The position is passed as it comes
212 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
213 ; remove the parameter from the stack on return.
214 ; No checks are done if the new position is valid (within the bounding box or
215 ; the screen). No return code required.
218 MOVE: sei ; No interrupts
221 stx YPos+1 ; New Y position
230 sta XPos ; New X position
232 jsr CMOVEX ; Move the cursor
234 cli ; Allow interrupts
237 ;----------------------------------------------------------------------------
238 ; BUTTONS: Return the button mask in a/x.
245 ;----------------------------------------------------------------------------
246 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
247 ; No return code required.
249 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
251 sei ; Disable interrupts
252 lda XPos ; Transfer the position
261 cli ; Enable interrupts
264 sta (ptr1),y ; Store last byte
268 ;----------------------------------------------------------------------------
269 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
270 ; struct pointed to by ptr1. No return code required.
272 ; We're cheating here to keep the code smaller: The first fields of the
273 ; mouse_info struct are identical to the mouse_pos struct, so we will just
274 ; call _mouse_pos to initialize the struct pointer and fill the position
279 ; Fill in the button state
282 ldy #MOUSE_INFO::BUTTONS
287 ;----------------------------------------------------------------------------
288 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
289 ; specific data in ptr1, and the ioctl code in A.
290 ; Must return an error code in a/x.
293 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
294 ldx #>MOUSE_ERR_INV_IOCTL
297 ;----------------------------------------------------------------------------
298 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
305 lda CIA1_PRB ; Read port #1
307 eor #%00001100 ; Make all bits active high
328 bne @AddX ; Branch always
332 ; Calculate the new X coordinate (--> a/y)
335 tay ; Remember low byte
340 ; Limit the X coordinate to the bounding box
358 ; Move the mouse pointer to the new X pos
369 ; Calculate the new Y coordinate (--> a/y)
372 tay ; Remember low byte
377 ; Limit the Y coordinate to the bounding box
395 ; Move the mouse pointer to the new X pos
400 clc ; Interrupt not "handled"