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 ; 2010-02-06, 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
44 .byte MOUSE_FLAG_LATE_IRQ
46 ; Callback table, set by the kernel before INSTALL is called
48 CHIDE: jmp $0000 ; Hide the cursor
49 CSHOW: jmp $0000 ; Show the cursor
50 CMOVEX: jmp $0000 ; Move the cursor to X coord
51 CMOVEY: jmp $0000 ; Move the cursor to Y coord
54 ;----------------------------------------------------------------------------
57 SCREEN_HEIGHT = YSIZE * 8 - 1 ; (origin is zero)
58 SCREEN_WIDTH = XSIZE * 8 - 1
60 ;----------------------------------------------------------------------------
61 ; Global variables. The bounding box values are sorted so that they can be
62 ; written with the least effort in the SETBOX and GETBOX routines, so don't
68 OldPotX: .res 1 ; Old hw counter values
71 XPos: .res 2 ; Current mouse position, X
72 YPos: .res 2 ; Current mouse position, Y
73 XMin: .res 2 ; X1 value of bounding box
74 YMin: .res 2 ; Y1 value of bounding box
75 XMax: .res 2 ; X2 value of bounding box
76 YMax: .res 2 ; Y2 value of bounding box
77 Buttons: .res 1 ; button status bits
79 OldValue: .res 1 ; Temp for MoveCheck routine
80 NewValue: .res 1 ; Temp for MoveCheck routine
82 ; Default values for above variables
86 ; (We use ".proc" because we want to define both a label and a scope.)
89 .byte 0, 0 ; OldPotX/OldPotY
90 .word SCREEN_WIDTH/2 ; XPos
91 .word SCREEN_HEIGHT/2 ; YPos
94 .word SCREEN_WIDTH ; XMax
95 .word SCREEN_HEIGHT ; YMax
96 .byte %00000000 ; Buttons
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)
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
201 ;----------------------------------------------------------------------------
202 ; MOVE: Move the mouse to a new position. The position is passed as it comes
203 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
204 ; remove the parameter from the stack on return.
205 ; No checks are done if the new position is valid (within the bounding box or
206 ; the screen). No return code required.
209 MOVE: sei ; No interrupts
212 stx YPos+1 ; New Y position
221 sta XPos ; New X position
223 jsr CMOVEX ; Move the cursor
225 cli ; Allow interrupts
228 ;----------------------------------------------------------------------------
229 ; BUTTONS: Return the button mask in a/x.
237 ;----------------------------------------------------------------------------
238 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
239 ; No return code required.
241 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
243 sei ; Disable interrupts
244 lda XPos ; Transfer the position
253 cli ; Enable interrupts
256 sta (ptr1),y ; Store last byte
260 ;----------------------------------------------------------------------------
261 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
262 ; struct pointed to by ptr1. No return code required.
264 ; We're cheating here to keep the code smaller: The first fields of the
265 ; mouse_info struct are identical to the mouse_pos struct, so we will just
266 ; call _mouse_pos to initialize the struct pointer and fill the position
271 ; Fill in the button state
273 jsr BUTTONS ; Will not touch ptr1
274 ldy #MOUSE_INFO::BUTTONS
279 ;----------------------------------------------------------------------------
280 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
281 ; specific data in ptr1, and the ioctl code in A.
282 ; Must return an error code in a/x.
285 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
286 ldx #>MOUSE_ERR_INV_IOCTL
289 ;----------------------------------------------------------------------------
290 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
291 ; (so be careful). The routine MUST return carry set if the interrupt has been
292 ; 'handled' - which means that the interrupt source is gone. Otherwise it
293 ; MUST return carry clear.
298 ; Record the state of the buttons.
299 ; Avoid crosstalk between the keyboard and the mouse.
301 ldy #%00000000 ; Set ports A and B to input
303 sty CIA1_DDRA ; Keyboard won't look like mouse
304 lda CIA1_PRB ; Read Control-Port 1
305 dec CIA1_DDRA ; Set port A back to output
306 eor #%11111111 ; Bit goes up when button goes down
309 dec CIA1_DDRB ; Mouse won't look like keyboard
310 sty CIA1_PRB ; Set "all keys pushed"
312 @L0: lda SID_ADConv1 ; Get mouse X movement
314 jsr MoveCheck ; Calculate movement vector
316 ; Skip processing if nothing has changed
321 ; Calculate the new X coordinate (--> a/y)
324 tay ; Remember low byte
329 ; Limit the X coordinate to the bounding box
347 ; Move the mouse pointer to the new X pos
352 ; Calculate the Y movement vector
354 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
356 jsr MoveCheck ; Calculate movement
358 ; Skip processing if nothing has changed
363 ; Calculate the new Y coordinate (--> a/y)
374 ; Limit the Y coordinate to the bounding box
392 ; Move the mouse pointer to the new Y pos
399 clc ; Interrupt not "handled"
402 ; --------------------------------------------------------------------------
404 ; Move check routine, called for both coordinates.
406 ; Entry: y = old value of pot register
407 ; a = current value of pot register
408 ; Exit: y = value to use for old value
409 ; x/a = delta value for position
417 sub OldValue ; a = mod64 (new - old)
419 cmp #%01000000 ; if (a > 0)
422 beq @L2 ; if (a != 0)
423 ldy NewValue ; y = NewValue
427 @L1: ora #%11000000 ; else, "or" in high-order bits
428 cmp #$FF ; if (a != -1)
432 dex ; high byte = -1 (X = $FF)