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
92 OldDir: .res 1 ; previous direction bits
93 OldButton: .res 1 ; previous buttons
96 Temp: .res 1 ; Temporary value used in the int handler
98 ; Default values for above variables
102 ; (We use ".proc" because we want to define both a label and a scope.)
105 .word SCREEN_HEIGHT/2 ; YPos
106 .word SCREEN_WIDTH/2 ; XPos
109 .word SCREEN_WIDTH ; XMax
110 .word SCREEN_HEIGHT ; YMax
116 ;----------------------------------------------------------------------------
117 ; INSTALL routine. Is called after the driver is loaded into memory. If
118 ; possible, check if the hardware is present.
119 ; Must return an MOUSE_ERR_xx code in a/x.
123 ; Initialize variables. Just copy the default stuff over
125 ldx #.sizeof(DefVars)-1
131 ; Make sure the mouse cursor is at the default location.
140 ; Done, return zero (= MOUSE_ERR_OK)
146 ;----------------------------------------------------------------------------
147 ; UNINSTALL routine. Is called before the driver is removed from memory.
148 ; No return code required (the driver is removed from memory on return).
150 UNINSTALL = HIDE ; Hide cursor on exit
152 ;----------------------------------------------------------------------------
153 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
154 ; a counter for calls to show/hide, and the driver entry point is only called
155 ; if the mouse is currently visible and should get hidden. For most drivers,
156 ; no special action is required besides hiding the mouse cursor.
157 ; No return code required.
165 ;----------------------------------------------------------------------------
166 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
167 ; a counter for calls to show/hide, and the driver entry point is only called
168 ; if the mouse is currently hidden and should become visible. For most drivers,
169 ; no special action is required besides enabling the mouse cursor.
170 ; No return code required.
178 ;----------------------------------------------------------------------------
179 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
180 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
181 ; No checks are done if the mouse is currently inside the box, this is the job
182 ; of the caller. It is not necessary to validate the parameters, trust the
183 ; caller and save some code here. No return code required.
186 stx ptr1+1 ; Save data pointer
188 ldy #.sizeof (MOUSE_BOX)-1
200 ;----------------------------------------------------------------------------
201 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
202 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
205 stx ptr1+1 ; Save data pointer
207 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.
239 stx YPos+1 ; New Y position
248 sta XPos ; New X position
249 jsr CMOVEX ; Move the cursor
253 plp ; Restore interrupt flag
256 ;----------------------------------------------------------------------------
257 ; BUTTONS: Return the button mask in a/x.
264 ;----------------------------------------------------------------------------
265 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
266 ; No return code required.
268 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
271 sei ; Disable interrupts
272 lda XPos ; Transfer the position
281 plp ; Restore interrupt flag
284 sta (ptr1),y ; Store last byte
288 ;----------------------------------------------------------------------------
289 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
290 ; struct pointed to by ptr1. No return code required.
292 ; We're cheating here to keep the code smaller: The first fields of the
293 ; mouse_info struct are identical to the mouse_pos struct, so we will just
294 ; call _mouse_pos to initialize the struct pointer and fill the position
299 ; Fill in the button state
302 ldy #MOUSE_INFO::BUTTONS
307 ;----------------------------------------------------------------------------
308 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
309 ; specific data in ptr1, and the ioctl code in A.
310 ; Must return an error code in a/x.
313 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
314 ldx #>MOUSE_ERR_INV_IOCTL
317 ;----------------------------------------------------------------------------
318 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
319 ; (so be careful). The routine MUST return carry set if the interrupt has been
320 ; 'handled' - which means that the interrupt source is gone. Otherwise it
321 ; MUST return carry clear.
326 ; Check for a pressed button and place the result into Buttons
329 lda TRIG0 ; joystick #0 trigger
330 bne @L0 ; not pressed
334 lda PORTA ; get joystick direction bits
335 and #15 ; clear joystick #1 bits
341 ; Check if user activity occurred, and if yes, disable "attract mode"
348 sta ATRACT ; disable "attract mode"
358 @ChkCnt:lda Temp ; Read joystick #0
359 and #(JOY::LEFT | JOY::RIGHT)
362 ; We will cheat here and rely on the fact that either the left, OR the right
365 and #JOY::RIGHT ; Check RIGHT bit
369 bne @AddX ; Branch always
373 ; Calculate the new X coordinate (--> a/y)
376 tay ; Remember low byte
381 ; Limit the X coordinate to the bounding box
399 ; Move the mouse pointer to the new X pos
404 ; Calculate the Y movement vector
406 @SkipX: lda Temp ; Read joystick #0
407 and #(JOY::UP | JOY::DOWN) ; Check up/down
410 ; We will cheat here and rely on the fact that either the up, OR the down
421 ; Calculate the new Y coordinate (--> a/y)
424 tay ; Remember low byte
429 ; Limit the Y coordinate to the bounding box
447 ; Move the mouse pointer to the new X pos
455 clc ; Interrupt not "handled"