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;
16 void p32x_pwm_ctl_changed(void)
18 int control = Pico32x.regs[0x30 / 2];
19 int cycles = Pico32x.regs[0x32 / 2];
21 cycles = (cycles - 1) & 0x0fff;
23 pwm_mult = 0x10000 / cycles;
25 pwm_irq_reload = (control & 0x0f00) >> 8;
26 pwm_irq_reload = ((pwm_irq_reload - 1) & 0x0f) + 1;
28 if (Pico32x.pwm_irq_cnt == 0)
29 Pico32x.pwm_irq_cnt = pwm_irq_reload;
32 static void do_pwm_irq(SH2 *sh2, unsigned int m68k_cycles)
34 p32x_trigger_irq(sh2, m68k_cycles, P32XI_PWM);
36 if (Pico32x.regs[0x30 / 2] & P32XP_RTP) {
37 p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1);
38 // note: might recurse
43 #define consume_fifo(sh2, m68k_cycles) { \
44 int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \
45 if (cycles_diff >= pwm_cycles) \
46 consume_fifo_do(sh2, m68k_cycles, cycles_diff); \
49 static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles,
52 if (pwm_cycles == 0 || pwm_doing_fifo)
55 elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d",
56 m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm_cycles,
57 Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm_ptr);
59 // this is for recursion from dreq1 writes
62 while (sh2_cycles_diff >= pwm_cycles) {
63 struct Pico32xMem *mem = Pico32xMem;
64 short *fifo_l = mem->pwm_fifo[0];
65 short *fifo_r = mem->pwm_fifo[1];
67 if (Pico32x.pwm_p[0] > 0) {
68 fifo_l[0] = fifo_l[1];
69 fifo_l[1] = fifo_l[2];
70 fifo_l[2] = fifo_l[3];
73 if (Pico32x.pwm_p[1] > 0) {
74 fifo_r[0] = fifo_r[1];
75 fifo_r[1] = fifo_r[2];
76 fifo_r[2] = fifo_r[3];
80 mem->pwm[pwm_ptr * 2 ] = fifo_l[0];
81 mem->pwm[pwm_ptr * 2 + 1] = fifo_r[0];
82 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
84 sh2_cycles_diff -= pwm_cycles;
86 if (--Pico32x.pwm_irq_cnt == 0) {
87 Pico32x.pwm_irq_cnt = pwm_irq_reload;
88 do_pwm_irq(sh2, m68k_cycles);
91 Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
95 static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now)
97 unsigned int sh2_now = m68k_now * 3;
103 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
104 if (cycles_diff_sh2 >= pwm_cycles)
105 consume_fifo_do(sh2, m68k_now, cycles_diff_sh2);
107 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
108 return 0; // masked by everyone
110 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
111 return (Pico32x.pwm_irq_cnt * pwm_cycles
112 - cycles_diff_sh2) / 3 + 1;
115 void p32x_pwm_schedule(unsigned int m68k_now)
117 int after = p32x_pwm_schedule_(NULL, m68k_now);
119 p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
122 void p32x_pwm_schedule_sh2(SH2 *sh2)
124 int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2));
126 p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
129 void p32x_pwm_sync_to_sh2(SH2 *sh2)
131 int m68k_cycles = sh2_cycles_done_m68k(sh2);
132 consume_fifo(sh2, m68k_cycles);
135 void p32x_pwm_irq_event(unsigned int m68k_now)
137 p32x_pwm_schedule(m68k_now);
140 unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2,
141 unsigned int m68k_cycles)
145 consume_fifo(sh2, m68k_cycles);
151 d = Pico32x.regs[(0x30 + a) / 2];
155 if (Pico32x.pwm_p[0] == 3)
157 else if (Pico32x.pwm_p[0] == 0)
163 if (Pico32x.pwm_p[1] == 3)
165 else if (Pico32x.pwm_p[1] == 0)
170 elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)",
171 m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
175 void p32x_pwm_write16(unsigned int a, unsigned int d,
176 SH2 *sh2, unsigned int m68k_cycles)
178 elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
179 m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
181 consume_fifo(sh2, m68k_cycles);
184 if (a == 0) { // control
185 // supposedly we should stop FIFO when xMd is 0,
186 // but mars test disagrees
187 Pico32x.regs[0x30 / 2] = d;
188 p32x_pwm_ctl_changed();
189 Pico32x.pwm_irq_cnt = pwm_irq_reload; // ?
191 else if (a == 2) { // cycle
192 Pico32x.regs[0x32 / 2] = d & 0x0fff;
193 p32x_pwm_ctl_changed();
196 d = (d - 1) & 0x0fff;
199 d = (d - pwm_cycles / 2) * pwm_mult;
201 if (a == 4 || a == 8) { // L ch or MONO
202 short *fifo = Pico32xMem->pwm_fifo[0];
203 if (Pico32x.pwm_p[0] < 3)
209 fifo[Pico32x.pwm_p[0]] = d;
211 if (a == 6 || a == 8) { // R ch or MONO
212 short *fifo = Pico32xMem->pwm_fifo[1];
213 if (Pico32x.pwm_p[1] < 3)
219 fifo[Pico32x.pwm_p[1]] = d;
224 void p32x_pwm_update(int *buf32, int length, int stereo)
231 consume_fifo(NULL, SekCyclesDoneT2());
233 xmd = Pico32x.regs[0x30 / 2] & 0x0f;
234 if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f)
235 goto out; // invalid?
237 step = (pwm_ptr << 16) / length;
238 pwmb = Pico32xMem->pwm;
244 while (length-- > 0) {
249 pwmb += (p >> 16) * 2;
253 else if (xmd == 0x0a) {
255 while (length-- > 0) {
260 pwmb += (p >> 16) * 2;
265 // mono - LMD, RMD specify dst
266 if (xmd & 0x06) // src is R
268 if (xmd & 0x0c) // dst is R
270 while (length-- > 0) {
274 pwmb += (p >> 16) * 2;
283 while (length-- > 0) {
287 pwmb += (p >> 16) * 2;
292 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
293 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
299 void p32x_pwm_state_loaded(void)
303 p32x_pwm_ctl_changed();
305 // for old savestates
306 cycles_diff_sh2 = SekCycleCntT * 3 - Pico32x.pwm_cycle_p;
307 if (cycles_diff_sh2 >= pwm_cycles || cycles_diff_sh2 < 0) {
308 Pico32x.pwm_irq_cnt = pwm_irq_reload;
309 Pico32x.pwm_cycle_p = SekCycleCntT * 3;
310 p32x_pwm_schedule(SekCycleCntT);
314 // vim:shiftwidth=2:ts=2:expandtab