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