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 CPREP: jmp $0000 ; Prepare to move the cursor
57 CDRAW: jmp $0000 ; Draw the cursor
58 CMOVEX: jmp $0000 ; Move the cursor to X co-ord.
59 CMOVEY: jmp $0000 ; Move the cursor to Y co-ord.
62 ;----------------------------------------------------------------------------
65 SCREEN_WIDTH = XSIZE * 8
66 SCREEN_HEIGHT = YSIZE * 8
68 ;----------------------------------------------------------------------------
69 ; Global variables. The bounding box values are sorted so that they can be
70 ; written with the least effort in the SETBOX and GETBOX routines; so, don't
75 ; Default values for below variables
76 ; (We use ".proc" because we want to define both a label and a scope.)
81 .word SCREEN_WIDTH - 1 ; XMax
82 .word SCREEN_HEIGHT - 1 ; YMax
88 XMin: .res 2 ; X1 value of bounding box
89 YMin: .res 2 ; Y1 value of bounding box
90 XMax: .res 2 ; X2 value of bounding box
91 YMax: .res 2 ; Y2 value of bounding box
93 XPos: .res 2 ; Current lightpen position, X
94 YPos: .res 2 ; Current lightpen position, Y
96 OldPenX: .res 1 ; Previous HW-counter values
101 ; Default Inkwell calibration.
102 ; The first number is the width of the left border;
103 ; the second number is the actual calibration value.
105 XOffset: .byte (24 + 24) / 2 ; x-offset
107 ; Jump to a function that puts a new calibration value into XOffset.
113 ;----------------------------------------------------------------------------
114 ; INSTALL routine. Is called after the driver is loaded into memory. If
115 ; possible, check if the hardware is present.
116 ; Must return a MOUSE_ERR_xx code in .XA.
120 ; Initiate variables. Just copy the default stuff over.
122 ldx #.sizeof (DefVars) - 1
128 ldx #15 ; Change to system bank
136 ldx ExecReg ; Change back to execution bank
139 ; Call a calibration function through the library-reference.
143 sta ptr1 ; Point to mouse_adjuster
147 bze @L1 ; Don't call pointer if it's NULL
148 sta Calibrate+2 ; Point to function
152 lda #<XOffset ; Function will set this variable
156 ; Be sure that the lightpen cursor is invisible and at the default location.
157 ; It needs to be done here because the lightpen interrupt handler doesn't
158 ; set the lightpen position if it hasn't changed.
163 lda #<(SCREEN_HEIGHT / 2)
164 ldx #>(SCREEN_HEIGHT / 2)
166 lda #<(SCREEN_WIDTH / 2)
167 ldx #>(SCREEN_WIDTH / 2)
177 ;----------------------------------------------------------------------------
178 ; UNINSTALL routine. Is called before the driver is removed from memory.
179 ; No return code required (the driver is removed from memory on return).
181 UNINSTALL := HIDE ; Hide cursor on exit
183 ;----------------------------------------------------------------------------
184 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
185 ; a counter for calls to show/hide, and the driver entry point is called only
186 ; if the mouse is currently visible, and should get hidden. For most drivers,
187 ; no special action is required besides hiding the lightpen cursor.
188 ; No return code required.
195 ;----------------------------------------------------------------------------
196 ; SHOW routine. Is called to show the lightpen pointer. The mouse kernel manages
197 ; a counter for calls to show/hide, and the driver entry point is called only
198 ; if the mouse is currently hidden, and should become visible. For most drivers,
199 ; no special action is required besides enabling the lightpen cursor.
200 ; No return code required.
207 ;----------------------------------------------------------------------------
208 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
209 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
210 ; No checks are done if the lightpen is currently inside the box, that is the job
211 ; of the caller. It is not necessary to validate the parameters; trust the
212 ; caller; and, save some code here. No return code required.
215 stx ptr1+1 ; Save data pointer
217 ldy #.sizeof (MOUSE_BOX) - 1
228 ;----------------------------------------------------------------------------
229 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
230 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
233 stx ptr1+1 ; Save data pointer
235 ldy #.sizeof (MOUSE_BOX) - 1
242 ;----------------------------------------------------------------------------
243 ; MOVE: Move the mouse to a new position. The position is passed as it comes
244 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
245 ; remove the parameter from the stack on return.
246 ; No checks are done if the new position is valid (within the bounding box or
247 ; the screen). No return code required.
250 MOVE: sei ; No interrupts
258 jsr MoveX ; Move the cursor
260 cli ; Allow interrupts
263 ;----------------------------------------------------------------------------
264 ; BUTTONS: Return the button mask in .XA.
267 ldx #15 ; To system bank
270 lda (cia2),y ; Read joystick inputs
271 ldx ExecReg ; Back to execution bank
274 ; Joystick 1, directions in bits 3-0.
275 ; Make the lightpen button look like a 1351 mouse.
277 asl a ; Move joystick-left bit ...
278 asl a ; ... to fire-button bit
284 ;----------------------------------------------------------------------------
285 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
286 ; No return code required.
288 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
290 sei ; Disable interrupts
291 lda XPos ; Transfer the position
300 cli ; Enable interrupts
303 sta (ptr1),y ; Store last byte
306 ;----------------------------------------------------------------------------
307 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
308 ; struct pointed to by ptr1. No return code required.
310 ; We're cheating here, to keep the code smaller: The first fields of the
311 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
312 ; call _mouse_pos to initiate the struct pointer, and fill the position
317 ; Fill in the button state
319 jsr BUTTONS ; Will not touch ptr1
320 ldy #MOUSE_INFO::BUTTONS
324 ;----------------------------------------------------------------------------
325 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
326 ; specific data in ptr1, and the ioctl code in .A.
327 ; Must return an error code in .XA.
330 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
331 ldx #>MOUSE_ERR_INV_IOCTL
334 ;----------------------------------------------------------------------------
335 ; IRQ: Irq.-handler entry point. Called as a subroutine, but in the IRQ context
336 ; (so, be careful). The routine MUST return carry set if the interrupt has been
337 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
338 ; MUST return carry clear.
342 ldx #15 ; To system bank
345 ; Read the VIC-II lightpen registers.
351 ; Skip processing if nothing has changed.
355 ldx ExecReg ; Back to execution bank
358 ; Subtract the height of the top border, so that the lightpen co-ordinate
359 ; will match the TGI co-ordinate.
362 tay ; Remember low byte
365 ; Limit the Y co-ordinate to the bounding box.
385 ldx #15 ; To system bank
387 @SkipY: ldy #VIC_LPEN_X
389 ldx ExecReg ; Back to execution bank
393 ; Skip processing if nothing has changed.
398 ; Adjust the value by the calibration offset.
402 ; Calculate the new X co-ordinate.
403 ; The VIC-II register is eight bits; but, the screen co-ordinate is nine bits.
404 ; Therefore, the VIC-II number is doubled. Then, it points to every other pixel;
405 ; but, it can reach across the screen.
408 tay ; Remember low byte
411 tax ; Remember high byte
413 ; Limit the X co-ordinate to the bounding box.
435 clc ; Interrupt not "handled"
438 ; Move the lightpen pointer to the new Y pos.
444 ; Move the lightpen pointer to the new X pos.