2 ; Driver for a "joystick mouse".
4 ; C128 version: Ullrich von Bassewitz, 2004-04-05, 2009-09-26
5 ; Adapted to Atari: Christian Groessler, 2014-01-02
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
47 .byte MOUSE_FLAG_LATE_IRQ
49 ; Callback table, set by the kernel before INSTALL is called
51 CHIDE: jmp $0000 ; Hide the cursor
52 CSHOW: jmp $0000 ; Show the cursor
53 CMOVEX: jmp $0000 ; Move the cursor to X coord
54 CMOVEY: jmp $0000 ; Move the cursor to Y coord
57 ;----------------------------------------------------------------------------
70 ;----------------------------------------------------------------------------
71 ; Global variables. The bounding box values are sorted so that they can be
72 ; written with the least effort in the SETBOX and GETBOX routines, so don't
78 YPos: .res 2 ; Current mouse position, Y
79 XPos: .res 2 ; Current mouse position, X
80 XMin: .res 2 ; X1 value of bounding box
81 YMin: .res 2 ; Y1 value of bounding box
82 XMax: .res 2 ; X2 value of bounding box
83 YMax: .res 2 ; Y2 value of bounding box
84 Buttons: .res 1 ; Button mask
87 Temp: .res 1 ; Temporary value used in the int handler
90 ; Default values for above variables
94 ; (We use ".proc" because we want to define both a label and a scope.)
97 .word SCREEN_HEIGHT/2 ; YPos
98 .word SCREEN_WIDTH/2 ; XPos
101 .word SCREEN_WIDTH ; XMax
102 .word SCREEN_HEIGHT ; YMax
108 ;----------------------------------------------------------------------------
109 ; INSTALL routine. Is called after the driver is loaded into memory. If
110 ; possible, check if the hardware is present.
111 ; Must return an MOUSE_ERR_xx code in a/x.
115 ; Initialize variables. Just copy the default stuff over
117 ldx #.sizeof(DefVars)-1
123 ; Be sure the mouse cursor is invisible and at the default location.
133 ; Done, return zero (= MOUSE_ERR_OK)
139 ;----------------------------------------------------------------------------
140 ; UNINSTALL routine. Is called before the driver is removed from memory.
141 ; No return code required (the driver is removed from memory on return).
143 UNINSTALL = HIDE ; Hide cursor on exit
145 ;----------------------------------------------------------------------------
146 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
147 ; a counter for calls to show/hide, and the driver entry point is only called
148 ; if the mouse is currently visible and should get hidden. For most drivers,
149 ; no special action is required besides hiding the mouse cursor.
150 ; No return code required.
159 ;----------------------------------------------------------------------------
160 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
161 ; a counter for calls to show/hide, and the driver entry point is only called
162 ; if the mouse is currently hidden and should become visible. For most drivers,
163 ; no special action is required besides enabling the mouse cursor.
164 ; No return code required.
173 ;----------------------------------------------------------------------------
174 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
175 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
176 ; No checks are done if the mouse is currently inside the box, this is the job
177 ; of the caller. It is not necessary to validate the parameters, trust the
178 ; caller and save some code here. No return code required.
181 stx ptr1+1 ; Save data pointer
183 ldy #.sizeof (MOUSE_BOX)-1
195 ;----------------------------------------------------------------------------
196 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
197 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
200 stx ptr1+1 ; Save data pointer
202 ldy #.sizeof (MOUSE_BOX)-1
214 ;----------------------------------------------------------------------------
215 ; MOVE: Move the mouse to a new position. The position is passed as it comes
216 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
217 ; remove the parameter from the stack on return.
218 ; No checks are done if the new position is valid (within the bounding box or
219 ; the screen). No return code required.
234 stx YPos+1 ; New Y position
243 sta XPos ; New X position
245 jsr CMOVEX ; Move the cursor
252 @Ret: plp ; Restore interrupt flag
255 ;----------------------------------------------------------------------------
256 ; BUTTONS: Return the button mask in a/x.
263 ;----------------------------------------------------------------------------
264 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
265 ; No return code required.
267 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
270 sei ; Disable interrupts
271 lda XPos ; Transfer the position
280 plp ; Restore interrupt flag
283 sta (ptr1),y ; Store last byte
287 ;----------------------------------------------------------------------------
288 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
289 ; struct pointed to by ptr1. No return code required.
291 ; We're cheating here to keep the code smaller: The first fields of the
292 ; mouse_info struct are identical to the mouse_pos struct, so we will just
293 ; call _mouse_pos to initialize the struct pointer and fill the position
298 ; Fill in the button state
301 ldy #MOUSE_INFO::BUTTONS
306 ;----------------------------------------------------------------------------
307 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
308 ; specific data in ptr1, and the ioctl code in A.
309 ; Must return an error code in a/x.
312 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
313 ldx #>MOUSE_ERR_INV_IOCTL
316 ;----------------------------------------------------------------------------
317 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
318 ; (so be careful). The routine MUST return carry set if the interrupt has been
319 ; 'handled' - which means that the interrupt source is gone. Otherwise it
320 ; MUST return carry clear.
325 ; Check for a pressed button and place the result into Buttons
328 lda TRIG0 ; joystick #0 trigger
329 bne @L0 ; not pressed
333 lda PORTA ; get joystick direction bits
334 and #15 ; clear joystick #1 bits
342 lda Temp ; Read joystick #0
343 and #(JOY::LEFT | JOY::RIGHT)
346 ; We will cheat here and rely on the fact that either the left, OR the right
349 and #JOY::RIGHT ; Check RIGHT bit
353 bne @AddX ; Branch always
357 ; Calculate the new X coordinate (--> a/y)
360 tay ; Remember low byte
365 ; Limit the X coordinate to the bounding box
383 ; Move the mouse pointer to the new X pos
388 ; Calculate the Y movement vector
390 @SkipX: lda Temp ; Read joystick #0
391 and #(JOY::UP | JOY::DOWN) ; Check up/down
394 ; We will cheat here and rely on the fact that either the up, OR the down
405 ; Calculate the new Y coordinate (--> a/y)
408 tay ; Remember low byte
413 ; Limit the Y coordinate to the bounding box
431 ; Move the mouse pointer to the new X pos
443 @Done: clc ; Interrupt not "handled"