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
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 sei ; Disable interrupts
111 ora INIT_STATUS ; Restore the old state
113 lda #$FE ; Clear bit for sprite #0
115 sta VIC_SPR_ENA ; Disable sprite
116 cli ; Enable interrupts
121 ; --------------------------------------------------------------------------
123 ; void mouse_hide (void);
128 lda Invisible ; Get the flag
129 bne @L1 ; Jump if already invisible
131 lda #$FE ; Clear bit for sprite #0
132 sei ; Disable interrupts
134 sta VIC_SPR_ENA ; Disable sprite
135 cli ; Enable interrupts
137 @L1: inc Invisible ; Set the flag to invisible
142 ; --------------------------------------------------------------------------
144 ; void mouse_show (void);
149 lda Invisible ; Mouse invisible?
151 dec Invisible ; Set the flag
152 bne @L1 ; Jump if still invisible
154 sei ; Disable interrupts
155 jsr MoveSprite1 ; Move the sprite to it's position
156 lda VIC_SPR_ENA ; Get sprite enable register
157 ora #$01 ; Enable sprite #0
158 sta VIC_SPR_ENA ; Write back
159 cli ; Enable interrupts
165 ; --------------------------------------------------------------------------
167 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
172 ldy #0 ; Stack offset
174 add YCorr ; Adjust the Y value
178 @L1: sei ; Disable interrupts
209 cli ; Enable interrupts
211 jmp addysp1 ; Drop params, return
215 ; --------------------------------------------------------------------------
217 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
218 ; /* Return the current mouse position */
224 stx ptr1+1 ; Remember the argument pointer
226 ldy #0 ; Structure offset
227 sec ; Needed for the SBC later
229 sei ; Disable interrupts
230 lda XPos ; Transfer the position
239 cli ; Restore initial interrupt state
241 sub YCorr ; Apply the Y correction value
245 sta (ptr1),y ; Store YPos
254 ; --------------------------------------------------------------------------
256 ; void __fastcall__ mouse_info (struct mouse_info* info);
257 ; /* Return the state of the mouse buttons and the position of the mouse */
262 ; We're cheating here to keep the code smaller: The first fields of the
263 ; mouse_info struct are identical to the mouse_pos struct, so we will just
264 ; call _mouse_pos to initialize the struct pointer and fill the position
269 ; Fill in the button state
271 jsr _mouse_buttons ; Will not touch ptr1
279 ; --------------------------------------------------------------------------
281 ; void __fastcall__ mouse_move (int x, int y);
286 add YCorr ; Add Y coordinate correction
296 adc #XCORR ; Adjust X coordinate
301 stx XPos+1 ; Set new position
302 jsr MoveSprite ; Move the sprite to the mouse pos
303 cli ; Enable interrupts
309 ; --------------------------------------------------------------------------
311 ; unsigned char mouse_buttons (void);
316 lda #$00 ; Use port #0
317 jmp _readjoy ; Same as joystick
321 ; --------------------------------------------------------------------------
323 ; Mouse interrupt handler
330 lda Initialized ; Mouse initialized?
331 beq IRQDone ; Jump if no
332 lda SID_ADConv1 ; 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
365 lda SID_ADConv2 ; Get mouse Y movement
367 jsr MoveCheck ; Calculate movement
370 ; Calculate the new Y coordinate (--> a/y)
397 ; Move the mouse sprite to the current mouse position. Must be called
398 ; with interrupts off. MoveSprite1 is an entry without checking.
402 lda Invisible ; Mouse visible?
403 bne Done ; Jump if no
408 lda VIC_SPR_HI_X ; Get high X bits of all sprites
409 and #$FE ; Clear bit for sprite #0
410 ldy XPos+1 ; Test Y position
412 ora #$01 ; Set high X bit
413 @L5: sta VIC_SPR_HI_X ; Set hi X sprite values
418 sta VIC_SPR0_X ; Set low byte
422 ldy YPos+1 ; Negative or too large?
423 bne Done ; Jump if yes
425 sta VIC_SPR0_Y ; Set Y position
431 ; --------------------------------------------------------------------------
433 ; Move check routine, called for both coordinates.
435 ; Entry: y = old value of pot register
436 ; a = current value of pot register
437 ; Exit: y = value to use for old value
438 ; x/a = delta value for position
447 sub OldValue ; a = mod64 (new - old)
449 cmp #%01000000 ; if (a > 0)
452 beq @L2 ; if (a != 0)
453 ldy NewValue ; y = NewValue
456 @L1: ora #%11000000 ; else or in high order bits
457 cmp #$FF ; if (a != -1)
461 dex ; high byte = -1 (X = $FF)
470 ; --------------------------------------------------------------------------
475 Initialized: .res 1 ; True if mouse initialized
476 OldInitStatus: .res 1 ; Old IRQ flag value
477 OldValue: .res 1 ; Temp for MoveCheck routine
478 NewValue: .res 1 ; Temp for MoveCheck routine
479 YCorr: .res 1 ; Correction for Y coordinate
481 Invisible: .res 1 ; Is the mouse invisible?
482 OldPotX: .res 1 ; Old hw counter values
485 XPos: .res 2 ; Current mouse position, X
486 YPos: .res 2 ; Current mouse position, Y
488 XMin: .res 2 ; X1 value of bounding box
489 YMin: .res 2 ; Y1 value of bounding box
490 XMax: .res 2 ; X2 value of bounding box
491 YMax: .res 2 ; Y2 value of bounding box