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, popsreg, addysp1
16 .importzp ptr1, sp, sreg
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 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
189 ; /* Return the current mouse position */
194 stx ptr1+1 ; Remember the argument pointer
196 ldy #0 ; Structure offset
199 sei ; Disable interrupts
201 lda XPos ; Transfer the position
213 plp ; Restore initial interrupt state
217 ; --------------------------------------------------------------------------
219 ; void __fastcall__ mouse_info (struct mouse_info* info);
220 ; /* Return the state of the mouse buttons and the position of the mouse */
225 ; We're cheating here to keep the code smaller: The first fields of the
226 ; mouse_info struct are identical to the mouse_pos struct, so we will just
227 ; call _mouse_pos to initialize the struct pointer and fill the position
232 ; Fill in the button state
234 jsr _mouse_buttons ; Will not touch ptr1
240 ; --------------------------------------------------------------------------
242 ; void __fastcall__ mouse_move (int x, int y);
247 sei ; Disable interrupts
254 stx XPos+1 ; Set new position
256 jsr MoveSprite ; Move the sprite to the mouse pos
258 @L9: cli ; Enable interrupts
262 ; --------------------------------------------------------------------------
264 ; unsigned char mouse_buttons (void);
268 lda MousePort ; Get the port
269 jmp _readjoy ; Same as joystick
271 ; --------------------------------------------------------------------------
273 ; Mouse interrupt handler
278 lda SID_ADConv1 ; Get mouse X movement
280 jsr MoveCheck ; Calculate movement vector
283 ; Calculate the new X coordinate (--> a/y)
286 tay ; Remember low byte
291 ; Limit the X coordinate to the bounding box
309 ; Calculate the Y movement vector
311 lda SID_ADConv2 ; Get mouse Y movement
313 jsr MoveCheck ; Calculate movement
316 ; Calculate the new Y coordinate (--> a/y)
343 ; Move the mouse sprite if it is enabled
345 jsr MoveSprite ; Move the sprite
347 ; Jump to the next IRQ handler
352 ; --------------------------------------------------------------------------
354 ; Move check routine, called for both coordinates.
356 ; Entry: y = old value of pot register
357 ; a = current value of pot register
358 ; Exit: y = value to use for old value
359 ; x/a = delta value for position
367 sub OldValue ; a = mod64 (new - old)
369 cmp #%01000000 ; if (a > 0)
372 beq @L2 ; if (a != 0)
373 ldy NewValue ; y = NewValue
376 @L1: ora #%11000000 ; else or in high order bits
377 cmp #$FF ; if (a != -1)
381 dex ; high byte = -1 (X = $FF)
388 ; --------------------------------------------------------------------------
390 ; Move the mouse sprite to the current mouse position. Must be called
391 ; with interrupts off. MoveSprite1 is an entry without checking and
397 lda Visible ; Mouse visible?
398 bne MoveSpriteDone ; Jump if no
399 ldx MouseSprite ; Sprite defined?
400 beq MoveSpriteDone ; Jump if no
405 lda VIC_SPR_HI_X ; Get high X bits of all sprites
406 and NotMask-1,x ; Mask out sprite bit
407 ldy XPos+1 ; Test Y position
409 ora BitMask-1,x ; Set high X bit
410 @L1: sta VIC_SPR_HI_X ; Set hi X sprite values
418 sta VIC_SPR0_X-2,x ; Set low byte
422 ldy YPos+1 ; Negative or too large?
423 bne MoveSpriteDone ; Jump if yes
425 sta VIC_SPR0_Y-2,x ; Set Y position
432 ; --------------------------------------------------------------------------
437 OldIRQ: .res 2 ; Old IRQ vector
438 MousePort: .res 1 ; Port used for the mouse
439 MouseSprite: .res 1 ; Number of sprite to control
440 OldValue: .res 1 ; Temp for MoveCheck routine
441 NewValue: .res 1 ; Temp for MoveCheck routine
443 Visible: .res 1 ; Is the mouse visible?
444 OldPotX: .res 1 ; Old hw counter values
447 XPos: .res 2 ; Current mouse position, X
448 YPos: .res 2 ; Current mouse position, Y
450 XMin: .res 2 ; X1 value of bounding box
451 YMin: .res 2 ; Y1 value of bounding box
452 XMax: .res 2 ; X2 value of bounding box
453 YMax: .res 2 ; Y2 value of bounding box
457 BitMask: .byte $01, $02, $04, $08, $10, $20, $40, $80
458 NotMask: .byte $FE, $FD, $FB, $F7, $EF, $DF, $BF, $7F