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);
103 lda Initialized ; Initialized?
104 beq mddone ; Jump if no
106 sta Initialized ; Reset the initialized flag
107 lda OldInitStatus ; Load the old BASIC int bit
109 ora INIT_STATUS ; Restore the old state
112 ; Disable the mouse sprite
115 lda #$FE ; Clear bit for sprite #0
116 sei ; Disable interrupts
118 sta VIC_SPR_ENA ; Disable sprite
119 cli ; Enable interrupts
123 ; --------------------------------------------------------------------------
125 ; void mouse_hide (void);
130 lda Invisible ; Get the flag
131 bne @L1 ; Jump if already invisible
132 jsr DisableSprite ; Disable the mouse sprite
133 @L1: inc Invisible ; Set the flag to invisible
138 ; --------------------------------------------------------------------------
140 ; void mouse_show (void);
145 lda Invisible ; Mouse invisible?
147 dec Invisible ; Set the flag
148 bne @L1 ; Jump if still invisible
150 sei ; Disable interrupts
151 jsr MoveSprite1 ; Move the sprite to it's position
152 lda VIC_SPR_ENA ; Get sprite enable register
153 ora #$01 ; Enable sprite #0
154 sta VIC_SPR_ENA ; Write back
155 cli ; Enable interrupts
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
297 stx XPos+1 ; Set new position
298 jsr MoveSprite ; Move the sprite to the mouse pos
299 cli ; Enable interrupts
305 ; --------------------------------------------------------------------------
307 ; unsigned char mouse_buttons (void);
312 lda #$00 ; Use port #0
313 jmp _readjoy ; Same as joystick
317 ; --------------------------------------------------------------------------
319 ; Mouse interrupt handler
326 lda Initialized ; Mouse initialized?
327 beq IRQDone ; Jump if no
328 lda SID_ADConv1 ; Get mouse X movement
330 jsr MoveCheck ; Calculate movement vector
333 ; Calculate the new X coordinate (--> a/y)
336 tay ; Remember low byte
341 ; Limit the X coordinate to the bounding box
359 ; Calculate the Y movement vector
361 lda SID_ADConv2 ; Get mouse Y movement
363 jsr MoveCheck ; Calculate movement
366 ; Calculate the new Y coordinate (--> a/y)
393 ; Move the mouse sprite to the current mouse position. Must be called
394 ; with interrupts off. MoveSprite1 is an entry without checking.
398 lda Invisible ; Mouse visible?
399 bne Done ; Jump if no
404 lda VIC_SPR_HI_X ; Get high X bits of all sprites
405 and #$FE ; Clear bit for sprite #0
406 ldy XPos+1 ; Test Y position
408 ora #$01 ; Set high X bit
409 @L5: sta VIC_SPR_HI_X ; Set hi X sprite values
414 sta VIC_SPR0_X ; Set low byte
418 ldy YPos+1 ; Negative or too large?
419 bne Done ; Jump if yes
421 sta VIC_SPR0_Y ; Set Y position
427 ; --------------------------------------------------------------------------
429 ; Move check routine, called for both coordinates.
431 ; Entry: y = old value of pot register
432 ; a = current value of pot register
433 ; Exit: y = value to use for old value
434 ; x/a = delta value for position
443 sub OldValue ; a = mod64 (new - old)
445 cmp #%01000000 ; if (a > 0)
448 beq @L2 ; if (a != 0)
449 ldy NewValue ; y = NewValue
452 @L1: ora #%11000000 ; else or in high order bits
453 cmp #$FF ; if (a != -1)
457 dex ; high byte = -1 (X = $FF)
466 ; --------------------------------------------------------------------------
471 Initialized: .res 1 ; True if mouse initialized
472 OldInitStatus: .res 1 ; Old IRQ flag value
473 OldValue: .res 1 ; Temp for MoveCheck routine
474 NewValue: .res 1 ; Temp for MoveCheck routine
475 YCorr: .res 1 ; Correction for Y coordinate
477 Invisible: .res 1 ; Is the mouse invisible?
478 OldPotX: .res 1 ; Old hw counter values
481 XPos: .res 2 ; Current mouse position, X
482 YPos: .res 2 ; Current mouse position, Y
484 XMin: .res 2 ; X1 value of bounding box
485 YMin: .res 2 ; Y1 value of bounding box
486 XMax: .res 2 ; X2 value of bounding box
487 YMax: .res 2 ; Y2 value of bounding box