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"
15 ; ------------------------------------------------------------------------
16 ; Header. Includes jump table
19 module_header _atrxjoy_mou
21 module_header _atrjoy_mou
28 .byte $6d, $6f, $75 ; "mou"
29 .byte MOUSE_API_VERSION ; Mouse driver API version number
52 .byte MOUSE_FLAG_LATE_IRQ
54 ; Callback table, set by the kernel before INSTALL is called
56 CHIDE: jmp $0000 ; Hide the cursor
57 CSHOW: jmp $0000 ; Show the cursor
58 CPREP: jmp $0000 ; Prepare to move the cursor
59 CDRAW: jmp $0000 ; Draw the cursor
60 CMOVEX: jmp $0000 ; Move the cursor to X coord
61 CMOVEY: jmp $0000 ; Move the cursor to Y coord
64 ;----------------------------------------------------------------------------
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
94 Temp: .res 1 ; Temporary value used in the int handler
96 ; Default values for above variables
100 ; (We use ".proc" because we want to define both a label and a scope.)
103 .word SCREEN_HEIGHT/2 ; YPos
104 .word SCREEN_WIDTH/2 ; XPos
107 .word SCREEN_WIDTH ; XMax
108 .word SCREEN_HEIGHT ; YMax
114 ;----------------------------------------------------------------------------
115 ; INSTALL routine. Is called after the driver is loaded into memory. If
116 ; possible, check if the hardware is present.
117 ; Must return an MOUSE_ERR_xx code in a/x.
121 ; Initialize variables. Just copy the default stuff over
123 ldx #.sizeof(DefVars)-1
129 ; Make sure the mouse cursor is at the default location.
138 ; Done, return zero (= MOUSE_ERR_OK)
144 ;----------------------------------------------------------------------------
145 ; UNINSTALL routine. Is called before the driver is removed from memory.
146 ; No return code required (the driver is removed from memory on return).
148 UNINSTALL = HIDE ; Hide cursor on exit
150 ;----------------------------------------------------------------------------
151 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
152 ; a counter for calls to show/hide, and the driver entry point is only called
153 ; if the mouse is currently visible and should get hidden. For most drivers,
154 ; no special action is required besides hiding the mouse cursor.
155 ; No return code required.
163 ;----------------------------------------------------------------------------
164 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
165 ; a counter for calls to show/hide, and the driver entry point is only called
166 ; if the mouse is currently hidden and should become visible. For most drivers,
167 ; no special action is required besides enabling the mouse cursor.
168 ; No return code required.
176 ;----------------------------------------------------------------------------
177 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
178 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
179 ; No checks are done if the mouse is currently inside the box, this is the job
180 ; of the caller. It is not necessary to validate the parameters, trust the
181 ; caller and save some code here. No return code required.
184 stx ptr1+1 ; Save data pointer
186 ldy #.sizeof (MOUSE_BOX)-1
198 ;----------------------------------------------------------------------------
199 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
200 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
203 stx ptr1+1 ; Save data pointer
205 ldy #.sizeof (MOUSE_BOX)-1
217 ;----------------------------------------------------------------------------
218 ; MOVE: Move the mouse to a new position. The position is passed as it comes
219 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
220 ; remove the parameter from the stack on return.
221 ; No checks are done if the new position is valid (within the bounding box or
222 ; the screen). No return code required.
237 stx YPos+1 ; New Y position
246 sta XPos ; New X position
247 jsr CMOVEX ; Move the cursor
251 plp ; Restore interrupt flag
254 ;----------------------------------------------------------------------------
255 ; BUTTONS: Return the button mask in a/x.
262 ;----------------------------------------------------------------------------
263 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
264 ; No return code required.
266 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
269 sei ; Disable interrupts
270 lda XPos ; Transfer the position
279 plp ; Restore interrupt flag
282 sta (ptr1),y ; Store last byte
286 ;----------------------------------------------------------------------------
287 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
288 ; struct pointed to by ptr1. No return code required.
290 ; We're cheating here to keep the code smaller: The first fields of the
291 ; mouse_info struct are identical to the mouse_pos struct, so we will just
292 ; call _mouse_pos to initialize the struct pointer and fill the position
297 ; Fill in the button state
300 ldy #MOUSE_INFO::BUTTONS
305 ;----------------------------------------------------------------------------
306 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
307 ; specific data in ptr1, and the ioctl code in A.
308 ; Must return an error code in a/x.
311 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
312 ldx #>MOUSE_ERR_INV_IOCTL
315 ;----------------------------------------------------------------------------
316 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
317 ; (so be careful). The routine MUST return carry set if the interrupt has been
318 ; 'handled' - which means that the interrupt source is gone. Otherwise it
319 ; MUST return carry clear.
324 ; Check for a pressed button and place the result into Buttons
327 lda TRIG0 ; joystick #0 trigger
328 bne @L0 ; not pressed
332 lda PORTA ; get joystick direction bits
333 and #15 ; clear joystick #1 bits
341 lda Temp ; Read joystick #0
342 and #(JOY::LEFT | JOY::RIGHT)
345 ; We will cheat here and rely on the fact that either the left, OR the right
348 and #JOY::RIGHT ; Check RIGHT bit
352 bne @AddX ; Branch always
356 ; Calculate the new X coordinate (--> a/y)
359 tay ; Remember low byte
364 ; Limit the X coordinate to the bounding box
382 ; Move the mouse pointer to the new X pos
387 ; Calculate the Y movement vector
389 @SkipX: lda Temp ; Read joystick #0
390 and #(JOY::UP | JOY::DOWN) ; Check up/down
393 ; We will cheat here and rely on the fact that either the up, OR the down
404 ; Calculate the new Y coordinate (--> a/y)
407 tay ; Remember low byte
412 ; Limit the Y coordinate to the bounding box
430 ; Move the mouse pointer to the new X pos
438 clc ; Interrupt not "handled"