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_cycle_counter;
11 static int pwm_cycles;
15 static int pwm_smp_cnt;
16 static int pwm_smp_expect;
18 static int timer_cycles[2];
19 static int timer_tick_cycles[2];
21 // timers. This includes PWM timer in 32x and internal SH2 timers
22 void p32x_timers_recalc(void)
24 int cycles = Pico32x.regs[0x32 / 2];
27 cycles = (cycles - 1) & 0x0fff;
29 elprintf(EL_32X|EL_PWM|EL_ANOMALY, "pwm: low cycle value: %d", cycles + 1);
33 pwm_mult = 0x10000 / cycles;
36 for (i = 0; i < 2; i++) {
37 tmp = PREG8(Pico32xMem->sh2_peri_regs[i], 0x80) & 7;
38 // Sclk cycles per timer tick
43 timer_tick_cycles[i] = cycles;
44 elprintf(EL_32X, "WDT cycles[%d] = %d", i, cycles);
48 // PWM irq for every tm samples
49 void p32x_timers_do(unsigned int cycles)
55 // since we run things in async fashion, allow pwm to lag behind
56 // but don't allow our "queue" to be infinite
57 cnt = pwm_smp_expect - pwm_smp_cnt;
58 if (cnt <= 0 || cnt * pwm_cycles < OSC_NTSC/7*3 / 60 / 2) {
59 pwm_cycle_counter += cycles;
60 while (pwm_cycle_counter > pwm_cycles) {
61 pwm_cycle_counter -= pwm_cycles;
67 for (i = 0; i < 2; i++) {
68 void *pregs = Pico32xMem->sh2_peri_regs[i];
69 if (PREG8(pregs, 0x80) & 0x20) { // TME
70 timer_cycles[i] += cycles;
71 cnt = PREG8(pregs, 0x81);
72 while (timer_cycles[i] >= timer_tick_cycles[i]) {
73 timer_cycles[i] -= timer_tick_cycles[i];
77 int level = PREG8(pregs, 0xe3) >> 4;
78 int vector = PREG8(pregs, 0xe4) & 0x7f;
79 elprintf(EL_32X, "%csh2 WDT irq (%d, %d)",
80 i ? 's' : 'm', level, vector);
81 sh2_internal_irq(&sh2s[i], level, vector);
84 PREG8(pregs, 0x81) = cnt;
89 static int p32x_pwm_schedule_(void)
93 if (Pico32x.emu_flags & P32XF_PWM_PEND)
94 return 0; // already scheduled
95 if (Pico32x.sh2irqs & P32XI_PWM)
96 return 0; // previous not acked
97 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
98 return 0; // masked by everyone
100 Pico32x.emu_flags |= P32XF_PWM_PEND;
101 tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
102 tm = ((tm - 1) & 0x0f) + 1;
103 return pwm_cycles * tm / 3;
106 void p32x_pwm_schedule(unsigned int now)
108 int after = p32x_pwm_schedule_();
110 p32x_event_schedule(now, P32X_EVENT_PWM, after);
113 void p32x_pwm_schedule_sh2(SH2 *sh2)
115 int after = p32x_pwm_schedule_();
117 p32x_event_schedule_sh2(sh2, P32X_EVENT_PWM, after);
120 unsigned int p32x_pwm_read16(unsigned int a)
129 return Pico32x.regs[(0x30 + a) / 2];
134 diff = pwm_smp_cnt - pwm_smp_expect;
135 elprintf(EL_PWM, "pwm: read status: ptr %d/%d %d",
136 pwm_smp_cnt, pwm_smp_expect, diff);
147 void p32x_pwm_write16(unsigned int a, unsigned int d)
150 if (a == 0) // control
151 Pico32x.regs[0x30 / 2] = d;
152 else if (a == 2) { // cycle
153 Pico32x.regs[0x32 / 2] = d & 0x0fff;
154 p32x_timers_recalc();
155 Pico32x.pwm_irq_sample_cnt = 0; // resets?
161 d = (d - pwm_cycles / 2) * pwm_mult;
164 Pico32xMem->pwm[pwm_ptr * 2] = d;
165 else if (a == 6) // R ch
166 Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
168 Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
170 if (a >= 6) { // R or MONO
172 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
173 elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x",
174 pwm_smp_cnt, pwm_ptr, d);
179 void p32x_pwm_update(int *buf32, int length, int stereo)
185 if (pwm_ptr <= 16) // at least some samples..
188 step = (pwm_ptr << 16) / length; // FIXME: division..
189 pwmb = Pico32xMem->pwm;
193 while (length-- > 0) {
198 pwmb += (p >> 16) * 2;
204 while (length-- > 0) {
208 pwmb += (p >> 16) * 2;
213 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
214 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
219 // vim:shiftwidth=2:ts=2:expandtab