2 ; Driver for a potentiometer "mouse", e.g. Koala Pad
4 ; 2006-08-20, Stefan Haubenthal
5 ; 2009-09-26, Ullrich von Bassewitz
6 ; 2014-04-26, Christian Groessler
7 ; 2014-05-05, Greg King
10 .include "zeropage.inc"
11 .include "mouse-kernel.inc"
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table
21 module_header _c128_pot_mou
27 .byte $6d, $6f, $75 ; "mou"
28 .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 ;----------------------------------------------------------------------------
77 ;----------------------------------------------------------------------------
78 ; Global variables. The bounding box values are sorted so that they can be
79 ; written with the least effort in the SETBOX and GETBOX routines, so don't
85 YPos: .res 2 ; Current mouse position, Y
86 XPos: .res 2 ; Current mouse position, X
87 XMin: .res 2 ; X1 value of bounding box
88 YMin: .res 2 ; Y1 value of bounding box
89 XMax: .res 2 ; X2 value of bounding box
90 YMax: .res 2 ; Y2 value of bounding box
91 Buttons: .res 1 ; Button mask
95 ; Keyboard buffer fill level at start of interrupt
105 ; Default values for above variables
106 ; (We use ".proc" because we want to define both a label and a scope.)
109 .word SCREEN_HEIGHT/2 ; YPos
110 .word SCREEN_WIDTH/2 ; XPos
113 .word SCREEN_WIDTH - 1 ; XMax
114 .word SCREEN_HEIGHT - 1 ; YMax
120 ;----------------------------------------------------------------------------
121 ; INSTALL routine. Is called after the driver is loaded into memory. If
122 ; possible, check if the hardware is present.
123 ; Must return an MOUSE_ERR_xx code in a/x.
127 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
128 ; That allows direct access to the VIC-IIe's sprite registers.
135 ; Initialize variables. Just copy the default stuff over
137 ldx #.sizeof(DefVars)-1
143 ; Be sure the mouse cursor is invisible and at the default location. We
144 ; need to do that here, because our mouse interrupt handler doesn't set the
145 ; mouse position if it hasn't changed.
156 ; Initiate our IRQ magic.
158 ; Remember the ROM IRQ continuation address.
169 ; Set the ROM IRQ continuation address to point to the provided routine.
177 ; Set the address of our IRQ callback routine.
178 ; Because it's called via "rts", we must use "address-1".
186 ; Set the ROM entry-point vector.
187 ; Because it's called via "rts", we must decrement it by one.
198 ; Done, return zero (= MOUSE_ERR_OK)
204 ;----------------------------------------------------------------------------
205 ; UNINSTALL routine. Is called before the driver is removed from memory.
206 ; No return code required (the driver is removed from memory on return).
214 ;cli ; This will be done at end of HIDE
216 jsr HIDE ; Hide cursor on exit
221 ;----------------------------------------------------------------------------
222 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
223 ; a counter for calls to show/hide, and the driver entry point is only called
224 ; if the mouse is currently visible and should get hidden. For most drivers,
225 ; no special action is required besides hiding the mouse cursor.
226 ; No return code required.
233 ;----------------------------------------------------------------------------
234 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
235 ; a counter for calls to show/hide, and the driver entry point is only called
236 ; if the mouse is currently hidden and should become visible. For most drivers,
237 ; no special action is required besides enabling the mouse cursor.
238 ; No return code required.
245 ;----------------------------------------------------------------------------
246 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
247 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
248 ; No checks are done if the mouse is currently inside the box, this is the job
249 ; of the caller. It is not necessary to validate the parameters, trust the
250 ; caller and save some code here. No return code required.
253 stx ptr1+1 ; Save data pointer
255 ldy #.sizeof (MOUSE_BOX)-1
266 ;----------------------------------------------------------------------------
267 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
268 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
271 stx ptr1+1 ; Save data pointer
273 ldy #.sizeof (MOUSE_BOX)-1
284 ;----------------------------------------------------------------------------
285 ; MOVE: Move the mouse to a new position. The position is passed as it comes
286 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
287 ; remove the parameter from the stack on return.
288 ; No checks are done if the new position is valid (within the bounding box or
289 ; the screen). No return code required.
292 MOVE: sei ; No interrupts
295 stx YPos+1 ; New Y position
304 sta XPos ; New X position
306 jsr CMOVEX ; Move the cursor
308 cli ; Allow interrupts
311 ;----------------------------------------------------------------------------
312 ; BUTTONS: Return the button mask in a/x.
318 ; Make the buttons look like a 1351 mouse.
320 and #JOY::LEFT | JOY::RIGHT
323 ;clc ; ("lsr" shifted zero into carry flag)
324 adc #%00001110 ; Shift bit 1 over to bit 4
325 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
328 ;----------------------------------------------------------------------------
329 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
330 ; No return code required.
332 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
334 sei ; Disable interrupts
335 lda XPos ; Transfer the position
344 cli ; Enable interrupts
347 sta (ptr1),y ; Store last byte
351 ;----------------------------------------------------------------------------
352 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
353 ; struct pointed to by ptr1. No return code required.
355 ; We're cheating here to keep the code smaller: The first fields of the
356 ; mouse_info struct are identical to the mouse_pos struct, so we will just
357 ; call _mouse_pos to initialize the struct pointer and fill the position
362 ; Fill in the button state
365 ldy #MOUSE_INFO::BUTTONS
370 ;----------------------------------------------------------------------------
371 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
372 ; specific data in ptr1, and the ioctl code in A.
373 ; Must return an error code in a/x.
376 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls for now
377 ldx #>MOUSE_ERR_INV_IOCTL
380 ;----------------------------------------------------------------------------
381 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
390 lda CIA1_PRB ; Read port #1
391 eor #%11111111 ; Make all bits active high
394 ldx #%01000000 ; Read port 1 paddles
406 bne @AddX ; Branch always
410 ; Calculate the new X coordinate (--> a/y)
413 tay ; Remember low byte
418 ; Limit the X coordinate to the bounding box
436 ; Move the mouse pointer to the new X pos
447 ; Calculate the new Y coordinate (--> a/y)
450 tay ; Remember low byte
455 ; Limit the Y coordinate to the bounding box
473 ; Move the mouse pointer to the new X pos
478 clc ; Interrupt not "handled"
481 .define OLD_BUTTONS Buttons ; Tells callback.inc where the old port status is stored
482 .include "callback.inc"