2 ; Driver for the 1351 proportional mouse. Parts of the code are from
3 ; the Commodore 1351 mouse users guide.
5 ; Ullrich von Bassewitz, 2003-12-29, 2009-09-26
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
47 .byte MOUSE_FLAG_LATE_IRQ
49 ; Callback table, set by the kernel before INSTALL is called
51 CHIDE: jmp $0000 ; Hide the cursor
52 CSHOW: jmp $0000 ; Show the cursor
53 CDRAW: jmp $0000 ; Draw the cursor
54 CMOVE: jmp $0000 ; Prepare to move the cursor
55 CMOVEX: jmp $0000 ; Move the cursor to X coord
56 CMOVEY: jmp $0000 ; Move the cursor to Y coord
59 ;----------------------------------------------------------------------------
65 ;----------------------------------------------------------------------------
66 ; Global variables. The bounding box values are sorted so that they can be
67 ; written with the least effort in the SETBOX and GETBOX routines, so don't
73 OldPotX: .res 1 ; Old hw counter values
76 YPos: .res 2 ; Current mouse position, Y
77 XPos: .res 2 ; Current mouse position, X
78 XMin: .res 2 ; X1 value of bounding box
79 YMin: .res 2 ; Y1 value of bounding box
80 XMax: .res 2 ; X2 value of bounding box
81 YMax: .res 2 ; Y2 value of bounding box
83 OldValue: .res 1 ; Temp for MoveCheck routine
84 NewValue: .res 1 ; Temp for MoveCheck routine
88 ; Default values for above variables
89 ; (We use ".proc" because we want to define both a label and a scope.)
92 .byte 0, 0 ; OldPotX/OldPotY
93 .word SCREEN_HEIGHT/2 ; YPos
94 .word SCREEN_WIDTH/2 ; XPos
97 .word SCREEN_WIDTH ; XMax
98 .word SCREEN_HEIGHT ; YMax
103 ;----------------------------------------------------------------------------
104 ; INSTALL routine. Is called after the driver is loaded into memory. If
105 ; possible, check if the hardware is present.
106 ; Must return an MOUSE_ERR_xx code in a/x.
110 ; Initialize variables. Just copy the default stuff over
112 ldx #.sizeof(DefVars)-1
118 ; Be sure the mouse cursor is invisible and at the default location. We
119 ; need to do that here, because our mouse interrupt handler doesn't set the
120 ; mouse position if it hasn't changed.
132 ; Done, return zero (= MOUSE_ERR_OK)
136 rts ; Run into UNINSTALL instead
138 ;----------------------------------------------------------------------------
139 ; UNINSTALL routine. Is called before the driver is removed from memory.
140 ; No return code required (the driver is removed from memory on return).
142 UNINSTALL = HIDE ; Hide cursor on exit
144 ;----------------------------------------------------------------------------
145 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
146 ; a counter for calls to show/hide, and the driver entry point is only called
147 ; if the mouse is currently visible and should get hidden. For most drivers,
148 ; no special action is required besides hiding the mouse cursor.
149 ; No return code required.
156 ;----------------------------------------------------------------------------
157 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
158 ; a counter for calls to show/hide, and the driver entry point is only called
159 ; if the mouse is currently hidden and should become visible. For most drivers,
160 ; no special action is required besides enabling the mouse cursor.
161 ; No return code required.
168 ;----------------------------------------------------------------------------
169 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
170 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
171 ; No checks are done if the mouse is currently inside the box, this is the job
172 ; of the caller. It is not necessary to validate the parameters, trust the
173 ; caller and save some code here. No return code required.
176 stx ptr1+1 ; Save data pointer
178 ldy #.sizeof (MOUSE_BOX)-1
189 ;----------------------------------------------------------------------------
190 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
191 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
194 stx ptr1+1 ; Save data pointer
196 ldy #.sizeof (MOUSE_BOX)-1
207 ;----------------------------------------------------------------------------
208 ; MOVE: Move the mouse to a new position. The position is passed as it comes
209 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
210 ; remove the parameter from the stack on return.
211 ; No checks are done if the new position is valid (within the bounding box or
212 ; the screen). No return code required.
215 MOVE: sei ; No interrupts
218 stx YPos+1 ; New Y position
227 sta XPos ; New X position
229 jsr CMOVEX ; Move the cursor
231 cli ; Allow interrupts
234 ;----------------------------------------------------------------------------
235 ; BUTTONS: Return the button mask in a/x.
241 lda CIA1_PRB ; Read joystick #0
248 ;----------------------------------------------------------------------------
249 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
250 ; No return code required.
252 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
254 sei ; Disable interrupts
255 lda XPos ; Transfer the position
264 cli ; Enable interrupts
267 sta (ptr1),y ; Store last byte
271 ;----------------------------------------------------------------------------
272 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
273 ; struct pointed to by ptr1. No return code required.
275 ; We're cheating here to keep the code smaller: The first fields of the
276 ; mouse_info struct are identical to the mouse_pos struct, so we will just
277 ; call _mouse_pos to initialize the struct pointer and fill the position
282 ; Fill in the button state
284 jsr BUTTONS ; Will not touch ptr1
285 ldy #MOUSE_INFO::BUTTONS
290 ;----------------------------------------------------------------------------
291 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
292 ; specific data in ptr1, and the ioctl code in A.
293 ; Must return an error code in a/x.
296 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
297 ldx #>MOUSE_ERR_INV_IOCTL
300 ;----------------------------------------------------------------------------
301 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
302 ; (so be careful). The routine MUST return carry set if the interrupt has been
303 ; 'handled' - which means that the interrupt source is gone. Otherwise it
304 ; MUST return carry clear.
308 lda SID_ADConv1 ; Get mouse X movement
310 jsr MoveCheck ; Calculate movement vector
313 ; Skip processing if nothing has changed
317 ; Calculate the new X coordinate (--> a/y)
320 tay ; Remember low byte
325 ; Limit the X coordinate to the bounding box
343 ; Move the mouse pointer to the new X pos
348 ; Calculate the Y movement vector
350 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
352 jsr MoveCheck ; Calculate movement
355 ; Skip processing if nothing has changed
359 ; Calculate the new Y coordinate (--> a/y)
370 ; Limit the Y coordinate to the bounding box
388 ; Move the mouse pointer to the new X pos
396 clc ; Interrupt not "handled"
399 ; --------------------------------------------------------------------------
401 ; Move check routine, called for both coordinates.
403 ; Entry: y = old value of pot register
404 ; a = current value of pot register
405 ; Exit: y = value to use for old value
406 ; x/a = delta value for position
414 sub OldValue ; a = mod64 (new - old)
416 cmp #%01000000 ; if (a > 0)
419 beq @L2 ; if (a != 0)
420 ldy NewValue ; y = NewValue
424 @L1: ora #%11000000 ; else or in high order bits
425 cmp #$FF ; if (a != -1)
429 dex ; high byte = -1 (X = $FF)