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 CMOVEX: jmp $0000 ; Move the cursor to X coord
54 CMOVEY: jmp $0000 ; Move the cursor to Y coord
57 ;----------------------------------------------------------------------------
63 ;----------------------------------------------------------------------------
64 ; Global variables. The bounding box values are sorted so that they can be
65 ; written with the least effort in the SETBOX and GETBOX routines, so don't
71 OldPotX: .res 1 ; Old hw counter values
74 YPos: .res 2 ; Current mouse position, Y
75 XPos: .res 2 ; Current mouse position, X
76 XMin: .res 2 ; X1 value of bounding box
77 YMin: .res 2 ; Y1 value of bounding box
78 XMax: .res 2 ; X2 value of bounding box
79 YMax: .res 2 ; Y2 value of bounding box
81 OldValue: .res 1 ; Temp for MoveCheck routine
82 NewValue: .res 1 ; Temp for MoveCheck routine
86 ; Default values for below variables
87 ; (We use ".proc" because we want to define both a label and a scope.)
90 .byte 0, 0 ; OldPotX/OldPotY
91 .word SCREEN_HEIGHT/2 ; YPos
92 .word SCREEN_WIDTH/2 ; XPos
95 .word SCREEN_WIDTH ; XMax
96 .word SCREEN_HEIGHT ; YMax
101 ;----------------------------------------------------------------------------
102 ; INSTALL routine. Is called after the driver is loaded into memory. If
103 ; possible, check if the hardware is present.
104 ; Must return an MOUSE_ERR_xx code in a/x.
108 ; Initialize variables. Just copy the default stuff over
110 ldx #.sizeof(DefVars)-1
116 ; Be sure the mouse cursor is invisible and at the default location. We
117 ; need to do that here, because our mouse interrupt handler doesn't set the
118 ; mouse position if it hasn't changed.
130 ; Done, return zero (= MOUSE_ERR_OK)
134 rts ; Run into UNINSTALL instead
136 ;----------------------------------------------------------------------------
137 ; UNINSTALL routine. Is called before the driver is removed from memory.
138 ; No return code required (the driver is removed from memory on return).
140 UNINSTALL = HIDE ; Hide cursor on exit
142 ;----------------------------------------------------------------------------
143 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
144 ; a counter for calls to show/hide, and the driver entry point is only called
145 ; if the mouse is currently visible and should get hidden. For most drivers,
146 ; no special action is required besides hiding the mouse cursor.
147 ; No return code required.
154 ;----------------------------------------------------------------------------
155 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
156 ; a counter for calls to show/hide, and the driver entry point is only called
157 ; if the mouse is currently hidden and should become visible. For most drivers,
158 ; no special action is required besides enabling the mouse cursor.
159 ; No return code required.
166 ;----------------------------------------------------------------------------
167 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
168 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
169 ; No checks are done if the mouse is currently inside the box, this is the job
170 ; of the caller. It is not necessary to validate the parameters, trust the
171 ; caller and save some code here. No return code required.
174 stx ptr1+1 ; Save data pointer
176 ldy #.sizeof (MOUSE_BOX)-1
187 ;----------------------------------------------------------------------------
188 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
189 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
192 stx ptr1+1 ; Save data pointer
194 ldy #.sizeof (MOUSE_BOX)-1
205 ;----------------------------------------------------------------------------
206 ; MOVE: Move the mouse to a new position. The position is passed as it comes
207 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
208 ; remove the parameter from the stack on return.
209 ; No checks are done if the new position is valid (within the bounding box or
210 ; the screen). No return code required.
213 MOVE: sei ; No interrupts
216 stx YPos+1 ; New Y position
225 sta XPos ; New X position
227 jsr CMOVEX ; Move the cursor
229 cli ; Allow interrupts
232 ;----------------------------------------------------------------------------
233 ; BUTTONS: Return the button mask in a/x.
239 lda CIA1_PRB ; Read joystick #0
246 ;----------------------------------------------------------------------------
247 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
248 ; No return code required.
250 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
252 sei ; Disable interrupts
253 lda XPos ; Transfer the position
262 cli ; Enable interrupts
265 sta (ptr1),y ; Store last byte
269 ;----------------------------------------------------------------------------
270 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
271 ; struct pointed to by ptr1. No return code required.
273 ; We're cheating here to keep the code smaller: The first fields of the
274 ; mouse_info struct are identical to the mouse_pos struct, so we will just
275 ; call _mouse_pos to initialize the struct pointer and fill the position
280 ; Fill in the button state
282 jsr BUTTONS ; Will not touch ptr1
283 ldy #MOUSE_INFO::BUTTONS
288 ;----------------------------------------------------------------------------
289 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
290 ; specific data in ptr1, and the ioctl code in A.
291 ; Must return an error code in a/x.
294 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
295 ldx #>MOUSE_ERR_INV_IOCTL
298 ;----------------------------------------------------------------------------
299 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
300 ; (so be careful). The routine MUST return carry set if the interrupt has been
301 ; 'handled' - which means that the interrupt source is gone. Otherwise it
302 ; MUST return carry clear.
305 IRQ: lda SID_ADConv1 ; Get mouse X movement
307 jsr MoveCheck ; Calculate movement vector
310 ; Skip processing if nothing has changed
314 ; Calculate the new X coordinate (--> a/y)
317 tay ; Remember low byte
322 ; Limit the X coordinate to the bounding box
340 ; Move the mouse pointer to the new X pos
345 ; Calculate the Y movement vector
347 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
349 jsr MoveCheck ; Calculate movement
352 ; Skip processing if nothing has changed
356 ; Calculate the new Y coordinate (--> a/y)
367 ; Limit the Y coordinate to the bounding box
385 ; Move the mouse pointer to the new X pos
392 clc ; Interrupt not "handled"
395 ; --------------------------------------------------------------------------
397 ; Move check routine, called for both coordinates.
399 ; Entry: y = old value of pot register
400 ; a = current value of pot register
401 ; Exit: y = value to use for old value
402 ; x/a = delta value for position
410 sub OldValue ; a = mod64 (new - old)
412 cmp #%01000000 ; if (a > 0)
415 beq @L2 ; if (a != 0)
416 ldy NewValue ; y = NewValue
420 @L1: ora #%11000000 ; else or in high order bits
421 cmp #$FF ; if (a != -1)
425 dex ; high byte = -1 (X = $FF)