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-17, Greg King
9 .include "zeropage.inc"
10 .include "mouse-kernel.inc"
17 ; ------------------------------------------------------------------------
18 ; Header. Includes jump table
26 .byte $6d, $6f, $75 ; "mou"
27 .byte MOUSE_API_VERSION ; Mouse driver API version number
51 .byte MOUSE_FLAG_LATE_IRQ
53 ; Callback table, set by the kernel before INSTALL is called
55 CHIDE: jmp $0000 ; Hide the cursor
56 CSHOW: jmp $0000 ; Show the cursor
57 CPREP: jmp $0000 ; Prepare to move the cursor
58 CDRAW: jmp $0000 ; Draw the cursor
59 CMOVEX: jmp $0000 ; Move the cursor to X coord
60 CMOVEY: jmp $0000 ; Move the cursor to Y coord
63 ;----------------------------------------------------------------------------
69 ;----------------------------------------------------------------------------
70 ; Global variables. The bounding box values are sorted so that they can be
71 ; written with the least effort in the SETBOX and GETBOX routines, so don't
77 OldPotX: .res 1 ; Old hw counter values
80 YPos: .res 2 ; Current mouse position, Y
81 XPos: .res 2 ; Current mouse position, X
82 XMin: .res 2 ; X1 value of bounding box
83 YMin: .res 2 ; Y1 value of bounding box
84 XMax: .res 2 ; X2 value of bounding box
85 YMax: .res 2 ; Y2 value of bounding box
87 OldValue: .res 1 ; Temp for MoveCheck routine
88 NewValue: .res 1 ; Temp for MoveCheck routine
91 Buttons: .res 1 ; Button mask
93 ; Keyboard buffer fill level at start of interrupt
103 ; 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_HEIGHT/2 ; YPos
109 .word SCREEN_WIDTH/2 ; XPos
112 .word SCREEN_WIDTH - 1 ; XMax
113 .word SCREEN_HEIGHT - 1 ; YMax
118 ;----------------------------------------------------------------------------
119 ; INSTALL routine. Is called after the driver is loaded into memory. If
120 ; possible, check if the hardware is present.
121 ; Must return an MOUSE_ERR_xx code in a/x.
125 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
126 ; That allows direct access to the VIC-IIe's sprite registers.
133 ; Initialize variables. Just copy the default stuff over
135 ldx #.sizeof(DefVars)-1
141 ; Be sure the mouse cursor is invisible and at the default location. We
142 ; need to do that here, because our mouse interrupt handler doesn't set the
143 ; mouse position if it hasn't changed.
154 ; Initialize our IRQ magic
156 ; remember ROM IRQ continuation address
167 ; set ROM IRQ continuation address to point to the provided routine
175 ; set address of our IRQ callback routine
176 ; since it's called via "rts" we have to use "address-1"
185 ; set ROM entry point vector
186 ; since it's called via "rts" we have to decrement it by one
197 ; Done, return zero (= MOUSE_ERR_OK)
203 ;----------------------------------------------------------------------------
204 ; UNINSTALL routine. Is called before the driver is removed from memory.
205 ; No return code required (the driver is removed from memory on return).
215 jsr HIDE ; Hide cursor on exit
220 ;----------------------------------------------------------------------------
221 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
222 ; a counter for calls to show/hide, and the driver entry point is only called
223 ; if the mouse is currently visible and should get hidden. For most drivers,
224 ; no special action is required besides hiding the mouse cursor.
225 ; No return code required.
232 ;----------------------------------------------------------------------------
233 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
234 ; a counter for calls to show/hide, and the driver entry point is only called
235 ; if the mouse is currently hidden and should become visible. For most drivers,
236 ; no special action is required besides enabling the mouse cursor.
237 ; No return code required.
244 ;----------------------------------------------------------------------------
245 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
246 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
247 ; No checks are done if the mouse is currently inside the box, this is the job
248 ; of the caller. It is not necessary to validate the parameters, trust the
249 ; caller and save some code here. No return code required.
252 stx ptr1+1 ; Save data pointer
254 ldy #.sizeof (MOUSE_BOX)-1
265 ;----------------------------------------------------------------------------
266 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
267 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
270 stx ptr1+1 ; Save data pointer
272 ldy #.sizeof (MOUSE_BOX)-1
283 ;----------------------------------------------------------------------------
284 ; MOVE: Move the mouse to a new position. The position is passed as it comes
285 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
286 ; remove the parameter from the stack on return.
287 ; No checks are done if the new position is valid (within the bounding box or
288 ; the screen). No return code required.
291 MOVE: sei ; No interrupts
294 stx YPos+1 ; New Y position
303 sta XPos ; New X position
305 jsr CMOVEX ; Move the cursor
307 cli ; Allow interrupts
310 ;----------------------------------------------------------------------------
311 ; BUTTONS: Return the button mask in a/x.
318 ;----------------------------------------------------------------------------
319 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
320 ; No return code required.
322 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
324 sei ; Disable interrupts
325 lda XPos ; Transfer the position
334 cli ; Enable interrupts
337 sta (ptr1),y ; Store last byte
341 ;----------------------------------------------------------------------------
342 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
343 ; struct pointed to by ptr1. No return code required.
345 ; We're cheating here to keep the code smaller: The first fields of the
346 ; mouse_info struct are identical to the mouse_pos struct, so we will just
347 ; call _mouse_pos to initialize the struct pointer and fill the position
352 ; Fill in the button state
354 jsr BUTTONS ; Will not touch ptr1
355 ldy #MOUSE_INFO::BUTTONS
360 ;----------------------------------------------------------------------------
361 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
362 ; specific data in ptr1, and the ioctl code in A.
363 ; Must return an error code in a/x.
366 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
367 ldx #>MOUSE_ERR_INV_IOCTL
370 ;----------------------------------------------------------------------------
371 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
372 ; (so be careful). The routine MUST return carry set if the interrupt has been
373 ; 'handled' - which means that the interrupt source is gone. Otherwise it
374 ; MUST return carry clear.
382 lda CIA1_PRB ; Read joystick #0
384 eor #$1F ; Make all bits active high
387 lda SID_ADConv1 ; Get mouse X movement
389 jsr MoveCheck ; Calculate movement vector
392 ; Skip processing if nothing has changed
396 ; Calculate the new X coordinate (--> a/y)
399 tay ; Remember low byte
404 ; Limit the X coordinate to the bounding box
422 ; Move the mouse pointer to the new X pos
427 ; Calculate the Y movement vector
429 @SkipX: lda SID_ADConv2 ; Get mouse Y movement
431 jsr MoveCheck ; Calculate movement
434 ; Skip processing if nothing has changed
438 ; Calculate the new Y coordinate (--> a/y)
449 ; Limit the Y coordinate to the bounding box
467 ; Move the mouse pointer to the new X pos
475 clc ; Interrupt not "handled"
478 ; --------------------------------------------------------------------------
480 ; Move check routine, called for both coordinates.
482 ; Entry: y = old value of pot register
483 ; a = current value of pot register
484 ; Exit: y = value to use for old value
485 ; x/a = delta value for position
493 sub OldValue ; a = mod64 (new - old)
495 cmp #%01000000 ; if (a > 0)
498 beq @L2 ; if (a != 0)
499 ldy NewValue ; y = NewValue
503 @L1: ora #%11000000 ; else or in high order bits
504 cmp #$FF ; if (a != -1)
508 dex ; high byte = -1 (X = $FF)
517 .define OLD_BUTTONS Buttons ; tells callback.inc where the old port status is stored
518 .include "callback.inc"