2 ; Ullrich von Bassewitz, 24.04.1999
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 popax, addysp1
17 .importzp ptr1, sp, sreg
26 ; --------------------------------------------------------------------------
37 ; --------------------------------------------------------------------------
39 ; unsigned char __fastcall__ mouse_init (unsigned char type);
43 lda Initialized ; Already initialized?
44 bne AlreadyInitialized ; Jump if yes
46 ; Initialize variables
58 lda #50 ; ## FIXME: This is the PAL value
63 sbc #SPRITE_HEIGHT ; Sprite height in pixels
65 stx YMin+1 ; YMin = 29
66 lda #SCREEN_HEIGHT ; Vertical screen res
67 add YCorr ; Add correction factor
71 stx Invisible ; Mouse *not* visible
72 lda #<(SCREEN_WIDTH + SPRITE_WIDTH)
74 stx XMax+1 ; XMax = 320 + sprite width
76 ; Save the old init status and reset bit 0. This will disable the BASIC
77 ; IRQ handler which will otherwise try to manage the mouse sprite on its
85 ; Mouse successfully initialized
97 ; --------------------------------------------------------------------------
99 ; void mouse_done (void);
104 lda Initialized ; Initialized?
107 sta Initialized ; Reset the initialized flag
108 lda OldInitStatus ; Load the old BASIC int bit
110 ora INIT_STATUS ; Restore the old state
116 ; --------------------------------------------------------------------------
118 ; void mouse_hide (void);
123 lda Invisible ; Get the flag
124 bne @L1 ; Jump if already invisible
126 lda #$FE ; Clear bit for sprite #0
127 sei ; Disable interrupts
129 sta VIC_SPR_ENA ; Disable sprite
130 cli ; Enable interrupts
132 @L1: inc Invisible ; Set the flag to invisible
137 ; --------------------------------------------------------------------------
139 ; void mouse_show (void);
144 lda Invisible ; Mouse invisible?
146 dec Invisible ; Set the flag
147 bne @L1 ; Jump if still invisible
149 sei ; Disable interrupts
150 jsr MoveSprite1 ; Move the sprite to it's position
151 lda VIC_SPR_ENA ; Get sprite enable register
152 ora #$01 ; Enable sprite #0
153 sta VIC_SPR_ENA ; Write back
154 cli ; Enable interrupts
160 ; --------------------------------------------------------------------------
162 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
167 ldy #0 ; Stack offset
169 add YCorr ; Adjust the Y value
173 @L1: sei ; Disable interrupts
204 cli ; Enable interrupts
206 jmp addysp1 ; Drop params, return
210 ; --------------------------------------------------------------------------
212 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
213 ; /* Return the current mouse position */
219 stx ptr1+1 ; Remember the argument pointer
221 ldy #0 ; Structure offset
222 sec ; Needed for the SBC later
224 sei ; Disable interrupts
225 lda XPos ; Transfer the position
234 cli ; Restore initial interrupt state
236 sub YCorr ; Apply the Y correction value
240 sta (ptr1),y ; Store YPos
249 ; --------------------------------------------------------------------------
251 ; void __fastcall__ mouse_info (struct mouse_info* info);
252 ; /* Return the state of the mouse buttons and the position of the mouse */
257 ; We're cheating here to keep the code smaller: The first fields of the
258 ; mouse_info struct are identical to the mouse_pos struct, so we will just
259 ; call _mouse_pos to initialize the struct pointer and fill the position
264 ; Fill in the button state
266 jsr _mouse_buttons ; Will not touch ptr1
274 ; --------------------------------------------------------------------------
276 ; void __fastcall__ mouse_move (int x, int y);
281 add YCorr ; Add Y coordinate correction
291 adc #XCORR ; Adjust X coordinate
296 stx XPos+1 ; Set new position
297 jsr MoveSprite ; Move the sprite to the mouse pos
298 cli ; Enable interrupts
304 ; --------------------------------------------------------------------------
306 ; unsigned char mouse_buttons (void);
311 lda #$00 ; Use port #0
312 jmp _readjoy ; Same as joystick
316 ; --------------------------------------------------------------------------
318 ; Mouse interrupt handler
325 lda Initialized ; Mouse initialized?
326 beq IRQDone ; Jump if no
327 lda SID_ADConv1 ; Get mouse X movement
329 jsr MoveCheck ; Calculate movement vector
332 ; Calculate the new X coordinate (--> a/y)
335 tay ; Remember low byte
340 ; Limit the X coordinate to the bounding box
358 ; Calculate the Y movement vector
360 lda SID_ADConv2 ; Get mouse Y movement
362 jsr MoveCheck ; Calculate movement
365 ; Calculate the new Y coordinate (--> a/y)
392 ; Move the mouse sprite to the current mouse position. Must be called
393 ; with interrupts off. MoveSprite1 is an entry without checking.
397 lda Invisible ; Mouse visible?
398 bne Done ; Jump if no
403 lda VIC_SPR_HI_X ; Get high X bits of all sprites
404 and #$FE ; Clear bit for sprite #0
405 ldy XPos+1 ; Test Y position
407 ora #$01 ; Set high X bit
408 @L5: sta VIC_SPR_HI_X ; Set hi X sprite values
413 sta VIC_SPR0_X ; Set low byte
417 ldy YPos+1 ; Negative or too large?
418 bne Done ; Jump if yes
420 sta VIC_SPR0_Y ; Set Y position
426 ; --------------------------------------------------------------------------
428 ; Move check routine, called for both coordinates.
430 ; Entry: y = old value of pot register
431 ; a = current value of pot register
432 ; Exit: y = value to use for old value
433 ; x/a = delta value for position
442 sub OldValue ; a = mod64 (new - old)
444 cmp #%01000000 ; if (a > 0)
447 beq @L2 ; if (a != 0)
448 ldy NewValue ; y = NewValue
451 @L1: ora #%11000000 ; else or in high order bits
452 cmp #$FF ; if (a != -1)
456 dex ; high byte = -1 (X = $FF)
465 ; --------------------------------------------------------------------------
470 Initialized: .res 1 ; True if mouse initialized
471 OldInitStatus: .res 1 ; Old IRQ flag value
472 OldValue: .res 1 ; Temp for MoveCheck routine
473 NewValue: .res 1 ; Temp for MoveCheck routine
474 YCorr: .res 1 ; Correction for Y coordinate
476 Invisible: .res 1 ; Is the mouse invisible?
477 OldPotX: .res 1 ; Old hw counter values
480 XPos: .res 2 ; Current mouse position, X
481 YPos: .res 2 ; Current mouse position, Y
483 XMin: .res 2 ; X1 value of bounding box
484 YMin: .res 2 ; Y1 value of bounding box
485 XMax: .res 2 ; X2 value of bounding box
486 YMax: .res 2 ; Y2 value of bounding box