2 ; Driver for the Inkwell Systems 170-C and 184-C lightpens.
4 ; 2014-09-10, Greg King
7 .include "zeropage.inc"
8 .include "mouse-kernel.inc"
15 ; ------------------------------------------------------------------------
16 ; Header. Includes jump table.
18 module_header _c64_inkwell_mou
24 .byte $6d, $6f, $75 ; ASCII "mou"
25 .byte MOUSE_API_VERSION ; Mouse driver API version number
48 .byte MOUSE_FLAG_EARLY_IRQ
50 ; Callback table, set by the kernel before INSTALL is called.
52 CHIDE: jmp $0000 ; Hide the cursor
53 CSHOW: jmp $0000 ; Show the cursor
54 CPREP: jmp $0000 ; Prepare to move the cursor
55 CDRAW: jmp $0000 ; Draw 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
67 ;----------------------------------------------------------------------------
68 ; Global variables. The bounding box values are sorted so that they can be
69 ; written with the least effort in the SETBOX and GETBOX routines; so, don't
74 ; Default values for below variables
75 ; (We use ".proc" because we want to define both a label and a scope.)
80 .word SCREEN_WIDTH - 1 ; XMax
81 .word SCREEN_HEIGHT - 1 ; YMax
82 .byte %00000000 ; Buttons
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
92 Buttons: .res 1 ; Button status bits
94 XPos: .res 2 ; Current lightpen position, X
95 YPos: .res 2 ; Current lightpen position, Y
97 OldPenX: .res 1 ; Previous HW-counter values
102 ; Default Inkwell calibration.
103 ; The first number is the width of the left border;
104 ; the second number is the actual calibration value.
105 ; See a comment below (at "Calculate the new X co-ordinate")
106 ; for the reason for the third number.
108 XOffset: .byte (24 + 24) / 2 ; x-offset
110 ; Jump to a function that puts a new calibration value into XOffset.
116 ;----------------------------------------------------------------------------
117 ; INSTALL routine. Is called after the driver is loaded into memory. If
118 ; possible, check if the hardware is present.
119 ; Must return a MOUSE_ERR_xx code in .XA.
123 ; Initiate variables. Just copy the default stuff over.
125 ldx #.sizeof (DefVars) - 1
136 ; Call a calibration function through the library-reference.
140 sta ptr1 ; Point to mouse_adjuster
144 bze @L1 ; Don't call pointer if it's NULL
145 sta Calibrate+2 ; Point to function
149 lda #<XOffset ; Function will set this variable
153 ; Be sure that the lightpen cursor is invisible and at the default location.
154 ; It needs to be done here because the lightpen interrupt handler doesn't
155 ; set the lightpen position if it hasn't changed.
160 lda #<(SCREEN_HEIGHT / 2)
161 ldx #>(SCREEN_HEIGHT / 2)
163 lda #<(SCREEN_WIDTH / 2)
164 ldx #>(SCREEN_WIDTH / 2)
174 ;----------------------------------------------------------------------------
175 ; UNINSTALL routine. Is called before the driver is removed from memory.
176 ; No return code required (the driver is removed from memory on return).
178 UNINSTALL := HIDE ; Hide cursor on exit
180 ;----------------------------------------------------------------------------
181 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
182 ; a counter for calls to show/hide, and the driver entry point is called only
183 ; if the mouse is currently visible, and should get hidden. For most drivers,
184 ; no special action is required besides hiding the lightpen cursor.
185 ; No return code required.
192 ;----------------------------------------------------------------------------
193 ; SHOW routine. Is called to show the lightpen pointer. The mouse kernel manages
194 ; a counter for calls to show/hide, and the driver entry point is called only
195 ; if the mouse is currently hidden, and should become visible. For most drivers,
196 ; no special action is required besides enabling the lightpen cursor.
197 ; No return code required.
204 ;----------------------------------------------------------------------------
205 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
206 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
207 ; No checks are done if the lightpen is currently inside the box, that is the job
208 ; of the caller. It is not necessary to validate the parameters; trust the
209 ; caller; and, save some code here. No return code required.
212 stx ptr1+1 ; Save data pointer
214 ldy #.sizeof (MOUSE_BOX) - 1
225 ;----------------------------------------------------------------------------
226 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
227 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
230 stx ptr1+1 ; Save data pointer
232 ldy #.sizeof (MOUSE_BOX) - 1
239 ;----------------------------------------------------------------------------
240 ; MOVE: Move the mouse to a new position. The position is passed as it comes
241 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
242 ; remove the parameter from the stack on return.
243 ; No checks are done if the new position is valid (within the bounding box or
244 ; the screen). No return code required.
247 MOVE: sei ; No interrupts
255 jsr MoveX ; Move the cursor
257 cli ; Allow interrupts
260 ;----------------------------------------------------------------------------
261 ; BUTTONS: Return the button mask in .XA.
267 ; Make the lightpen buttons look like a 1351 mouse.
270 asl SID_ADConv2 ; PotY
273 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
276 ;----------------------------------------------------------------------------
277 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
278 ; No return code required.
280 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
282 sei ; Disable interrupts
283 lda XPos ; Transfer the position
292 cli ; Enable interrupts
295 sta (ptr1),y ; Store last byte
298 ;----------------------------------------------------------------------------
299 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
300 ; struct pointed to by ptr1. No return code required.
302 ; We're cheating here, to keep the code smaller: The first fields of the
303 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
304 ; call _mouse_pos to initiate the struct pointer, and fill the position
309 ; Fill in the button state
311 jsr BUTTONS ; Will not touch ptr1
312 ldy #MOUSE_INFO::BUTTONS
316 ;----------------------------------------------------------------------------
317 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
318 ; specific data in ptr1, and the ioctl code in .A.
319 ; Must return an error code in .XA.
322 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
323 ldx #>MOUSE_ERR_INV_IOCTL
326 ;----------------------------------------------------------------------------
327 ; IRQ: IRQ handler entry point. Called as a subroutine, but in the IRQ context
328 ; (so, be careful). The routine MUST return carry set if the interrupt has been
329 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
330 ; MUST return carry clear.
335 ; Record the state of the buttons.
336 ; Try to avoid crosstalk between the keyboard and the lightpen.
338 ldy #%00000000 ; Set ports A and B to input
340 sty CIA1_DDRA ; Keyboard won't look like buttons
341 lda CIA1_PRB ; Read Control-Port 1
342 dec CIA1_DDRA ; Set port A back to output
343 eor #%11111111 ; Bit goes up when button goes down
346 lda #%11101111 ; (Don't change bit that feeds VIC-II)
347 sta CIA1_DDRB ; Buttons won't look like keyboard
348 sty CIA1_PRB ; Set "all keys pushed"
350 ; Read the VIC-II lightpen registers.
355 ; Skip processing if nothing has changed.
360 ; Subtract the height of the top border, so that the lightpen co-ordinate
361 ; will match the TGI co-ordinate.
364 tay ; Remember low byte
367 ; Limit the Y co-ordinate to the bounding box.
387 @SkipY: lda VIC_LPEN_X
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.
432 clc ; Interrupt not "handled"
435 ; Move the lightpen pointer to the new Y pos.
441 ; Move the lightpen pointer to the new X pos.