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_x, _mouse_y
14 .import popa, popsreg, addysp1
21 ; --------------------------------------------------------------------------
23 ; void __fastcall__ mouse_init (unsigned char port, unsigned char sprite);
27 tax ; Save sprite number
28 jsr popa ; Get the port number
30 ldy OldIRQ+1 ; Already initialized?
31 bne Done ; Jump if yes
33 stx MouseSprite ; Remember the sprite number
34 sta MousePort ; Remember the port number
36 ; Initialize variables
51 stx Visible ; Mouse *not* visible
58 ; Remember the old IRQ vector
65 ; Set our own IRQ vector
71 ; --------------------------------------------------------------------------
73 ; void mouse_done (void);
77 lda OldIRQ ; Initialized?
81 sty OldIRQ+1 ; Reset the initialized flag
82 SetIRQ: sei ; Disable interrupts
83 sta IRQVec ; Set the new/old vector
85 cli ; Enable interrupts
88 ; --------------------------------------------------------------------------
90 ; void mouse_hide (void);
94 lda Visible ; Get the flag
95 bne @L1 ; Jump if already invisible
96 ldx MouseSprite ; Sprite defined?
99 lda NotMask-1,x ; Get clean mask
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 lda Visible ; Mouse already visible?
116 beq @L1 ; Jump if yes
117 dec Visible ; Get the flag
118 bne @L1 ; Jump if still invisible
119 ldx MouseSprite ; Sprite defined?
122 sei ; Disable interrupts
123 jsr MoveSprite1 ; Move the sprite to it's position
125 ldx MouseSprite ; Get sprite number (again)
126 lda BitMask-1,x ; Get bit mask
128 sta VIC_SPR_ENA ; Enable sprite
130 cli ; Enable interrupts
134 ; --------------------------------------------------------------------------
136 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
140 ldy #0 ; Stack offset
143 sei ; Disable interrupts
168 plp ; Enable interrupts
170 jmp addysp1 ; Drop params, return
172 ; --------------------------------------------------------------------------
174 ; int __fastcall__ mouse_x (void);
185 ; --------------------------------------------------------------------------
187 ; int __fastcall__ mouse_y (void);
198 ; --------------------------------------------------------------------------
200 ; void mouse_info (...);
207 ; --------------------------------------------------------------------------
209 ; void __fastcall__ mouse_move (int x, int y);
214 sei ; Disable interrupts
221 stx XPos+1 ; Set new position
223 jsr MoveSprite ; Move the sprite to the mouse pos
225 @L9: cli ; Enable interrupts
228 ; --------------------------------------------------------------------------
230 ; Mouse interrupt handler
235 lda SID_ADConv1 ; Get mouse X movement
237 jsr MoveCheck ; Calculate movement vector
240 ; Calculate the new X coordinate (--> a/y)
244 tay ; Remember low byte
249 ; Limit the X coordinate to the bounding box
267 ; Calculate the Y movement vector
269 lda SID_ADConv2 ; Get mouse Y movement
271 jsr MoveCheck ; Calculate movement
274 ; Calculate the new Y coordinate (--> a/y)
278 tay ; Remember low byte
298 ; Move the mouse sprite if it is enabled
300 jsr MoveSprite ; Move the sprite
302 ; Jump to the next IRQ handler
307 ; --------------------------------------------------------------------------
309 ; Move check routine, called for both coordinates.
311 ; Entry: y = old value of pot register
312 ; a = current value of pot register
313 ; Exit: y = value to use for old value
314 ; x/a = delta value for position
322 sec ; a = mod64 (new - old)
325 cmp #%01000000 ; if (a > 0)
328 beq @L2 ; if (a != 0)
329 ldy NewValue ; y = NewValue
332 @L1: ora #%11000000 ; else or in high order bits
333 cmp #$FF ; if (a != -1)
337 ldx #$FF ; high byte = -1
344 ; --------------------------------------------------------------------------
346 ; Move the mouse sprite to the current mouse position. Must be called
347 ; with interrupts off. MoveSprite1 is an entry without checking and
353 lda Visible ; Mouse visible?
354 bne MoveSpriteDone ; Jump if no
355 ldx MouseSprite ; Sprite defined?
356 beq MoveSpriteDone ; Jump if no
361 lda VIC_SPR_HI_X ; Get high X bits of all sprites
362 and NotMask-1,x ; Mask out sprite bit
363 ldy XPos+1 ; Test Y position
365 ora BitMask-1,x ; Set high X bit
366 @L1: sta VIC_SPR_HI_X ; Set hi X sprite values
374 sta VIC_SPR0_X-2,x ; Set low byte
378 ldy YPos+1 ; Negative or too large?
379 bne MoveSpriteDone ; Jump if yes
381 sta VIC_SPR0_Y-2,x ; Set Y position
388 ; --------------------------------------------------------------------------
393 OldIRQ: .res 2 ; Old IRQ vector
394 MousePort: .res 1 ; Port used for the mouse
395 MouseSprite: .res 1 ; Number of sprite to control
396 OldValue: .res 1 ; Temp for MoveCheck routine
397 NewValue: .res 1 ; Temp for MoveCheck routine
399 Visible: .res 1 ; Is the mouse visible?
400 OldPotX: .res 1 ; Old hw counter values
403 XPos: .res 2 ; Current mouse position, X
404 YPos: .res 2 ; Current mouse position, Y
406 XMin: .res 2 ; X1 value of bounding box
407 YMin: .res 2 ; Y1 value of bounding box
408 XMax: .res 2 ; X2 value of bounding box
409 YMax: .res 2 ; Y2 value of bounding box
413 BitMask: .byte $01, $02, $04, $08, $10, $20, $40, $80
414 NotMask: .byte $FE, $FD, $FB, $F7, $EF, $DF, $BF, $7F