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
8 ; The driver prevents the keyboard from interfering by changing the
9 ; keyboard's output port into an input port while the driver reads its
10 ; controller device. That disables a wire that is left active by the
11 ; Kernal. That wire is used by the STOP-key to break out of BASIC
12 ; programs -- CC65 programs don't use that feature. The wire is shared
13 ; by these keys: STOP, "Q", Commodore, Space, "2", CTRL, Left-Arrow, and
14 ; "1". I listed them, in order, from bit 7 over to bit 0. The
15 ; rightmost five keys can look like joystick switches.
17 ; The driver prevents the mouse/joystick from interfering by "blinding"
18 ; the keyboard scanner while any button/switch is active. It changes
19 ; the input port into an output port, then stores all zero-bits in that
20 ; port's latch. Reading from an output port sees the bitwise-AND of the
21 ; latch and the input signals. Therefore, the scanner thinks that eight
22 ; keys are being pushed at the same time. It doesn't know what to do
23 ; about that condition; so, it does nothing. The driver lets the
24 ; scanner see normally, again, when no buttons/switches are active.
27 .include "zeropage.inc"
28 .include "mouse-kernel.inc"
33 ; ------------------------------------------------------------------------
34 ; Header. Includes jump table
42 .byte $6d, $6f, $75 ; "mou"
43 .byte MOUSE_API_VERSION ; Mouse driver API version number
62 .byte MOUSE_FLAG_LATE_IRQ
64 ; Callback table, set by the kernel before INSTALL is called
66 CHIDE: jmp $0000 ; Hide the cursor
67 CSHOW: jmp $0000 ; Show the cursor
68 CMOVEX: jmp $0000 ; Move the cursor to X coord
69 CMOVEY: jmp $0000 ; Move the cursor to Y coord
72 ;----------------------------------------------------------------------------
75 SCREEN_HEIGHT = YSIZE * 8 - 1 ; (origin is zero)
76 SCREEN_WIDTH = XSIZE * 8 - 1
78 ;----------------------------------------------------------------------------
79 ; Global variables. The bounding box values are sorted so that they can be
80 ; written with the least effort in the SETBOX and GETBOX routines, so don't
86 OldPotX: .res 1 ; Old hw counter values
89 XPos: .res 2 ; Current mouse position, X
90 YPos: .res 2 ; Current mouse position, Y
91 XMin: .res 2 ; X1 value of bounding box
92 YMin: .res 2 ; Y1 value of bounding box
93 XMax: .res 2 ; X2 value of bounding box
94 YMax: .res 2 ; Y2 value of bounding box
95 Buttons: .res 1 ; button status bits
97 OldValue: .res 1 ; Temp for MoveCheck routine
98 NewValue: .res 1 ; Temp for MoveCheck routine
100 ; Default values for above variables
104 ; (We use ".proc" because we want to define both a label and a scope.)
107 .byte 0, 0 ; OldPotX/OldPotY
108 .word SCREEN_WIDTH/2 ; XPos
109 .word SCREEN_HEIGHT/2 ; YPos
112 .word SCREEN_WIDTH ; XMax
113 .word SCREEN_HEIGHT ; YMax
114 .byte %00000000 ; Buttons
119 ;----------------------------------------------------------------------------
120 ; INSTALL routine. Is called after the driver is loaded into memory. If
121 ; possible, check if the hardware is present.
122 ; Must return an MOUSE_ERR_xx code in a/x.
126 ; Initialize variables. Just copy the default stuff over
128 ldx #.sizeof(DefVars)-1
134 ; Be sure the mouse cursor is invisible and at the default location. We
135 ; need to do that here, because our mouse interrupt handler doesn't set the
136 ; mouse position if it hasn't changed.
148 ; Done, return zero (= MOUSE_ERR_OK)
154 ;----------------------------------------------------------------------------
155 ; UNINSTALL routine. Is called before the driver is removed from memory.
156 ; No return code required (the driver is removed from memory on return).
158 UNINSTALL = HIDE ; Hide cursor on exit
160 ;----------------------------------------------------------------------------
161 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
162 ; a counter for calls to show/hide, and the driver entry point is only called
163 ; if the mouse is currently visible and should get hidden. For most drivers,
164 ; no special action is required besides hiding the mouse cursor.
165 ; No return code required.
172 ;----------------------------------------------------------------------------
173 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
174 ; a counter for calls to show/hide, and the driver entry point is only called
175 ; if the mouse is currently hidden and should become visible. For most drivers,
176 ; no special action is required besides enabling the mouse cursor.
177 ; No return code required.
184 ;----------------------------------------------------------------------------
185 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
186 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
187 ; No checks are done if the mouse is currently inside the box, this is the job
188 ; of the caller. It is not necessary to validate the parameters, trust the
189 ; caller and save some code here. No return code required.
192 stx ptr1+1 ; Save data pointer
194 ldy #.sizeof (MOUSE_BOX)-1
205 ;----------------------------------------------------------------------------
206 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
207 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
210 stx ptr1+1 ; Save data pointer
212 ldy #.sizeof (MOUSE_BOX)-1
219 ;----------------------------------------------------------------------------
220 ; MOVE: Move the mouse to a new position. The position is passed as it comes
221 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
222 ; remove the parameter from the stack on return.
223 ; No checks are done if the new position is valid (within the bounding box or
224 ; the screen). No return code required.
227 MOVE: sei ; No interrupts
230 stx YPos+1 ; New Y position
239 sta XPos ; New X position
241 jsr CMOVEX ; Move the cursor
243 cli ; Allow interrupts
246 ;----------------------------------------------------------------------------
247 ; BUTTONS: Return the button mask in a/x.
255 ;----------------------------------------------------------------------------
256 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
257 ; No return code required.
259 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
261 sei ; Disable interrupts
262 lda XPos ; Transfer the position
271 cli ; Enable interrupts
274 sta (ptr1),y ; Store last byte
278 ;----------------------------------------------------------------------------
279 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
280 ; struct pointed to by ptr1. No return code required.
282 ; We're cheating here to keep the code smaller: The first fields of the
283 ; mouse_info struct are identical to the mouse_pos struct, so we will just
284 ; call _mouse_pos to initialize the struct pointer and fill the position
289 ; Fill in the button state
291 jsr BUTTONS ; Will not touch ptr1
292 ldy #MOUSE_INFO::BUTTONS
297 ;----------------------------------------------------------------------------
298 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
299 ; specific data in ptr1, and the ioctl code in A.
300 ; Must return an error code in a/x.
303 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
304 ldx #>MOUSE_ERR_INV_IOCTL
307 ;----------------------------------------------------------------------------
308 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
309 ; (so be careful). The routine MUST return carry set if the interrupt has been
310 ; 'handled' - which means that the interrupt source is gone. Otherwise it
311 ; MUST return carry clear.
316 ; Record the state of the buttons.
317 ; Avoid crosstalk between the keyboard and the mouse.
319 ldy #%00000000 ; Set ports A and B to input
321 sty CIA1_DDRA ; Keyboard won't look like mouse
322 lda CIA1_PRB ; Read Control-Port 1
323 dec CIA1_DDRA ; Set port A back to output
324 eor #%11111111 ; Bit goes up when button goes down
327 dec CIA1_DDRB ; Mouse won't look like keyboard
328 sty CIA1_PRB ; Set "all keys pushed"
330 @L0: lda SID_ADConv1 ; Get mouse X movement
332 jsr MoveCheck ; Calculate movement vector
334 ; Skip processing if nothing has changed
339 ; Calculate the new X coordinate (--> a/y)
342 tay ; Remember low byte
347 ; Limit the X coordinate to the bounding box
365 ; Move the mouse pointer to the new X pos
370 ; Calculate the Y movement vector
372 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
374 jsr MoveCheck ; Calculate movement
376 ; Skip processing if nothing has changed
381 ; Calculate the new Y coordinate (--> a/y)
392 ; Limit the Y coordinate to the bounding box
410 ; Move the mouse pointer to the new Y pos
417 clc ; Interrupt not "handled"
420 ; --------------------------------------------------------------------------
422 ; Move check routine, called for both coordinates.
424 ; Entry: y = old value of pot register
425 ; a = current value of pot register
426 ; Exit: y = value to use for old value
427 ; x/a = delta value for position
435 sub OldValue ; a = mod64 (new - old)
437 cmp #%01000000 ; if (a > 0)
440 beq @L2 ; if (a != 0)
441 ldy NewValue ; y = NewValue
445 @L1: ora #%11000000 ; else, "or" in high-order bits
446 cmp #$FF ; if (a != -1)
450 dex ; high byte = -1 (X = $FF)