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(SH2 *sh2, unsigned int m68k_cycles)
33 Pico32x.sh2irqs |= P32XI_PWM;
34 p32x_update_irls(sh2, m68k_cycles);
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,
57 elprintf(EL_PWM, "pwm: %u: consume %d/%d, %d,%d ptr %d",
58 m68k_cycles, sh2_cycles_diff, sh2_cycles_diff / pwm_cycles,
59 Pico32x.pwm_p[0], Pico32x.pwm_p[1], pwm_ptr);
61 if (sh2_cycles_diff >= pwm_cycles * 17) {
63 Pico32x.pwm_cycle_p = m68k_cycles * 3;
64 Pico32x.pwm_p[0] = Pico32x.pwm_p[1] = 0;
68 while (sh2_cycles_diff >= pwm_cycles) {
69 struct Pico32xMem *mem = Pico32xMem;
70 short *fifo_l = mem->pwm_fifo[0];
71 short *fifo_r = mem->pwm_fifo[1];
73 if (Pico32x.pwm_p[0] > 0) {
74 fifo_l[0] = fifo_l[1];
75 fifo_l[1] = fifo_l[2];
76 fifo_l[2] = fifo_l[3];
79 if (Pico32x.pwm_p[1] > 0) {
80 fifo_r[0] = fifo_r[1];
81 fifo_r[1] = fifo_r[2];
82 fifo_r[2] = fifo_r[3];
86 mem->pwm[pwm_ptr * 2 ] = fifo_l[0];
87 mem->pwm[pwm_ptr * 2 + 1] = fifo_r[0];
88 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
90 sh2_cycles_diff -= pwm_cycles;
92 if (--Pico32x.pwm_irq_cnt == 0) {
93 Pico32x.pwm_irq_cnt = pwm_irq_reload;
94 // irq also does dreq1, so call it after cycle update
99 Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
102 do_pwm_irq(sh2, m68k_cycles);
105 static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now)
107 unsigned int sh2_now = m68k_now * 3;
113 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
114 if (cycles_diff_sh2 >= pwm_cycles)
115 consume_fifo_do(sh2, m68k_now, cycles_diff_sh2);
117 if (Pico32x.sh2irqs & P32XI_PWM)
118 return 0; // previous not acked
119 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
120 return 0; // masked by everyone
122 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
123 return (Pico32x.pwm_irq_cnt * pwm_cycles
124 - cycles_diff_sh2) / 3 + 1;
127 void p32x_pwm_schedule(unsigned int m68k_now)
129 int after = p32x_pwm_schedule_(NULL, m68k_now);
131 p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
134 void p32x_pwm_schedule_sh2(SH2 *sh2)
136 int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2));
138 p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
141 void p32x_pwm_irq_event(unsigned int m68k_now)
143 p32x_pwm_schedule(m68k_now);
146 unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2,
147 unsigned int m68k_cycles)
151 consume_fifo(sh2, m68k_cycles);
157 d = Pico32x.regs[(0x30 + a) / 2];
161 if (Pico32x.pwm_p[0] == 3)
163 else if (Pico32x.pwm_p[0] == 0)
169 if (Pico32x.pwm_p[1] == 3)
171 else if (Pico32x.pwm_p[1] == 0)
176 elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)",
177 m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
181 void p32x_pwm_write16(unsigned int a, unsigned int d,
182 SH2 *sh2, unsigned int m68k_cycles)
184 elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
185 m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
187 consume_fifo(sh2, m68k_cycles);
190 if (a == 0) { // control
191 // supposedly we should stop FIFO when xMd is 0,
192 // but mars test disagrees
193 Pico32x.regs[0x30 / 2] = d;
194 p32x_pwm_ctl_changed();
195 Pico32x.pwm_irq_cnt = pwm_irq_reload; // ?
197 else if (a == 2) { // cycle
198 Pico32x.regs[0x32 / 2] = d & 0x0fff;
199 p32x_pwm_ctl_changed();
202 d = (d - 1) & 0x0fff;
205 d = (d - pwm_cycles / 2) * pwm_mult;
207 if (a == 4 || a == 8) { // L ch or MONO
208 short *fifo = Pico32xMem->pwm_fifo[0];
209 if (Pico32x.pwm_p[0] < 3)
215 fifo[Pico32x.pwm_p[0]] = d;
217 if (a == 6 || a == 8) { // R ch or MONO
218 short *fifo = Pico32xMem->pwm_fifo[1];
219 if (Pico32x.pwm_p[1] < 3)
225 fifo[Pico32x.pwm_p[1]] = d;
230 void p32x_pwm_update(int *buf32, int length, int stereo)
237 xmd = Pico32x.regs[0x30 / 2] & 0x0f;
238 if ((xmd != 0x05 && xmd != 0x0a) || pwm_ptr <= 16)
241 step = (pwm_ptr << 16) / length; // FIXME: division..
242 pwmb = Pico32xMem->pwm;
248 while (length-- > 0) {
253 pwmb += (p >> 16) * 2;
258 while (length-- > 0) {
263 pwmb += (p >> 16) * 2;
270 while (length-- > 0) {
274 pwmb += (p >> 16) * 2;
279 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
280 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
286 void p32x_pwm_state_loaded(void)
290 p32x_pwm_ctl_changed();
292 // for old savestates
293 cycles_diff_sh2 = SekCycleCntT * 3 - Pico32x.pwm_cycle_p;
294 if (cycles_diff_sh2 >= pwm_cycles || cycles_diff_sh2 < 0) {
295 Pico32x.pwm_irq_cnt = pwm_irq_reload;
296 Pico32x.pwm_cycle_p = SekCycleCntT * 3;
297 p32x_pwm_schedule(SekCycleCntT);
301 // vim:shiftwidth=2:ts=2:expandtab