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;
15 void p32x_pwm_ctl_changed(void)
17 int control = Pico32x.regs[0x30 / 2];
18 int cycles = Pico32x.regs[0x32 / 2];
20 cycles = (cycles - 1) & 0x0fff;
22 pwm_mult = 0x10000 / cycles;
24 pwm_irq_reload = (control & 0x0f00) >> 8;
25 pwm_irq_reload = ((pwm_irq_reload - 1) & 0x0f) + 1;
27 if (Pico32x.pwm_irq_cnt == 0)
28 Pico32x.pwm_irq_cnt = pwm_irq_reload;
31 static void do_pwm_irq(unsigned int m68k_cycles)
33 Pico32x.sh2irqs |= P32XI_PWM;
34 p32x_update_irls(NULL);
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(m68k_cycles) { \
44 int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \
45 if (cycles_diff >= pwm_cycles) \
46 consume_fifo_do(m68k_cycles, cycles_diff); \
49 static void consume_fifo_do(unsigned int m68k_cycles, int sh2_cycles_diff)
56 elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d",
57 m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm_cycles,
58 Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm_ptr);
60 if (sh2_cycles_diff >= pwm_cycles * 17) {
62 Pico32x.pwm_cycle_p = m68k_cycles * 3;
63 Pico32x.pwm_p[0] = Pico32x.pwm_p[1] = 0;
67 while (sh2_cycles_diff >= pwm_cycles) {
68 struct Pico32xMem *mem = Pico32xMem;
69 short *fifo_l = mem->pwm_fifo[0];
70 short *fifo_r = mem->pwm_fifo[1];
72 if (Pico32x.pwm_p[0] > 0) {
73 fifo_l[0] = fifo_l[1];
74 fifo_l[1] = fifo_l[2];
75 fifo_l[2] = fifo_l[3];
78 if (Pico32x.pwm_p[1] > 0) {
79 fifo_r[0] = fifo_r[1];
80 fifo_r[1] = fifo_r[2];
81 fifo_r[2] = fifo_r[3];
85 mem->pwm[pwm_ptr * 2 ] = fifo_l[0];
86 mem->pwm[pwm_ptr * 2 + 1] = fifo_r[0];
87 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
89 sh2_cycles_diff -= pwm_cycles;
91 if (--Pico32x.pwm_irq_cnt == 0) {
92 Pico32x.pwm_irq_cnt = pwm_irq_reload;
93 // irq also does dreq1, so call it after cycle update
98 Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
101 do_pwm_irq(m68k_cycles);
104 static int p32x_pwm_schedule_(unsigned int m68k_now)
106 unsigned int sh2_now = m68k_now * 3;
112 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
113 if (cycles_diff_sh2 >= pwm_cycles)
114 consume_fifo_do(m68k_now, cycles_diff_sh2);
116 if (Pico32x.sh2irqs & P32XI_PWM)
117 return 0; // previous not acked
118 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
119 return 0; // masked by everyone
121 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
122 return (Pico32x.pwm_irq_cnt * pwm_cycles
123 - cycles_diff_sh2) / 3 + 1;
126 void p32x_pwm_schedule(unsigned int m68k_now)
128 int after = p32x_pwm_schedule_(m68k_now);
130 p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
133 void p32x_pwm_schedule_sh2(SH2 *sh2)
135 int after = p32x_pwm_schedule_(sh2_cycles_done_m68k(sh2));
137 p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
140 void p32x_pwm_irq_event(unsigned int m68k_now)
142 p32x_pwm_schedule(m68k_now);
145 unsigned int p32x_pwm_read16(unsigned int a, unsigned int m68k_cycles)
149 consume_fifo(m68k_cycles);
155 d = Pico32x.regs[(0x30 + a) / 2];
159 if (Pico32x.pwm_p[0] == 3)
161 else if (Pico32x.pwm_p[0] == 0)
167 if (Pico32x.pwm_p[1] == 3)
169 else if (Pico32x.pwm_p[1] == 0)
174 elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)",
175 m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
179 void p32x_pwm_write16(unsigned int a, unsigned int d,
180 unsigned int m68k_cycles)
182 elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
183 m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
185 consume_fifo(m68k_cycles);
188 if (a == 0) { // control
189 // supposedly we should stop FIFO when xMd is 0,
190 // but mars test disagrees
191 Pico32x.regs[0x30 / 2] = d;
192 p32x_pwm_ctl_changed();
193 Pico32x.pwm_irq_cnt = pwm_irq_reload; // ?
195 else if (a == 2) { // cycle
196 Pico32x.regs[0x32 / 2] = d & 0x0fff;
197 p32x_pwm_ctl_changed();
200 d = (d - 1) & 0x0fff;
203 d = (d - pwm_cycles / 2) * pwm_mult;
205 if (a == 4 || a == 8) { // L ch or MONO
206 short *fifo = Pico32xMem->pwm_fifo[0];
207 if (Pico32x.pwm_p[0] < 3)
213 fifo[Pico32x.pwm_p[0]] = d;
215 if (a == 6 || a == 8) { // R ch or MONO
216 short *fifo = Pico32xMem->pwm_fifo[1];
217 if (Pico32x.pwm_p[1] < 3)
223 fifo[Pico32x.pwm_p[1]] = d;
228 void p32x_pwm_update(int *buf32, int length, int stereo)
235 xmd = Pico32x.regs[0x30 / 2] & 0x0f;
236 if ((xmd != 0x05 && xmd != 0x0a) || pwm_ptr <= 16)
239 step = (pwm_ptr << 16) / length; // FIXME: division..
240 pwmb = Pico32xMem->pwm;
246 while (length-- > 0) {
251 pwmb += (p >> 16) * 2;
256 while (length-- > 0) {
261 pwmb += (p >> 16) * 2;
268 while (length-- > 0) {
272 pwmb += (p >> 16) * 2;
277 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
278 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
284 void p32x_pwm_state_loaded(void)
288 p32x_pwm_ctl_changed();
290 // for old savestates
291 cycles_diff_sh2 = SekCycleCntT * 3 - Pico32x.pwm_cycle_p;
292 if (cycles_diff_sh2 >= pwm_cycles || cycles_diff_sh2 < 0) {
293 Pico32x.pwm_irq_cnt = pwm_irq_reload;
294 Pico32x.pwm_cycle_p = SekCycleCntT * 3;
295 p32x_pwm_schedule(SekCycleCntT);
299 // vim:shiftwidth=2:ts=2:expandtab