2 ; Ullrich von Bassewitz, 19.09.2001
4 ; Routines for the 1351 proportional mouse. Parts of the code are from
5 ; the Commodore 1351 mouse users guide.
8 .export _mouse_init, _mouse_done
9 .export _mouse_hide, _mouse_show
10 .export _mouse_box, _mouse_info
11 .export _mouse_move, _mouse_pos
12 .export _mouse_buttons, _mouse_info
16 .import sys_bank, restore_bank
17 .import popax, addysp1
18 .importzp vic, sid, ptr1, sp
20 .include "zeropage.inc"
28 ; --------------------------------------------------------------------------
39 ; --------------------------------------------------------------------------
41 ; unsigned char __fastcall__ mouse_init (unsigned char type);
45 lda Initialized ; Already initialized?
46 bne AlreadyInitialized ; Jump if yes
48 ; Initialize variables
60 lda #50 ; ## FIXME: This is the PAL value
65 sbc #SPRITE_HEIGHT ; Sprite height in pixels
67 stx YMin+1 ; YMin = 29
68 lda #SCREEN_HEIGHT ; Vertical screen res
69 add YCorr ; Add correction factor
73 stx Invisible ; Mouse *not* visible
74 lda #<(SCREEN_WIDTH + SPRITE_WIDTH)
76 stx XMax+1 ; XMax = 320 + sprite width
78 ; Mouse successfully initialized
90 ; --------------------------------------------------------------------------
92 ; void mouse_done (void);
98 sta Initialized ; Reset the initialized flag
100 ; Disable the mouse sprite
106 sta IndReg ; Switch to the system bank
109 sei ; Disable interrupts
111 and #$FE ; Clear bit for sprite #0
112 sta (vic),y ; Disable sprite
113 cli ; Enable interrupts
115 stx IndReg ; Switch back the segment
118 ; --------------------------------------------------------------------------
120 ; void mouse_hide (void);
125 lda Invisible ; Get the flag
126 bne @L1 ; Jump if already invisible
127 jsr DisableSprite ; Disabe the mouse sprite
128 @L1: inc Invisible ; Set the flag to invisible
133 ; --------------------------------------------------------------------------
135 ; void mouse_show (void);
140 lda Invisible ; Mouse invisible?
142 dec Invisible ; Set the flag
143 bne @L1 ; Jump if still invisible
145 jsr sys_bank ; Switch to the system bank
147 sei ; Disable interrupts
148 jsr MoveSprite1 ; Move the sprite to it's position
150 lda (vic),y ; Get sprite enable register
151 ora #$01 ; Enable sprite #0
152 sta (vic),y ; Write back
153 cli ; Enable interrupts
155 jsr restore_bank ; Switch back the bank
161 ; --------------------------------------------------------------------------
163 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
168 ldy #0 ; Stack offset
170 add YCorr ; Adjust the Y value
174 @L1: sei ; Disable interrupts
205 cli ; Enable interrupts
207 jmp addysp1 ; Drop params, return
211 ; --------------------------------------------------------------------------
213 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
214 ; /* Return the current mouse position */
220 stx ptr1+1 ; Remember the argument pointer
222 ldy #0 ; Structure offset
223 sec ; Needed for the SBC later
225 sei ; Disable interrupts
226 lda XPos ; Transfer the position
235 cli ; Restore initial interrupt state
237 sub YCorr ; Apply the Y correction value
241 sta (ptr1),y ; Store YPos
250 ; --------------------------------------------------------------------------
252 ; void __fastcall__ mouse_info (struct mouse_info* info);
253 ; /* Return the state of the mouse buttons and the position of the mouse */
258 ; We're cheating here to keep the code smaller: The first fields of the
259 ; mouse_info struct are identical to the mouse_pos struct, so we will just
260 ; call _mouse_pos to initialize the struct pointer and fill the position
265 ; Fill in the button state
267 jsr _mouse_buttons ; Will not touch ptr1
275 ; --------------------------------------------------------------------------
277 ; void __fastcall__ mouse_move (int x, int y);
282 add YCorr ; Add Y coordinate correction
292 adc #XCORR ; Adjust X coordinate
298 stx XPos+1 ; Set new position
299 jsr MoveSprite ; Move the sprite to the mouse pos
300 cli ; Enable interrupts
307 ; --------------------------------------------------------------------------
309 ; unsigned char mouse_buttons (void);
314 lda #$00 ; Use port #0
315 jmp _readjoy ; Same as joystick
320 ; --------------------------------------------------------------------------
322 ; Mouse interrupt handler
328 lda Initialized ; Mouse initialized?
329 beq IRQDone ; Jump if no
332 lda (sid),y ; Get mouse X movement
334 jsr MoveCheck ; Calculate movement vector
337 ; Calculate the new X coordinate (--> a/y)
340 tay ; Remember low byte
345 ; Limit the X coordinate to the bounding box
363 ; Calculate the Y movement vector
366 lda (sid),y ; Get mouse Y movement
368 jsr MoveCheck ; Calculate movement
371 ; Calculate the new Y coordinate (--> a/y)
398 ; Move the mouse sprite to the current mouse position. Must be called
399 ; with interrupts off and the system bank enabled. MoveSprite1 is an entry
404 lda Invisible ; Mouse visible?
405 bne Done ; Jump if no
411 lda (vic),y ; Get high X bits of all sprites
412 and #$FE ; Clear bit for sprite #0
413 ldx XPos+1 ; Test Y position
415 ora #$01 ; Set high X bit
416 @L5: sta (vic),y ; Set hi X sprite values
422 sta (vic),y ; Set low byte
426 ldy YPos+1 ; Negative or too large?
427 bne Done ; Jump if yes
430 sta (vic),y ; Set Y position
436 ; --------------------------------------------------------------------------
438 ; Move check routine, called for both coordinates.
440 ; Entry: y = old value of pot register
441 ; a = current value of pot register
442 ; Exit: y = value to use for old value
443 ; x/a = delta value for position
452 sub OldValue ; a = mod64 (new - old)
454 cmp #%01000000 ; if (a > 0)
457 beq @L2 ; if (a != 0)
458 ldy NewValue ; y = NewValue
461 @L1: ora #%11000000 ; else or in high order bits
462 cmp #$FF ; if (a != -1)
466 dex ; high byte = -1 (X = $FF)
475 ; --------------------------------------------------------------------------
480 Initialized: .res 1 ; True if mouse initialized
481 OldInitStatus: .res 1 ; Old IRQ flag value
482 OldValue: .res 1 ; Temp for MoveCheck routine
483 NewValue: .res 1 ; Temp for MoveCheck routine
484 YCorr: .res 1 ; Correction for Y coordinate
486 Invisible: .res 1 ; Is the mouse invisible?
487 OldPotX: .res 1 ; Old hw counter values
490 XPos: .res 2 ; Current mouse position, X
491 YPos: .res 2 ; Current mouse position, Y
493 XMin: .res 2 ; X1 value of bounding box
494 YMin: .res 2 ; Y1 value of bounding box
495 XMax: .res 2 ; X2 value of bounding box
496 YMax: .res 2 ; Y2 value of bounding box