2 ; Ullrich von Bassewitz, 19.09.2001
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
16 .import sys_bank, restore_bank
17 .import popax, addysp1
18 .importzp vic, sid, ptr1, sp
27 ; --------------------------------------------------------------------------
38 ; --------------------------------------------------------------------------
40 ; unsigned char __fastcall__ mouse_init (unsigned char type);
44 lda Initialized ; Already initialized?
45 bne AlreadyInitialized ; Jump if yes
47 ; Initialize variables
59 lda #50 ; ## FIXME: This is the PAL value
64 sbc #SPRITE_HEIGHT ; Sprite height in pixels
66 stx YMin+1 ; YMin = 29
67 lda #SCREEN_HEIGHT ; Vertical screen res
68 add YCorr ; Add correction factor
72 stx Invisible ; Mouse *not* visible
73 lda #<(SCREEN_WIDTH + SPRITE_WIDTH)
75 stx XMax+1 ; XMax = 320 + sprite width
77 ; Mouse successfully initialized
89 ; --------------------------------------------------------------------------
91 ; void mouse_done (void);
97 sta Initialized ; Reset the initialized flag
99 ; Disable the mouse sprite
105 sta IndReg ; Switch to the system bank
108 sei ; Disable interrupts
110 and #$FE ; Clear bit for sprite #0
111 sta (vic),y ; Disable sprite
112 cli ; Enable interrupts
114 stx IndReg ; Switch back the segment
117 ; --------------------------------------------------------------------------
119 ; void mouse_hide (void);
124 lda Invisible ; Get the flag
125 bne @L1 ; Jump if already invisible
126 jsr DisableSprite ; Disabe the mouse sprite
127 @L1: inc Invisible ; Set the flag to invisible
132 ; --------------------------------------------------------------------------
134 ; void mouse_show (void);
139 lda Invisible ; Mouse invisible?
141 dec Invisible ; Set the flag
142 bne @L1 ; Jump if still invisible
144 jsr sys_bank ; Switch to the system bank
146 sei ; Disable interrupts
147 jsr MoveSprite1 ; Move the sprite to it's position
149 lda (vic),y ; Get sprite enable register
150 ora #$01 ; Enable sprite #0
151 sta (vic),y ; Write back
152 cli ; Enable interrupts
154 jsr restore_bank ; Switch back the bank
160 ; --------------------------------------------------------------------------
162 ; void __fastcall__ mouse_box (int minx, int miny, int maxx, int maxy);
167 ldy #0 ; Stack offset
169 add YCorr ; Adjust the Y value
173 @L1: sei ; Disable interrupts
204 cli ; Enable interrupts
206 jmp addysp1 ; Drop params, return
210 ; --------------------------------------------------------------------------
212 ; void __fastcall__ mouse_pos (struct mouse_pos* pos);
213 ; /* Return the current mouse position */
219 stx ptr1+1 ; Remember the argument pointer
221 ldy #0 ; Structure offset
222 sec ; Needed for the SBC later
224 sei ; Disable interrupts
225 lda XPos ; Transfer the position
234 cli ; Restore initial interrupt state
236 sub YCorr ; Apply the Y correction value
240 sta (ptr1),y ; Store YPos
249 ; --------------------------------------------------------------------------
251 ; void __fastcall__ mouse_info (struct mouse_info* info);
252 ; /* Return the state of the mouse buttons and the position of the mouse */
257 ; We're cheating here to keep the code smaller: The first fields of the
258 ; mouse_info struct are identical to the mouse_pos struct, so we will just
259 ; call _mouse_pos to initialize the struct pointer and fill the position
264 ; Fill in the button state
266 jsr _mouse_buttons ; Will not touch ptr1
274 ; --------------------------------------------------------------------------
276 ; void __fastcall__ mouse_move (int x, int y);
281 add YCorr ; Add Y coordinate correction
291 adc #XCORR ; Adjust X coordinate
297 stx XPos+1 ; Set new position
298 jsr MoveSprite ; Move the sprite to the mouse pos
299 cli ; Enable interrupts
306 ; --------------------------------------------------------------------------
308 ; unsigned char mouse_buttons (void);
313 lda #$00 ; Use port #0
314 jmp _readjoy ; Same as joystick
319 ; --------------------------------------------------------------------------
321 ; Mouse interrupt handler
327 lda Initialized ; Mouse initialized?
328 beq IRQDone ; Jump if no
331 lda (sid),y ; Get mouse X movement
333 jsr MoveCheck ; Calculate movement vector
336 ; Calculate the new X coordinate (--> a/y)
339 tay ; Remember low byte
344 ; Limit the X coordinate to the bounding box
362 ; Calculate the Y movement vector
365 lda (sid),y ; Get mouse Y movement
367 jsr MoveCheck ; Calculate movement
370 ; Calculate the new Y coordinate (--> a/y)
397 ; Move the mouse sprite to the current mouse position. Must be called
398 ; with interrupts off and the system bank enabled. MoveSprite1 is an entry
403 lda Invisible ; Mouse visible?
404 bne Done ; Jump if no
410 lda (vic),y ; Get high X bits of all sprites
411 and #$FE ; Clear bit for sprite #0
412 ldx XPos+1 ; Test Y position
414 ora #$01 ; Set high X bit
415 @L5: sta (vic),y ; Set hi X sprite values
421 sta (vic),y ; Set low byte
425 ldy YPos+1 ; Negative or too large?
426 bne Done ; Jump if yes
429 sta (vic),y ; Set Y position
435 ; --------------------------------------------------------------------------
437 ; Move check routine, called for both coordinates.
439 ; Entry: y = old value of pot register
440 ; a = current value of pot register
441 ; Exit: y = value to use for old value
442 ; x/a = delta value for position
451 sub OldValue ; a = mod64 (new - old)
453 cmp #%01000000 ; if (a > 0)
456 beq @L2 ; if (a != 0)
457 ldy NewValue ; y = NewValue
460 @L1: ora #%11000000 ; else or in high order bits
461 cmp #$FF ; if (a != -1)
465 dex ; high byte = -1 (X = $FF)
474 ; --------------------------------------------------------------------------
479 Initialized: .res 1 ; True if mouse initialized
480 OldInitStatus: .res 1 ; Old IRQ flag value
481 OldValue: .res 1 ; Temp for MoveCheck routine
482 NewValue: .res 1 ; Temp for MoveCheck routine
483 YCorr: .res 1 ; Correction for Y coordinate
485 Invisible: .res 1 ; Is the mouse invisible?
486 OldPotX: .res 1 ; Old hw counter values
489 XPos: .res 2 ; Current mouse position, X
490 YPos: .res 2 ; Current mouse position, Y
492 XMin: .res 2 ; X1 value of bounding box
493 YMin: .res 2 ; Y1 value of bounding box
494 XMax: .res 2 ; X2 value of bounding box
495 YMax: .res 2 ; Y2 value of bounding box