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
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
42 .byte MOUSE_FLAG_LATE_IRQ
44 ; Callback table, set by the kernel before INSTALL is called
46 CHIDE: jmp $0000 ; Hide the cursor
47 CSHOW: jmp $0000 ; Show the cursor
48 CMOVEX: jmp $0000 ; Move the cursor to X coord
49 CMOVEY: jmp $0000 ; Move the cursor to Y coord
52 ;----------------------------------------------------------------------------
58 ;----------------------------------------------------------------------------
59 ; Global variables. The bounding box values are sorted so that they can be
60 ; written with the least effort in the BOX routine, so don't reorder them.
65 OldPotX: .res 1 ; Old hw counter values
68 YPos: .res 2 ; Current mouse position, Y
69 XPos: .res 2 ; Current mouse position, X
70 YMax: .res 2 ; Y2 value of bounding box
71 XMax: .res 2 ; X2 value of bounding box
72 YMin: .res 2 ; Y1 value of bounding box
73 XMin: .res 2 ; X1 value of bounding box
75 OldValue: .res 1 ; Temp for MoveCheck routine
76 NewValue: .res 1 ; Temp for MoveCheck routine
78 ; Default values for above variables
83 .byte 0, 0 ; OldPotX/OldPotY
84 .word SCREEN_HEIGHT/2 ; YPos
85 .word SCREEN_WIDTH/2 ; XPos
86 .word SCREEN_HEIGHT ; YMax
87 .word SCREEN_WIDTH ; XMax
94 ;----------------------------------------------------------------------------
95 ; INSTALL routine. Is called after the driver is loaded into memory. If
96 ; possible, check if the hardware is present.
97 ; Must return an MOUSE_ERR_xx code in a/x.
101 ; Initialize variables. Just copy the default stuff over
103 ldx #.sizeof(DefVars)-1
109 ; Be sure the mouse cursor is invisible and at the default location. We
110 ; need to do that here, because our mouse interrupt handler doesn't set the
111 ; mouse position if it hasn't changed.
123 ; Done, return zero (= MOUSE_ERR_OK)
127 rts ; Run into UNINSTALL instead
129 ;----------------------------------------------------------------------------
130 ; UNINSTALL routine. Is called before the driver is removed from memory.
131 ; No return code required (the driver is removed from memory on return).
133 UNINSTALL = HIDE ; Hide cursor on exit
135 ;----------------------------------------------------------------------------
136 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
137 ; a counter for calls to show/hide, and the driver entry point is only called
138 ; if the mouse is currently visible and should get hidden. For most drivers,
139 ; no special action is required besides hiding the mouse cursor.
140 ; No return code required.
147 ;----------------------------------------------------------------------------
148 ; SHOW routine. Is called to show 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 hidden and should become visible. For most drivers,
151 ; no special action is required besides enabling the mouse cursor.
152 ; No return code required.
159 ;----------------------------------------------------------------------------
160 ; BOX: Set the mouse bounding box. The parameters are passed as they come from
161 ; the C program, that is, maxy in a/x and the other parameters on the stack.
162 ; The C wrapper will remove the parameters from the stack when the driver
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.
181 ;----------------------------------------------------------------------------
182 ; MOVE: Move the mouse to a new position. The position is passed as it comes
183 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
184 ; remove the parameter from the stack on return.
185 ; No checks are done if the new position is valid (within the bounding box or
186 ; the screen). No return code required.
189 MOVE: sei ; No interrupts
192 stx YPos+1 ; New Y position
201 sta XPos ; New X position
203 jsr CMOVEX ; Move the cursor
205 cli ; Allow interrupts
208 ;----------------------------------------------------------------------------
209 ; BUTTONS: Return the button mask in a/x.
215 lda CIA1_PRB ; Read joystick #0
222 ;----------------------------------------------------------------------------
223 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
224 ; No return code required.
226 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
228 sei ; Disable interrupts
229 lda XPos ; Transfer the position
238 cli ; Enable interrupts
241 sta (ptr1),y ; Store last byte
245 ;----------------------------------------------------------------------------
246 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
247 ; struct pointed to by ptr1. No return code required.
249 ; We're cheating here to keep the code smaller: The first fields of the
250 ; mouse_info struct are identical to the mouse_pos struct, so we will just
251 ; call _mouse_pos to initialize the struct pointer and fill the position
256 ; Fill in the button state
258 jsr BUTTONS ; Will not touch ptr1
259 ldy #MOUSE_INFO::BUTTONS
264 ;----------------------------------------------------------------------------
265 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
266 ; specific data in ptr1, and the ioctl code in A.
267 ; Must return an error code in a/x.
270 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
271 ldx #>MOUSE_ERR_INV_IOCTL
274 ;----------------------------------------------------------------------------
275 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
276 ; (so be careful). The routine MUST return carry set if the interrupt has been
277 ; 'handled' - which means that the interrupt source is gone. Otherwise it
278 ; MUST return carry clear.
281 IRQ: lda SID_ADConv1 ; Get mouse X movement
283 jsr MoveCheck ; Calculate movement vector
286 ; Skip processing if nothing has changed
290 ; Calculate the new X coordinate (--> a/y)
293 tay ; Remember low byte
298 ; Limit the X coordinate to the bounding box
316 ; Move the mouse pointer to the new X pos
321 ; Calculate the Y movement vector
323 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
325 jsr MoveCheck ; Calculate movement
328 ; Skip processing if nothing has changed
332 ; Calculate the new Y coordinate (--> a/y)
343 ; Limit the Y coordinate to the bounding box
361 ; Move the mouse pointer to the new X pos
368 clc ; Interrupt not "handled"
371 ; --------------------------------------------------------------------------
373 ; Move check routine, called for both coordinates.
375 ; Entry: y = old value of pot register
376 ; a = current value of pot register
377 ; Exit: y = value to use for old value
378 ; x/a = delta value for position
386 sub OldValue ; a = mod64 (new - old)
388 cmp #%01000000 ; if (a > 0)
391 beq @L2 ; if (a != 0)
392 ldy NewValue ; y = NewValue
396 @L1: ora #%11000000 ; else or in high order bits
397 cmp #$FF ; if (a != -1)
401 dex ; high byte = -1 (X = $FF)