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-03-17, 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
46 ; Callback table, set by the kernel before INSTALL is called
48 CHIDE: jmp $0000 ; Hide the cursor
49 CSHOW: jmp $0000 ; Show the cursor
50 CPREP: jmp $0000 ; Prepare to move the cursor
51 CDRAW: jmp $0000 ; Draw the cursor
52 CMOVEX: jmp $0000 ; Move the cursor to X coord
53 CMOVEY: jmp $0000 ; Move the cursor to Y coord
56 ;----------------------------------------------------------------------------
70 ;----------------------------------------------------------------------------
71 ; Global variables. The bounding box values are sorted so that they can be
72 ; written with the least effort in the SETBOX and GETBOX routines, so don't
78 YPos: .res 2 ; Current mouse position, Y
79 XPos: .res 2 ; Current mouse position, X
80 XMin: .res 2 ; X1 value of bounding box
81 YMin: .res 2 ; Y1 value of bounding box
82 XMax: .res 2 ; X2 value of bounding box
83 YMax: .res 2 ; Y2 value of bounding box
84 Buttons: .res 1 ; Button mask
88 ; Temporary value used in the int handler
94 ; Default values for above variables
95 ; (We use ".proc" because we want to define both a label and a scope.)
98 .word SCREEN_HEIGHT/2 ; YPos
99 .word SCREEN_WIDTH/2 ; XPos
102 .word SCREEN_WIDTH - 1 ; XMax
103 .word SCREEN_HEIGHT - 1 ; YMax
109 ;----------------------------------------------------------------------------
110 ; INSTALL routine. Is called after the driver is loaded into memory. If
111 ; possible, check if the hardware is present.
112 ; Must return an MOUSE_ERR_xx code in a/x.
116 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
117 ; That allows direct access to the VIC-IIe's sprite registers.
124 ; Initialize variables. Just copy the default stuff over
126 ldx #.sizeof(DefVars)-1
132 ; Be sure the mouse cursor is invisible and at the default location. We
133 ; need to do that here, because our mouse interrupt handler doesn't set the
134 ; mouse position if it hasn't changed.
146 ; Done, return zero (= MOUSE_ERR_OK)
152 ;----------------------------------------------------------------------------
153 ; UNINSTALL routine. Is called before the driver is removed from memory.
154 ; No return code required (the driver is removed from memory on return).
157 jsr HIDE ; Hide cursor on exit
162 ;----------------------------------------------------------------------------
163 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
164 ; a counter for calls to show/hide, and the driver entry point is only called
165 ; if the mouse is currently visible and should get hidden. For most drivers,
166 ; no special action is required besides hiding the mouse cursor.
167 ; No return code required.
174 ;----------------------------------------------------------------------------
175 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
176 ; a counter for calls to show/hide, and the driver entry point is only called
177 ; if the mouse is currently hidden and should become visible. For most drivers,
178 ; no special action is required besides enabling the mouse cursor.
179 ; No return code required.
186 ;----------------------------------------------------------------------------
187 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
188 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
189 ; No checks are done if the mouse is currently inside the box, this is the job
190 ; of the caller. It is not necessary to validate the parameters, trust the
191 ; caller and save some code here. No return code required.
194 stx ptr1+1 ; Save data pointer
196 ldy #.sizeof (MOUSE_BOX)-1
207 ;----------------------------------------------------------------------------
208 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
209 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
212 stx ptr1+1 ; Save data pointer
214 ldy #.sizeof (MOUSE_BOX)-1
225 ;----------------------------------------------------------------------------
226 ; MOVE: Move the mouse to a new position. The position is passed as it comes
227 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
228 ; remove the parameter from the stack on return.
229 ; No checks are done if the new position is valid (within the bounding box or
230 ; the screen). No return code required.
233 MOVE: sei ; No interrupts
236 stx YPos+1 ; New Y position
245 sta XPos ; New X position
247 jsr CMOVEX ; Move the cursor
249 cli ; Allow interrupts
252 ;----------------------------------------------------------------------------
253 ; BUTTONS: Return the button mask in a/x.
260 ;----------------------------------------------------------------------------
261 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
262 ; No return code required.
264 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
266 sei ; Disable interrupts
267 lda XPos ; Transfer the position
276 cli ; Enable interrupts
279 sta (ptr1),y ; Store last byte
283 ;----------------------------------------------------------------------------
284 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
285 ; struct pointed to by ptr1. No return code required.
287 ; We're cheating here to keep the code smaller: The first fields of the
288 ; mouse_info struct are identical to the mouse_pos struct, so we will just
289 ; call _mouse_pos to initialize the struct pointer and fill the position
294 ; Fill in the button state
297 ldy #MOUSE_INFO::BUTTONS
302 ;----------------------------------------------------------------------------
303 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
304 ; specific data in ptr1, and the ioctl code in A.
305 ; Must return an error code in a/x.
308 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
309 ldx #>MOUSE_ERR_INV_IOCTL
312 ;----------------------------------------------------------------------------
313 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
320 lda CIA1_PRB ; Read port #1
322 eor #%00001100 ; Make all bits active high
343 bne @AddX ; Branch always
347 ; Calculate the new X coordinate (--> a/y)
350 tay ; Remember low byte
355 ; Limit the X coordinate to the bounding box
373 ; Move the mouse pointer to the new X pos
384 ; Calculate the new Y coordinate (--> a/y)
387 tay ; Remember low byte
392 ; Limit the Y coordinate to the bounding box
410 ; Move the mouse pointer to the new X pos
415 clc ; Interrupt not "handled"