2 ; Mouse driver for Atari Touch Tablet
4 ; Christian Groessler, 2014-01-05
7 .include "zeropage.inc"
8 .include "mouse-kernel.inc"
13 ; ------------------------------------------------------------------------
14 ; Header. Includes jump table
22 .byte $6d, $6f, $75 ; "mou"
23 .byte MOUSE_API_VERSION ; Mouse driver API version number
46 .byte MOUSE_FLAG_LATE_IRQ
48 ; Callback table, set by the kernel before INSTALL is called
50 CHIDE: jmp $0000 ; Hide the cursor
51 CSHOW: jmp $0000 ; Show the cursor
52 CMOVEX: jmp $0000 ; Move the cursor to X coord
53 CMOVEY: jmp $0000 ; Move the cursor to Y coord
56 ;----------------------------------------------------------------------------
69 ;----------------------------------------------------------------------------
70 ; Global variables. The bounding box values are sorted so that they can be
71 ; written with the least effort in the SETBOX and GETBOX routines, so don't
77 YPos: .res 2 ; Current mouse position, Y
78 XPos: .res 2 ; Current mouse position, X
79 XMin: .res 2 ; X1 value of bounding box
80 YMin: .res 2 ; Y1 value of bounding box
81 XMax: .res 2 ; X2 value of bounding box
82 YMax: .res 2 ; Y2 value of bounding box
83 Buttons: .res 1 ; Button mask
88 ; Default values for above variables
92 ; (We use ".proc" because we want to define both a label and a scope.)
95 .word SCREEN_HEIGHT/2 ; YPos
96 .word SCREEN_WIDTH/2 ; XPos
99 .word SCREEN_WIDTH ; XMax
100 .word SCREEN_HEIGHT ; YMax
106 ;----------------------------------------------------------------------------
107 ; INSTALL routine. Is called after the driver is loaded into memory. If
108 ; possible, check if the hardware is present.
109 ; Must return an MOUSE_ERR_xx code in a/x.
113 ; Initialize variables. Just copy the default stuff over
115 ldx #.sizeof(DefVars)-1
121 ; Be sure the mouse cursor is invisible and at the default location.
131 ; Done, return zero (= MOUSE_ERR_OK)
137 ;----------------------------------------------------------------------------
138 ; UNINSTALL routine. Is called before the driver is removed from memory.
139 ; No return code required (the driver is removed from memory on return).
141 UNINSTALL = HIDE ; Hide cursor on exit
143 ;----------------------------------------------------------------------------
144 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
145 ; a counter for calls to show/hide, and the driver entry point is only called
146 ; if the mouse is currently visible and should get hidden. For most drivers,
147 ; no special action is required besides hiding the mouse cursor.
148 ; No return code required.
157 ;----------------------------------------------------------------------------
158 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
159 ; a counter for calls to show/hide, and the driver entry point is only called
160 ; if the mouse is currently hidden and should become visible. For most drivers,
161 ; no special action is required besides enabling the mouse cursor.
162 ; No return code required.
171 ;----------------------------------------------------------------------------
172 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
173 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
174 ; No checks are done if the mouse is currently inside the box, this is the job
175 ; of the caller. It is not necessary to validate the parameters, trust the
176 ; caller and save some code here. No return code required.
179 stx ptr1+1 ; Save data pointer
181 ldy #.sizeof (MOUSE_BOX)-1
193 ;----------------------------------------------------------------------------
194 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
195 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
198 stx ptr1+1 ; Save data pointer
200 ldy #.sizeof (MOUSE_BOX)-1
212 ;----------------------------------------------------------------------------
213 ; MOVE: Move the mouse to a new position. The position is passed as it comes
214 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
215 ; remove the parameter from the stack on return.
216 ; No checks are done if the new position is valid (within the bounding box or
217 ; 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
330 lda PORTA ; get other buttons
333 and #5 ; pen button and left button are mapped to left mouse button
345 ; If we read 228 for X or Y positions, we assume the user has lifted the pen
346 ; and don't change the cursor position.
350 beq @Cont ; CF set if equal
352 cmp #228 ; CF set if equal
367 ; Get cursor position
368 ; -------------------
369 ; The touch pad is read thru the paddle potentiometers. The possible
370 ; values are 1..228. Since the maximum value is less than the X
371 ; dimension we have to "stretch" this value. In order to use only
372 ; divisions by powers of two, we use the following appoximation:
374 ; 1+1/2-1/8+1/32 = 1.4062
375 ; For Y we subtract 1/8 of it to get in the YMax ballpark.
377 ; A small area in the Y dimension of the touchpad isn't used with
378 ; this approximation. The Y value is inverted, (0,0) is the bottom
379 ; left corner of the touchpad.
383 ldx PADDL0 ; get X postion
384 dex ; decrement, since it's 1-based
395 lsr a ; port value / 4
396 lsr a ; port value / 8
406 lsr a ; port value / 16
407 lsr a ; port value / 32
418 ; Limit the X coordinate to the bounding box
436 ; Move the mouse pointer to the new X pos
443 ldx PADDL1 ; get Y postion
444 dex ; decrement, since it's 1-based
448 sbc YPos ; invert value
462 ; Limit the Y coordinate to the bounding box
480 ; Move the mouse pointer to the new X pos
489 @Done: clc ; Interrupt not "handled"