32x: fix some more timing problems

This commit is contained in:
notaz 2013-08-07 01:46:45 +03:00
parent 4d5dfee861
commit c1931173ab
4 changed files with 38 additions and 28 deletions

View file

@ -29,6 +29,7 @@ static int REGPARM(2) sh2_irq_cb(SH2 *sh2, int level)
} }
} }
// MUST specify active_sh2 when called from sh2 memhandlers
void p32x_update_irls(SH2 *active_sh2, int m68k_cycles) void p32x_update_irls(SH2 *active_sh2, int m68k_cycles)
{ {
int irqs, mlvl = 0, slvl = 0; int irqs, mlvl = 0, slvl = 0;
@ -49,13 +50,19 @@ void p32x_update_irls(SH2 *active_sh2, int m68k_cycles)
slvl++; slvl++;
slvl *= 2; slvl *= 2;
mrun = sh2_irl_irq(&msh2, mlvl, active_sh2 != NULL); mrun = sh2_irl_irq(&msh2, mlvl, active_sh2 == &msh2);
if (mrun) if (mrun) {
p32x_sh2_poll_event(&msh2, SH2_IDLE_STATES, m68k_cycles); p32x_sh2_poll_event(&msh2, SH2_IDLE_STATES, m68k_cycles);
if (active_sh2 == &msh2)
sh2_end_run(active_sh2, 1);
}
srun = sh2_irl_irq(&ssh2, slvl, active_sh2 != NULL); srun = sh2_irl_irq(&ssh2, slvl, active_sh2 == &ssh2);
if (srun) if (srun) {
p32x_sh2_poll_event(&ssh2, SH2_IDLE_STATES, m68k_cycles); p32x_sh2_poll_event(&ssh2, SH2_IDLE_STATES, m68k_cycles);
if (active_sh2 == &ssh2)
sh2_end_run(active_sh2, 1);
}
elprintf(EL_32X, "update_irls: m %d/%d, s %d/%d", mlvl, mrun, slvl, srun); elprintf(EL_32X, "update_irls: m %d/%d, s %d/%d", mlvl, mrun, slvl, srun);
} }
@ -268,8 +275,7 @@ void p32x_event_schedule_sh2(SH2 *sh2, enum p32x_event event, int after)
p32x_event_schedule(now, event, after); p32x_event_schedule(now, event, after);
left_to_next = (event_time_next - now) * 3; left_to_next = (event_time_next - now) * 3;
if (sh2_cycles_left(sh2) > left_to_next) sh2_end_run(sh2, left_to_next);
sh2_end_run(sh2, left_to_next);
} }
static void run_events(unsigned int until) static void run_events(unsigned int until)

View file

@ -213,7 +213,7 @@ static u32 p32x_reg_read16(u32 a)
} }
if ((a & 0x30) == 0x30) if ((a & 0x30) == 0x30)
return p32x_pwm_read16(a, SekCyclesDoneT()); return p32x_pwm_read16(a, NULL, SekCyclesDoneT());
out: out:
return Pico32x.regs[a / 2]; return Pico32x.regs[a / 2];
@ -352,7 +352,7 @@ static void p32x_reg_write16(u32 a, u32 d)
} }
// PWM // PWM
else if ((a & 0x30) == 0x30) { else if ((a & 0x30) == 0x30) {
p32x_pwm_write16(a, d, SekCyclesDoneT()); p32x_pwm_write16(a, d, NULL, SekCyclesDoneT());
return; return;
} }
@ -462,7 +462,7 @@ static u32 p32x_sh2reg_read16(u32 a, int cpuid)
return r[a / 2]; return r[a / 2];
} }
if ((a & 0x30) == 0x30) { if ((a & 0x30) == 0x30) {
return p32x_pwm_read16(a, sh2_cycles_done_m68k(&sh2s[cpuid])); return p32x_pwm_read16(a, &sh2s[cpuid], sh2_cycles_done_m68k(&sh2s[cpuid]));
} }
return 0; return 0;
@ -538,7 +538,7 @@ static void p32x_sh2reg_write16(u32 a, u32 d, int cpuid)
} }
// PWM // PWM
else if ((a & 0x30) == 0x30) { else if ((a & 0x30) == 0x30) {
p32x_pwm_write16(a, d, sh2_cycles_done_m68k(&sh2s[cpuid])); p32x_pwm_write16(a, d, &sh2s[cpuid], sh2_cycles_done_m68k(&sh2s[cpuid]));
return; return;
} }

View file

