2 ; Mouse driver for ST & Amiga mouses and Atari trakball.
4 ; Original access routines: 05/07/2000 Freddy Offenga
5 ; Converted to driver: Christian Groessler, 2014-01-04
8 ; AMIGA_MOUSE - builds Amiga mouse version
9 ; TRAK_MOUSE - builds trakball version
10 ; If none of these defines are active, the ST mouse version
16 DISABLE_TIMEOUT = 30 ; # of vertical blank interrupts after which, if
17 ; no mouse motion occurred, the polling IRQ gets
19 ; VBI frequency is 50Hz for PAL and 60Hz for NTSC
21 .include "zeropage.inc"
22 .include "mouse-kernel.inc"
27 .if .not ( .defined (AMIGA_MOUSE) .or .defined (TRAK_MOUSE))
31 ; ------------------------------------------------------------------------
32 ; Header. Includes jump table
40 .byte $6d, $6f, $75 ; "mou"
41 .byte MOUSE_API_VERSION ; Mouse driver API version number
64 .byte MOUSE_FLAG_LATE_IRQ
66 ; Callback table, set by the kernel before INSTALL is called
68 CHIDE: jmp $0000 ; Hide the cursor
69 CSHOW: jmp $0000 ; Show the cursor
70 CPREP: jmp $0000 ; Prepare to move the cursor
71 CDRAW: jmp $0000 ; Draw the cursor
72 CMOVEX: jmp $0000 ; Move the cursor to X coord
73 CMOVEY: jmp $0000 ; Move the cursor to Y coord
76 ;----------------------------------------------------------------------------
89 ;----------------------------------------------------------------------------
90 ; Global variables. The bounding box values are sorted so that they can be
91 ; written with the least effort in the SETBOX and GETBOX routines, so don't
97 YPos: .res 2 ; Current mouse position, Y
98 XPos: .res 2 ; Current mouse position, X
99 XMin: .res 2 ; X1 value of bounding box
100 YMin: .res 2 ; Y1 value of bounding box
101 XMax: .res 2 ; X2 value of bounding box
102 YMax: .res 2 ; Y2 value of bounding box
103 Buttons: .res 1 ; Button mask
108 irq_enabled: .res 1 ; flag indicating that the high frequency polling interrupt is enabled
109 old_porta_vbi: .res 1 ; previous PORTA value of the VBI interrupt (IRQ)
110 how_long: .res 1 ; counter for how many VBI interrupts the mouse hasn't been moved
112 .if .defined (AMIGA_MOUSE) .or .defined (ST_MOUSE)
132 ; Default values for some of the above variables
133 ; (We use ".proc" because we want to define both a label and a scope.)
136 .word (SCREEN_HEIGHT+1)/2 ; YPos
137 .word (SCREEN_WIDTH+1)/2 ; XPos
140 .word SCREEN_WIDTH ; XMax
141 .word SCREEN_HEIGHT ; YMax
147 ; ST mouse lookup table
149 STTab: .byte $FF,$01,$00,$01
150 .byte $00,$FF,$00,$01
151 .byte $01,$00,$FF,$00
152 .byte $01,$00,$01,$FF
158 ; Amiga mouse lookup table
160 AmiTab: .byte $FF,$01,$00,$FF
161 .byte $00,$FF,$FF,$01
162 .byte $01,$FF,$FF,$00
163 .byte $FF,$00,$01,$FF
169 ;----------------------------------------------------------------------------
170 ; INSTALL routine. Is called after the driver is loaded into memory. If
171 ; possible, check if the hardware is present.
172 ; Must return an MOUSE_ERR_xx code in a/x.
176 ; Initialize variables. Just copy the default stuff over
178 ldx #.sizeof(DefVars)-1
184 ; Make sure the mouse cursor is at the default location.
197 ; Install timer irq routine to poll mouse.
201 ; Setup pointer to wrapper install/deinstall function.
203 sta set_VTIMR1_handler+1
205 sta set_VTIMR1_handler+2
207 ; Install my handler.
211 jsr set_VTIMR1_handler
240 .if 0 ; the IRQ will now be dynamically enabled when the mouse is moved
242 ora #%00000001 ; timer 1 enable
252 ; Done, return zero (= MOUSE_ERR_OK)
258 ;----------------------------------------------------------------------------
259 ; UNINSTALL routine. Is called before the driver is removed from memory.
260 ; No return code required (the driver is removed from memory on return).
264 ; uninstall timer irq routine
267 and #%11111110 ; timer 1 disable
274 jsr set_VTIMR1_handler
289 ;----------------------------------------------------------------------------
290 ; HIDE routine. Is called to hide the mouse pointer. The mouse kernel manages
291 ; a counter for calls to show/hide, and the driver entry point is only called
292 ; if the mouse is currently visible and should get hidden. For most drivers,
293 ; no special action is required besides hiding the mouse cursor.
294 ; No return code required.
302 ;----------------------------------------------------------------------------
303 ; SHOW routine. Is called to show the mouse pointer. The mouse kernel manages
304 ; a counter for calls to show/hide, and the driver entry point is only called
305 ; if the mouse is currently hidden and should become visible. For most drivers,
306 ; no special action is required besides enabling the mouse cursor.
307 ; No return code required.
315 ;----------------------------------------------------------------------------
316 ; SETBOX: Set the mouse bounding box. The parameters are passed as they come
317 ; from the C program, that is, a pointer to a mouse_box struct in a/x.
318 ; No checks are done if the mouse is currently inside the box, this is the job
319 ; of the caller. It is not necessary to validate the parameters, trust the
320 ; caller and save some code here. No return code required.
323 stx ptr1+1 ; Save data pointer
325 ldy #.sizeof (MOUSE_BOX)-1
337 ;----------------------------------------------------------------------------
338 ; GETBOX: Return the mouse bounding box. The parameters are passed as they
339 ; come from the C program, that is, a pointer to a mouse_box struct in a/x.
342 stx ptr1+1 ; Save data pointer
344 ldy #.sizeof (MOUSE_BOX)-1
356 ;----------------------------------------------------------------------------
357 ; MOVE: Move the mouse to a new position. The position is passed as it comes
358 ; from the C program, that is: X on the stack and Y in a/x. The C wrapper will
359 ; remove the parameter from the stack on return.
360 ; No checks are done if the new position is valid (within the bounding box or
361 ; the screen). No return code required.
377 stx YPos+1 ; New Y position
388 sta XPos ; New X position
390 jsr CMOVEX ; Move the cursor
394 plp ; Restore interrupt flag
397 ;----------------------------------------------------------------------------
398 ; BUTTONS: Return the button mask in a/x.
405 ;----------------------------------------------------------------------------
406 ; POS: Return the mouse position in the MOUSE_POS struct pointed to by ptr1.
407 ; No return code required.
409 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
412 sei ; Disable interrupts
413 lda XPos ; Transfer the position
422 plp ; Restore interrupt flag
425 sta (ptr1),y ; Store last byte
429 ;----------------------------------------------------------------------------
430 ; INFO: Returns mouse position and current button mask in the MOUSE_INFO
431 ; struct pointed to by ptr1. No return code required.
433 ; We're cheating here to keep the code smaller: The first fields of the
434 ; mouse_info struct are identical to the mouse_pos struct, so we will just
435 ; call _mouse_pos to initialize the struct pointer and fill the position
440 ; Fill in the button state
443 ldy #MOUSE_INFO::BUTTONS
448 ;----------------------------------------------------------------------------
449 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
450 ; specific data in ptr1, and the ioctl code in A.
451 ; Must return an error code in a/x.
454 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioclts for now
455 ldx #>MOUSE_ERR_INV_IOCTL
458 ;----------------------------------------------------------------------------
459 ; IRQ: Irq handler entry point. Called as a subroutine but in IRQ context
460 ; (so be careful). The routine MUST return carry set if the interrupt has been
461 ; 'handled' - which means that the interrupt source is gone. Otherwise it
462 ; MUST return carry clear.
465 IRQ: lda PORTA ; mouse port contents
466 and #$0f ; check port 1 only
470 ; IRQ is disabled, check for mouse motion and enable IRQ if mouse motion detected
475 ; Turn mouse polling IRQ back on
478 ora #%00000001 ; timer 1 enable
487 @L1: cmp old_porta_vbi ; mouse motion since last VBI?
489 beq @L2 ; no, increment timeout to disable IRQ
492 sta how_long ; yes, reinitialize wait counter
496 @L2: inc how_long ; no motion, increment wait counter
498 cmp #DISABLE_TIMEOUT ; timeout?
501 lda #0 ; yes, turn off IRQ
504 ; no mouse input -- turn IRQ off
508 and #%11111110 ; timer 1 disable
512 ; Check for a pressed button and place the result into Buttons
515 lda TRIG0 ; joystick #0 trigger
516 bne @L4 ; not pressed
522 ; Limit the X coordinate to the bounding box
545 ; Limit the Y coordinate to the bounding box
571 ; print on upper right corner 'E' or 'D', indicating the IRQ is enabled or disabled
574 lda #37 ; screen code for 'E'
575 .byte $2c ; bit opcode, eats next 2 bytes
576 @L9: lda #36 ; screen code for 'D'
585 ;----------------------------------------------------------------------------
586 ; T1Han: Local IRQ routine to poll mouse
589 T1Han: lda CRITIC ; if CRITIC flag is set, disable the
590 bne disable_me ; high frequency polling IRQ, in order
591 ; not to interfere with SIO I/O (e.g.
663 .elseif .defined (AMIGA_MOUSE)
665 ; Amiga mouse version
718 .elseif .defined (TRAK_MOUSE)
777 ; Disable the interrupt source which caused us to be called.
778 ; The interrupt will be enabled again by the "IRQ" routine.
779 ; The "IRQ" routine, despite its name, is called from the
780 ; vertical blank NMI interrupt *only* if the CRITIC flag has
785 and #%11111110 ; timer 1 disable