X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2F32x%2Fpwm.c;h=36261d7f973895bfb25d4bca0898c925feb812c2;hb=9c9cda8c39bd2a6b99b8420a3034c454bc713954;hp=3adbf211f39de0db98a956f6c46b1d69244e15ba;hpb=a8fd6e376175c06e2423d0914359c761829d6e93;p=picodrive.git diff --git a/pico/32x/pwm.c b/pico/32x/pwm.c index 3adbf21..36261d7 100644 --- a/pico/32x/pwm.c +++ b/pico/32x/pwm.c @@ -52,10 +52,15 @@ void p32x_timers_do(unsigned int cycles) cycles *= 3; - pwm_cycle_counter += cycles; - while (pwm_cycle_counter > pwm_cycles) { - pwm_cycle_counter -= pwm_cycles; - pwm_smp_expect++; + // 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++; + } } // WDT timers @@ -81,21 +86,35 @@ void p32x_timers_do(unsigned int cycles) } } -void p32x_pwm_schedule(unsigned int now) +static int p32x_pwm_schedule_(void) { int tm; if (Pico32x.emu_flags & P32XF_PWM_PEND) - return; // already scheduled + return 0; // already scheduled if (Pico32x.sh2irqs & P32XI_PWM) - return; // previous not acked + return 0; // previous not acked if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1)) - return; // masked by everyone + return 0; // masked by everyone + Pico32x.emu_flags |= P32XF_PWM_PEND; tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8; tm = ((tm - 1) & 0x0f) + 1; - p32x_event_schedule(P32X_EVENT_PWM, now, pwm_cycles * tm / 3); - Pico32x.emu_flags |= P32XF_PWM_PEND; + return pwm_cycles * tm / 3; +} + +void p32x_pwm_schedule(unsigned int now) +{ + int after = p32x_pwm_schedule_(); + if (after != 0) + p32x_event_schedule(now, P32X_EVENT_PWM, after); +} + +void p32x_pwm_schedule_sh2(SH2 *sh2) +{ + int after = p32x_pwm_schedule_(); + if (after != 0) + p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after); } unsigned int p32x_pwm_read16(unsigned int a)