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
12 .export _mouse_move, _mouse_buttons
15 .import popa, popsreg, addysp1
24 ; --------------------------------------------------------------------------
26 ; unsigned char __fastcall__ mouse_init (unsigned char port,
27 ; unsigned char sprite,
28 ; unsigned char type);
32 jsr popa ; Ignore the type, get sprite param
33 tax ; Save sprite number
34 jsr popa ; Get the port number
36 ldy OldIRQ+1 ; Already initialized?
37 bne AlreadyInitialized ; Jump if yes
39 stx MouseSprite ; Remember the sprite number
40 sta MousePort ; Remember the port number
42 ; Initialize variables
55 stx YMin+1 ; YMin = 29
58 stx YMax+1 ; YMax = 250
60 stx Visible ; Mouse *not* visible
63 stx XMax+1 ; XMax = 344
65 ; Remember the old IRQ vector
72 ; Set our own IRQ vector. We cheat here to save a few bytes of code:
73 ; The function is expected to return a value not equal to zero on success,
74 ; and since we know that the high byte of the IRQ handler address is never
75 ; zweo, we will return just this byte.
79 bne SetIRQ ; Branch always
85 ; --------------------------------------------------------------------------
87 ; void mouse_done (void);
91 ldx OldIRQ ; Initialized?
95 sty OldIRQ+1 ; Reset the initialized flag
96 SetIRQ: sei ; Disable interrupts
97 stx IRQVec ; Set the new/old vector
99 cli ; Enable interrupts
102 ; --------------------------------------------------------------------------
104 ; void mouse_hide (void);
108 lda Visible ; Get the flag
109 bne @L1 ; Jump if already invisible
110 ldx MouseSprite ; Sprite defined?
113 lda NotMask-1,x ; Get clean mask
115 sei ; Disable interrupts
117 sta VIC_SPR_ENA ; Disable sprite
118 cli ; Enable interrupts
120 @L1: inc Visible ; Set the flag to invisible
123 ; --------------------------------------------------------------------------
125 ; void mouse_show (void);
129 lda Visible ; Mouse already visible?
130 beq @L1 ; Jump if yes
131 dec Visible ; Get the flag
132 bne @L1 ; Jump if still invisible
133 ldx MouseSprite ; Sprite defined?
136 sei ; Disable interrupts
137 jsr MoveSprite1 ; Move the sprite to it's position
139 ldx MouseSprite ; Get sprite number (again)
140 lda BitMask-1,x ; Get bit mask
142 sta VIC_SPR_ENA ; Enable sprite
144 cli ; Enable interrupts
148 ; --------------------------------------------------------------------------
150 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
154 ldy #0 ; Stack offset
157 sei ; Disable interrupts
182 plp ; Enable interrupts
184 jmp addysp1 ; Drop params, return
186 ; --------------------------------------------------------------------------
188 ; int __fastcall__ mouse_x (void);
199 ; --------------------------------------------------------------------------
201 ; int __fastcall__ mouse_y (void);
212 ; --------------------------------------------------------------------------
214 ; void mouse_info (...);
221 ; --------------------------------------------------------------------------
223 ; void __fastcall__ mouse_move (int x, int y);
228 sei ; Disable interrupts
235 stx XPos+1 ; Set new position
237 jsr MoveSprite ; Move the sprite to the mouse pos
239 @L9: cli ; Enable interrupts
243 ; --------------------------------------------------------------------------
245 ; unsigned char mouse_buttons (void);
249 lda MousePort ; Get the port
250 jmp _readjoy ; Same as joystick
252 ; --------------------------------------------------------------------------
254 ; Mouse interrupt handler
259 lda SID_ADConv1 ; Get mouse X movement
261 jsr MoveCheck ; Calculate movement vector
264 ; Calculate the new X coordinate (--> a/y)
267 tay ; Remember low byte
272 ; Limit the X coordinate to the bounding box
290 ; Calculate the Y movement vector
292 lda SID_ADConv2 ; Get mouse Y movement
294 jsr MoveCheck ; Calculate movement
297 ; Calculate the new Y coordinate (--> a/y)
324 ; Move the mouse sprite if it is enabled
326 jsr MoveSprite ; Move the sprite
328 ; Jump to the next IRQ handler
333 ; --------------------------------------------------------------------------
335 ; Move check routine, called for both coordinates.
337 ; Entry: y = old value of pot register
338 ; a = current value of pot register
339 ; Exit: y = value to use for old value
340 ; x/a = delta value for position
348 sub OldValue ; a = mod64 (new - old)
350 cmp #%01000000 ; if (a > 0)
353 beq @L2 ; if (a != 0)
354 ldy NewValue ; y = NewValue
357 @L1: ora #%11000000 ; else or in high order bits
358 cmp #$FF ; if (a != -1)
362 dex ; high byte = -1 (X = $FF)
369 ; --------------------------------------------------------------------------
371 ; Move the mouse sprite to the current mouse position. Must be called
372 ; with interrupts off. MoveSprite1 is an entry without checking and
378 lda Visible ; Mouse visible?
379 bne MoveSpriteDone ; Jump if no
380 ldx MouseSprite ; Sprite defined?
381 beq MoveSpriteDone ; Jump if no
386 lda VIC_SPR_HI_X ; Get high X bits of all sprites
387 and NotMask-1,x ; Mask out sprite bit
388 ldy XPos+1 ; Test Y position
390 ora BitMask-1,x ; Set high X bit
391 @L1: sta VIC_SPR_HI_X ; Set hi X sprite values
399 sta VIC_SPR0_X-2,x ; Set low byte
403 ldy YPos+1 ; Negative or too large?
404 bne MoveSpriteDone ; Jump if yes
406 sta VIC_SPR0_Y-2,x ; Set Y position
413 ; --------------------------------------------------------------------------
418 OldIRQ: .res 2 ; Old IRQ vector
419 MousePort: .res 1 ; Port used for the mouse
420 MouseSprite: .res 1 ; Number of sprite to control
421 OldValue: .res 1 ; Temp for MoveCheck routine
422 NewValue: .res 1 ; Temp for MoveCheck routine
424 Visible: .res 1 ; Is the mouse visible?
425 OldPotX: .res 1 ; Old hw counter values
428 XPos: .res 2 ; Current mouse position, X
429 YPos: .res 2 ; Current mouse position, Y
431 XMin: .res 2 ; X1 value of bounding box
432 YMin: .res 2 ; Y1 value of bounding box
433 XMax: .res 2 ; X2 value of bounding box
434 YMax: .res 2 ; Y2 value of bounding box
438 BitMask: .byte $01, $02, $04, $08, $10, $20, $40, $80
439 NotMask: .byte $FE, $FD, $FB, $F7, $EF, $DF, $BF, $7F