2 ; Driver for the Inkwell Systems 170-C and 184-C lightpens.
4 ; 2014-04-26, Christian Groessler
5 ; 2014-05-01, 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.
122 XOffset: .byte (24 + 24) / 2 ; x-offset
124 ; Jump to a function that puts a new calibration value into XOffset.
130 ;----------------------------------------------------------------------------
131 ; INSTALL routine. Is called after the driver is loaded into memory. If
132 ; possible, check if the hardware is present.
133 ; Must return a MOUSE_ERR_xx code in .XA.
137 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
138 ; That allows direct access to the VIC-IIe's sprite registers.
145 ; Initiate some variables. Just copy the default stuff over.
147 ldx #.sizeof (DefVars) - 1
158 ; Initiate our IRQ magic.
160 ; Remember the ROM IRQ continuation address.
168 sta ptr3 ; Point to mouse_adjuster
171 ; Set the ROM IRQ continuation address to point to the provided routine.
180 ; Set the address of our IRQ callback routine.
181 ; Because it's called via "rts", we must use "address-1".
189 ; Set the ROM entry-point vector.
190 ; Because it's called via "rts", we must decrement it by one.
201 ; Call a calibration function through the library-reference.
205 bze @L1 ; Don't call pointer if it's NULL
206 sta Calibrate+2 ; Point to function
210 lda #<XOffset ; Function will set this variable
214 ; Be sure that the lightpen cursor is invisible and at the default location.
215 ; It needs to be done here because the lightpen interrupt handler doesn't
216 ; set the lightpen position if it hasn't changed.
221 lda #<(SCREEN_HEIGHT / 2)
222 ldx #>(SCREEN_HEIGHT / 2)
224 lda #<(SCREEN_WIDTH / 2)
225 ldx #>(SCREEN_WIDTH / 2)
235 ;----------------------------------------------------------------------------
236 ; UNINSTALL routine. Is called before the driver is removed from memory.
237 ; No return code required (the driver is removed from memory on return).
245 ;cli ; This will be done at end of HIDE
247 jsr HIDE ; Hide cursor on exit
252 ;----------------------------------------------------------------------------
253 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
254 ; a counter for calls to show/hide, and the driver entry point is called only
255 ; if the mouse is currently visible, and should get hidden. For most drivers,
256 ; no special action is required besides hiding the lightpen cursor.
257 ; No return code required.
264 ;----------------------------------------------------------------------------
265 ; SHOW routine. Is called to show the lightpen pointer. The mouse kernel manages
266 ; a counter for calls to show/hide, and the driver entry point is called only
267 ; if the mouse is currently hidden, and should become visible. For most drivers,
268 ; no special action is required besides enabling the lightpen cursor.
269 ; No return code required.
276 ;----------------------------------------------------------------------------
277 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
278 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
279 ; No checks are done if the lightpen is currently inside the box, that is the job
280 ; of the caller. It is not necessary to validate the parameters; trust the
281 ; caller; and, save some code here. No return code required.
284 stx ptr1+1 ; Save data pointer
286 ldy #.sizeof (MOUSE_BOX) - 1
297 ;----------------------------------------------------------------------------
298 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
299 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
302 stx ptr1+1 ; Save data pointer
304 ldy #.sizeof (MOUSE_BOX) - 1
311 ;----------------------------------------------------------------------------
312 ; MOVE: Move the mouse to a new position. The position is passed as it comes
313 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
314 ; remove the parameter from the stack on return.
315 ; No checks are done if the new position is valid (within the bounding box or
316 ; the screen). No return code required.
319 MOVE: sei ; No interrupts
327 jsr MoveX ; Move the cursor
329 cli ; Allow interrupts
332 ;----------------------------------------------------------------------------
333 ; BUTTONS: Return the button mask in .XA.
339 ; Make the lightpen buttons look like a 1351 mouse.
342 asl SID_ADConv2 ; PotY
345 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
348 ;----------------------------------------------------------------------------
349 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
350 ; No return code required.
352 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
354 sei ; Disable interrupts
355 lda XPos ; Transfer the position
364 cli ; Enable interrupts
367 sta (ptr1),y ; Store last byte
370 ;----------------------------------------------------------------------------
371 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
372 ; struct pointed to by ptr1. No return code required.
374 ; We're cheating here, to keep the code smaller: The first fields of the
375 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
376 ; call _mouse_pos to initiate the struct pointer, and fill the position
381 ; Fill in the button state
383 jsr BUTTONS ; Will not touch ptr1
384 ldy #MOUSE_INFO::BUTTONS
388 ;----------------------------------------------------------------------------
389 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
390 ; specific data in ptr1, and the ioctl code in .A.
391 ; Must return an error code in .XA.
394 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
395 ldx #>MOUSE_ERR_INV_IOCTL
398 ;----------------------------------------------------------------------------
399 ; IRQ: IRQ handler entry point. Called as a subroutine, but in the IRQ context
400 ; (so, be careful). The routine MUST return carry set if the interrupt has been
401 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
402 ; MUST return carry clear.
409 ; Record the state of the buttons.
410 ; Try to avoid crosstalk between the keyboard and the lightpen.
412 ldy #%00000000 ; Set ports A and B to input
414 sty CIA1_DDRA ; Keyboard won't look like buttons
415 ;lda #%01111111 ; (Keyboard scan leaves this in port A)
417 lda CIA1_PRB ; Read Control Port 1
418 dec CIA1_DDRA ; Set port A back to output
419 eor #%11111111 ; Bit goes up when button goes down
422 ; Read the VIC-II lightpen registers.
427 ; Skip processing if nothing has changed.
432 ; Subtract the height of the top border, so that the lightpen co-ordinate
433 ; will match the TGI co-ordinate.
436 tay ; Remember low byte
439 ; Limit the Y co-ordinate to the bounding box.
459 @SkipY: lda VIC_LPEN_X
462 ; Skip processing if nothing has changed.
467 ; Adjust the value by the calibration offset.
471 ; Calculate the new X co-ordinate.
472 ; The VIC-II register is eight bits; but, the screen co-ordinate is nine bits.
473 ; Therefore, the VIC-II number is doubled. Then, it points to every other pixel;
474 ; but, it can reach across the screen.
477 tay ; Remember low byte
480 tax ; Remember high byte
482 ; Limit the X co-ordinate to the bounding box.
504 clc ; Interrupt not "handled"
507 ; Move the lightpen pointer to the new Y pos.
513 ; Move the lightpen pointer to the new X pos.
519 .define OLD_BUTTONS Buttons ; Tells callback.inc where the old port status is stored
520 .include "callback.inc"