X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2F32x%2Fpwm.c;h=6f9cd07eedd99eac7b4cef51d9d1a5c42cf61d8d;hb=8ce9c3a782e47ed355a12789859e5359c4a81e77;hp=36261d7f973895bfb25d4bca0898c925feb812c2;hpb=19886062f1a36f70b1f01d58f3fa1b79162defac;p=picodrive.git diff --git a/pico/32x/pwm.c b/pico/32x/pwm.c index 36261d7..6f9cd07 100644 --- a/pico/32x/pwm.c +++ b/pico/32x/pwm.c @@ -1,177 +1,241 @@ /* * PicoDrive - * (C) notaz, 2009,2010 + * (C) notaz, 2009,2010,2013 * * This work is licensed under the terms of MAME license. * See COPYING file in the top-level directory. */ #include "../pico_int.h" -static int pwm_cycle_counter; static int pwm_cycles; static int pwm_mult; static int pwm_ptr; +static int pwm_irq_reload; +static int pwm_doing_fifo; +static int pwm_silent; -static int pwm_smp_cnt; -static int pwm_smp_expect; - -static int timer_cycles[2]; -static int timer_tick_cycles[2]; - -// timers. This includes PWM timer in 32x and internal SH2 timers -void p32x_timers_recalc(void) +void p32x_pwm_ctl_changed(void) { + int control = Pico32x.regs[0x30 / 2]; int cycles = Pico32x.regs[0x32 / 2]; - int tmp, i; cycles = (cycles - 1) & 0x0fff; - if (cycles < 500) { - elprintf(EL_32X|EL_PWM|EL_ANOMALY, "pwm: low cycle value: %d", cycles + 1); - cycles = 500; - } pwm_cycles = cycles; - pwm_mult = 0x10000 / cycles; - - // SH2 timer step - for (i = 0; i < 2; i++) { - tmp = PREG8(Pico32xMem->sh2_peri_regs[i], 0x80) & 7; - // Sclk cycles per timer tick - if (tmp) - cycles = 0x20 << tmp; - else - cycles = 2; - timer_tick_cycles[i] = cycles; - elprintf(EL_32X, "WDT cycles[%d] = %d", i, cycles); + + // supposedly we should stop FIFO when xMd is 0, + // but mars test disagrees + pwm_mult = 0; + if ((control & 0x0f) != 0) + pwm_mult = 0x10000 / cycles; + + pwm_irq_reload = (control & 0x0f00) >> 8; + pwm_irq_reload = ((pwm_irq_reload - 1) & 0x0f) + 1; + + if (Pico32x.pwm_irq_cnt == 0) + Pico32x.pwm_irq_cnt = pwm_irq_reload; +} + +static void do_pwm_irq(SH2 *sh2, unsigned int m68k_cycles) +{ + p32x_trigger_irq(sh2, m68k_cycles, P32XI_PWM); + + if (Pico32x.regs[0x30 / 2] & P32XP_RTP) { + p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1); + // note: might recurse + p32x_dreq1_trigger(); } } -// PWM irq for every tm samples -void p32x_timers_do(unsigned int cycles) +static int convert_sample(unsigned int v) +{ + if (v == 0) + return 0; + if (v > pwm_cycles) + v = pwm_cycles; + return ((int)v - pwm_cycles / 2) * pwm_mult; +} + +#define consume_fifo(sh2, m68k_cycles) { \ + int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \ + if (cycles_diff >= pwm_cycles) \ + consume_fifo_do(sh2, m68k_cycles, cycles_diff); \ +} + +static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles, + int sh2_cycles_diff) { - int cnt, i; - - cycles *= 3; - - // since we run things in async fashion, allow pwm to lag behind - // but don't allow our "queue" to be infinite - cnt = pwm_smp_expect - pwm_smp_cnt; - if (cnt <= 0 || cnt * pwm_cycles < OSC_NTSC/7*3 / 60 / 2) { - pwm_cycle_counter += cycles; - while (pwm_cycle_counter > pwm_cycles) { - pwm_cycle_counter -= pwm_cycles; - pwm_smp_expect++; + struct Pico32xMem *mem = Pico32xMem; + unsigned short *fifo_l = mem->pwm_fifo[0]; + unsigned short *fifo_r = mem->pwm_fifo[1]; + int sum = 0; + + if (pwm_cycles == 0 || pwm_doing_fifo) + return; + + elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d", + m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm_cycles, + Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm_ptr); + + // this is for recursion from dreq1 writes + pwm_doing_fifo = 1; + + for (; sh2_cycles_diff >= pwm_cycles; sh2_cycles_diff -= pwm_cycles) + { + if (Pico32x.pwm_p[0] > 0) { + fifo_l[0] = fifo_l[1]; + fifo_l[1] = fifo_l[2]; + fifo_l[2] = fifo_l[3]; + Pico32x.pwm_p[0]--; + mem->pwm_current[0] = convert_sample(fifo_l[0]); + sum += mem->pwm_current[0]; + } + if (Pico32x.pwm_p[1] > 0) { + fifo_r[0] = fifo_r[1]; + fifo_r[1] = fifo_r[2]; + fifo_r[2] = fifo_r[3]; + Pico32x.pwm_p[1]--; + mem->pwm_current[1] = convert_sample(fifo_r[0]); + sum += mem->pwm_current[1]; } - } - // WDT timers - for (i = 0; i < 2; i++) { - void *pregs = Pico32xMem->sh2_peri_regs[i]; - if (PREG8(pregs, 0x80) & 0x20) { // TME - timer_cycles[i] += cycles; - cnt = PREG8(pregs, 0x81); - while (timer_cycles[i] >= timer_tick_cycles[i]) { - timer_cycles[i] -= timer_tick_cycles[i]; - cnt++; - } - if (cnt >= 0x100) { - int level = PREG8(pregs, 0xe3) >> 4; - int vector = PREG8(pregs, 0xe4) & 0x7f; - elprintf(EL_32X, "%csh2 WDT irq (%d, %d)", - i ? 's' : 'm', level, vector); - sh2_internal_irq(&sh2s[i], level, vector); - cnt &= 0xff; - } - PREG8(pregs, 0x81) = cnt; + mem->pwm[pwm_ptr * 2 ] = mem->pwm_current[0]; + mem->pwm[pwm_ptr * 2 + 1] = mem->pwm_current[1]; + pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1); + + if (--Pico32x.pwm_irq_cnt == 0) { + Pico32x.pwm_irq_cnt = pwm_irq_reload; + do_pwm_irq(sh2, m68k_cycles); } } + Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff; + pwm_doing_fifo = 0; + if (sum != 0) + pwm_silent = 0; } -static int p32x_pwm_schedule_(void) +static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now) { - int tm; + unsigned int sh2_now = m68k_now * 3; + int cycles_diff_sh2; + + if (pwm_cycles == 0) + return 0; + + cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p; + if (cycles_diff_sh2 >= pwm_cycles) + consume_fifo_do(sh2, m68k_now, cycles_diff_sh2); - if (Pico32x.emu_flags & P32XF_PWM_PEND) - return 0; // already scheduled - if (Pico32x.sh2irqs & P32XI_PWM) - return 0; // previous not acked if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1)) return 0; // masked by everyone - Pico32x.emu_flags |= P32XF_PWM_PEND; - tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8; - tm = ((tm - 1) & 0x0f) + 1; - return pwm_cycles * tm / 3; + cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p; + return (Pico32x.pwm_irq_cnt * pwm_cycles + - cycles_diff_sh2) / 3 + 1; } -void p32x_pwm_schedule(unsigned int now) +void p32x_pwm_schedule(unsigned int m68k_now) { - int after = p32x_pwm_schedule_(); + int after = p32x_pwm_schedule_(NULL, m68k_now); if (after != 0) - p32x_event_schedule(now, P32X_EVENT_PWM, after); + p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after); } void p32x_pwm_schedule_sh2(SH2 *sh2) { - int after = p32x_pwm_schedule_(); + int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2)); if (after != 0) p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after); } -unsigned int p32x_pwm_read16(unsigned int a) +void p32x_pwm_sync_to_sh2(SH2 *sh2) +{ + int m68k_cycles = sh2_cycles_done_m68k(sh2); + consume_fifo(sh2, m68k_cycles); +} + +void p32x_pwm_irq_event(unsigned int m68k_now) +{ + p32x_pwm_schedule(m68k_now); +} + +unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2, + unsigned int m68k_cycles) { unsigned int d = 0; - int diff; + + consume_fifo(sh2, m68k_cycles); a &= 0x0e; switch (a) { case 0: // control case 2: // cycle - return Pico32x.regs[(0x30 + a) / 2]; + d = Pico32x.regs[(0x30 + a) / 2]; + break; case 4: // L ch + if (Pico32x.pwm_p[0] == 3) + d |= P32XP_FULL; + else if (Pico32x.pwm_p[0] == 0) + d |= P32XP_EMPTY; + break; + case 6: // R ch case 8: // MONO - diff = pwm_smp_cnt - pwm_smp_expect; - elprintf(EL_PWM, "pwm: read status: ptr %d/%d %d", - pwm_smp_cnt, pwm_smp_expect, diff); - if (diff > 3) + if (Pico32x.pwm_p[1] == 3) d |= P32XP_FULL; - else if (diff < 0) + else if (Pico32x.pwm_p[1] == 0) d |= P32XP_EMPTY; break; } + elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)", + m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]); return d; } -void p32x_pwm_write16(unsigned int a, unsigned int d) +void p32x_pwm_write16(unsigned int a, unsigned int d, + SH2 *sh2, unsigned int m68k_cycles) { + elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)", + m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]); + + consume_fifo(sh2, m68k_cycles); + a &= 0x0e; - if (a == 0) // control + if (a == 0) { // control + // avoiding pops.. + if ((Pico32x.regs[0x30 / 2] & 0x0f) == 0) + Pico32xMem->pwm_fifo[0][0] = Pico32xMem->pwm_fifo[1][0] = 0; Pico32x.regs[0x30 / 2] = d; + p32x_pwm_ctl_changed(); + Pico32x.pwm_irq_cnt = pwm_irq_reload; // ? + } else if (a == 2) { // cycle Pico32x.regs[0x32 / 2] = d & 0x0fff; - p32x_timers_recalc(); - Pico32x.pwm_irq_sample_cnt = 0; // resets? + p32x_pwm_ctl_changed(); } else if (a <= 8) { - d &= 0x0fff; - if (d > pwm_cycles) - d = pwm_cycles; - d = (d - pwm_cycles / 2) * pwm_mult; - - if (a < 6) // L ch - Pico32xMem->pwm[pwm_ptr * 2] = d; - else if (a == 6) // R ch - Pico32xMem->pwm[pwm_ptr * 2 + 1] = d; - else // MONO - Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d; - - if (a >= 6) { // R or MONO - pwm_smp_cnt++; - pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1); - elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x", - pwm_smp_cnt, pwm_ptr, d); + d = (d - 1) & 0x0fff; + + if (a == 4 || a == 8) { // L ch or MONO + unsigned short *fifo = Pico32xMem->pwm_fifo[0]; + if (Pico32x.pwm_p[0] < 3) + Pico32x.pwm_p[0]++; + else { + fifo[1] = fifo[2]; + fifo[2] = fifo[3]; + } + fifo[Pico32x.pwm_p[0]] = d; + } + if (a == 6 || a == 8) { // R ch or MONO + unsigned short *fifo = Pico32xMem->pwm_fifo[1]; + if (Pico32x.pwm_p[1] < 3) + Pico32x.pwm_p[1]++; + else { + fifo[1] = fifo[2]; + fifo[2] = fifo[3]; + } + fifo[Pico32x.pwm_p[1]] = d; } } } @@ -181,26 +245,62 @@ void p32x_pwm_update(int *buf32, int length, int stereo) short *pwmb; int step; int p = 0; + int xmd; + + consume_fifo(NULL, SekCyclesDoneT2()); - if (pwm_ptr <= 16) // at least some samples.. + xmd = Pico32x.regs[0x30 / 2] & 0x0f; + if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f) + goto out; // invalid? + if (pwm_silent) return; - step = (pwm_ptr << 16) / length; // FIXME: division.. + step = (pwm_ptr << 16) / length; pwmb = Pico32xMem->pwm; if (stereo) { - while (length-- > 0) { - *buf32++ += pwmb[0]; - *buf32++ += pwmb[1]; + if (xmd == 0x05) { + // normal + while (length-- > 0) { + *buf32++ += pwmb[0]; + *buf32++ += pwmb[1]; - p += step; - pwmb += (p >> 16) * 2; - p &= 0xffff; + p += step; + pwmb += (p >> 16) * 2; + p &= 0xffff; + } + } + else if (xmd == 0x0a) { + // channel swap + while (length-- > 0) { + *buf32++ += pwmb[1]; + *buf32++ += pwmb[0]; + + p += step; + pwmb += (p >> 16) * 2; + p &= 0xffff; + } + } + else { + // mono - LMD, RMD specify dst + if (xmd & 0x06) // src is R + pwmb++; + if (xmd & 0x0c) // dst is R + buf32++; + while (length-- > 0) { + *buf32 += *pwmb; + + p += step; + pwmb += (p >> 16) * 2; + p &= 0xffff; + buf32 += 2; + } } } else { + // mostly unused while (length-- > 0) { *buf32++ += pwmb[0]; @@ -213,7 +313,25 @@ void p32x_pwm_update(int *buf32, int length, int stereo) elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d", pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2); +out: pwm_ptr = 0; + pwm_silent = Pico32xMem->pwm_current[0] == 0 + && Pico32xMem->pwm_current[1] == 0; +} + +void p32x_pwm_state_loaded(void) +{ + int cycles_diff_sh2; + + p32x_pwm_ctl_changed(); + + // for old savestates + cycles_diff_sh2 = SekCycleCntT * 3 - Pico32x.pwm_cycle_p; + if (cycles_diff_sh2 >= pwm_cycles || cycles_diff_sh2 < 0) { + Pico32x.pwm_irq_cnt = pwm_irq_reload; + Pico32x.pwm_cycle_p = SekCycleCntT * 3; + p32x_pwm_schedule(SekCycleCntT); + } } // vim:shiftwidth=2:ts=2:expandtab