@ -28,10 +28,10 @@ void p32x_pwm_ctl_changed(void)
Pico32x.pwm_irq_cnt = pwm_irq_reload; 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; Pico32x.sh2irqs |= P32XI_PWM;
p32x_update_irls(NULL, m68k_cycles); p32x_update_irls(sh2, m68k_cycles);
if (Pico32x.regs[0x30 / 2] & P32XP_RTP) { if (Pico32x.regs[0x30 / 2] & P32XP_RTP) {
p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1); p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1);
@ -40,13 +40,14 @@ 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; \ int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \
if (cycles_diff >= pwm_cycles) \ 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; int do_irq = 0;
@ -98,10 +99,10 @@ static void consume_fifo_do(unsigned int m68k_cycles, int sh2_cycles_diff)
Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff; Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
if (do_irq) if (do_irq)
do_pwm_irq(m68k_cycles); do_pwm_irq(sh2, m68k_cycles);
} }
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; unsigned int sh2_now = m68k_now * 3;
int cycles_diff_sh2; int cycles_diff_sh2;
@ -111,7 +112,7 @@ static int p32x_pwm_schedule_(unsigned int m68k_now)
cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p; cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
if (cycles_diff_sh2 >= pwm_cycles) 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) if (Pico32x.sh2irqs & P32XI_PWM)
return 0; // previous not acked return 0; // previous not acked
@ -125,14 +126,14 @@ static int p32x_pwm_schedule_(unsigned int m68k_now)
void 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) if (after != 0)
p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after); p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
} }
void p32x_pwm_schedule_sh2(SH2 *sh2) 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) if (after != 0)
p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after); p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
} }
@ -142,11 +143,12 @@ void p32x_pwm_irq_event(unsigned int m68k_now)
p32x_pwm_schedule(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; unsigned int d = 0;
consume_fifo(m68k_cycles); consume_fifo(sh2, m68k_cycles);
a &= 0x0e; a &= 0x0e;
switch (a) { switch (a) {
@ -177,12 +179,12 @@ unsigned int p32x_pwm_read16(unsigned int a, unsigned int m68k_cycles)
} }
void p32x_pwm_write16(unsigned int a, unsigned int d, 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)", elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]); m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
consume_fifo(m68k_cycles); consume_fifo(sh2, m68k_cycles);
a &= 0x0e; a &= 0x0e;
if (a == 0) { // control if (a == 0) { // control

View file

@ -240,7 +240,7 @@ extern SH2 sh2s[2];
#ifndef DRC_SH2 #ifndef DRC_SH2
# define sh2_end_run(sh2, after_) do { \ # define sh2_end_run(sh2, after_) do { \
if ((sh2)->icount > (after_)) { \ if ((sh2)->icount > (after_)) { \
(sh2)->cycles_timeslice -= (sh2)->icount; \ (sh2)->cycles_timeslice -= (sh2)->icount - (after_); \
(sh2)->icount = after_; \ (sh2)->icount = after_; \
} \ } \
} while (0) } while (0)
@ -250,7 +250,7 @@ extern SH2 sh2s[2];
# define sh2_end_run(sh2, after_) do { \ # define sh2_end_run(sh2, after_) do { \
int left_ = (signed int)(sh2)->sr >> 12; \ int left_ = (signed int)(sh2)->sr >> 12; \
if (left_ > (after_)) { \ if (left_ > (after_)) { \
(sh2)->cycles_timeslice -= left_; \ (sh2)->cycles_timeslice -= left_ - (after_); \
(sh2)->sr &= 0xfff; \ (sh2)->sr &= 0xfff; \
(sh2)->sr |= (after_) << 12; \ (sh2)->sr |= (after_) << 12; \
} \ } \
@ -778,8 +778,10 @@ enum {
extern int Pico32xDrawMode; extern int Pico32xDrawMode;
// 32x/pwm.c // 32x/pwm.c
unsigned int p32x_pwm_read16(unsigned int a, unsigned int cycles); unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2,
void p32x_pwm_write16(unsigned int a, unsigned int d, unsigned int cycles); unsigned int m68k_cycles);
void p32x_pwm_write16(unsigned int a, unsigned int d,
SH2 *sh2, unsigned int m68k_cycles);
void p32x_pwm_update(int *buf32, int length, int stereo); void p32x_pwm_update(int *buf32, int length, int stereo);
void p32x_pwm_ctl_changed(void); void p32x_pwm_ctl_changed(void);
void p32x_pwm_schedule(unsigned int m68k_now); void p32x_pwm_schedule(unsigned int m68k_now);