2 ; Driver for the Inkwell Systems 170-C and 184-C lightpens.
4 ; 2014-04-26, Christian Groessler
5 ; 2014-09-10, Greg King
8 .include "zeropage.inc"
9 .include "mouse-kernel.inc"
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table.
19 module_header _c128_inkwell_mou
25 .byte $6d, $6f, $75 ; ASCII "mou"
26 .byte MOUSE_API_VERSION ; Mouse driver API version number
49 .byte MOUSE_FLAG_EARLY_IRQ
51 ; Callback table, set by the kernel before INSTALL is called.
53 CHIDE: jmp $0000 ; Hide the cursor
54 CSHOW: jmp $0000 ; Show the cursor
55 CPREP: jmp $0000 ; Prepare to move the cursor
56 CDRAW: jmp $0000 ; Draw the cursor
57 CMOVEX: jmp $0000 ; Move the cursor to X co-ord.
58 CMOVEY: jmp $0000 ; Move the cursor to Y co-ord.
61 ;----------------------------------------------------------------------------
64 ; This driver is for the 40-column screen.
69 SCREEN_WIDTH = XSIZE * 8
70 SCREEN_HEIGHT = YSIZE * 8
73 ;----------------------------------------------------------------------------
74 ; Global variables. The bounding box values are sorted so that they can be
75 ; written with the least effort in the SETBOX and GETBOX routines; so, don't
80 ; Default values for below variables
81 ; (We use ".proc" because we want to define both a label and a scope.)
86 .word SCREEN_WIDTH - 1 ; XMax
87 .word SCREEN_HEIGHT - 1 ; YMax
88 .byte %00000000 ; Buttons
94 XMin: .res 2 ; X1 value of bounding box
95 YMin: .res 2 ; Y1 value of bounding box
96 XMax: .res 2 ; X2 value of bounding box
97 YMax: .res 2 ; Y2 value of bounding box
98 Buttons: .res 1 ; Button status bits
100 XPos: .res 2 ; Current lightpen position, X
101 YPos: .res 2 ; Current lightpen position, Y
103 OldPenX: .res 1 ; Previous HW-counter values
108 ; Keyboard buffer fill level at start of interrupt
110 old_key_count: .res 1
112 ; Original IRQ vector
118 ; Default Inkwell calibration.
119 ; The first number is the width of the left border;
120 ; the second number is the actual calibration value.
121 ; See a comment below (at "Calculate the new X co-ordinate")
122 ; for the reason for the third number.
124 XOffset: .byte (24 + 24) / 2 ; x-offset
126 ; Jump to a function that puts a new calibration value into XOffset.
132 ;----------------------------------------------------------------------------
133 ; INSTALL routine. Is called after the driver is loaded into memory. If
134 ; possible, check if the hardware is present.
135 ; Must return a MOUSE_ERR_xx code in .XA.
139 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
140 ; That allows direct access to the VIC-IIe's sprite registers.
147 ; Initiate some variables. Just copy the default stuff over.
149 ldx #.sizeof (DefVars) - 1
160 ; Initiate our IRQ magic.
162 ; Remember the ROM IRQ continuation address.
170 sta ptr3 ; Point to mouse_adjuster
173 ; Set the ROM IRQ continuation address to point to the provided routine.
182 ; Set the address of our IRQ callback routine.
183 ; Because it's called via "rts", we must use "address-1".
191 ; Set the ROM entry-point vector.
192 ; Because it's called via "rts", we must decrement it by one.
203 ; Call a calibration function through the library-reference.
207 bze @L1 ; Don't call pointer if it's NULL
208 sta Calibrate+2 ; Point to function
212 lda #<XOffset ; Function will set this variable
216 ; Be sure that the lightpen cursor is invisible and at the default location.
217 ; It needs to be done here because the lightpen interrupt handler doesn't
218 ; set the lightpen position if it hasn't changed.
223 lda #<(SCREEN_HEIGHT / 2)
224 ldx #>(SCREEN_HEIGHT / 2)
226 lda #<(SCREEN_WIDTH / 2)
227 ldx #>(SCREEN_WIDTH / 2)
237 ;----------------------------------------------------------------------------
238 ; UNINSTALL routine. Is called before the driver is removed from memory.
239 ; No return code required (the driver is removed from memory on return).
247 ;cli ; This will be done at end of HIDE
249 jsr HIDE ; Hide cursor on exit
254 ;----------------------------------------------------------------------------
255 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
256 ; a counter for calls to show/hide, and the driver entry point is called only
257 ; if the mouse is currently visible, and should get hidden. For most drivers,
258 ; no special action is required besides hiding the lightpen cursor.
259 ; No return code required.
266 ;----------------------------------------------------------------------------
267 ; SHOW routine. Is called to show 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 hidden, and should become visible. For most drivers,
270 ; no special action is required besides enabling the lightpen cursor.
271 ; No return code required.
278 ;----------------------------------------------------------------------------
279 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
280 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
281 ; No checks are done if the lightpen is currently inside the box, that is the job
282 ; of the caller. It is not necessary to validate the parameters; trust the
283 ; caller; and, save some code here. No return code required.
286 stx ptr1+1 ; Save data pointer
288 ldy #.sizeof (MOUSE_BOX) - 1
299 ;----------------------------------------------------------------------------
300 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
301 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
304 stx ptr1+1 ; Save data pointer
306 ldy #.sizeof (MOUSE_BOX) - 1
313 ;----------------------------------------------------------------------------
314 ; MOVE: Move the mouse to a new position. The position is passed as it comes
315 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
316 ; remove the parameter from the stack on return.
317 ; No checks are done if the new position is valid (within the bounding box or
318 ; the screen). No return code required.
321 MOVE: sei ; No interrupts
329 jsr MoveX ; Move the cursor
331 cli ; Allow interrupts
334 ;----------------------------------------------------------------------------
335 ; BUTTONS: Return the button mask in .XA.
341 ; Make the lightpen buttons look like a 1351 mouse.
344 asl SID_ADConv2 ; PotY
347 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
350 ;----------------------------------------------------------------------------
351 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
352 ; No return code required.
354 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
356 sei ; Disable interrupts
357 lda XPos ; Transfer the position
366 cli ; Enable interrupts
369 sta (ptr1),y ; Store last byte
372 ;----------------------------------------------------------------------------
373 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
374 ; struct pointed to by ptr1. No return code required.
376 ; We're cheating here, to keep the code smaller: The first fields of the
377 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
378 ; call _mouse_pos to initiate the struct pointer, and fill the position
383 ; Fill in the button state
385 jsr BUTTONS ; Will not touch ptr1
386 ldy #MOUSE_INFO::BUTTONS
390 ;----------------------------------------------------------------------------
391 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
392 ; specific data in ptr1, and the ioctl code in .A.
393 ; Must return an error code in .XA.
396 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
397 ldx #>MOUSE_ERR_INV_IOCTL
400 ;----------------------------------------------------------------------------
401 ; IRQ: IRQ handler entry point. Called as a subroutine, but in the IRQ context
402 ; (so, be careful). The routine MUST return carry set if the interrupt has been
403 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
404 ; MUST return carry clear.
411 ; Record the state of the buttons.
412 ; Try to avoid crosstalk between the keyboard and the lightpen.
414 ldy #%00000000 ; Set ports A and B to input
416 sty CIA1_DDRA ; Keyboard won't look like buttons
417 ;lda #%01111111 ; (Keyboard scan leaves this in port A)
419 lda CIA1_PRB ; Read Control Port 1
420 dec CIA1_DDRA ; Set port A back to output
421 eor #%11111111 ; Bit goes up when button goes down
424 ; Read the VIC-II lightpen registers.
429 ; Skip processing if nothing has changed.
434 ; Subtract the height of the top border, so that the lightpen co-ordinate
435 ; will match the TGI co-ordinate.
438 tay ; Remember low byte
441 ; Limit the Y co-ordinate to the bounding box.
461 @SkipY: lda VIC_LPEN_X
464 ; Skip processing if nothing has changed.
469 ; Adjust the value by the calibration offset.
473 ; Calculate the new X co-ordinate.
474 ; The VIC-II register is eight bits; but, the screen co-ordinate is nine bits.
475 ; Therefore, the VIC-II number is doubled. Then, it points to every other pixel;
476 ; but, it can reach across the screen.
479 tay ; Remember low byte
482 tax ; Remember high byte
484 ; Limit the X co-ordinate to the bounding box.
506 clc ; Interrupt not "handled"
509 ; Move the lightpen pointer to the new Y pos.
515 ; Move the lightpen pointer to the new X pos.
521 .define OLD_BUTTONS Buttons ; Tells callback.inc where the old port status is stored
522 .include "callback.inc"