X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pico%2F32x%2Fpwm.c;h=6fe7954cece6fe917e4a299e3bf9152ecf5a28ed;hb=8ad1d2adf2feb934a687025402ceed4689337fac;hp=dc7c4d6b99ee550227219635b6a6c4e288f23029;hpb=045a4c528a71e5513acc2cfb8a77e31efcad5d87;p=picodrive.git diff --git a/pico/32x/pwm.c b/pico/32x/pwm.c index dc7c4d6..6fe7954 100644 --- a/pico/32x/pwm.c +++ b/pico/32x/pwm.c @@ -11,6 +11,7 @@ static int pwm_cycles; static int pwm_mult; static int pwm_ptr; static int pwm_irq_reload; +static int pwm_doing_fifo; void p32x_pwm_ctl_changed(void) { @@ -28,10 +29,10 @@ void p32x_pwm_ctl_changed(void) Pico32x.pwm_irq_cnt = pwm_irq_reload; } -static void do_pwm_irq(unsigned int m68k_cycles) +static void do_pwm_irq(SH2 *sh2, unsigned int m68k_cycles) { Pico32x.sh2irqs |= P32XI_PWM; - p32x_update_irls(NULL); + p32x_update_irls(sh2, m68k_cycles); if (Pico32x.regs[0x30 / 2] & P32XP_RTP) { p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1); @@ -40,29 +41,24 @@ static void do_pwm_irq(unsigned int m68k_cycles) } } -#define consume_fifo(m68k_cycles) { \ +#define consume_fifo(sh2, m68k_cycles) { \ int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \ if (cycles_diff >= pwm_cycles) \ - consume_fifo_do(m68k_cycles, cycles_diff); \ + consume_fifo_do(sh2, m68k_cycles, cycles_diff); \ } -static void consume_fifo_do(unsigned int m68k_cycles, int sh2_cycles_diff) +static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles, + int sh2_cycles_diff) { - int do_irq = 0; - - if (pwm_cycles == 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); - if (sh2_cycles_diff >= pwm_cycles * 17) { - // silence/skip - Pico32x.pwm_cycle_p = m68k_cycles * 3; - Pico32x.pwm_p[0] = Pico32x.pwm_p[1] = 0; - return; - } + // this is for recursion from dreq1 writes + pwm_doing_fifo = 1; while (sh2_cycles_diff >= pwm_cycles) { struct Pico32xMem *mem = Pico32xMem; @@ -90,18 +86,14 @@ static void consume_fifo_do(unsigned int m68k_cycles, int sh2_cycles_diff) if (--Pico32x.pwm_irq_cnt == 0) { Pico32x.pwm_irq_cnt = pwm_irq_reload; - // irq also does dreq1, so call it after cycle update - do_irq = 1; - break; + do_pwm_irq(sh2, m68k_cycles); } } Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff; - - if (do_irq) - do_pwm_irq(m68k_cycles); + pwm_doing_fifo = 0; } -static int p32x_pwm_schedule_(unsigned int m68k_now) +static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now) { unsigned int sh2_now = m68k_now * 3; int cycles_diff_sh2; @@ -111,7 +103,7 @@ static int p32x_pwm_schedule_(unsigned int m68k_now) cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p; if (cycles_diff_sh2 >= pwm_cycles) - consume_fifo_do(m68k_now, cycles_diff_sh2); + consume_fifo_do(sh2, m68k_now, cycles_diff_sh2); if (Pico32x.sh2irqs & P32XI_PWM) return 0; // previous not acked @@ -125,14 +117,14 @@ static int p32x_pwm_schedule_(unsigned int m68k_now) void p32x_pwm_schedule(unsigned int m68k_now) { - int after = p32x_pwm_schedule_(m68k_now); + int after = p32x_pwm_schedule_(NULL, m68k_now); if (after != 0) p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after); } void p32x_pwm_schedule_sh2(SH2 *sh2) { - int after = p32x_pwm_schedule_(sh2_cycles_done_m68k(sh2)); + int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2)); if (after != 0) p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after); } @@ -142,11 +134,12 @@ void p32x_pwm_irq_event(unsigned int m68k_now) p32x_pwm_schedule(m68k_now); } -unsigned int p32x_pwm_read16(unsigned int a, unsigned int m68k_cycles) +unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2, + unsigned int m68k_cycles) { unsigned int d = 0; - consume_fifo(m68k_cycles); + consume_fifo(sh2, m68k_cycles); a &= 0x0e; switch (a) { @@ -177,12 +170,12 @@ unsigned int p32x_pwm_read16(unsigned int a, unsigned int m68k_cycles) } void p32x_pwm_write16(unsigned int a, unsigned int d, - unsigned int m68k_cycles) + 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(m68k_cycles); + consume_fifo(sh2, m68k_cycles); a &= 0x0e; if (a == 0) { // control @@ -232,16 +225,29 @@ void p32x_pwm_update(int *buf32, int length, int stereo) int p = 0; int xmd; + consume_fifo(NULL, SekCyclesDoneT2()); + xmd = Pico32x.regs[0x30 / 2] & 0x0f; - if ((xmd != 0x05 && xmd != 0x0a) || pwm_ptr <= 16) - goto out; + if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f) + goto out; // invalid? - step = (pwm_ptr << 16) / length; // FIXME: division.. + step = (pwm_ptr << 16) / length; pwmb = Pico32xMem->pwm; if (stereo) { - if (xmd == 0x0a) { + if (xmd == 0x05) { + // normal + while (length-- > 0) { + *buf32++ += pwmb[0]; + *buf32++ += pwmb[1]; + + p += step; + pwmb += (p >> 16) * 2; + p &= 0xffff; + } + } + else if (xmd == 0x0a) { // channel swap while (length-- > 0) { *buf32++ += pwmb[1]; @@ -253,18 +259,24 @@ void p32x_pwm_update(int *buf32, int length, int stereo) } } 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[0]; - *buf32++ += pwmb[1]; + *buf32 += *pwmb; p += step; pwmb += (p >> 16) * 2; p &= 0xffff; + buf32 += 2; } } } else { + // mostly unused while (length-- > 0) { *buf32++ += pwmb[0];