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 popa, popax, addysp1
16 .importzp ptr1, sp, sreg
25 ; --------------------------------------------------------------------------
36 ; --------------------------------------------------------------------------
38 ; unsigned char __fastcall__ mouse_init (unsigned char port,
39 ; unsigned char type);
43 jsr popa ; Ignore type and port
45 ldy OldIRQ+1 ; Already initialized?
46 bne AlreadyInitialized ; Jump if yes
48 ; Initialize variables
59 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. We cheat here to save a few bytes of code:
83 ; The function is expected to return a value not equal to zero on success,
84 ; and since we know that the high byte of the IRQ handler address is never
85 ; zero, we will return just this byte.
89 bne SetIRQ ; Branch always
95 ; --------------------------------------------------------------------------
97 ; void mouse_done (void);
101 ldx OldIRQ ; Initialized?
103 beq Done ; Jump if no
105 sty OldIRQ+1 ; Reset the initialized flag
106 SetIRQ: sei ; Disable interrupts
107 stx IRQVec ; Set the new/old vector
109 cli ; Enable interrupts
112 ; --------------------------------------------------------------------------
114 ; void mouse_hide (void);
118 lda Invisible ; Get the flag
119 bne @L1 ; Jump if already invisible
121 lda #$FE ; Clear bit for sprite #0
122 sei ; Disable interrupts
124 sta VIC_SPR_ENA ; Disable sprite
125 cli ; Enable interrupts
127 @L1: inc Invisible ; Set the flag to invisible
130 ; --------------------------------------------------------------------------
132 ; void mouse_show (void);
136 lda Invisible ; Mouse invisible?
138 dec Invisible ; Set the flag
139 bne @L1 ; Jump if still invisible
141 sei ; Disable interrupts
142 jsr MoveSprite1 ; Move the sprite to it's position
143 lda VIC_SPR_ENA ; Get sprite enable register
144 ora #$01 ; Enable sprite #0
145 sta VIC_SPR_ENA ; Write back
146 cli ; Enable interrupts
150 ; --------------------------------------------------------------------------
152 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
156 ldy #0 ; Stack offset
158 add YCorr ; Adjust the Y value
162 @L1: sei ; Disable interrupts
193 cli ; Enable interrupts
195 jmp addysp1 ; Drop params, return
197 ; --------------------------------------------------------------------------
199 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
200 ; /* Return the current mouse position */
205 stx ptr1+1 ; Remember the argument pointer
207 ldy #0 ; Structure offset
208 sec ; Needed for the SBC later
210 sei ; Disable interrupts
211 lda XPos ; Transfer the position
220 cli ; Restore initial interrupt state
222 sub YCorr ; Apply the Y correction value
226 sta (ptr1),y ; Store YPos
233 ; --------------------------------------------------------------------------
235 ; void __fastcall__ mouse_info (struct mouse_info* info);
236 ; /* Return the state of the mouse buttons and the position of the mouse */
241 ; We're cheating here to keep the code smaller: The first fields of the
242 ; mouse_info struct are identical to the mouse_pos struct, so we will just
243 ; call _mouse_pos to initialize the struct pointer and fill the position
248 ; Fill in the button state
250 jsr _mouse_buttons ; Will not touch ptr1
256 ; --------------------------------------------------------------------------
258 ; void __fastcall__ mouse_move (int x, int y);
262 add YCorr ; Add Y coordinate correction
272 adc #XCORR ; Adjust X coordinate
277 stx XPos+1 ; Set new position
278 jsr MoveSprite ; Move the sprite to the mouse pos
279 cli ; Enable interrupts
283 ; --------------------------------------------------------------------------
285 ; unsigned char mouse_buttons (void);
289 lda #$00 ; Use port #0
290 jmp _readjoy ; Same as joystick
292 ; --------------------------------------------------------------------------
294 ; Mouse interrupt handler
299 lda SID_ADConv1 ; Get mouse X movement
301 jsr MoveCheck ; Calculate movement vector
304 ; Calculate the new X coordinate (--> a/y)
307 tay ; Remember low byte
312 ; Limit the X coordinate to the bounding box
330 ; Calculate the Y movement vector
332 lda SID_ADConv2 ; Get mouse Y movement
334 jsr MoveCheck ; Calculate movement
337 ; Calculate the new Y coordinate (--> a/y)
364 ; Move the mouse sprite if it is enabled
366 jsr MoveSprite ; Move the sprite
368 ; Jump to the next IRQ handler
373 ; --------------------------------------------------------------------------
375 ; Move check routine, called for both coordinates.
377 ; Entry: y = old value of pot register
378 ; a = current value of pot register
379 ; Exit: y = value to use for old value
380 ; x/a = delta value for position
388 sub OldValue ; a = mod64 (new - old)
390 cmp #%01000000 ; if (a > 0)
393 beq @L2 ; if (a != 0)
394 ldy NewValue ; y = NewValue
397 @L1: ora #%11000000 ; else or in high order bits
398 cmp #$FF ; if (a != -1)
402 dex ; high byte = -1 (X = $FF)
409 ; --------------------------------------------------------------------------
411 ; Move the mouse sprite to the current mouse position. Must be called
412 ; with interrupts off. MoveSprite1 is an entry without checking.
417 lda Invisible ; Mouse visible?
418 bne MoveSpriteDone ; Jump if no
423 lda VIC_SPR_HI_X ; Get high X bits of all sprites
424 and #$FE ; Clear bit for sprite #0
425 ldy XPos+1 ; Test Y position
427 ora #$01 ; Set high X bit
428 @L1: sta VIC_SPR_HI_X ; Set hi X sprite values
433 sta VIC_SPR0_X ; Set low byte
437 ldy YPos+1 ; Negative or too large?
438 bne MoveSpriteDone ; Jump if yes
440 sta VIC_SPR0_Y ; Set Y position
447 ; --------------------------------------------------------------------------
452 OldIRQ: .res 2 ; Old IRQ vector
453 OldValue: .res 1 ; Temp for MoveCheck routine
454 NewValue: .res 1 ; Temp for MoveCheck routine
455 YCorr: .res 1 ; Correction for Y coordinate
457 Invisible: .res 1 ; Is the mouse invisible?
458 OldPotX: .res 1 ; Old hw counter values
461 XPos: .res 2 ; Current mouse position, X
462 YPos: .res 2 ; Current mouse position, Y
464 XMin: .res 2 ; X1 value of bounding box
465 YMin: .res 2 ; Y1 value of bounding box
466 XMax: .res 2 ; X2 value of bounding box
467 YMax: .res 2 ; Y2 value of bounding box