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
40 ; Callback table, set by the kernel before INSTALL is called
42 CHIDE: jmp $0000 ; Hide the cursor
43 CSHOW: jmp $0000 ; Show the cursor
44 CMOVEX: jmp $0000 ; Move the cursor to X coord
45 CMOVEY: jmp $0000 ; Move the cursor to Y coord
48 ;----------------------------------------------------------------------------
54 ;----------------------------------------------------------------------------
55 ; Global variables. The bounding box values are sorted so that they can be
56 ; written with the least effort in the BOX routine, so don't reorder them.
61 OldPotX: .res 1 ; Old hw counter values
64 YPos: .res 2 ; Current mouse position, Y
65 XPos: .res 2 ; Current mouse position, X
66 YMax: .res 2 ; Y2 value of bounding box
67 XMax: .res 2 ; X2 value of bounding box
68 YMin: .res 2 ; Y1 value of bounding box
69 XMin: .res 2 ; X1 value of bounding box
71 OldValue: .res 1 ; Temp for MoveCheck routine
72 NewValue: .res 1 ; Temp for MoveCheck routine
74 ; Default values for above variables
79 .byte 0, 0 ; OldPotX/OldPotY
80 .word SCREEN_HEIGHT/2 ; YPos
81 .word SCREEN_WIDTH/2 ; XPos
82 .word SCREEN_HEIGHT ; YMax
83 .word SCREEN_WIDTH ; XMax
90 ;----------------------------------------------------------------------------
91 ; INSTALL routine. Is called after the driver is loaded into memory. If
92 ; possible, check if the hardware is present.
93 ; Must return an MOUSE_ERR_xx code in a/x.
97 ; Initialize variables. Just copy the default stuff over
99 ldx #.sizeof(DefVars)-1
105 ; Be sure the mouse cursor is invisible and at the default location. We
106 ; need to do that here, because our mouse interrupt handler doesn't set the
107 ; mouse position if it hasn't changed.
119 ; Done, return zero (= MOUSE_ERR_OK)
123 rts ; Run into UNINSTALL instead
125 ;----------------------------------------------------------------------------
126 ; UNINSTALL routine. Is called before the driver is removed from memory.
127 ; No return code required (the driver is removed from memory on return).
129 UNINSTALL = HIDE ; Hide cursor on exit
131 ;----------------------------------------------------------------------------
132 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
133 ; a counter for calls to show/hide, and the driver entry point is only called
134 ; if the mouse is currently visible and should get hidden. For most drivers,
135 ; no special action is required besides hiding the mouse cursor.
136 ; No return code required.
143 ;----------------------------------------------------------------------------
144 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
145 ; a counter for calls to show/hide, and the driver entry point is only called
146 ; if the mouse is currently hidden and should become visible. For most drivers,
147 ; no special action is required besides enabling the mouse cursor.
148 ; No return code required.
155 ;----------------------------------------------------------------------------
156 ; BOX: Set the mouse bounding box. The parameters are passed as they come from
157 ; the C program, that is, maxy in a/x and the other parameters on the stack.
158 ; The C wrapper will remove the parameters from the stack when the driver
160 ; No checks are done if the mouse is currently inside the box, this is the job
161 ; of the caller. It is not necessary to validate the parameters, trust the
162 ; caller and save some code here. No return code required.
177 ;----------------------------------------------------------------------------
178 ; MOVE: Move the mouse to a new position. The position is passed as it comes
179 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
180 ; remove the parameter from the stack on return.
181 ; No checks are done if the new position is valid (within the bounding box or
182 ; the screen). No return code required.
185 MOVE: sei ; No interrupts
188 stx YPos+1 ; New Y position
197 sta XPos ; New X position
199 jsr CMOVEX ; Move the cursor
201 cli ; Allow interrupts
204 ;----------------------------------------------------------------------------
205 ; BUTTONS: Return the button mask in a/x.
211 lda CIA1_PRB ; Read joystick #0
218 ;----------------------------------------------------------------------------
219 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
220 ; No return code required.
222 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
224 sei ; Disable interrupts
225 lda XPos ; Transfer the position
234 cli ; Enable interrupts
237 sta (ptr1),y ; Store last byte
241 ;----------------------------------------------------------------------------
242 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
243 ; struct pointed to by ptr1. No return code required.
245 ; We're cheating here to keep the code smaller: The first fields of the
246 ; mouse_info struct are identical to the mouse_pos struct, so we will just
247 ; call _mouse_pos to initialize the struct pointer and fill the position
252 ; Fill in the button state
254 jsr BUTTONS ; Will not touch ptr1
255 ldy #MOUSE_INFO::BUTTONS
260 ;----------------------------------------------------------------------------
261 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
262 ; specific data in ptr1, and the ioctl code in A.
263 ; Must return an error code in a/x.
266 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
267 ldx #>MOUSE_ERR_INV_IOCTL
270 ;----------------------------------------------------------------------------
271 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
275 IRQ: lda SID_ADConv1 ; Get mouse X movement
277 jsr MoveCheck ; Calculate movement vector
280 ; Skip processing if nothing has changed
284 ; Calculate the new X coordinate (--> a/y)
287 tay ; Remember low byte
292 ; Limit the X coordinate to the bounding box
310 ; Move the mouse pointer to the new X pos
315 ; Calculate the Y movement vector
317 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
319 jsr MoveCheck ; Calculate movement
322 ; Skip processing if nothing has changed
326 ; Calculate the new Y coordinate (--> a/y)
337 ; Limit the Y coordinate to the bounding box
355 ; Move the mouse pointer to the new X pos
364 ; --------------------------------------------------------------------------
366 ; Move check routine, called for both coordinates.
368 ; Entry: y = old value of pot register
369 ; a = current value of pot register
370 ; Exit: y = value to use for old value
371 ; x/a = delta value for position
379 sub OldValue ; a = mod64 (new - old)
381 cmp #%01000000 ; if (a > 0)
384 beq @L2 ; if (a != 0)
385 ldy NewValue ; y = NewValue
389 @L1: ora #%11000000 ; else or in high order bits
390 cmp #$FF ; if (a != -1)
394 dex ; high byte = -1 (X = $FF)