3 * (C) notaz, 2009,2010,2013
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
8 #include "../pico_int.h"
10 static int pwm_cycles;
13 static int pwm_irq_reload;
14 static int pwm_doing_fifo;
15 static int pwm_silent;
17 void p32x_pwm_ctl_changed(void)
19 int control = Pico32x.regs[0x30 / 2];
20 int cycles = Pico32x.regs[0x32 / 2];
22 cycles = (cycles - 1) & 0x0fff;
25 // supposedly we should stop FIFO when xMd is 0,
26 // but mars test disagrees
28 if ((control & 0x0f) != 0)
29 pwm_mult = 0x10000 / cycles;
31 pwm_irq_reload = (control & 0x0f00) >> 8;
32 pwm_irq_reload = ((pwm_irq_reload - 1) & 0x0f) + 1;
34 if (Pico32x.pwm_irq_cnt == 0)
35 Pico32x.pwm_irq_cnt = pwm_irq_reload;
38 static void do_pwm_irq(SH2 *sh2, unsigned int m68k_cycles)
40 p32x_trigger_irq(sh2, m68k_cycles, P32XI_PWM);
42 if (Pico32x.regs[0x30 / 2] & P32XP_RTP) {
43 p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1);
44 // note: might recurse
49 static int convert_sample(unsigned int v)
55 return ((int)v - pwm_cycles / 2) * pwm_mult;
58 #define consume_fifo(sh2, m68k_cycles) { \
59 int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \
60 if (cycles_diff >= pwm_cycles) \
61 consume_fifo_do(sh2, m68k_cycles, cycles_diff); \
64 static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles,
67 struct Pico32xMem *mem = Pico32xMem;
68 unsigned short *fifo_l = mem->pwm_fifo[0];
69 unsigned short *fifo_r = mem->pwm_fifo[1];
72 if (pwm_cycles == 0 || pwm_doing_fifo)
75 elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d",
76 m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm_cycles,
77 Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm_ptr);
79 // this is for recursion from dreq1 writes
82 for (; sh2_cycles_diff >= pwm_cycles; sh2_cycles_diff -= pwm_cycles)
84 if (Pico32x.pwm_p[0] > 0) {
85 fifo_l[0] = fifo_l[1];
86 fifo_l[1] = fifo_l[2];
87 fifo_l[2] = fifo_l[3];
89 mem->pwm_current[0] = convert_sample(fifo_l[0]);
90 sum += mem->pwm_current[0];
92 if (Pico32x.pwm_p[1] > 0) {
93 fifo_r[0] = fifo_r[1];
94 fifo_r[1] = fifo_r[2];
95 fifo_r[2] = fifo_r[3];
97 mem->pwm_current[1] = convert_sample(fifo_r[0]);
98 sum += mem->pwm_current[1];
101 mem->pwm[pwm_ptr * 2 ] = mem->pwm_current[0];
102 mem->pwm[pwm_ptr * 2 + 1] = mem->pwm_current[1];
103 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
105 if (--Pico32x.pwm_irq_cnt == 0) {
106 Pico32x.pwm_irq_cnt = pwm_irq_reload;
107 do_pwm_irq(sh2, m68k_cycles);
110 Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
116 static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now)
118 unsigned int sh2_now = m68k_now * 3;
124 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
125 if (cycles_diff_sh2 >= pwm_cycles)
126 consume_fifo_do(sh2, m68k_now, cycles_diff_sh2);
128 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
129 return 0; // masked by everyone
131 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
132 return (Pico32x.pwm_irq_cnt * pwm_cycles
133 - cycles_diff_sh2) / 3 + 1;
136 void p32x_pwm_schedule(unsigned int m68k_now)
138 int after = p32x_pwm_schedule_(NULL, m68k_now);
140 p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
143 void p32x_pwm_schedule_sh2(SH2 *sh2)
145 int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2));
147 p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
150 void p32x_pwm_sync_to_sh2(SH2 *sh2)
152 int m68k_cycles = sh2_cycles_done_m68k(sh2);
153 consume_fifo(sh2, m68k_cycles);
156 void p32x_pwm_irq_event(unsigned int m68k_now)
158 p32x_pwm_schedule(m68k_now);
161 unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2,
162 unsigned int m68k_cycles)
166 consume_fifo(sh2, m68k_cycles);
172 d = Pico32x.regs[(0x30 + a) / 2];
176 if (Pico32x.pwm_p[0] == 3)
178 else if (Pico32x.pwm_p[0] == 0)
184 if (Pico32x.pwm_p[1] == 3)
186 else if (Pico32x.pwm_p[1] == 0)
191 elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)",
192 m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
196 void p32x_pwm_write16(unsigned int a, unsigned int d,
197 SH2 *sh2, unsigned int m68k_cycles)
199 elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
200 m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
202 consume_fifo(sh2, m68k_cycles);
205 if (a == 0) { // control
207 if ((Pico32x.regs[0x30 / 2] & 0x0f) == 0)
208 Pico32xMem->pwm_fifo[0][0] = Pico32xMem->pwm_fifo[1][0] = 0;
209 Pico32x.regs[0x30 / 2] = d;
210 p32x_pwm_ctl_changed();
211 Pico32x.pwm_irq_cnt = pwm_irq_reload; // ?
213 else if (a == 2) { // cycle
214 Pico32x.regs[0x32 / 2] = d & 0x0fff;
215 p32x_pwm_ctl_changed();
218 d = (d - 1) & 0x0fff;
220 if (a == 4 || a == 8) { // L ch or MONO
221 unsigned short *fifo = Pico32xMem->pwm_fifo[0];
222 if (Pico32x.pwm_p[0] < 3)
228 fifo[Pico32x.pwm_p[0]] = d;
230 if (a == 6 || a == 8) { // R ch or MONO
231 unsigned short *fifo = Pico32xMem->pwm_fifo[1];
232 if (Pico32x.pwm_p[1] < 3)
238 fifo[Pico32x.pwm_p[1]] = d;
243 void p32x_pwm_update(int *buf32, int length, int stereo)
250 consume_fifo(NULL, SekCyclesDone());
252 xmd = Pico32x.regs[0x30 / 2] & 0x0f;
253 if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f)
254 goto out; // invalid?
258 step = (pwm_ptr << 16) / length;
259 pwmb = Pico32xMem->pwm;
265 while (length-- > 0) {
270 pwmb += (p >> 16) * 2;
274 else if (xmd == 0x0a) {
276 while (length-- > 0) {
281 pwmb += (p >> 16) * 2;
286 // mono - LMD, RMD specify dst
287 if (xmd & 0x06) // src is R
289 if (xmd & 0x0c) // dst is R
291 while (length-- > 0) {
295 pwmb += (p >> 16) * 2;
304 while (length-- > 0) {
308 pwmb += (p >> 16) * 2;
313 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
314 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
318 pwm_silent = Pico32xMem->pwm_current[0] == 0
319 && Pico32xMem->pwm_current[1] == 0;
322 void p32x_pwm_state_loaded(void)
326 p32x_pwm_ctl_changed();
328 // for old savestates
329 cycles_diff_sh2 = Pico.t.m68c_cnt * 3 - Pico32x.pwm_cycle_p;
330 if (cycles_diff_sh2 >= pwm_cycles || cycles_diff_sh2 < 0) {
331 Pico32x.pwm_irq_cnt = pwm_irq_reload;
332 Pico32x.pwm_cycle_p = Pico.t.m68c_cnt * 3;
333 p32x_pwm_schedule(Pico.t.m68c_cnt);
337 // vim:shiftwidth=2:ts=2:expandtab