2 ; Driver for the 1351 proportional mouse. Parts of the code are from
3 ; the Commodore 1351 mouse users guide.
5 ; 2009-09-26, Ullrich von Bassewitz
6 ; 2014-03-15, 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
48 .byte MOUSE_FLAG_LATE_IRQ
50 ; Callback table, set by the kernel before INSTALL is called
52 CHIDE: jmp $0000 ; Hide the cursor
53 CSHOW: jmp $0000 ; Show the cursor
54 CPREP: jmp $0000 ; Prepare to move the cursor
55 CDRAW: jmp $0000 ; Draw the cursor
56 CMOVEX: jmp $0000 ; Move the cursor to X coord
57 CMOVEY: jmp $0000 ; Move the cursor to Y coord
60 ;----------------------------------------------------------------------------
66 ;----------------------------------------------------------------------------
67 ; Global variables. The bounding box values are sorted so that they can be
68 ; written with the least effort in the SETBOX and GETBOX routines, so don't
74 OldPotX: .res 1 ; Old hw counter values
77 YPos: .res 2 ; Current mouse position, Y
78 XPos: .res 2 ; Current mouse position, X
79 XMin: .res 2 ; X1 value of bounding box
80 YMin: .res 2 ; Y1 value of bounding box
81 XMax: .res 2 ; X2 value of bounding box
82 YMax: .res 2 ; Y2 value of bounding box
84 OldValue: .res 1 ; Temp for MoveCheck routine
85 NewValue: .res 1 ; Temp for MoveCheck routine
91 ; Default values for above variables
92 ; (We use ".proc" because we want to define both a label and a scope.)
95 .byte 0, 0 ; OldPotX/OldPotY
96 .word SCREEN_HEIGHT/2 ; YPos
97 .word SCREEN_WIDTH/2 ; XPos
100 .word SCREEN_WIDTH ; XMax
101 .word SCREEN_HEIGHT ; YMax
106 ;----------------------------------------------------------------------------
107 ; INSTALL routine. Is called after the driver is loaded into memory. If
108 ; possible, check if the hardware is present.
109 ; Must return an MOUSE_ERR_xx code in a/x.
113 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
114 ; That allows direct access to the VIC-IIe's sprite registers.
121 ; Initialize variables. Just copy the default stuff over
123 ldx #.sizeof(DefVars)-1
129 ; Be sure the mouse cursor is invisible and at the default location. We
130 ; need to do that here, because our mouse interrupt handler doesn't set the
131 ; mouse position if it hasn't changed.
143 ; Done, return zero (= MOUSE_ERR_OK)
149 ;----------------------------------------------------------------------------
150 ; UNINSTALL routine. Is called before the driver is removed from memory.
151 ; No return code required (the driver is removed from memory on return).
154 jsr HIDE ; Hide cursor on exit
159 ;----------------------------------------------------------------------------
160 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
161 ; a counter for calls to show/hide, and the driver entry point is only called
162 ; if the mouse is currently visible and should get hidden. For most drivers,
163 ; no special action is required besides hiding the mouse cursor.
164 ; No return code required.
171 ;----------------------------------------------------------------------------
172 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
173 ; a counter for calls to show/hide, and the driver entry point is only called
174 ; if the mouse is currently hidden and should become visible. For most drivers,
175 ; no special action is required besides enabling the mouse cursor.
176 ; No return code required.
183 ;----------------------------------------------------------------------------
184 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
185 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
186 ; No checks are done if the mouse is currently inside the box, this is the job
187 ; of the caller. It is not necessary to validate the parameters, trust the
188 ; caller and save some code here. No return code required.
191 stx ptr1+1 ; Save data pointer
193 ldy #.sizeof (MOUSE_BOX)-1
204 ;----------------------------------------------------------------------------
205 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
206 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
209 stx ptr1+1 ; Save data pointer
211 ldy #.sizeof (MOUSE_BOX)-1
222 ;----------------------------------------------------------------------------
223 ; MOVE: Move the mouse to a new position. The position is passed as it comes
224 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
225 ; remove the parameter from the stack on return.
226 ; No checks are done if the new position is valid (within the bounding box or
227 ; the screen). No return code required.
230 MOVE: sei ; No interrupts
233 stx YPos+1 ; New Y position
242 sta XPos ; New X position
244 jsr CMOVEX ; Move the cursor
246 cli ; Allow interrupts
249 ;----------------------------------------------------------------------------
250 ; BUTTONS: Return the button mask in a/x.
256 lda CIA1_PRB ; Read joystick #0
263 ;----------------------------------------------------------------------------
264 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
265 ; No return code required.
267 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
269 sei ; Disable interrupts
270 lda XPos ; Transfer the position
279 cli ; Enable interrupts
282 sta (ptr1),y ; Store last byte
286 ;----------------------------------------------------------------------------
287 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
288 ; struct pointed to by ptr1. No return code required.
290 ; We're cheating here to keep the code smaller: The first fields of the
291 ; mouse_info struct are identical to the mouse_pos struct, so we will just
292 ; call _mouse_pos to initialize the struct pointer and fill the position
297 ; Fill in the button state
299 jsr BUTTONS ; Will not touch ptr1
300 ldy #MOUSE_INFO::BUTTONS
305 ;----------------------------------------------------------------------------
306 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
307 ; specific data in ptr1, and the ioctl code in A.
308 ; Must return an error code in a/x.
311 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
312 ldx #>MOUSE_ERR_INV_IOCTL
315 ;----------------------------------------------------------------------------
316 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
317 ; (so be careful). The routine MUST return carry set if the interrupt has been
318 ; 'handled' - which means that the interrupt source is gone. Otherwise it
319 ; MUST return carry clear.
323 lda SID_ADConv1 ; Get mouse X movement
325 jsr MoveCheck ; Calculate movement vector
328 ; Skip processing if nothing has changed
332 ; Calculate the new X coordinate (--> a/y)
335 tay ; Remember low byte
340 ; Limit the X coordinate to the bounding box
358 ; Move the mouse pointer to the new X pos
363 ; Calculate the Y movement vector
365 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
367 jsr MoveCheck ; Calculate movement
370 ; Skip processing if nothing has changed
374 ; Calculate the new Y coordinate (--> a/y)
385 ; Limit the Y coordinate to the bounding box
403 ; Move the mouse pointer to the new X pos
411 clc ; Interrupt not "handled"
414 ; --------------------------------------------------------------------------
416 ; Move check routine, called for both coordinates.
418 ; Entry: y = old value of pot register
419 ; a = current value of pot register
420 ; Exit: y = value to use for old value
421 ; x/a = delta value for position
429 sub OldValue ; a = mod64 (new - old)
431 cmp #%01000000 ; if (a > 0)
434 beq @L2 ; if (a != 0)
435 ldy NewValue ; y = NewValue
439 @L1: ora #%11000000 ; else or in high order bits
440 cmp #$FF ; if (a != -1)
444 dex ; high byte = -1 (X = $FF)