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
15 .import popax, addysp1
25 ; --------------------------------------------------------------------------
36 ; --------------------------------------------------------------------------
38 ; unsigned char __fastcall__ mouse_init (unsigned char type);
42 lda Initialized ; Already initialized?
43 bne AlreadyInitialized ; Jump if yes
45 ; Initialize variables
57 lda #50 ; ## FIXME: This is the PAL value
62 sbc #SPRITE_HEIGHT ; Sprite height in pixels
64 stx YMin+1 ; YMin = 29
65 lda #SCREEN_HEIGHT ; Vertical screen res
66 add YCorr ; Add correction factor
70 stx Invisible ; Mouse *not* visible
71 lda #<(SCREEN_WIDTH + SPRITE_WIDTH)
73 stx XMax+1 ; XMax = 320 + sprite width
75 ; Save the old init status and reset bit 0. This will disable the BASIC
76 ; IRQ handler which will otherwise try to manage the mouse sprite on its
84 ; Mouse successfully initialized
96 ; --------------------------------------------------------------------------
98 ; void mouse_done (void);
102 lda Initialized ; Initialized?
103 beq mddone ; Jump if no
105 sta Initialized ; Reset the initialized flag
106 lda OldInitStatus ; Load the old BASIC int bit
108 ora INIT_STATUS ; Restore the old state
111 ; Disable the mouse sprite
114 lda #$FE ; Clear bit for sprite #0
115 sei ; Disable interrupts
117 sta VIC_SPR_ENA ; Disable sprite
118 cli ; Enable interrupts
122 ; --------------------------------------------------------------------------
124 ; void mouse_hide (void);
129 lda Invisible ; Get the flag
130 bne @L1 ; Jump if already invisible
131 jsr DisableSprite ; Disable the mouse sprite
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);
314 lda CIA1_PRB ; Read joystick #0
323 ; --------------------------------------------------------------------------
325 ; Mouse interrupt handler
332 lda Initialized ; Mouse initialized?
333 beq IRQDone ; Jump if no
334 lda SID_ADConv1 ; Get mouse X movement
336 jsr MoveCheck ; Calculate movement vector
339 ; Calculate the new X coordinate (--> a/y)
342 tay ; Remember low byte
347 ; Limit the X coordinate to the bounding box
365 ; Calculate the Y movement vector
367 lda SID_ADConv2 ; Get mouse Y movement
369 jsr MoveCheck ; Calculate movement
372 ; Calculate the new Y coordinate (--> a/y)
399 ; Move the mouse sprite to the current mouse position. Must be called
400 ; with interrupts off. MoveSprite1 is an entry without checking.
404 lda Invisible ; Mouse visible?
405 bne Done ; Jump if no
410 lda VIC_SPR_HI_X ; Get high X bits of all sprites
411 and #$FE ; Clear bit for sprite #0
412 ldy XPos+1 ; Test Y position
414 ora #$01 ; Set high X bit
415 @L5: sta VIC_SPR_HI_X ; Set hi X sprite values
420 sta VIC_SPR0_X ; Set low byte
424 ldy YPos+1 ; Negative or too large?
425 bne Done ; Jump if yes
427 sta VIC_SPR0_Y ; Set Y position
433 ; --------------------------------------------------------------------------
435 ; Move check routine, called for both coordinates.
437 ; Entry: y = old value of pot register
438 ; a = current value of pot register
439 ; Exit: y = value to use for old value
440 ; x/a = delta value for position
449 sub OldValue ; a = mod64 (new - old)
451 cmp #%01000000 ; if (a > 0)
454 beq @L2 ; if (a != 0)
455 ldy NewValue ; y = NewValue
458 @L1: ora #%11000000 ; else or in high order bits
459 cmp #$FF ; if (a != -1)
463 dex ; high byte = -1 (X = $FF)
472 ; --------------------------------------------------------------------------
477 Initialized: .res 1 ; True if mouse initialized
478 OldInitStatus: .res 1 ; Old IRQ flag value
479 OldValue: .res 1 ; Temp for MoveCheck routine
480 NewValue: .res 1 ; Temp for MoveCheck routine
481 YCorr: .res 1 ; Correction for Y coordinate
483 Invisible: .res 1 ; Is the mouse invisible?
484 OldPotX: .res 1 ; Old hw counter values
487 XPos: .res 2 ; Current mouse position, X
488 YPos: .res 2 ; Current mouse position, Y
490 XMin: .res 2 ; X1 value of bounding box
491 YMin: .res 2 ; Y1 value of bounding box
492 XMax: .res 2 ; X2 value of bounding box
493 YMax: .res 2 ; Y2 value of bounding box