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
13 .import popa, popsreg, addysp1
20 ; --------------------------------------------------------------------------
22 ; void __fastcall__ mouse_init (unsigned char port, unsigned char sprite);
26 tax ; Save sprite number
27 jsr popa ; Get the port number
29 ldy OldIRQ+1 ; Already initialized?
30 bne Done ; Jump if yes
32 stx MouseSprite ; Remember the sprite number
33 sta MousePort ; Remember the port number
35 ; Initialize variables
50 stx Visible ; Mouse *not* visible
57 ; Remember the old IRQ vector
64 ; Set our own IRQ vector
70 ; --------------------------------------------------------------------------
72 ; void mouse_done (void);
76 lda OldIRQ ; Initialized?
80 sty OldIRQ+1 ; Reset the initialized flag
81 SetIRQ: sei ; Disable interrupts
82 sta IRQVec ; Set the new/old vector
84 cli ; Enable interrupts
87 ; --------------------------------------------------------------------------
89 ; void mouse_hide (void);
93 lda Visible ; Get the flag
94 bne @L1 ; Jump if already invisible
95 ldx MouseSprite ; Sprite defined?
98 lda BitMask-1,x ; Get bit mask
99 eor #$FF ; We must clear the bit
101 sei ; Disable interrupts
103 sta VIC_SPR_ENA ; Disable sprite
104 cli ; Enable interrupts
106 @L1: inc Visible ; Set the flag to invisible
109 ; --------------------------------------------------------------------------
111 ; void mouse_show (void);
115 dec Visible ; Get the flag
116 bne @L1 ; Jump if still invisible
117 ldx MouseSprite ; Sprite defined?
120 lda BitMask-1,x ; Get bit mask
121 sei ; Disable interrupts
123 sta VIC_SPR_ENA ; Enable sprite
124 cli ; Enable interrupts
128 ; --------------------------------------------------------------------------
130 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
134 sei ; Disable interrupts
160 cli ; Enable interrupts
162 jmp addysp1 ; Drop params, return
164 ; --------------------------------------------------------------------------
166 ; void mouse_info (...);
173 ; --------------------------------------------------------------------------
175 ; void __fastcall__ mouse_move (int x, int y);
180 sei ; Disable interrupts
187 stx XPos+1 ; Set new position
189 lda Visible ; Mouse visible?
191 lda MouseSprite ; Sprite defined?
194 jsr MoveSprite ; Move the sprite to the mouse pos
196 @L9: cli ; Enable interrupts
199 ; --------------------------------------------------------------------------
201 ; Mouse interrupt handler
206 lda SID_ADConv1 ; Get mouse X movement
208 jsr MoveCheck ; Calculate movement vector
211 ; Calculate the new X coordinate (--> a/y)
215 tay ; Remember low byte
219 ; Limit the X coordinate to the bounding box
239 ; Calculate the Y movement vector
241 lda SID_ADConv2 ; Get mouse Y movement
243 jsr MoveCheck ; Calculate movement
246 ; Calculate the new Y coordinate (--> a/y)
250 tay ; Remember low byte
254 ; Limit the Y coordinate to the bounding box
274 ; Jump to the next IRQ handler
279 ; --------------------------------------------------------------------------
281 ; Move check routine, called for both coordinates.
283 ; Entry: y = old value of pot register
284 ; a = current value of pot register
285 ; Exit: y = value to use for old value
286 ; x/a = delta value for position
294 sec ; a = mod64 (new - old)
297 cmp #%01000000 ; if (a > 0)
300 beq @L2 ; if (a != 0)
301 ldy NewValue ; y = NewValue
304 @L1: ora #%11000000 ; else or in high order bits
305 cmp #$FF ; if (a != -1)
309 ldx #$FF ; high byte = -1
316 ; --------------------------------------------------------------------------
318 ; Move the mouse sprite to the current mouse position. Must be called
319 ; with interrupts off.
323 lda Visible ; Mouse pointer visible?
325 ldx MouseSprite ; Sprite defined?
327 ldy BitMask-1,x ; Get high bit mask
334 lda XPos+1 ; Negative?
335 bmi @L2 ; Jump if yes
337 tya ; Load high position bit
338 @L1: ora VIC_SPR_HI_X ; Set high bit
341 sta VIC_SPR0_X,x ; Set low byte
345 @L2: ldy YPos+1 ; Negative or too large?
346 bne @L9 ; Jump if yes
348 sta VIC_SPR0_Y,x ; Set Y position
354 ; --------------------------------------------------------------------------
359 OldIRQ: .res 2 ; Old IRQ vector
360 MousePort: .res 1 ; Port used for the mouse
361 MouseSprite: .res 1 ; Number of sprite to control
362 OldValue: .res 1 ; Temp for MoveCheck routine
363 NewValue: .res 1 ; Temp for MoveCheck routine
365 Visible: .res 1 ; Is the mouse visible?
366 OldPotX: .res 1 ; Old hw counter values
369 XPos: .res 2 ; Current mouse position, X
370 YPos: .res 2 ; Current mouse position, Y
372 XMin: .res 2 ; X1 value of bounding box
373 YMin: .res 2 ; Y1 value of bounding box
374 XMax: .res 2 ; X2 value of bounding box
375 YMax: .res 2 ; Y2 value of bounding box
379 BitMask: .byte $01, $02, $04, $08, $10, $20, $40, $80