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 Pico32x.sh2irqs |= P32XI_PWM;
35 p32x_update_irls(sh2, m68k_cycles);
37 if (Pico32x.regs[0x30 / 2] & P32XP_RTP) {
38 p32x_event_schedule(m68k_cycles, P32X_EVENT_PWM, pwm_cycles / 3 + 1);
39 // note: might recurse
44 #define consume_fifo(sh2, m68k_cycles) { \
45 int cycles_diff = ((m68k_cycles) * 3) - Pico32x.pwm_cycle_p; \
46 if (cycles_diff >= pwm_cycles) \
47 consume_fifo_do(sh2, m68k_cycles, cycles_diff); \
50 static void consume_fifo_do(SH2 *sh2, unsigned int m68k_cycles,
53 if (pwm_cycles == 0 || pwm_doing_fifo)
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 // this is for recursion from dreq1 writes
63 while (sh2_cycles_diff >= pwm_cycles) {
64 struct Pico32xMem *mem = Pico32xMem;
65 short *fifo_l = mem->pwm_fifo[0];
66 short *fifo_r = mem->pwm_fifo[1];
68 if (Pico32x.pwm_p[0] > 0) {
69 fifo_l[0] = fifo_l[1];
70 fifo_l[1] = fifo_l[2];
71 fifo_l[2] = fifo_l[3];
74 if (Pico32x.pwm_p[1] > 0) {
75 fifo_r[0] = fifo_r[1];
76 fifo_r[1] = fifo_r[2];
77 fifo_r[2] = fifo_r[3];
81 mem->pwm[pwm_ptr * 2 ] = fifo_l[0];
82 mem->pwm[pwm_ptr * 2 + 1] = fifo_r[0];
83 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
85 sh2_cycles_diff -= pwm_cycles;
87 if (--Pico32x.pwm_irq_cnt == 0) {
88 Pico32x.pwm_irq_cnt = pwm_irq_reload;
89 do_pwm_irq(sh2, m68k_cycles);
92 Pico32x.pwm_cycle_p = m68k_cycles * 3 - sh2_cycles_diff;
96 static int p32x_pwm_schedule_(SH2 *sh2, unsigned int m68k_now)
98 unsigned int sh2_now = m68k_now * 3;
104 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
105 if (cycles_diff_sh2 >= pwm_cycles)
106 consume_fifo_do(sh2, m68k_now, cycles_diff_sh2);
108 if (Pico32x.sh2irqs & P32XI_PWM)
109 return 0; // previous not acked
110 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
111 return 0; // masked by everyone
113 cycles_diff_sh2 = sh2_now - Pico32x.pwm_cycle_p;
114 return (Pico32x.pwm_irq_cnt * pwm_cycles
115 - cycles_diff_sh2) / 3 + 1;
118 void p32x_pwm_schedule(unsigned int m68k_now)
120 int after = p32x_pwm_schedule_(NULL, m68k_now);
122 p32x_event_schedule(m68k_now, P32X_EVENT_PWM, after);
125 void p32x_pwm_schedule_sh2(SH2 *sh2)
127 int after = p32x_pwm_schedule_(sh2, sh2_cycles_done_m68k(sh2));
129 p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
132 void p32x_pwm_irq_event(unsigned int m68k_now)
134 p32x_pwm_schedule(m68k_now);
137 unsigned int p32x_pwm_read16(unsigned int a, SH2 *sh2,
138 unsigned int m68k_cycles)
142 consume_fifo(sh2, m68k_cycles);
148 d = Pico32x.regs[(0x30 + a) / 2];
152 if (Pico32x.pwm_p[0] == 3)
154 else if (Pico32x.pwm_p[0] == 0)
160 if (Pico32x.pwm_p[1] == 3)
162 else if (Pico32x.pwm_p[1] == 0)
167 elprintf(EL_PWM, "pwm: %u: r16 %02x %04x (p %d %d)",
168 m68k_cycles, a, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
172 void p32x_pwm_write16(unsigned int a, unsigned int d,
173 SH2 *sh2, unsigned int m68k_cycles)
175 elprintf(EL_PWM, "pwm: %u: w16 %02x %04x (p %d %d)",
176 m68k_cycles, a & 0x0e, d, Pico32x.pwm_p[0], Pico32x.pwm_p[1]);
178 consume_fifo(sh2, m68k_cycles);
181 if (a == 0) { // control
182 // supposedly we should stop FIFO when xMd is 0,
183 // but mars test disagrees
184 Pico32x.regs[0x30 / 2] = d;
185 p32x_pwm_ctl_changed();
186 Pico32x.pwm_irq_cnt = pwm_irq_reload; // ?
188 else if (a == 2) { // cycle
189 Pico32x.regs[0x32 / 2] = d & 0x0fff;
190 p32x_pwm_ctl_changed();
193 d = (d - 1) & 0x0fff;
196 d = (d - pwm_cycles / 2) * pwm_mult;
198 if (a == 4 || a == 8) { // L ch or MONO
199 short *fifo = Pico32xMem->pwm_fifo[0];
200 if (Pico32x.pwm_p[0] < 3)
206 fifo[Pico32x.pwm_p[0]] = d;
208 if (a == 6 || a == 8) { // R ch or MONO
209 short *fifo = Pico32xMem->pwm_fifo[1];
210 if (Pico32x.pwm_p[1] < 3)
216 fifo[Pico32x.pwm_p[1]] = d;
221 void p32x_pwm_update(int *buf32, int length, int stereo)
228 consume_fifo(NULL, SekCyclesDoneT2());
230 xmd = Pico32x.regs[0x30 / 2] & 0x0f;
231 if (xmd == 0 || xmd == 0x06 || xmd == 0x09 || xmd == 0x0f)
232 goto out; // invalid?
234 step = (pwm_ptr << 16) / length;
235 pwmb = Pico32xMem->pwm;
241 while (length-- > 0) {
246 pwmb += (p >> 16) * 2;
250 else if (xmd == 0x0a) {
252 while (length-- > 0) {
257 pwmb += (p >> 16) * 2;
262 // mono - LMD, RMD specify dst
263 if (xmd & 0x06) // src is R
265 if (xmd & 0x0c) // dst is R
267 while (length-- > 0) {
271 pwmb += (p >> 16) * 2;
280 while (length-- > 0) {
284 pwmb += (p >> 16) * 2;
289 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
290 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
296 void p32x_pwm_state_loaded(void)
300 p32x_pwm_ctl_changed();
302 // for old savestates
303 cycles_diff_sh2 = SekCycleCntT * 3 - Pico32x.pwm_cycle_p;
304 if (cycles_diff_sh2 >= pwm_cycles || cycles_diff_sh2 < 0) {
305 Pico32x.pwm_irq_cnt = pwm_irq_reload;
306 Pico32x.pwm_cycle_p = SekCycleCntT * 3;
307 p32x_pwm_schedule(SekCycleCntT);
311 // vim:shiftwidth=2:ts=2:expandtab