3 * (C) notaz, 2009,2010,2013
4 * (C) irixxxx, 2019-2023
6 * This work is licensed under the terms of MAME license.
7 * See COPYING file in the top-level directory.
9 #include "../pico_int.h"
23 enum { PWM_IRQ_LOCKED, PWM_IRQ_STOPPED, PWM_IRQ_LOW, PWM_IRQ_HIGH };
25 void p32x_pwm_ctl_changed(void)
27 int control = Pico32x.regs[0x30 / 2];
28 int cycles = Pico32x.regs[0x32 / 2];
29 int pwm_irq_opt = PicoIn.opt & POPT_PWM_IRQ_OPT;
31 cycles = (cycles - 1) & 0x0fff;
34 // supposedly we should stop FIFO when xMd is 0,
35 // but mars test disagrees
37 if ((control & 0x0f) != 0)
38 pwm.mult = (0x10000<<8) / (cycles+1);
40 pwm.irq_timer = (control & 0x0f00) >> 8;
41 pwm.irq_timer = ((pwm.irq_timer - 1) & 0x0f) + 1;
42 pwm.irq_reload = pwm.irq_timer;
43 pwm.irq_state = pwm_irq_opt ? PWM_IRQ_STOPPED: PWM_IRQ_LOCKED;
45 if (Pico32x.pwm_irq_cnt <= 0)
46 Pico32x.pwm_irq_cnt = pwm.irq_reload;
49 static void do_pwm_irq(SH2 *sh2, unsigned int m68k_cycles)
51 p32x_trigger_irq(NULL, m68k_cycles, P32XI_PWM);
53 if (Pico32x.regs[0x30 / 2] & P32XP_RTP) {
54 p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm.cycles / 3 + 1);
55 // note: might recurse
60 static int convert_sample(unsigned int v)
64 return (v * pwm.mult >> 8) - 0x10000/2;
67 #define consume_fifo(sh2, m68k_cycles) { \
68 int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \
69 if (cycles_diff >= pwm.cycles) \
70 consume_fifo_do(sh2, m68k_cycles, cycles_diff); \
73 static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles,
76 struct Pico32xMem *mem = Pico32xMem;
77 unsigned short *fifo_l = mem->pwm_fifo[0];
78 unsigned short *fifo_r = mem->pwm_fifo[1];
81 if (pwm.cycles == 0 || pwm.doing_fifo)
84 elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d",
85 m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm.cycles,
86 Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm.ptr);
88 // this is for recursion from dreq1 writes
91 while (sh2_cycles_diff >= pwm.cycles)
93 sh2_cycles_diff -= pwm.cycles;
95 if (Pico32x.pwm_p[0] > 0) {
96 mem->pwm_index[0] = (mem->pwm_index[0]+1) % 4;
98 pwm.current[0] = convert_sample(fifo_l[mem->pwm_index[0]]);
99 sum |= (u16)pwm.current[0];
101 if (Pico32x.pwm_p[1] > 0) {
102 mem->pwm_index[1] = (mem->pwm_index[1]+1) % 4;
104 pwm.current[1] = convert_sample(fifo_r[mem->pwm_index[1]]);
105 sum |= (u16)pwm.current[1];
108 mem->pwm[pwm.ptr * 2 ] = pwm.current[0];
109 mem->pwm[pwm.ptr * 2 + 1] = pwm.current[1];
110 pwm.ptr = (pwm.ptr + 1) & (PWM_BUFF_LEN - 1);
112 if (--Pico32x.pwm_irq_cnt <= 0) {
113 Pico32x.pwm_irq_cnt = pwm.irq_reload;
114 do_pwm_irq(sh2, m68k_cycles);
115 } else if (Pico32x.pwm_p[1] == 0 && pwm.irq_state >= PWM_IRQ_LOW) {
116 // buffer underrun. Reduce reload rate if above programmed setting.
117 if (pwm.irq_reload > pwm.irq_timer)
119 pwm.irq_state = PWM_IRQ_LOW;
122 Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
128 static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now)
130 unsigned int pwm_now = m68k_now * 3;
136 cycles_diff_sh2 = pwm_now - Pico32x.pwm_cycle_p;
137 if (cycles_diff_sh2 >= pwm.cycles)
138 consume_fifo_do(sh2, m68k_now, cycles_diff_sh2);
140 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
141 return 0; // masked by everyone
143 cycles_diff_sh2 = pwm_now - Pico32x.pwm_cycle_p;
144 return (Pico32x.pwm_irq_cnt * pwm.cycles
145 - cycles_diff_sh2) / 3 + 1;
148 void p32x_pwm_schedule(unsigned int m68k_now)
150 int after = p32x_pwm_schedule_(NULL, m68k_now);
152 p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
155 void p32x_pwm_schedule_sh2(SH2 *sh2)
157 int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2));
159 p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
162 void p32x_pwm_sync_to_sh2(SH2 *sh2)
164 int m68k_cycles = sh2_cycles_done_m68k(sh2);
165 consume_fifo(sh2, m68k_cycles);
168 void p32x_pwm_irq_event(unsigned int m68k_now)
170 p32x_pwm_schedule(m68k_now);
173 unsigned int p32x_pwm_read16(u32 a, SH2 *sh2, unsigned int m68k_cycles)
177 consume_fifo(sh2, m68k_cycles);
183 d = Pico32x.regs[(0x30 + a) / 2];
187 if (Pico32x.pwm_p[0] == 3)
189 else if (Pico32x.pwm_p[0] == 0)
195 if (Pico32x.pwm_p[1] == 3)
197 else if (Pico32x.pwm_p[1] == 0)
202 elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)",
203 m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
207 void p32x_pwm_write16(u32 a, unsigned int d, SH2 *sh2, unsigned int m68k_cycles)
209 unsigned short *fifo;
212 elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
213 m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
215 consume_fifo(sh2, m68k_cycles);
221 if ((Pico32x.regs[0x30 / 2] & 0x0f) == 0)
222 Pico32xMem->pwm_fifo[0][0] = Pico32xMem->pwm_fifo[1][0] = 0;
223 Pico32x.regs[0x30 / 2] = d;
224 p32x_pwm_ctl_changed();
225 Pico32x.pwm_irq_cnt = pwm.irq_reload; // ?
228 Pico32x.regs[0x32 / 2] = d & 0x0fff;
229 p32x_pwm_ctl_changed();
233 fifo = Pico32xMem->pwm_fifo[1];
234 idx = Pico32xMem->pwm_index[1];
235 if (Pico32x.pwm_p[1] < 3) {
236 if (Pico32x.pwm_p[1] == 2 && pwm.irq_state >= PWM_IRQ_STOPPED) {
237 // buffer full. If there was no buffer underrun after last fill,
238 // try increasing reload rate to reduce IRQs
239 if (pwm.irq_reload < 3 && pwm.irq_state == PWM_IRQ_HIGH)
241 pwm.irq_state = PWM_IRQ_HIGH;
245 // buffer overflow. Some roms always fill the complete buffer even if
246 // reload rate is set below max. Lock reload rate to programmed setting.
247 pwm.irq_reload = pwm.irq_timer;
248 pwm.irq_state = PWM_IRQ_LOCKED;
250 Pico32xMem->pwm_index[1] = idx;
252 fifo[(idx+Pico32x.pwm_p[1]) % 4] = (d - 1) & 0x0fff;
253 if (a != 8) break; // fallthrough if MONO
255 fifo = Pico32xMem->pwm_fifo[0];
256 idx = Pico32xMem->pwm_index[0];
257 if (Pico32x.pwm_p[0] < 3)
261 Pico32xMem->pwm_index[0] = idx;
263 fifo[(idx+Pico32x.pwm_p[0]) % 4] = (d - 1) & 0x0fff;
268 void p32x_pwm_update(s32 *buf32, int length, int stereo)
275 consume_fifo(NULL, SekCyclesDone());
277 xmd = Pico32x.regs[0x30 / 2] & 0x0f;
278 if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f)
279 goto out; // invalid?
283 step = (pwm.ptr << 16) / length;
284 pwmb = Pico32xMem->pwm;
290 while (length-- > 0) {
295 pwmb += (p >> 16) * 2;
299 else if (xmd == 0x0a) {
301 while (length-- > 0) {
306 pwmb += (p >> 16) * 2;
311 // mono - LMD, RMD specify dst
312 if (xmd & 0x06) // src is R
314 if (xmd & 0x0c) // dst is R
316 while (length-- > 0) {
320 pwmb += (p >> 16) * 2;
329 while (length-- > 0) {
333 pwmb += (p >> 16) * 2;
338 elprintf(EL_PWM, "pwm_update: pwm.ptr %d, len %d, step %04x, done %d",
339 pwm.ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
343 pwm.silent = pwm.current[0] == 0 && pwm.current[1] == 0;
346 void p32x_pwm_state_loaded(void)
350 p32x_pwm_ctl_changed();
352 // for old savestates
353 cycles_diff_sh2 = Pico.t.m68c_cnt * 3 - Pico32x.pwm_cycle_p;
354 if (cycles_diff_sh2 >= pwm.cycles || cycles_diff_sh2 < 0) {
355 Pico32x.pwm_irq_cnt = pwm.irq_reload;
356 Pico32x.pwm_cycle_p = Pico.t.m68c_cnt * 3;
357 p32x_pwm_schedule(Pico.t.m68c_cnt);
361 // vim:shiftwidth=2:ts=2:expandtab