2 ; Driver for the Inkwell Systems 170-C and 184-C lightpens.
4 ; 2013-06-03, Greg King
7 .include "zeropage.inc"
8 .include "mouse-kernel.inc"
14 ; ------------------------------------------------------------------------
15 ; Header. Includes jump table.
23 .byte $6d, $6f, $75 ; ASCII "mou"
24 .byte MOUSE_API_VERSION ; Mouse driver API version number
47 .byte MOUSE_FLAG_EARLY_IRQ
49 ; Callback table, set by the kernel before INSTALL is called.
51 CHIDE: jmp $0000 ; Hide the cursor
52 CSHOW: jmp $0000 ; Show the cursor
53 CMOVEX: jmp $0000 ; Move the cursor to X co-ord.
54 CMOVEY: jmp $0000 ; Move the cursor to Y co-ord.
57 ;----------------------------------------------------------------------------
60 SCREEN_HEIGHT = YSIZE * 8
61 SCREEN_WIDTH = XSIZE * 8
62 SCREEN_ZONE = YSIZE / 4 * XSIZE
64 ; This driver is for the standard 40-column screen.
70 ;----------------------------------------------------------------------------
71 ; Global variables. The bounding box values are sorted so that they can be
72 ; written with the least effort in the SETBOX and GETBOX routines; so, don't
78 XMin: .res 2 ; X1 value of bounding box
79 YMin: .res 2 ; Y1 value of bounding box
80 XMax: .res 2 ; X2 value of bounding box
81 YMax: .res 2 ; Y2 value of bounding box
82 Buttons: .res 1 ; Button status bits
83 XPos: .res 2 ; Current lightpen position, X
84 YPos: .res 2 ; Current lightpen position, Y
86 OldPenX: .res 1 ; Old HW-counter values
91 ; Start with an average offset.
93 XOffset: .byte 48 / 2 ; Calibration offset, for X position
97 ; Default values for above variables
98 ; (We use ".proc" because we want to define both a label and a scope.)
103 .word SCREEN_WIDTH - 1 ; XMax
104 .word SCREEN_HEIGHT - 1 ; YMax
105 .byte %00000000 ; Buttons
109 scrcode "Adjust by clicking on line."
112 scrcode "Finish by clicking off box."
118 ;----------------------------------------------------------------------------
119 ; INSTALL routine. Is called after the driver is loaded into memory. If
120 ; possible, check if the hardware is present.
121 ; Must return a MOUSE_ERR_xx code in .XA.
125 ; Initiate variables. Just copy the default stuff over.
128 ldx #.sizeof (DefVars) - 1
140 ; There is a delay between when the VIC sends its signal, and when the display
141 ; shows that signal. There is another delay between the display and when
142 ; the lightpen says that it saw that signal. Each display and pen is different.
143 ; Therefore, this driver must be calibrated to them. A white box is painted on
144 ; the screen; and, a line is drawn down the middle of it. When the user clicks
145 ; on that line, the difference between its position and where the VIC thinks
146 ; that the pen is pointing becomes an offset that is subtracted from what the
155 ldy #.sizeof (Command2) - 1
157 sta SCREEN + SCREEN_ZONE * 2 + XSIZE * 3 + (XSIZE - .sizeof (Command2)) / 2,y
158 lda #15 ; Light gray text
159 sta COLOR_RAM + SCREEN_ZONE * 2 + XSIZE * 3 + (XSIZE - .sizeof (Command1)) / 2,y
162 ldy #.sizeof (Command1) - 1
164 sta SCREEN + SCREEN_ZONE * 2 + XSIZE * 1 + (XSIZE - .sizeof (Command1)) / 2,y
165 lda #15 ; Light gray text
166 sta COLOR_RAM + SCREEN_ZONE * 2 + XSIZE * 1 + (XSIZE - .sizeof (Command1)) / 2,y
171 @L3: lda #$80 | $20 ; Reversed space screen-code
172 sta SCREEN + SCREEN_ZONE - 1,x
173 lda #15 ; Light gray box
174 sta COLOR_RAM + SCREEN_ZONE - 1,x
178 ldy #$80 | $5d ; Reversed vertical-bar screen-code
180 sty SCREEN + SCREEN_ZONE + (L + 1) * XSIZE + XSIZE / 2
184 ldx #12 ; Medium gray pointer
189 lda #<(SCREEN_HEIGHT / 4 / 2)
190 ldx #>(SCREEN_HEIGHT / 4 / 2)
193 ; Wait for the main button to be released.
198 ; Wait for the main button to be pressed.
203 ; Find out if the pen is on or off the box.
208 cpy #<(YSIZE / 4 * 1 * 8)
209 sbc #>(YSIZE / 4 * 1 * 8)
212 cpy #<(YSIZE / 4 * 2 * 8)
213 sbc #>(YSIZE / 4 * 2 * 8)
216 ; The pen is on the box; adjust the offset.
219 sub #(XSIZE * 8 / 2 + 8/2) / 2
221 sta OldPenX ; Make IRQ update X co-ordinate
224 ; Wait for the main button to be released.
229 lda XOffset ; Tell test program about calibration
238 ; Be sure the lightpen cursor is invisible and at the default location.
239 ; It needs to be done here because the lightpen interrupt handler doesn't
240 ; set the lightpen position if it hasn't changed.
244 lda #<(SCREEN_HEIGHT / 2)
245 ldx #>(SCREEN_HEIGHT / 2)
249 lda #<(SCREEN_WIDTH / 2)
250 ldx #>(SCREEN_WIDTH / 2)
260 ;----------------------------------------------------------------------------
261 ; UNINSTALL routine. Is called before the driver is removed from memory.
262 ; No return code required (the driver is removed from memory on return).
264 UNINSTALL := HIDE ; Hide cursor on exit
266 ;----------------------------------------------------------------------------
267 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
268 ; a counter for calls to show/hide, and the driver entry point is called only
269 ; if the mouse is currently visible, and should get hidden. For most drivers,
270 ; no special action is required besides hiding the lightpen cursor.
271 ; No return code required.
278 ;----------------------------------------------------------------------------
279 ; SHOW routine. Is called to show the lightpen pointer. The mouse kernel manages
280 ; a counter for calls to show/hide, and the driver entry point is called only
281 ; if the mouse is currently hidden, and should become visible. For most drivers,
282 ; no special action is required besides enabling the lightpen cursor.
283 ; No return code required.
290 ;----------------------------------------------------------------------------
291 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
292 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
293 ; No checks are done if the lightpen is currently inside the box, that is the job
294 ; of the caller. It is not necessary to validate the parameters; trust the
295 ; caller; and, save some code here. No return code required.
298 stx ptr1+1 ; Save data pointer
300 ldy #.sizeof (MOUSE_BOX) - 1
311 ;----------------------------------------------------------------------------
312 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
313 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
316 stx ptr1+1 ; Save data pointer
318 ldy #.sizeof (MOUSE_BOX) - 1
325 ;----------------------------------------------------------------------------
326 ; MOVE: Move the mouse to a new position. The position is passed as it comes
327 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
328 ; remove the parameter from the stack on return.
329 ; No checks are done if the new position is valid (within the bounding box or
330 ; the screen). No return code required.
333 MOVE: sei ; No interrupts
341 jsr MoveX ; Move the cursor
343 cli ; Allow interrupts
346 ;----------------------------------------------------------------------------
347 ; BUTTONS: Return the button mask in .XA.
353 ; Make the lightpen buttons look like a 1351 mouse.
356 asl SID_ADConv2 ; PotY
359 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
362 ;----------------------------------------------------------------------------
363 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
364 ; No return code required.
366 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
368 sei ; Disable interrupts
369 lda XPos ; Transfer the position
378 cli ; Enable interrupts
381 sta (ptr1),y ; Store last byte
384 ;----------------------------------------------------------------------------
385 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
386 ; struct pointed to by ptr1. No return code required.
388 ; We're cheating here, to keep the code smaller: The first fields of the
389 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
390 ; call _mouse_pos to initiate the struct pointer, and fill the position
395 ; Fill in the button state
397 jsr BUTTONS ; Will not touch ptr1
398 ldy #MOUSE_INFO::BUTTONS
402 ;----------------------------------------------------------------------------
403 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
404 ; specific data in ptr1, and the ioctl code in .A.
405 ; Must return an error code in .XA.
408 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
409 ldx #>MOUSE_ERR_INV_IOCTL
412 ;----------------------------------------------------------------------------
413 ; IRQ: IRQ handler entry point. Called as a subroutine, but in the IRQ context
414 ; (so, be careful). The routine MUST return carry set if the interrupt has been
415 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
416 ; MUST return carry clear.
421 ; Record the state of the buttons.
422 ; Try to avoid crosstalk between the keyboard and the lightpen.
424 ldy #%00000000 ; Set ports A and B to input
426 sty CIA1_DDRA ; Keyboard won't look like buttons
427 lda CIA1_PRB ; Read Control-Port 1
428 dec CIA1_DDRA ; Set port A back to output
429 eor #%11111111 ; Bit goes up when button goes down
432 lda #%11101111 ; (Don't change bit that feeds VIC-II)
433 sta CIA1_DDRB ; Buttons won't look like keyboard
434 sty CIA1_PRB ; Set "all keys pushed"
436 ; Read the VIC-II lightpen registers.
441 ; Skip processing if nothing has changed.
446 ; Subtract the height of the top border, so that the lightpen co-ordinate
447 ; will match the TGI co-ordinate.
450 tay ; Remember low byte
453 ; Limit the Y co-ordinate to the bounding box.
473 @SkipY: lda VIC_LPEN_X
476 ; Skip processing if nothing has changed.
481 ; Adjust the co-ordinate by the calibration offset.
485 ; Calculate the new X co-ordinate (--> .AY --> .XY).
486 ; The VIC-II register is eight bits; but, the screen co-ordinate is nine bits.
487 ; Therefor, the VIC-II number is doubled. Then, it points to every other pixel;
488 ; but, it can reach across the screen.
491 tay ; Remember low byte
496 ; Limit the X co-ordinate to the bounding box.
517 @SkipX: clc ; Interrupt not "handled"
520 ; Move the lightpen pointer to the new Y pos.
526 ; Move the lightpen pointer to the new X pos.