| 1 | /* |
| 2 | * PicoDrive |
| 3 | * (C) notaz, 2009,2010,2013 |
| 4 | * |
| 5 | * This work is licensed under the terms of MAME license. |
| 6 | * See COPYING file in the top-level directory. |
| 7 | */ |
| 8 | #include "../pico_int.h" |
| 9 | |
| 10 | static int pwm_cycles; |
| 11 | static int pwm_mult; |
| 12 | static int pwm_ptr; |
| 13 | static int pwm_irq_reload; |
| 14 | static int pwm_doing_fifo; |
| 15 | static int pwm_silent; |
| 16 | |
| 17 | void p32x_pwm_ctl_changed(void) |
| 18 | { |
| 19 | int control = Pico32x.regs[0x30 / 2]; |
| 20 | int cycles = Pico32x.regs[0x32 / 2]; |
| 21 | |
| 22 | cycles = (cycles - 1) & 0x0fff; |
| 23 | pwm_cycles = cycles; |
| 24 | |
| 25 | // supposedly we should stop FIFO when xMd is 0, |
| 26 | // but mars test disagrees |
| 27 | pwm_mult = 0; |
| 28 | if ((control & 0x0f) != 0) |
| 29 | pwm_mult = 0x10000 / cycles; |
| 30 | |
| 31 | pwm_irq_reload = (control & 0x0f00) >> 8; |
| 32 | pwm_irq_reload = ((pwm_irq_reload - 1) & 0x0f) + 1; |
| 33 | |
| 34 | if (Pico32x.pwm_irq_cnt == 0) |
| 35 | Pico32x.pwm_irq_cnt = pwm_irq_reload; |
| 36 | } |
| 37 | |
| 38 | static void do_pwm_irq(SH2 *sh2, unsigned int m68k_cycles) |
| 39 | { |
| 40 | p32x_trigger_irq(sh2, m68k_cycles, P32XI_PWM); |
| 41 | |
| 42 | if (Pico32x.regs[0x30 / 2] & P32XP_RTP) { |
| 43 | p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1); |
| 44 | // note: might recurse |
| 45 | p32x_dreq1_trigger(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static int convert_sample(unsigned int v) |
| 50 | { |
| 51 | if (v == 0) |
| 52 | return 0; |
| 53 | if (v > pwm_cycles) |
| 54 | v = pwm_cycles; |
| 55 | return ((int)v - pwm_cycles / 2) * pwm_mult; |
| 56 | } |
| 57 | |
| 58 | #define consume_fifo(sh2, m68k_cycles) { \ |
| 59 | int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \ |
| 60 | if (cycles_diff >= pwm_cycles) \ |
| 61 | consume_fifo_do(sh2, m68k_cycles, cycles_diff); \ |
| 62 | } |
| 63 | |
| 64 | static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles, |
| 65 | int sh2_cycles_diff) |
| 66 | { |
| 67 | struct Pico32xMem *mem = Pico32xMem; |
| 68 | unsigned short *fifo_l = mem->pwm_fifo[0]; |
| 69 | unsigned short *fifo_r = mem->pwm_fifo[1]; |
| 70 | int sum = 0; |
| 71 | |
| 72 | if (pwm_cycles == 0 || pwm_doing_fifo) |
| 73 | return; |
| 74 | |
| 75 | elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d", |
| 76 | m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm_cycles, |
| 77 | Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm_ptr); |
| 78 | |
| 79 | // this is for recursion from dreq1 writes |
| 80 | pwm_doing_fifo = 1; |
| 81 | |
| 82 | for (; sh2_cycles_diff >= pwm_cycles; sh2_cycles_diff -= pwm_cycles) |
| 83 | { |
| 84 | if (Pico32x.pwm_p[0] > 0) { |
| 85 | fifo_l[0] = fifo_l[1]; |
| 86 | fifo_l[1] = fifo_l[2]; |
| 87 | fifo_l[2] = fifo_l[3]; |
| 88 | Pico32x.pwm_p[0]--; |
| 89 | mem->pwm_current[0] = convert_sample(fifo_l[0]); |
| 90 | sum += mem->pwm_current[0]; |
| 91 | } |
| 92 | if (Pico32x.pwm_p[1] > 0) { |
| 93 | fifo_r[0] = fifo_r[1]; |
| 94 | fifo_r[1] = fifo_r[2]; |
| 95 | fifo_r[2] = fifo_r[3]; |
| 96 | Pico32x.pwm_p[1]--; |
| 97 | mem->pwm_current[1] = convert_sample(fifo_r[0]); |
| 98 | sum += mem->pwm_current[1]; |
| 99 | } |
| 100 | |
| 101 | mem->pwm[pwm_ptr * 2 ] = mem->pwm_current[0]; |
| 102 | mem->pwm[pwm_ptr * 2 + 1] = mem->pwm_current[1]; |
| 103 | pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1); |
| 104 | |
| 105 | if (--Pico32x.pwm_irq_cnt == 0) { |
| 106 | Pico32x.pwm_irq_cnt = pwm_irq_reload; |
| 107 | do_pwm_irq(sh2, m68k_cycles); |
| 108 | } |
| 109 | } |
| 110 | Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff; |
| 111 | pwm_doing_fifo = 0; |
| 112 | if (sum != 0) |
| 113 | pwm_silent = 0; |
| 114 | } |
| 115 | |
| 116 | static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now) |
| 117 | { |
| 118 | unsigned int sh2_now = m68k_now * 3; |
| 119 | int cycles_diff_sh2; |
| 120 | |
| 121 | if (pwm_cycles == 0) |
| 122 | return 0; |
| 123 | |
| 124 | cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p; |
| 125 | if (cycles_diff_sh2 >= pwm_cycles) |
| 126 | consume_fifo_do(sh2, m68k_now, cycles_diff_sh2); |
| 127 | |
| 128 | if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1)) |
| 129 | return 0; // masked by everyone |
| 130 | |
| 131 | cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p; |
| 132 | return (Pico32x.pwm_irq_cnt * pwm_cycles |
| 133 | - cycles_diff_sh2) / 3 + 1; |
| 134 | } |
| 135 | |
| 136 | void p32x_pwm_schedule(unsigned int m68k_now) |
| 137 | { |
| 138 | int after = p32x_pwm_schedule_(NULL, m68k_now); |
| 139 | if (after != 0) |
| 140 | p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after); |
| 141 | } |
| 142 | |
| 143 | void p32x_pwm_schedule_sh2(SH2 *sh2) |
| 144 | { |
| 145 | int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2)); |
| 146 | if (after != 0) |
| 147 | p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after); |
| 148 | } |
| 149 | |
| 150 | void p32x_pwm_sync_to_sh2(SH2 *sh2) |
| 151 | { |
| 152 | int m68k_cycles = sh2_cycles_done_m68k(sh2); |
| 153 | consume_fifo(sh2, m68k_cycles); |
| 154 | } |
| 155 | |
| 156 | void p32x_pwm_irq_event(unsigned int m68k_now) |
| 157 | { |
| 158 | p32x_pwm_schedule(m68k_now); |
| 159 | } |
| 160 | |
| 161 | unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2, |
| 162 | unsigned int m68k_cycles) |
| 163 | { |
| 164 | unsigned int d = 0; |
| 165 | |
| 166 | consume_fifo(sh2, m68k_cycles); |
| 167 | |
| 168 | a &= 0x0e; |
| 169 | switch (a) { |
| 170 | case 0: // control |
| 171 | case 2: // cycle |
| 172 | d = Pico32x.regs[(0x30 + a) / 2]; |
| 173 | break; |
| 174 | |
| 175 | case 4: // L ch |
| 176 | if (Pico32x.pwm_p[0] == 3) |
| 177 | d |= P32XP_FULL; |
| 178 | else if (Pico32x.pwm_p[0] == 0) |
| 179 | d |= P32XP_EMPTY; |
| 180 | break; |
| 181 | |
| 182 | case 6: // R ch |
| 183 | case 8: // MONO |
| 184 | if (Pico32x.pwm_p[1] == 3) |
| 185 | d |= P32XP_FULL; |
| 186 | else if (Pico32x.pwm_p[1] == 0) |
| 187 | d |= P32XP_EMPTY; |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)", |
| 192 | m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]); |
| 193 | return d; |
| 194 | } |
| 195 | |
| 196 | void p32x_pwm_write16(unsigned int a, unsigned int d, |
| 197 | SH2 *sh2, unsigned int m68k_cycles) |
| 198 | { |
| 199 | elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)", |
| 200 | m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]); |
| 201 | |
| 202 | consume_fifo(sh2, m68k_cycles); |
| 203 | |
| 204 | a &= 0x0e; |
| 205 | if (a == 0) { // control |
| 206 | // avoiding pops.. |
| 207 | if ((Pico32x.regs[0x30 / 2] & 0x0f) == 0) |
| 208 | Pico32xMem->pwm_fifo[0][0] = Pico32xMem->pwm_fifo[1][0] = 0; |
| 209 | Pico32x.regs[0x30 / 2] = d; |
| 210 | p32x_pwm_ctl_changed(); |
| 211 | Pico32x.pwm_irq_cnt = pwm_irq_reload; // ? |
| 212 | } |
| 213 | else if (a == 2) { // cycle |
| 214 | Pico32x.regs[0x32 / 2] = d & 0x0fff; |
| 215 | p32x_pwm_ctl_changed(); |
| 216 | } |
| 217 | else if (a <= 8) { |
| 218 | d = (d - 1) & 0x0fff; |
| 219 | |
| 220 | if (a == 4 || a == 8) { // L ch or MONO |
| 221 | unsigned short *fifo = Pico32xMem->pwm_fifo[0]; |
| 222 | if (Pico32x.pwm_p[0] < 3) |
| 223 | Pico32x.pwm_p[0]++; |
| 224 | else { |
| 225 | fifo[1] = fifo[2]; |
| 226 | fifo[2] = fifo[3]; |
| 227 | } |
| 228 | fifo[Pico32x.pwm_p[0]] = d; |
| 229 | } |
| 230 | if (a == 6 || a == 8) { // R ch or MONO |
| 231 | unsigned short *fifo = Pico32xMem->pwm_fifo[1]; |
| 232 | if (Pico32x.pwm_p[1] < 3) |
| 233 | Pico32x.pwm_p[1]++; |
| 234 | else { |
| 235 | fifo[1] = fifo[2]; |
| 236 | fifo[2] = fifo[3]; |
| 237 | } |
| 238 | fifo[Pico32x.pwm_p[1]] = d; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void p32x_pwm_update(int *buf32, int length, int stereo) |
| 244 | { |
| 245 | short *pwmb; |
| 246 | int step; |
| 247 | int p = 0; |
| 248 | int xmd; |
| 249 | |
| 250 | consume_fifo(NULL, SekCyclesDone()); |
| 251 | |
| 252 | xmd = Pico32x.regs[0x30 / 2] & 0x0f; |
| 253 | if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f) |
| 254 | goto out; // invalid? |
| 255 | if (pwm_silent) |
| 256 | return; |
| 257 | |
| 258 | step = (pwm_ptr << 16) / length; |
| 259 | pwmb = Pico32xMem->pwm; |
| 260 | |
| 261 | if (stereo) |
| 262 | { |
| 263 | if (xmd == 0x05) { |
| 264 | // normal |
| 265 | while (length-- > 0) { |
| 266 | *buf32++ += pwmb[0]; |
| 267 | *buf32++ += pwmb[1]; |
| 268 | |
| 269 | p += step; |
| 270 | pwmb += (p >> 16) * 2; |
| 271 | p &= 0xffff; |
| 272 | } |
| 273 | } |
| 274 | else if (xmd == 0x0a) { |
| 275 | // channel swap |
| 276 | while (length-- > 0) { |
| 277 | *buf32++ += pwmb[1]; |
| 278 | *buf32++ += pwmb[0]; |
| 279 | |
| 280 | p += step; |
| 281 | pwmb += (p >> 16) * 2; |
| 282 | p &= 0xffff; |
| 283 | } |
| 284 | } |
| 285 | else { |
| 286 | // mono - LMD, RMD specify dst |
| 287 | if (xmd & 0x06) // src is R |
| 288 | pwmb++; |
| 289 | if (xmd & 0x0c) // dst is R |
| 290 | buf32++; |
| 291 | while (length-- > 0) { |
| 292 | *buf32 += *pwmb; |
| 293 | |
| 294 | p += step; |
| 295 | pwmb += (p >> 16) * 2; |
| 296 | p &= 0xffff; |
| 297 | buf32 += 2; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | // mostly unused |
| 304 | while (length-- > 0) { |
| 305 | *buf32++ += pwmb[0]; |
| 306 | |
| 307 | p += step; |
| 308 | pwmb += (p >> 16) * 2; |
| 309 | p &= 0xffff; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d", |
| 314 | pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2); |
| 315 | |
| 316 | out: |
| 317 | pwm_ptr = 0; |
| 318 | pwm_silent = Pico32xMem->pwm_current[0] == 0 |
| 319 | && Pico32xMem->pwm_current[1] == 0; |
| 320 | } |
| 321 | |
| 322 | void p32x_pwm_state_loaded(void) |
| 323 | { |
| 324 | int cycles_diff_sh2; |
| 325 | |
| 326 | p32x_pwm_ctl_changed(); |
| 327 | |
| 328 | // for old savestates |
| 329 | cycles_diff_sh2 = SekCycleCnt * 3 - Pico32x.pwm_cycle_p; |
| 330 | if (cycles_diff_sh2 >= pwm_cycles || cycles_diff_sh2 < 0) { |
| 331 | Pico32x.pwm_irq_cnt = pwm_irq_reload; |
| 332 | Pico32x.pwm_cycle_p = SekCycleCnt * 3; |
| 333 | p32x_pwm_schedule(SekCycleCnt); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // vim:shiftwidth=2:ts=2:expandtab |