adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 1 | |
| 2 | ;--------------------------------------------------------------------- |
adamdunkels | c3f548b | 2003-04-25 08:43:17 +0000 | [diff] [blame^] | 3 | ;; The 1351 mouse code was taken from the CC65 libraries, |
| 4 | ;; written by Ullrich von Bassewitz |
| 5 | ;--------------------------------------------------------------------- |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 6 | |
adamdunkels | b31fe57 | 2003-04-24 17:08:51 +0000 | [diff] [blame] | 7 | .macpack generic |
| 8 | |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 9 | .import _ctk_mouse_joyx, _ctk_mouse_joyy |
| 10 | .import _ctk_mouse_firebutton |
| 11 | .export _ctk_mouse_asm_irq |
| 12 | ;--------------------------------------------------------------------- |
| 13 | .bss |
| 14 | lastjoydy: .res 1 |
| 15 | lastjoydx: .res 1 |
| 16 | joydycnt: .res 1 |
| 17 | joydxcnt: .res 1 |
adamdunkels | b31fe57 | 2003-04-24 17:08:51 +0000 | [diff] [blame] | 18 | OldValue: .res 1 ; Temp for MoveCheck routine |
| 19 | NewValue: .res 1 ; Temp for MoveCheck routine |
| 20 | YCorr: .res 1 ; Correction for Y coordinate |
| 21 | |
| 22 | OldPotX: .res 1 ; Old hw counter values |
| 23 | OldPotY: .res 1 |
| 24 | |
| 25 | SID_ADConv1 = $D419 |
| 26 | SID_ADConv2 = $D41A |
| 27 | CIA1_PRA = $DC00 |
| 28 | CIA1_PRB = $DC01 |
| 29 | |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 30 | ;--------------------------------------------------------------------- |
| 31 | .data |
adamdunkels | b31fe57 | 2003-04-24 17:08:51 +0000 | [diff] [blame] | 32 | XMin: .word 0 ; X1 value of bounding box |
| 33 | YMin: .word 0 ; Y1 value of bounding box |
| 34 | XMax: .word 319 ; X2 value of bounding box |
| 35 | YMax: .word 199 ; Y2 value of bounding box |
| 36 | |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 37 | ;--------------------------------------------------------------------- |
| 38 | .code |
adamdunkels | b31fe57 | 2003-04-24 17:08:51 +0000 | [diff] [blame] | 39 | |
| 40 | ;--------------------------------------------------------------------- |
| 41 | joystick: |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 42 | lda $dc00 |
| 43 | ldy #0 |
| 44 | ldx #0 |
| 45 | lsr |
| 46 | bcs nodey |
| 47 | dey |
| 48 | nodey: |
| 49 | lsr |
| 50 | bcs noiny |
| 51 | iny |
| 52 | noiny: |
| 53 | lsr |
| 54 | bcs nodex |
| 55 | dex |
| 56 | nodex: |
| 57 | lsr |
| 58 | bcs noinx |
| 59 | inx |
| 60 | noinx: |
| 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 |
| 75 | nostajoydycnt: |
| 76 | lsr |
| 77 | lsr |
| 78 | lsr |
| 79 | lsr |
| 80 | tay |
| 81 | pla |
| 82 | asldeyloop: |
| 83 | asl |
| 84 | dey |
| 85 | bpl asldeyloop |
| 86 | tay |
| 87 | jmp ydy |
| 88 | noydy: |
| 89 | lda #0 |
| 90 | sta joydycnt |
| 91 | ydy: |
| 92 | |
| 93 | tya |
| 94 | clc |
| 95 | adc _ctk_mouse_joyy |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 96 | |
adamdunkels | e6f14d6 | 2003-04-11 20:24:08 +0000 | [diff] [blame] | 97 | cmp #240 |
| 98 | bcc :+ |
| 99 | lda #0 |
| 100 | : |
| 101 | |
| 102 | cmp #200 |
| 103 | bcc :+ |
| 104 | lda #199 |
| 105 | : |
| 106 | sta _ctk_mouse_joyy |
| 107 | |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 108 | 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 |
| 118 | nostajoydxcnt: |
| 119 | lsr |
| 120 | lsr |
| 121 | lsr |
| 122 | lsr |
| 123 | tax |
| 124 | pla |
| 125 | asldexloop: |
| 126 | asl |
| 127 | dex |
| 128 | bpl asldexloop |
| 129 | tax |
| 130 | jmp xdx |
| 131 | noxdx: |
| 132 | lda #0 |
| 133 | sta joydxcnt |
| 134 | xdx: |
| 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 |
| 145 | nolda02: |
| 146 | plp |
| 147 | adc _ctk_mouse_joyx+1 |
adamdunkels | e6f14d6 | 2003-04-11 20:24:08 +0000 | [diff] [blame] | 148 | and #1 |
| 149 | sta _ctk_mouse_joyx+1 |
adamdunkels | b31fe57 | 2003-04-24 17:08:51 +0000 | [diff] [blame] | 150 | rts |
| 151 | ;--------------------------------------------------------------------- |
| 152 | ;; Most of the mouse code is taken from the CC65 libraries written by |
| 153 | ;; Ullrich von Bassewitz |
| 154 | MoveCheck: |
| 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 | ;--------------------------------------------------------------------- |
| 181 | mouse: |
| 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 | |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 261 | lda _ctk_mouse_joyy |
| 262 | clc |
adamdunkels | 9e5028e | 2003-04-09 09:02:05 +0000 | [diff] [blame] | 263 | adc #$32 |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 264 | sta $d001 |
adamdunkels | 9e5028e | 2003-04-09 09:02:05 +0000 | [diff] [blame] | 265 | sta $d003 |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 266 | |
| 267 | lda _ctk_mouse_joyx |
| 268 | clc |
| 269 | adc #$18 |
| 270 | sta $d000 |
adamdunkels | 9e5028e | 2003-04-09 09:02:05 +0000 | [diff] [blame] | 271 | sta $d002 |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 272 | lda #0 |
| 273 | adc _ctk_mouse_joyx+1 |
| 274 | and #1 |
adamdunkels | e6f14d6 | 2003-04-11 20:24:08 +0000 | [diff] [blame] | 275 | beq :+ |
| 276 | lda #3 |
| 277 | : |
adamdunkels | 942aabf | 2003-04-09 00:31:13 +0000 | [diff] [blame] | 278 | sta $d010 |
| 279 | jmp $ea31 |