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
14 .import popax, addysp1
24 ; --------------------------------------------------------------------------
35 ; --------------------------------------------------------------------------
37 ; unsigned char __fastcall__ mouse_init (unsigned char type);
41 lda OldIRQ+1 ; Already initialized?
42 bne AlreadyInitialized ; Jump if yes
44 ; Initialize variables
56 lda #50 ; ## FIXME: This is the PAL value
61 sbc #SPRITE_HEIGHT ; Sprite height in pixels
63 stx YMin+1 ; YMin = 29
64 lda #SCREEN_HEIGHT ; Vertical screen res
65 add YCorr ; Add correction factor
69 stx Invisible ; Mouse *not* visible
70 lda #<(SCREEN_WIDTH + SPRITE_WIDTH)
72 stx XMax+1 ; XMax = 320 + sprite width
74 ; Remember the old IRQ vector
81 ; Set our own IRQ vector.
85 jsr SetIRQ ; Set our IRQ vector, disable sprite 0
86 lda #1 ; Mouse successfully initialized
93 ; --------------------------------------------------------------------------
95 ; void mouse_done (void);
100 ldy OldIRQ+1 ; Initialized?
101 beq Done ; Jump if no
103 sta OldIRQ+1 ; Reset the initialized flag
104 SetIRQ: sei ; Disable interrupts
105 stx IRQVec ; Set the new/old vector
107 lda VIC_SPR_ENA ; Get sprite enable register
108 and #$FE ; Disable sprite #0
109 sta VIC_SPR_ENA ; Write back
110 cli ; Enable interrupts
114 ; --------------------------------------------------------------------------
116 ; void mouse_hide (void);
120 lda Invisible ; Get the flag
121 bne @L1 ; Jump if already invisible
123 lda #$FE ; Clear bit for sprite #0
124 sei ; Disable interrupts
126 sta VIC_SPR_ENA ; Disable sprite
127 cli ; Enable interrupts
129 @L1: inc Invisible ; Set the flag to invisible
132 ; --------------------------------------------------------------------------
134 ; void mouse_show (void);
138 lda Invisible ; Mouse invisible?
140 dec Invisible ; Set the flag
141 bne @L1 ; Jump if still invisible
143 sei ; Disable interrupts
144 jsr MoveSprite1 ; Move the sprite to it's position
145 lda VIC_SPR_ENA ; Get sprite enable register
146 ora #$01 ; Enable sprite #0
147 sta VIC_SPR_ENA ; Write back
148 cli ; Enable interrupts
152 ; --------------------------------------------------------------------------
154 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
158 ldy #0 ; Stack offset
160 add YCorr ; Adjust the Y value
164 @L1: sei ; Disable interrupts
195 cli ; Enable interrupts
197 jmp addysp1 ; Drop params, return
199 ; --------------------------------------------------------------------------
201 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
202 ; /* Return the current mouse position */
207 stx ptr1+1 ; Remember the argument pointer
209 ldy #0 ; Structure offset
210 sec ; Needed for the SBC later
212 sei ; Disable interrupts
213 lda XPos ; Transfer the position
222 cli ; Restore initial interrupt state
224 sub YCorr ; Apply the Y correction value
228 sta (ptr1),y ; Store YPos
235 ; --------------------------------------------------------------------------
237 ; void __fastcall__ mouse_info (struct mouse_info* info);
238 ; /* Return the state of the mouse buttons and the position of the mouse */
243 ; We're cheating here to keep the code smaller: The first fields of the
244 ; mouse_info struct are identical to the mouse_pos struct, so we will just
245 ; call _mouse_pos to initialize the struct pointer and fill the position
250 ; Fill in the button state
252 jsr _mouse_buttons ; Will not touch ptr1
258 ; --------------------------------------------------------------------------
260 ; void __fastcall__ mouse_move (int x, int y);
264 add YCorr ; Add Y coordinate correction
274 adc #XCORR ; Adjust X coordinate
279 stx XPos+1 ; Set new position
280 jsr MoveSprite ; Move the sprite to the mouse pos
281 cli ; Enable interrupts
285 ; --------------------------------------------------------------------------
287 ; unsigned char mouse_buttons (void);
295 lda CIA1_PRB ; Read joystick #0
304 ; --------------------------------------------------------------------------
306 ; Mouse interrupt handler
311 lda SID_ADConv1 ; Get mouse X movement
313 jsr MoveCheck ; Calculate movement vector
316 ; Calculate the new X coordinate (--> a/y)
319 tay ; Remember low byte
324 ; Limit the X coordinate to the bounding box
342 ; Calculate the Y movement vector
344 lda SID_ADConv2 ; Get mouse Y movement
346 jsr MoveCheck ; Calculate movement
349 ; Calculate the new Y coordinate (--> a/y)
376 ; Move the mouse sprite if it is enabled
378 jsr MoveSprite ; Move the sprite
380 ; Jump to the next IRQ handler
385 ; --------------------------------------------------------------------------
387 ; Move check routine, called for both coordinates.
389 ; Entry: y = old value of pot register
390 ; a = current value of pot register
391 ; Exit: y = value to use for old value
392 ; x/a = delta value for position
400 sub OldValue ; a = mod64 (new - old)
402 cmp #%01000000 ; if (a > 0)
405 beq @L2 ; if (a != 0)
406 ldy NewValue ; y = NewValue
409 @L1: ora #%11000000 ; else or in high order bits
410 cmp #$FF ; if (a != -1)
414 dex ; high byte = -1 (X = $FF)
421 ; --------------------------------------------------------------------------
423 ; Move the mouse sprite to the current mouse position. Must be called
424 ; with interrupts off. MoveSprite1 is an entry without checking.
429 lda Invisible ; Mouse visible?
430 bne MoveSpriteDone ; Jump if no
435 lda VIC_SPR_HI_X ; Get high X bits of all sprites
436 and #$FE ; Clear bit for sprite #0
437 ldy XPos+1 ; Test Y position
439 ora #$01 ; Set high X bit
440 @L1: sta VIC_SPR_HI_X ; Set hi X sprite values
445 sta VIC_SPR0_X ; Set low byte
449 ldy YPos+1 ; Negative or too large?
450 bne MoveSpriteDone ; Jump if yes
452 sta VIC_SPR0_Y ; Set Y position
459 ; --------------------------------------------------------------------------
464 OldIRQ: .res 2 ; Old IRQ vector
465 OldValue: .res 1 ; Temp for MoveCheck routine
466 NewValue: .res 1 ; Temp for MoveCheck routine
467 YCorr: .res 1 ; Correction for Y coordinate
469 Invisible: .res 1 ; Is the mouse invisible?
470 OldPotX: .res 1 ; Old hw counter values
473 XPos: .res 2 ; Current mouse position, X
474 YPos: .res 2 ; Current mouse position, Y
476 XMin: .res 2 ; X1 value of bounding box
477 YMin: .res 2 ; Y1 value of bounding box
478 XMax: .res 2 ; X2 value of bounding box
479 YMax: .res 2 ; Y2 value of bounding box