2 ; Driver for the Inkwell Systems 170-C and 184-C lightpens.
4 ; 2013-07-01, Greg King
7 .include "zeropage.inc"
8 .include "mouse-kernel.inc"
13 ; ------------------------------------------------------------------------
14 ; Header. Includes jump table.
22 .byte $6d, $6f, $75 ; ASCII "mou"
23 .byte MOUSE_API_VERSION ; Mouse driver API version number
46 .byte MOUSE_FLAG_EARLY_IRQ
48 ; Callback table, set by the kernel before INSTALL is called.
50 CHIDE: jmp $0000 ; Hide the cursor
51 CSHOW: jmp $0000 ; Show the cursor
52 CMOVEX: jmp $0000 ; Move the cursor to X co-ord.
53 CMOVEY: jmp $0000 ; Move the cursor to Y co-ord.
56 ;----------------------------------------------------------------------------
59 ; This driver is for the 40-column screen.
64 SCREEN_WIDTH = XSIZE * 8
65 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
83 .byte %00000000 ; Buttons
89 XMin: .res 2 ; X1 value of bounding box
90 YMin: .res 2 ; Y1 value of bounding box
91 XMax: .res 2 ; X2 value of bounding box
92 YMax: .res 2 ; Y2 value of bounding box
93 Buttons: .res 1 ; Button status bits
95 XPos: .res 2 ; Current lightpen position, X
96 YPos: .res 2 ; Current lightpen position, Y
98 OldPenX: .res 1 ; Previous HW-counter values
105 ; Default Inkwell calibration.
106 ; The first number is the width of the left border;
107 ; the second number is the actual calibration value.
109 XOffset: .byte (24 + 24) / 2 ; x-offset
111 ; Jump to a function that puts a new calibration value into XOffset.
117 ;----------------------------------------------------------------------------
118 ; INSTALL routine. Is called after the driver is loaded into memory. If
119 ; possible, check if the hardware is present.
120 ; Must return a MOUSE_ERR_xx code in .XA.
124 ; Disable the BASIC interpreter's interrupt-driven sprite-motion code.
125 ; That allows direct access to the VIC-IIe's sprite registers.
132 ; Initiate variables. Just copy the default stuff over.
134 ldx #.sizeof (DefVars) - 1
145 ; Call a calibration function through the library-reference.
149 sta ptr1 ; Point to mouse_adjuster
153 bze @L1 ; Don't call pointer if it's NULL
154 sta Calibrate+2 ; Point to function
158 lda #<XOffset ; Function will set this variable
162 ; Be sure that the lightpen cursor is invisible and at the default location.
163 ; It needs to be done here because the lightpen interrupt handler doesn't
164 ; set the lightpen position if it hasn't changed.
169 lda #<(SCREEN_HEIGHT / 2)
170 ldx #>(SCREEN_HEIGHT / 2)
172 lda #<(SCREEN_WIDTH / 2)
173 ldx #>(SCREEN_WIDTH / 2)
183 ;----------------------------------------------------------------------------
184 ; UNINSTALL routine. Is called before the driver is removed from memory.
185 ; No return code required (the driver is removed from memory on return).
188 jsr HIDE ; Hide cursor on exit
193 ;----------------------------------------------------------------------------
194 ; HIDE routine. Is called to hide the lightpen pointer. The mouse kernel manages
195 ; a counter for calls to show/hide, and the driver entry point is called only
196 ; if the mouse is currently visible, and should get hidden. For most drivers,
197 ; no special action is required besides hiding the lightpen cursor.
198 ; No return code required.
205 ;----------------------------------------------------------------------------
206 ; SHOW routine. Is called to show the lightpen pointer. The mouse kernel manages
207 ; a counter for calls to show/hide, and the driver entry point is called only
208 ; if the mouse is currently hidden, and should become visible. For most drivers,
209 ; no special action is required besides enabling the lightpen cursor.
210 ; No return code required.
217 ;----------------------------------------------------------------------------
218 ; SETBOX: Set the lightpen bounding box. The parameters are passed as they come
219 ; from the C program, that is, a pointer to a mouse_box struct in .XA.
220 ; No checks are done if the lightpen is currently inside the box, that is the job
221 ; of the caller. It is not necessary to validate the parameters; trust the
222 ; caller; and, save some code here. No return code required.
225 stx ptr1+1 ; Save data pointer
227 ldy #.sizeof (MOUSE_BOX) - 1
238 ;----------------------------------------------------------------------------
239 ; GETBOX: Return the lightpen bounding box. The parameters are passed as they
240 ; come from the C program, that is, a pointer to a mouse_box struct in .XA.
243 stx ptr1+1 ; Save data pointer
245 ldy #.sizeof (MOUSE_BOX) - 1
252 ;----------------------------------------------------------------------------
253 ; MOVE: Move the mouse to a new position. The position is passed as it comes
254 ; from the C program, that is: X on the stack and Y in .XA. The C wrapper will
255 ; remove the parameter from the stack on return.
256 ; No checks are done if the new position is valid (within the bounding box or
257 ; the screen). No return code required.
260 MOVE: sei ; No interrupts
268 jsr MoveX ; Move the cursor
270 cli ; Allow interrupts
273 ;----------------------------------------------------------------------------
274 ; BUTTONS: Return the button mask in .XA.
280 ; Make the lightpen buttons look like a 1351 mouse.
283 asl SID_ADConv2 ; PotY
286 and #MOUSE_BTN_LEFT | MOUSE_BTN_RIGHT
289 ;----------------------------------------------------------------------------
290 ; POS: Return the lightpen position in the MOUSE_POS struct pointed to by ptr1.
291 ; No return code required.
293 POS: ldy #MOUSE_POS::XCOORD ; Structure offset
295 sei ; Disable interrupts
296 lda XPos ; Transfer the position
305 cli ; Enable interrupts
308 sta (ptr1),y ; Store last byte
311 ;----------------------------------------------------------------------------
312 ; INFO: Returns lightpen position and current button mask in the MOUSE_INFO
313 ; struct pointed to by ptr1. No return code required.
315 ; We're cheating here, to keep the code smaller: The first fields of the
316 ; mouse_info struct are identical to the mouse_pos struct; so, we'll just
317 ; call _mouse_pos to initiate the struct pointer, and fill the position
322 ; Fill in the button state
324 jsr BUTTONS ; Will not touch ptr1
325 ldy #MOUSE_INFO::BUTTONS
329 ;----------------------------------------------------------------------------
330 ; IOCTL: Driver-defined entry point. The wrapper will pass a pointer to ioctl-
331 ; specific data in ptr1, and the ioctl code in .A.
332 ; Must return an error code in .XA.
335 IOCTL: lda #<MOUSE_ERR_INV_IOCTL ; We don't support ioctls, for now
336 ldx #>MOUSE_ERR_INV_IOCTL
339 ;----------------------------------------------------------------------------
340 ; IRQ: IRQ handler entry point. Called as a subroutine, but in the IRQ context
341 ; (so, be careful). The routine MUST return carry set if the interrupt has been
342 ; 'handled' -- which means that the interrupt source is gone. Otherwise, it
343 ; MUST return carry clear.
348 ; Record the state of the buttons.
349 ; Try to avoid crosstalk between the keyboard and the lightpen.
351 ldy #%00000000 ; Set ports A and B to input
353 sty CIA1_DDRA ; Keyboard won't look like buttons
354 lda CIA1_PRB ; Read Control-Port 1
355 dec CIA1_DDRA ; Set port A back to output
356 eor #%11111111 ; Bit goes up when button goes down
359 lda #%11101111 ; (Don't change bit that feeds VIC-II)
360 sta CIA1_DDRB ; Buttons won't look like keyboard
361 sty CIA1_PRB ; Set "all keys pushed"
363 ; Read the VIC-II lightpen registers.
368 ; Skip processing if nothing has changed.
373 ; Subtract the height of the top border, so that the lightpen co-ordinate
374 ; will match the TGI co-ordinate.
377 tay ; Remember low byte
380 ; Limit the Y co-ordinate to the bounding box.
400 @SkipY: lda VIC_LPEN_X
403 ; Skip processing if nothing has changed.
408 ; Adjust the value by the calibration offset.
412 ; Calculate the new X co-ordinate.
413 ; The VIC-II register is eight bits; but, the screen co-ordinate is nine bits.
414 ; Therefore, the VIC-II number is doubled. Then, it points to every other pixel;
415 ; but, it can reach across the screen.
418 tay ; Remember low byte
421 tax ; Remember high byte
423 ; Limit the X co-ordinate to the bounding box.
444 @SkipX: clc ; Interrupt not "handled"
447 ; Move the lightpen pointer to the new Y pos.
453 ; Move the lightpen pointer to the new X pos.