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
43 .byte MOUSE_FLAG_LATE_IRQ
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 CMOVEX: jmp $0000 ; Move the cursor to X coord
50 CMOVEY: jmp $0000 ; Move the cursor to Y coord
53 ;----------------------------------------------------------------------------
59 ;----------------------------------------------------------------------------
60 ; Global variables. The bounding box values are sorted so that they can be
61 ; written with the least effort in the SETBOX and GETBOX routines, so don't
67 OldPotX: .res 1 ; Old hw counter values
70 YPos: .res 2 ; Current mouse position, Y
71 XPos: .res 2 ; Current mouse position, X
72 XMin: .res 2 ; X1 value of bounding box
73 YMin: .res 2 ; Y1 value of bounding box
74 XMax: .res 2 ; X2 value of bounding box
75 YMax: .res 2 ; Y2 value of bounding box
77 OldValue: .res 1 ; Temp for MoveCheck routine
78 NewValue: .res 1 ; Temp for MoveCheck routine
80 ; Default values for above variables
85 .byte 0, 0 ; OldPotX/OldPotY
86 .word SCREEN_HEIGHT/2 ; YPos
87 .word SCREEN_WIDTH/2 ; XPos
90 .word SCREEN_WIDTH ; XMax
91 .word SCREEN_HEIGHT ; YMax
96 ;----------------------------------------------------------------------------
97 ; INSTALL routine. Is called after the driver is loaded into memory. If
98 ; possible, check if the hardware is present.
99 ; Must return an MOUSE_ERR_xx code in a/x.
103 ; Initialize variables. Just copy the default stuff over
105 ldx #.sizeof(DefVars)-1
111 ; Be sure the mouse cursor is invisible and at the default location. We
112 ; need to do that here, because our mouse interrupt handler doesn't set the
113 ; mouse position if it hasn't changed.
125 ; Done, return zero (= MOUSE_ERR_OK)
129 rts ; Run into UNINSTALL instead
131 ;----------------------------------------------------------------------------
132 ; UNINSTALL routine. Is called before the driver is removed from memory.
133 ; No return code required (the driver is removed from memory on return).
135 UNINSTALL = HIDE ; Hide cursor on exit
137 ;----------------------------------------------------------------------------
138 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
139 ; a counter for calls to show/hide, and the driver entry point is only called
140 ; if the mouse is currently visible and should get hidden. For most drivers,
141 ; no special action is required besides hiding the mouse cursor.
142 ; No return code required.
149 ;----------------------------------------------------------------------------
150 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
151 ; a counter for calls to show/hide, and the driver entry point is only called
152 ; if the mouse is currently hidden and should become visible. For most drivers,
153 ; no special action is required besides enabling the mouse cursor.
154 ; No return code required.
161 ;----------------------------------------------------------------------------
162 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
163 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
164 ; No checks are done if the mouse is currently inside the box, this is the job
165 ; of the caller. It is not necessary to validate the parameters, trust the
166 ; caller and save some code here. No return code required.
169 stx ptr1+1 ; Save data pointer
171 ldy #.sizeof (MOUSE_BOX)-1
182 ;----------------------------------------------------------------------------
183 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
184 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
187 stx ptr1+1 ; Save data pointer
189 ldy #.sizeof (MOUSE_BOX)-1
200 ;----------------------------------------------------------------------------
201 ; MOVE: Move the mouse to a new position. The position is passed as it comes
202 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
203 ; remove the parameter from the stack on return.
204 ; No checks are done if the new position is valid (within the bounding box or
205 ; the screen). No return code required.
208 MOVE: sei ; No interrupts
211 stx YPos+1 ; New Y position
220 sta XPos ; New X position
222 jsr CMOVEX ; Move the cursor
224 cli ; Allow interrupts
227 ;----------------------------------------------------------------------------
228 ; BUTTONS: Return the button mask in a/x.
234 lda CIA1_PRB ; Read joystick #0
241 ;----------------------------------------------------------------------------
242 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
243 ; No return code required.
245 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
247 sei ; Disable interrupts
248 lda XPos ; Transfer the position
257 cli ; Enable interrupts
260 sta (ptr1),y ; Store last byte
264 ;----------------------------------------------------------------------------
265 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
266 ; struct pointed to by ptr1. No return code required.
268 ; We're cheating here to keep the code smaller: The first fields of the
269 ; mouse_info struct are identical to the mouse_pos struct, so we will just
270 ; call _mouse_pos to initialize the struct pointer and fill the position
275 ; Fill in the button state
277 jsr BUTTONS ; Will not touch ptr1
278 ldy #MOUSE_INFO::BUTTONS
283 ;----------------------------------------------------------------------------
284 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
285 ; specific data in ptr1, and the ioctl code in A.
286 ; Must return an error code in a/x.
289 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
290 ldx #>MOUSE_ERR_INV_IOCTL
293 ;----------------------------------------------------------------------------
294 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
295 ; (so be careful). The routine MUST return carry set if the interrupt has been
296 ; 'handled' - which means that the interrupt source is gone. Otherwise it
297 ; MUST return carry clear.
300 IRQ: lda SID_ADConv1 ; Get mouse X movement
302 jsr MoveCheck ; Calculate movement vector
305 ; Skip processing if nothing has changed
309 ; Calculate the new X coordinate (--> a/y)
312 tay ; Remember low byte
317 ; Limit the X coordinate to the bounding box
335 ; Move the mouse pointer to the new X pos
340 ; Calculate the Y movement vector
342 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
344 jsr MoveCheck ; Calculate movement
347 ; Skip processing if nothing has changed
351 ; Calculate the new Y coordinate (--> a/y)
362 ; Limit the Y coordinate to the bounding box
380 ; Move the mouse pointer to the new X pos
387 clc ; Interrupt not handled
390 ; --------------------------------------------------------------------------
392 ; Move check routine, called for both coordinates.
394 ; Entry: y = old value of pot register
395 ; a = current value of pot register
396 ; Exit: y = value to use for old value
397 ; x/a = delta value for position
405 sub OldValue ; a = mod64 (new - old)
407 cmp #%01000000 ; if (a > 0)
410 beq @L2 ; if (a != 0)
411 ldy NewValue ; y = NewValue
415 @L1: ora #%11000000 ; else or in high order bits
416 cmp #$FF ; if (a != -1)
420 dex ; high byte = -1 (X = $FF)