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