2 ; Mouse driver for Atari Touch Tablet
4 ; Christian Groessler, 2014-01-05
7 .include "zeropage.inc"
8 .include "mouse-kernel.inc"
14 ; ------------------------------------------------------------------------
15 ; Header. Includes jump table
18 module_header _atrxtt_mou
20 module_header _atrtt_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 ;----------------------------------------------------------------------------
76 ;----------------------------------------------------------------------------
77 ; Global variables. The bounding box values are sorted so that they can be
78 ; written with the least effort in the SETBOX and GETBOX routines, so don't
84 YPos: .res 2 ; Current mouse position, Y
85 XPos: .res 2 ; Current mouse position, X
86 XMin: .res 2 ; X1 value of bounding box
87 YMin: .res 2 ; Y1 value of bounding box
88 XMax: .res 2 ; X2 value of bounding box
89 YMax: .res 2 ; Y2 value of bounding box
90 Buttons: .res 1 ; Button mask
92 ; Default values for above variables
96 ; (We use ".proc" because we want to define both a label and a scope.)
99 .word SCREEN_HEIGHT/2 ; YPos
100 .word SCREEN_WIDTH/2 ; XPos
103 .word SCREEN_WIDTH ; XMax
104 .word SCREEN_HEIGHT ; YMax
110 ;----------------------------------------------------------------------------
111 ; INSTALL routine. Is called after the driver is loaded into memory. If
112 ; possible, check if the hardware is present.
113 ; Must return an MOUSE_ERR_xx code in a/x.
117 ; Initialize variables. Just copy the default stuff over
119 ldx #.sizeof(DefVars)-1
125 ; Make sure the mouse cursor is at the default location.
134 ; Done, return zero (= MOUSE_ERR_OK)
140 ;----------------------------------------------------------------------------
141 ; UNINSTALL routine. Is called before the driver is removed from memory.
142 ; No return code required (the driver is removed from memory on return).
144 UNINSTALL = HIDE ; Hide cursor on exit
146 ;----------------------------------------------------------------------------
147 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
148 ; a counter for calls to show/hide, and the driver entry point is only called
149 ; if the mouse is currently visible and should get hidden. For most drivers,
150 ; no special action is required besides hiding the mouse cursor.
151 ; 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.
172 ;----------------------------------------------------------------------------
173 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
174 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
175 ; No checks are done if the mouse is currently inside the box, this is the job
176 ; of the caller. It is not necessary to validate the parameters, trust the
177 ; caller and save some code here. No return code required.
180 stx ptr1+1 ; Save data pointer
182 ldy #.sizeof (MOUSE_BOX)-1
194 ;----------------------------------------------------------------------------
195 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
196 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
199 stx ptr1+1 ; Save data pointer
201 ldy #.sizeof (MOUSE_BOX)-1
213 ;----------------------------------------------------------------------------
214 ; MOVE: Move the mouse to a new position. The position is passed as it comes
215 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
216 ; remove the parameter from the stack on return.
217 ; No checks are done if the new position is valid (within the bounding box or
218 ; the screen). No return code required.
233 stx YPos+1 ; New Y position
242 sta XPos ; New X position
243 jsr CMOVEX ; Move the cursor
247 plp ; Restore interrupt flag
250 ;----------------------------------------------------------------------------
251 ; BUTTONS: Return the button mask in a/x.
258 ;----------------------------------------------------------------------------
259 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
260 ; No return code required.
262 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
265 sei ; Disable interrupts
266 lda XPos ; Transfer the position
275 plp ; Restore interrupt flag
278 sta (ptr1),y ; Store last byte
282 ;----------------------------------------------------------------------------
283 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
284 ; struct pointed to by ptr1. No return code required.
286 ; We're cheating here to keep the code smaller: The first fields of the
287 ; mouse_info struct are identical to the mouse_pos struct, so we will just
288 ; call _mouse_pos to initialize the struct pointer and fill the position
293 ; Fill in the button state
296 ldy #MOUSE_INFO::BUTTONS
301 ;----------------------------------------------------------------------------
302 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
303 ; specific data in ptr1, and the ioctl code in A.
304 ; Must return an error code in a/x.
307 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
308 ldx #>MOUSE_ERR_INV_IOCTL
311 ;----------------------------------------------------------------------------
312 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
313 ; (so be careful). The routine MUST return carry set if the interrupt has been
314 ; 'handled' - which means that the interrupt source is gone. Otherwise it
315 ; MUST return carry clear.
320 ; Check for a pressed button and place the result into Buttons
325 lda PORTA ; get other buttons
328 and #5 ; pen button and left button are mapped to left mouse button
340 ; If we read 228 for X or Y positions, we assume the user has lifted the pen
341 ; and don't change the cursor position.
345 beq @Cont ; CF set if equal
347 cmp #228 ; CF set if equal
349 @Cont: php ; remember CF
360 ; Get cursor position
361 ; -------------------
362 ; The touch pad is read thru the paddle potentiometers. The possible
363 ; values are 1..228. Since the maximum value is less than the X
364 ; dimension we have to "stretch" this value. In order to use only
365 ; divisions by powers of two, we use the following appoximation:
367 ; 1+1/2-1/8+1/32 = 1.4062
368 ; For Y we subtract 1/8 of it to get in the YMax ballpark.
370 ; A small area in the Y dimension of the touchpad isn't used with
371 ; this approximation. The Y value is inverted, (0,0) is the bottom
372 ; left corner of the touchpad.
376 ldx PADDL0 ; get X postion
377 dex ; decrement, since it's 1-based
388 lsr a ; port value / 4
389 lsr a ; port value / 8
399 lsr a ; port value / 16
400 lsr a ; port value / 32
411 ; Limit the X coordinate to the bounding box
429 ; Move the mouse pointer to the new X pos
436 ldx PADDL1 ; get Y postion
437 dex ; decrement, since it's 1-based
441 sbc YPos ; invert value
455 ; Limit the Y coordinate to the bounding box
473 ; Move the mouse pointer to the new X pos
480 clc ; Interrupt not "handled"