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 popa, popax, addysp1
17 .importzp ptr1, sp, sreg
26 ; --------------------------------------------------------------------------
37 ; --------------------------------------------------------------------------
39 ; unsigned char __fastcall__ mouse_init (unsigned char port,
40 ; unsigned char type);
45 jsr popa ; Ignore type and port
47 lda Initialized ; Already initialized?
48 bne AlreadyInitialized ; Jump if yes
50 ; Initialize variables
62 lda #50 ; ## FIXME: This is the PAL value
67 sbc #SPRITE_HEIGHT ; Sprite height in pixels
69 stx YMin+1 ; YMin = 29
70 lda #SCREEN_HEIGHT ; Vertical screen res
71 add YCorr ; Add correction factor
75 stx Invisible ; Mouse *not* visible
76 lda #<(SCREEN_WIDTH + SPRITE_WIDTH)
78 stx XMax+1 ; XMax = 320 + sprite width
80 ; Save the old init status and reset bit 0. This will disable the BASIC
81 ; IRQ handler which will otherwise try to manage the mouse sprite on its
89 ; Mouse successfully initialized
101 ; --------------------------------------------------------------------------
103 ; void mouse_done (void);
108 lda Initialized ; Initialized?
111 sta Initialized ; Reset the initialized flag
112 lda OldInitStatus ; Load the old BASIC int bit
114 ora INIT_STATUS ; Restore the old state
120 ; --------------------------------------------------------------------------
122 ; void mouse_hide (void);
127 lda Invisible ; Get the flag
128 bne @L1 ; Jump if already invisible
130 lda #$FE ; Clear bit for sprite #0
131 sei ; Disable interrupts
133 sta VIC_SPR_ENA ; Disable sprite
134 cli ; Enable interrupts
136 @L1: inc Invisible ; Set the flag to invisible
141 ; --------------------------------------------------------------------------
143 ; void mouse_show (void);
148 lda Invisible ; Mouse invisible?
150 dec Invisible ; Set the flag
151 bne @L1 ; Jump if still invisible
153 sei ; Disable interrupts
154 jsr MoveSprite1 ; Move the sprite to it's position
155 lda VIC_SPR_ENA ; Get sprite enable register
156 ora #$01 ; Enable sprite #0
157 sta VIC_SPR_ENA ; Write back
158 cli ; Enable interrupts
164 ; --------------------------------------------------------------------------
166 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
171 ldy #0 ; Stack offset
173 add YCorr ; Adjust the Y value
177 @L1: sei ; Disable interrupts
208 cli ; Enable interrupts
210 jmp addysp1 ; Drop params, return
214 ; --------------------------------------------------------------------------
216 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
217 ; /* Return the current mouse position */
223 stx ptr1+1 ; Remember the argument pointer
225 ldy #0 ; Structure offset
226 sec ; Needed for the SBC later
228 sei ; Disable interrupts
229 lda XPos ; Transfer the position
238 cli ; Restore initial interrupt state
240 sub YCorr ; Apply the Y correction value
244 sta (ptr1),y ; Store YPos
253 ; --------------------------------------------------------------------------
255 ; void __fastcall__ mouse_info (struct mouse_info* info);
256 ; /* Return the state of the mouse buttons and the position of the mouse */
261 ; We're cheating here to keep the code smaller: The first fields of the
262 ; mouse_info struct are identical to the mouse_pos struct, so we will just
263 ; call _mouse_pos to initialize the struct pointer and fill the position
268 ; Fill in the button state
270 jsr _mouse_buttons ; Will not touch ptr1
278 ; --------------------------------------------------------------------------
280 ; void __fastcall__ mouse_move (int x, int y);
285 add YCorr ; Add Y coordinate correction
295 adc #XCORR ; Adjust X coordinate
300 stx XPos+1 ; Set new position
301 jsr MoveSprite ; Move the sprite to the mouse pos
302 cli ; Enable interrupts
308 ; --------------------------------------------------------------------------
310 ; unsigned char mouse_buttons (void);
315 lda #$00 ; Use port #0
316 jmp _readjoy ; Same as joystick
320 ; --------------------------------------------------------------------------
322 ; Mouse interrupt handler
329 lda Initialized ; Mouse initialized?
330 beq IRQDone ; Jump if no
331 lda SID_ADConv1 ; Get mouse X movement
333 jsr MoveCheck ; Calculate movement vector
336 ; Calculate the new X coordinate (--> a/y)
339 tay ; Remember low byte
344 ; Limit the X coordinate to the bounding box
362 ; Calculate the Y movement vector
364 lda SID_ADConv2 ; Get mouse Y movement
366 jsr MoveCheck ; Calculate movement
369 ; Calculate the new Y coordinate (--> a/y)
396 ; Move the mouse sprite to the current mouse position. Must be called
397 ; with interrupts off. MoveSprite1 is an entry without checking.
401 lda Invisible ; Mouse visible?
402 bne Done ; Jump if no
407 lda VIC_SPR_HI_X ; Get high X bits of all sprites
408 and #$FE ; Clear bit for sprite #0
409 ldy XPos+1 ; Test Y position
411 ora #$01 ; Set high X bit
412 @L5: sta VIC_SPR_HI_X ; Set hi X sprite values
417 sta VIC_SPR0_X ; Set low byte
421 ldy YPos+1 ; Negative or too large?
422 bne Done ; Jump if yes
424 sta VIC_SPR0_Y ; Set Y position
430 ; --------------------------------------------------------------------------
432 ; Move check routine, called for both coordinates.
434 ; Entry: y = old value of pot register
435 ; a = current value of pot register
436 ; Exit: y = value to use for old value
437 ; x/a = delta value for position
446 sub OldValue ; a = mod64 (new - old)
448 cmp #%01000000 ; if (a > 0)
451 beq @L2 ; if (a != 0)
452 ldy NewValue ; y = NewValue
455 @L1: ora #%11000000 ; else or in high order bits
456 cmp #$FF ; if (a != -1)
460 dex ; high byte = -1 (X = $FF)
469 ; --------------------------------------------------------------------------
474 Initialized: .res 1 ; True if mouse initialized
475 OldInitStatus: .res 1 ; Old IRQ flag value
476 OldValue: .res 1 ; Temp for MoveCheck routine
477 NewValue: .res 1 ; Temp for MoveCheck routine
478 YCorr: .res 1 ; Correction for Y coordinate
480 Invisible: .res 1 ; Is the mouse invisible?
481 OldPotX: .res 1 ; Old hw counter values
484 XPos: .res 2 ; Current mouse position, X
485 YPos: .res 2 ; Current mouse position, Y
487 XMin: .res 2 ; X1 value of bounding box
488 YMin: .res 2 ; Y1 value of bounding box
489 XMax: .res 2 ; X2 value of bounding box
490 YMax: .res 2 ; Y2 value of bounding box