2 ; Driver for the Inkwell Systems 170-C and 184-C lightpens.
4 ; This driver reads only the main button on the 184-C.
6 ; 2013-09-05, Greg King
9 .include "zeropage.inc"
10 .include "../extzp.inc"
12 .include "mouse-kernel.inc"
17 ; ------------------------------------------------------------------------
18 ; Header. Includes jump table.
26 .byte $6d, $6f, $75 ; ASCII "mou"
27 .byte MOUSE_API_VERSION ; Mouse driver API version number
50 .byte MOUSE_FLAG_EARLY_IRQ
52 ; Callback table, set by the kernel before INSTALL is called.
54 CHIDE: jmp $0000 ; Hide the cursor
55 CSHOW: jmp $0000 ; Show the cursor
56 CMOVEX: jmp $0000 ; Move the cursor to X co-ord.
57 CMOVEY: jmp $0000 ; Move the cursor to Y co-ord.
60 ;----------------------------------------------------------------------------
63 SCREEN_WIDTH = XSIZE * 8
64 SCREEN_HEIGHT = YSIZE * 8
66 ;----------------------------------------------------------------------------
67 ; Global variables. The bounding box values are sorted so that they can be
68 ; written with the least effort in the SETBOX and GETBOX routines; so, don't
73 ; Default values for below variables
74 ; (We use ".proc" because we want to define both a label and a scope.)
79 .word SCREEN_WIDTH - 1 ; XMax
80 .word SCREEN_HEIGHT - 1 ; YMax
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
91 XPos: .res 2 ; Current lightpen position, X
92 YPos: .res 2 ; Current lightpen position, Y
94 OldPenX: .res 1 ; Previous HW-counter values
99 ; Default Inkwell calibration.
100 ; The first number is the width of the left border;
101 ; the second number is the actual calibration value.
103 XOffset: .byte (24 + 24) / 2 ; x-offset
105 ; Jump to a function that puts a new calibration value into XOffset.
111 ;----------------------------------------------------------------------------
112 ; INSTALL routine. Is called after the driver is loaded into memory. If
113 ; possible, check if the hardware is present.
114 ; Must return a MOUSE_ERR_xx code in .XA.
118 ; Initiate variables. Just copy the default stuff over.
120 ldx #.sizeof (DefVars) - 1
126 ldx #15 ; Change to system bank
134 ldx ExecReg ; Change back to execution bank
137 ; Call a calibration function through the library-reference.
141 sta ptr1 ; Point to mouse_adjuster
145 bze @L1 ; Don't call pointer if it's NULL
146 sta Calibrate+2 ; Point to function
150 lda #<XOffset ; Function will set this variable
154 ; Be sure that the lightpen cursor is invisible and at the default location.
155 ; It needs to be done here because the lightpen interrupt handler doesn't
156 ; set the lightpen position if it hasn't changed.
161 lda #<(SCREEN_HEIGHT / 2)
162 ldx #>(SCREEN_HEIGHT / 2)
164 lda #<(SCREEN_WIDTH / 2)
165 ldx #>(SCREEN_WIDTH / 2)
175 ;----------------------------------------------------------------------------
176 ; UNINSTALL routine. Is called before the driver is removed from memory.
177 ; No return code required (the driver is removed from memory on return).
179 UNINSTALL := HIDE ; Hide cursor on exit
181 ;----------------------------------------------------------------------------
182 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
183 ; a counter for calls to show/hide, and the driver entry point is called only
184 ; if the mouse is currently visible, and should get hidden. For most drivers,
185 ; no special action is required besides hiding the lightpen cursor.
186 ; No return code required.
193 ;----------------------------------------------------------------------------
194 ; SHOW routine. Is called to show the lightpen pointer. The mouse kernel manages
195 ; a counter for calls to show/hide, and the driver entry point is called only
196 ; if the mouse is currently hidden, and should become visible. For most drivers,
197 ; no special action is required besides enabling the lightpen cursor.
198 ; No return code required.
205 ;----------------------------------------------------------------------------
206 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
207 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
208 ; No checks are done if the lightpen is currently inside the box, that is the job
209 ; of the caller. It is not necessary to validate the parameters; trust the
210 ; caller; and, save some code here. No return code required.
213 stx ptr1+1 ; Save data pointer
215 ldy #.sizeof (MOUSE_BOX) - 1
226 ;----------------------------------------------------------------------------
227 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
228 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
231 stx ptr1+1 ; Save data pointer
233 ldy #.sizeof (MOUSE_BOX) - 1
240 ;----------------------------------------------------------------------------
241 ; MOVE: Move the mouse to a new position. The position is passed as it comes
242 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
243 ; remove the parameter from the stack on return.
244 ; No checks are done if the new position is valid (within the bounding box or
245 ; the screen). No return code required.
248 MOVE: sei ; No interrupts
256 jsr MoveX ; Move the cursor
258 cli ; Allow interrupts
261 ;----------------------------------------------------------------------------
262 ; BUTTONS: Return the button mask in .XA.
265 ldx #15 ; To system bank
268 lda (cia2),y ; Read joystick inputs
269 ldx ExecReg ; Back to execution bank
272 ; Joystick 1, directions in bits 3-0.
273 ; Make the lightpen button look like a 1351 mouse.
275 asl a ; Move joystick-left bit ...
276 asl a ; ... to fire-button bit
282 ;----------------------------------------------------------------------------
283 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
284 ; No return code required.
286 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
288 sei ; Disable interrupts
289 lda XPos ; Transfer the position
298 cli ; Enable interrupts
301 sta (ptr1),y ; Store last byte
304 ;----------------------------------------------------------------------------
305 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
306 ; struct pointed to by ptr1. No return code required.
308 ; We're cheating here, to keep the code smaller: The first fields of the
309 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
310 ; call _mouse_pos to initiate the struct pointer, and fill the position
315 ; Fill in the button state
317 jsr BUTTONS ; Will not touch ptr1
318 ldy #MOUSE_INFO::BUTTONS
322 ;----------------------------------------------------------------------------
323 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
324 ; specific data in ptr1, and the ioctl code in .A.
325 ; Must return an error code in .XA.
328 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
329 ldx #>MOUSE_ERR_INV_IOCTL
332 ;----------------------------------------------------------------------------
333 ; IRQ: Irq.-handler entry point. Called as a subroutine, but in the IRQ context
334 ; (so, be careful). The routine MUST return carry set if the interrupt has been
335 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
336 ; MUST return carry clear.
339 IRQ: ldx #15 ; To system bank
342 ; Read the VIC-II lightpen registers.
348 ; Skip processing if nothing has changed.
352 ldx ExecReg ; Back to execution bank
355 ; Subtract the height of the top border, so that the lightpen co-ordinate
356 ; will match the TGI co-ordinate.
359 tay ; Remember low byte
362 ; Limit the Y co-ordinate to the bounding box.
382 ldx #15 ; To system bank
384 @SkipY: ldy #VIC_LPEN_X
386 ldx ExecReg ; Back to execution bank
390 ; Skip processing if nothing has changed.
395 ; Adjust the value by the calibration offset.
399 ; Calculate the new X co-ordinate.
400 ; The VIC-II register is eight bits; but, the screen co-ordinate is nine bits.
401 ; Therefore, the VIC-II number is doubled. Then, it points to every other pixel;
402 ; but, it can reach across the screen.
405 tay ; Remember low byte
408 tax ; Remember high byte
410 ; Limit the X co-ordinate to the bounding box.
431 @SkipX: clc ; Interrupt not "handled"
434 ; Move the lightpen pointer to the new Y pos.
440 ; Move the lightpen pointer to the new X pos.