blob: fde57d9aac36002388ef85ffd373199693803bd1 [file] [log] [blame]
adamdunkels942aabf2003-04-09 00:31:13 +00001
2;---------------------------------------------------------------------
3
adamdunkelsb31fe572003-04-24 17:08:51 +00004 .macpack generic
5
adamdunkels942aabf2003-04-09 00:31:13 +00006 .import _ctk_mouse_joyx, _ctk_mouse_joyy
7 .import _ctk_mouse_firebutton
8 .export _ctk_mouse_asm_irq
9;---------------------------------------------------------------------
10.bss
11lastjoydy: .res 1
12lastjoydx: .res 1
13joydycnt: .res 1
14joydxcnt: .res 1
adamdunkelsb31fe572003-04-24 17:08:51 +000015OldValue: .res 1 ; Temp for MoveCheck routine
16NewValue: .res 1 ; Temp for MoveCheck routine
17YCorr: .res 1 ; Correction for Y coordinate
18
19OldPotX: .res 1 ; Old hw counter values
20OldPotY: .res 1
21
22SID_ADConv1 = $D419
23SID_ADConv2 = $D41A
24CIA1_PRA = $DC00
25CIA1_PRB = $DC01
26
adamdunkels942aabf2003-04-09 00:31:13 +000027;---------------------------------------------------------------------
28.data
adamdunkelsb31fe572003-04-24 17:08:51 +000029XMin: .word 0 ; X1 value of bounding box
30YMin: .word 0 ; Y1 value of bounding box
31XMax: .word 319 ; X2 value of bounding box
32YMax: .word 199 ; Y2 value of bounding box
33
adamdunkels942aabf2003-04-09 00:31:13 +000034;---------------------------------------------------------------------
35.code
adamdunkelsb31fe572003-04-24 17:08:51 +000036
37;---------------------------------------------------------------------
38joystick:
adamdunkels942aabf2003-04-09 00:31:13 +000039 lda $dc00
40 ldy #0
41 ldx #0
42 lsr
43 bcs nodey
44 dey
45nodey:
46 lsr
47 bcs noiny
48 iny
49noiny:
50 lsr
51 bcs nodex
52 dex
53nodex:
54 lsr
55 bcs noinx
56 inx
57noinx:
58 and #1
59 eor #1
60 sta _ctk_mouse_firebutton
61
62 cpy lastjoydy
63 sty lastjoydy
64 bne noydy
65 tya
66 pha
67 inc joydycnt
68 lda joydycnt
69 bpl nostajoydycnt
70 lda #$80
71 sta joydycnt
72nostajoydycnt:
73 lsr
74 lsr
75 lsr
76 lsr
77 tay
78 pla
79asldeyloop:
80 asl
81 dey
82 bpl asldeyloop
83 tay
84 jmp ydy
85noydy:
86 lda #0
87 sta joydycnt
88ydy:
89
90 tya
91 clc
92 adc _ctk_mouse_joyy
adamdunkels942aabf2003-04-09 00:31:13 +000093
adamdunkelse6f14d62003-04-11 20:24:08 +000094 cmp #240
95 bcc :+
96 lda #0
97:
98
99 cmp #200
100 bcc :+
101 lda #199
102:
103 sta _ctk_mouse_joyy
104
adamdunkels942aabf2003-04-09 00:31:13 +0000105 cpx lastjoydx
106 stx lastjoydx
107 bne noxdx
108 txa
109 pha
110 inc joydxcnt
111 lda joydxcnt
112 bpl nostajoydxcnt
113 lda #$80
114 sta joydxcnt
115nostajoydxcnt:
116 lsr
117 lsr
118 lsr
119 lsr
120 tax
121 pla
122asldexloop:
123 asl
124 dex
125 bpl asldexloop
126 tax
127 jmp xdx
128noxdx:
129 lda #0
130 sta joydxcnt
131xdx:
132
133 txa
134 clc
135 adc _ctk_mouse_joyx
136 sta _ctk_mouse_joyx
137 php
138 lda #$ff
139 cpx #0
140 bmi nolda02
141 lda #0
142nolda02:
143 plp
144 adc _ctk_mouse_joyx+1
adamdunkelse6f14d62003-04-11 20:24:08 +0000145 and #1
146 sta _ctk_mouse_joyx+1
adamdunkelsb31fe572003-04-24 17:08:51 +0000147 rts
148;---------------------------------------------------------------------
149 ;; Most of the mouse code is taken from the CC65 libraries written by
150 ;; Ullrich von Bassewitz
151MoveCheck:
152 sty OldValue
153 sta NewValue
154 ldx #$00
155
156 sub OldValue ; a = mod64 (new - old)
157 and #%01111111
158 cmp #%01000000 ; if (a > 0)
159 bcs @L1 ;
160 lsr a ; a /= 2;
161 beq @L2 ; if (a != 0)
162 ldy NewValue ; y = NewValue
163 rts ; return
164
165@L1: ora #%11000000 ; else or in high order bits
166 cmp #$FF ; if (a != -1)
167 beq @L2
168 sec
169 ror a ; a /= 2
170 dex ; high byte = -1 (X = $FF)
171 ldy NewValue
172 rts
173
174@L2: txa ; A = $00
175 rts
176
177;---------------------------------------------------------------------
178mouse:
179 lda SID_ADConv1 ; Get mouse X movement
180 ldy OldPotX
181 jsr MoveCheck ; Calculate movement vector
182 sty OldPotX
183
184; Calculate the new X coordinate (--> a/y)
185
186 add _ctk_mouse_joyx
187 tay ; Remember low byte
188 txa
189 adc _ctk_mouse_joyx+1
190 tax
191
192; Limit the X coordinate to the bounding box
193
194 cpy XMin
195 sbc XMin+1
196 bpl @L1
197 ldy XMin
198 ldx XMin+1
199 jmp @L2
200@L1: txa
201
202 cpy XMax
203 sbc XMax+1
204 bmi @L2
205 ldy XMax
206 ldx XMax+1
207@L2: sty _ctk_mouse_joyx
208 stx _ctk_mouse_joyx+1
209; Calculate the Y movement vector
210
211 lda SID_ADConv2 ; Get mouse Y movement
212 ldy OldPotY
213 jsr MoveCheck ; Calculate movement
214 sty OldPotY
215
216; Calculate the new Y coordinate (--> a/y)
217
218 sta OldValue
219 lda _ctk_mouse_joyy
220 sub OldValue
221 tay
222 stx OldValue
223 lda _ctk_mouse_joyy+1
224 sbc OldValue
225 tax
226
227 cpy YMin
228 sbc YMin+1
229 bpl @L3
230 ldy YMin
231 ldx YMin+1
232 jmp @L4
233@L3: txa
234
235 cpy YMax
236 sbc YMax+1
237 bmi @L4
238 ldy YMax
239 ldx YMax+1
240@L4: sty _ctk_mouse_joyy
241 stx _ctk_mouse_joyy+1
242
243 ;; Get mouse button
244 lda #$7F
245 sta CIA1_PRA
246 lda CIA1_PRB ; Read joystick #0
247 ldx #0
248 and #$1F
249 eor #$1F
250 sta _ctk_mouse_firebutton
251 rts
252;---------------------------------------------------------------------
253_ctk_mouse_asm_irq:
254 jsr joystick
255
256 jsr mouse
257
adamdunkels942aabf2003-04-09 00:31:13 +0000258 lda _ctk_mouse_joyy
259 clc
adamdunkels9e5028e2003-04-09 09:02:05 +0000260 adc #$32
adamdunkels942aabf2003-04-09 00:31:13 +0000261 sta $d001
adamdunkels9e5028e2003-04-09 09:02:05 +0000262 sta $d003
adamdunkels942aabf2003-04-09 00:31:13 +0000263
264 lda _ctk_mouse_joyx
265 clc
266 adc #$18
267 sta $d000
adamdunkels9e5028e2003-04-09 09:02:05 +0000268 sta $d002
adamdunkels942aabf2003-04-09 00:31:13 +0000269 lda #0
270 adc _ctk_mouse_joyx+1
271 and #1
adamdunkelse6f14d62003-04-11 20:24:08 +0000272 beq :+
273 lda #3
274:
adamdunkels942aabf2003-04-09 00:31:13 +0000275 sta $d010
276 jmp $ea31