blob: eeef7b595b9a04d48a075d5dedfb9ed3b4f01ab2 [file] [log] [blame]
adamdunkels6b828fb2003-08-09 13:13:24 +00001
2;---------------------------------------------------------------------
3 ;; The 1351 mouse code was taken from the CC65 libraries,
4 ;; written by Ullrich von Bassewitz
5;---------------------------------------------------------------------
6
7 .macpack generic
8
9 .import _ctk_mouse_joyx, _ctk_mouse_joyy
10 .import _ctk_mouse_firebutton
11 .export _ctk_mouse_1351
12;---------------------------------------------------------------------
13.bss
14OldValue: .res 1 ; Temp for MoveCheck routine
15NewValue: .res 1 ; Temp for MoveCheck routine
16YCorr: .res 1 ; Correction for Y coordinate
17
18OldPotX: .res 1 ; Old hw counter values
19OldPotY: .res 1
20
21SID_ADConv1 = $D419
22SID_ADConv2 = $D41A
adamdunkels4e4269a2003-08-12 21:07:39 +000023CIA1_PRA = $DC00
24CIA1_PRB = $DC01
adamdunkels6b828fb2003-08-09 13:13:24 +000025CIA1_DDRB = $DC03
26
27
28;---------------------------------------------------------------------
29.data
30XMin: .word 0 ; X1 value of bounding box
31YMin: .word 0 ; Y1 value of bounding box
32XMax: .word 319 ; X2 value of bounding box
33YMax: .word 199 ; Y2 value of bounding box
34
35;---------------------------------------------------------------------
36.code
37
38;---------------------------------------------------------------------
39;---------------------------------------------------------------------
40 ;; Most of the mouse code is taken from the CC65 libraries written by
41 ;; Ullrich von Bassewitz
42MoveCheck:
43 sty OldValue
44 sta NewValue
45 ldx #$00
46
47 sub OldValue ; a = mod64 (new - old)
48 and #%01111111
49 cmp #%01000000 ; if (a > 0)
50 bcs @L1 ;
51 lsr a ; a /= 2;
52 beq @L2 ; if (a != 0)
53 ldy NewValue ; y = NewValue
54 rts ; return
55
56@L1: ora #%11000000 ; else or in high order bits
57 cmp #$FF ; if (a != -1)
58 beq @L2
59 sec
60 ror a ; a /= 2
61 dex ; high byte = -1 (X = $FF)
62 ldy NewValue
63 rts
64
65@L2: txa ; A = $00
66 rts
67
68;---------------------------------------------------------------------
69_ctk_mouse_1351:
70 lda SID_ADConv1 ; Get mouse X movement
71 ldy OldPotX
72 jsr MoveCheck ; Calculate movement vector
73 sty OldPotX
74
75; Calculate the new X coordinate (--> a/y)
76
77 add _ctk_mouse_joyx
78 tay ; Remember low byte
79 txa
80 adc _ctk_mouse_joyx+1
81 tax
82
83; Limit the X coordinate to the bounding box
84
85 cpy XMin
86 sbc XMin+1
87 bpl @L1
88 ldy XMin
89 ldx XMin+1
90 jmp @L2
91@L1: txa
92
93 cpy XMax
94 sbc XMax+1
95 bmi @L2
96 ldy XMax
97 ldx XMax+1
98@L2: sty _ctk_mouse_joyx
99 stx _ctk_mouse_joyx+1
100; Calculate the Y movement vector
101
102 lda SID_ADConv2 ; Get mouse Y movement
103 ldy OldPotY
104 jsr MoveCheck ; Calculate movement
105 sty OldPotY
106
107; Calculate the new Y coordinate (--> a/y)
108
109 sta OldValue
110 lda _ctk_mouse_joyy
111 sub OldValue
112 tay
113 stx OldValue
114 lda _ctk_mouse_joyy+1
115 sbc OldValue
116 tax
117
118 cpy YMin
119 sbc YMin+1
120 bpl @L3
121 ldy YMin
122 ldx YMin+1
123 jmp @L4
124@L3: txa
125
126 cpy YMax
127 sbc YMax+1
128 bmi @L4
129 ldy YMax
130 ldx YMax+1
131@L4: sty _ctk_mouse_joyy
132 stx _ctk_mouse_joyy+1
133
134 ;; Get mouse button
adamdunkels4e4269a2003-08-12 21:07:39 +0000135 lda #$7F
136 sta CIA1_PRA
137 lda CIA1_PRB ; Read joystick #0
138 ldx #0
139 and #$1F
140 eor #$1F
141
adamdunkels6b828fb2003-08-09 13:13:24 +0000142 ora _ctk_mouse_firebutton
143 sta _ctk_mouse_firebutton
144
145 lda #0
adamdunkels4e4269a2003-08-12 21:07:39 +0000146 sta CIA1_PRA
adamdunkels6b828fb2003-08-09 13:13:24 +0000147 sta CIA1_DDRB
148 rts
149;---------------------------------------------------------------------