2 ; Driver for the Inkwell Systems 170-C lightpen.
4 ; 2013-09-05, Greg King
7 .include "zeropage.inc"
8 .include "../extzp.inc"
10 .include "mouse-kernel.inc"
15 ; ------------------------------------------------------------------------
16 ; Header. Includes jump table.
24 .byte $6d, $6f, $75 ; ASCII "mou"
25 .byte MOUSE_API_VERSION ; Mouse driver API version number
48 .byte MOUSE_FLAG_EARLY_IRQ
50 ; Callback table, set by the kernel before INSTALL is called.
52 CHIDE: jmp $0000 ; Hide the cursor
53 CSHOW: jmp $0000 ; Show the cursor
54 CMOVEX: jmp $0000 ; Move the cursor to X co-ord.
55 CMOVEY: jmp $0000 ; Move the cursor to Y co-ord.
58 ;----------------------------------------------------------------------------
61 SCREEN_WIDTH = XSIZE * 8
62 SCREEN_HEIGHT = YSIZE * 8
64 ;----------------------------------------------------------------------------
65 ; Global variables. The bounding box values are sorted so that they can be
66 ; written with the least effort in the SETBOX and GETBOX routines; so, don't
71 ; Default values for below variables
72 ; (We use ".proc" because we want to define both a label and a scope.)
77 .word SCREEN_WIDTH - 1 ; XMax
78 .word SCREEN_HEIGHT - 1 ; YMax
84 XMin: .res 2 ; X1 value of bounding box
85 YMin: .res 2 ; Y1 value of bounding box
86 XMax: .res 2 ; X2 value of bounding box
87 YMax: .res 2 ; Y2 value of bounding box
89 XPos: .res 2 ; Current lightpen position, X
90 YPos: .res 2 ; Current lightpen position, Y
92 OldPenX: .res 1 ; Previous HW-counter values
97 ; Default Inkwell calibration.
98 ; The first number is the width of the left border;
99 ; the second number is the actual calibration value.
101 XOffset: .byte (24 + 24) / 2 ; x-offset
103 ; Jump to a function that puts a new calibration value into XOffset.
109 ;----------------------------------------------------------------------------
110 ; INSTALL routine. Is called after the driver is loaded into memory. If
111 ; possible, check if the hardware is present.
112 ; Must return a MOUSE_ERR_xx code in .XA.
116 ; Initiate variables. Just copy the default stuff over.
118 ldx #.sizeof (DefVars) - 1
124 ldx #15 ; Change to system bank
132 ldx ExecReg ; Change back to execution bank
135 ; Call a calibration function through the library-reference.
139 sta ptr1 ; Point to mouse_adjuster
143 bze @L1 ; Don't call pointer if it's NULL
144 sta Calibrate+2 ; Point to function
148 lda #<XOffset ; Function will set this variable
152 ; Be sure that the lightpen cursor is invisible and at the default location.
153 ; It needs to be done here because the lightpen interrupt handler doesn't
154 ; set the lightpen position if it hasn't changed.
159 lda #<(SCREEN_HEIGHT / 2)
160 ldx #>(SCREEN_HEIGHT / 2)
162 lda #<(SCREEN_WIDTH / 2)
163 ldx #>(SCREEN_WIDTH / 2)
173 ;----------------------------------------------------------------------------
174 ; UNINSTALL routine. Is called before the driver is removed from memory.
175 ; No return code required (the driver is removed from memory on return).
177 UNINSTALL := HIDE ; Hide cursor on exit
179 ;----------------------------------------------------------------------------
180 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
181 ; a counter for calls to show/hide, and the driver entry point is called only
182 ; if the mouse is currently visible, and should get hidden. For most drivers,
183 ; no special action is required besides hiding the lightpen cursor.
184 ; No return code required.
191 ;----------------------------------------------------------------------------
192 ; SHOW routine. Is called to show the lightpen pointer. The mouse kernel manages
193 ; a counter for calls to show/hide, and the driver entry point is called only
194 ; if the mouse is currently hidden, and should become visible. For most drivers,
195 ; no special action is required besides enabling the lightpen cursor.
196 ; No return code required.
203 ;----------------------------------------------------------------------------
204 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
205 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
206 ; No checks are done if the lightpen is currently inside the box, that is the job
207 ; of the caller. It is not necessary to validate the parameters; trust the
208 ; caller; and, save some code here. No return code required.
211 stx ptr1+1 ; Save data pointer
213 ldy #.sizeof (MOUSE_BOX) - 1
224 ;----------------------------------------------------------------------------
225 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
226 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
229 stx ptr1+1 ; Save data pointer
231 ldy #.sizeof (MOUSE_BOX) - 1
238 ;----------------------------------------------------------------------------
239 ; MOVE: Move the mouse to a new position. The position is passed as it comes
240 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
241 ; remove the parameter from the stack on return.
242 ; No checks are done if the new position is valid (within the bounding box or
243 ; the screen). No return code required.
246 MOVE: sei ; No interrupts
254 jsr MoveX ; Move the cursor
256 cli ; Allow interrupts
259 ;----------------------------------------------------------------------------
260 ; BUTTONS: Return the button mask in .XA.
263 ldx #15 ; To system bank
266 lda (cia2),y ; Read joystick inputs
267 ldx ExecReg ; Back to execution bank
270 ; Joystick 1, directions in bits 3-0.
271 ; Make the lightpen button look like a 1351 mouse.
273 asl a ; Move joystick-left bit ...
274 asl a ; ... to fire-button bit
280 ;----------------------------------------------------------------------------
281 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
282 ; No return code required.
284 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
286 sei ; Disable interrupts
287 lda XPos ; Transfer the position
296 cli ; Enable interrupts
299 sta (ptr1),y ; Store last byte
302 ;----------------------------------------------------------------------------
303 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
304 ; struct pointed to by ptr1. No return code required.
306 ; We're cheating here, to keep the code smaller: The first fields of the
307 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
308 ; call _mouse_pos to initiate the struct pointer, and fill the position
313 ; Fill in the button state
315 jsr BUTTONS ; Will not touch ptr1
316 ldy #MOUSE_INFO::BUTTONS
320 ;----------------------------------------------------------------------------
321 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
322 ; specific data in ptr1, and the ioctl code in .A.
323 ; Must return an error code in .XA.
326 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
327 ldx #>MOUSE_ERR_INV_IOCTL
330 ;----------------------------------------------------------------------------
331 ; IRQ: Irq.-handler entry point. Called as a subroutine, but in the IRQ context
332 ; (so, be careful). The routine MUST return carry set if the interrupt has been
333 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
334 ; MUST return carry clear.
337 IRQ: ldx #15 ; To system bank
340 ; Read the VIC-II lightpen registers.
346 ; Skip processing if nothing has changed.
350 ldx ExecReg ; Back to execution bank
353 ; Subtract the height of the top border, so that the lightpen co-ordinate
354 ; will match the TGI co-ordinate.
357 tay ; Remember low byte
360 ; Limit the Y co-ordinate to the bounding box.
380 ldx #15 ; To system bank
382 @SkipY: ldy #VIC_LPEN_X
384 ldx ExecReg ; Back to execution bank
388 ; Skip processing if nothing has changed.
393 ; Adjust the value by the calibration offset.
397 ; Calculate the new X co-ordinate.
398 ; The VIC-II register is eight bits; but, the screen co-ordinate is nine bits.
399 ; Therefore, the VIC-II number is doubled. Then, it points to every other pixel;
400 ; but, it can reach across the screen.
403 tay ; Remember low byte
406 tax ; Remember high byte
408 ; Limit the X co-ordinate to the bounding box.
429 @SkipX: clc ; Interrupt not "handled"
432 ; Move the lightpen pointer to the new Y pos.
438 ; Move the lightpen pointer to the new X pos.