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 OldIRQ+1 ; 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 ; Remember the old IRQ vector
82 ; Set our own IRQ vector.
86 jsr SetIRQ ; Set our IRQ vector, disable sprite 0
87 lda #1 ; Mouse successfully initialized
94 ; --------------------------------------------------------------------------
96 ; void mouse_done (void);
101 ldy OldIRQ+1 ; Initialized?
102 beq Done ; Jump if no
104 sta OldIRQ+1 ; Reset the initialized flag
105 SetIRQ: sei ; Disable interrupts
106 stx IRQVec ; Set the new/old vector
108 lda VIC_SPR_ENA ; Get sprite enable register
109 and #$FE ; Disable sprite #0
110 sta VIC_SPR_ENA ; Write back
111 cli ; Enable interrupts
115 ; --------------------------------------------------------------------------
117 ; void mouse_hide (void);
121 lda Invisible ; Get the flag
122 bne @L1 ; Jump if already invisible
124 lda #$FE ; Clear bit for sprite #0
125 sei ; Disable interrupts
127 sta VIC_SPR_ENA ; Disable sprite
128 cli ; Enable interrupts
130 @L1: inc Invisible ; Set the flag to invisible
133 ; --------------------------------------------------------------------------
135 ; void mouse_show (void);
139 lda Invisible ; Mouse invisible?
141 dec Invisible ; Set the flag
142 bne @L1 ; Jump if still invisible
144 sei ; Disable interrupts
145 jsr MoveSprite1 ; Move the sprite to it's position
146 lda VIC_SPR_ENA ; Get sprite enable register
147 ora #$01 ; Enable sprite #0
148 sta VIC_SPR_ENA ; Write back
149 cli ; Enable interrupts
153 ; --------------------------------------------------------------------------
155 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
159 ldy #0 ; Stack offset
161 add YCorr ; Adjust the Y value
165 @L1: sei ; Disable interrupts
196 cli ; Enable interrupts
198 jmp addysp1 ; Drop params, return
200 ; --------------------------------------------------------------------------
202 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
203 ; /* Return the current mouse position */
208 stx ptr1+1 ; Remember the argument pointer
210 ldy #0 ; Structure offset
211 sec ; Needed for the SBC later
213 sei ; Disable interrupts
214 lda XPos ; Transfer the position
223 cli ; Restore initial interrupt state
225 sub YCorr ; Apply the Y correction value
229 sta (ptr1),y ; Store YPos
236 ; --------------------------------------------------------------------------
238 ; void __fastcall__ mouse_info (struct mouse_info* info);
239 ; /* Return the state of the mouse buttons and the position of the mouse */
244 ; We're cheating here to keep the code smaller: The first fields of the
245 ; mouse_info struct are identical to the mouse_pos struct, so we will just
246 ; call _mouse_pos to initialize the struct pointer and fill the position
251 ; Fill in the button state
253 jsr _mouse_buttons ; Will not touch ptr1
259 ; --------------------------------------------------------------------------
261 ; void __fastcall__ mouse_move (int x, int y);
265 add YCorr ; Add Y coordinate correction
275 adc #XCORR ; Adjust X coordinate
280 stx XPos+1 ; Set new position
281 jsr MoveSprite ; Move the sprite to the mouse pos
282 cli ; Enable interrupts
286 ; --------------------------------------------------------------------------
288 ; unsigned char mouse_buttons (void);
292 lda #$00 ; Use port #0
293 jmp _readjoy ; Same as joystick
295 ; --------------------------------------------------------------------------
297 ; Mouse interrupt handler
302 lda SID_ADConv1 ; Get mouse X movement
304 jsr MoveCheck ; Calculate movement vector
307 ; Calculate the new X coordinate (--> a/y)
310 tay ; Remember low byte
315 ; Limit the X coordinate to the bounding box
333 ; Calculate the Y movement vector
335 lda SID_ADConv2 ; Get mouse Y movement
337 jsr MoveCheck ; Calculate movement
340 ; Calculate the new Y coordinate (--> a/y)
367 ; Move the mouse sprite if it is enabled
369 jsr MoveSprite ; Move the sprite
371 ; Jump to the next IRQ handler
376 ; --------------------------------------------------------------------------
378 ; Move check routine, called for both coordinates.
380 ; Entry: y = old value of pot register
381 ; a = current value of pot register
382 ; Exit: y = value to use for old value
383 ; x/a = delta value for position
391 sub OldValue ; a = mod64 (new - old)
393 cmp #%01000000 ; if (a > 0)
396 beq @L2 ; if (a != 0)
397 ldy NewValue ; y = NewValue
400 @L1: ora #%11000000 ; else or in high order bits
401 cmp #$FF ; if (a != -1)
405 dex ; high byte = -1 (X = $FF)
412 ; --------------------------------------------------------------------------
414 ; Move the mouse sprite to the current mouse position. Must be called
415 ; with interrupts off. MoveSprite1 is an entry without checking.
420 lda Invisible ; Mouse visible?
421 bne MoveSpriteDone ; Jump if no
426 lda VIC_SPR_HI_X ; Get high X bits of all sprites
427 and #$FE ; Clear bit for sprite #0
428 ldy XPos+1 ; Test Y position
430 ora #$01 ; Set high X bit
431 @L1: sta VIC_SPR_HI_X ; Set hi X sprite values
436 sta VIC_SPR0_X ; Set low byte
440 ldy YPos+1 ; Negative or too large?
441 bne MoveSpriteDone ; Jump if yes
443 sta VIC_SPR0_Y ; Set Y position
450 ; --------------------------------------------------------------------------
455 OldIRQ: .res 2 ; Old IRQ vector
456 OldValue: .res 1 ; Temp for MoveCheck routine
457 NewValue: .res 1 ; Temp for MoveCheck routine
458 YCorr: .res 1 ; Correction for Y coordinate
460 Invisible: .res 1 ; Is the mouse invisible?
461 OldPotX: .res 1 ; Old hw counter values
464 XPos: .res 2 ; Current mouse position, X
465 YPos: .res 2 ; Current mouse position, Y
467 XMin: .res 2 ; X1 value of bounding box
468 YMin: .res 2 ; Y1 value of bounding box
469 XMax: .res 2 ; X2 value of bounding box
470 YMax: .res 2 ; Y2 value of bounding box