1 #include "../pico_int.h"
3 static int pwm_line_samples;
9 static int timer_line_ticks[2];
11 // timers. This includes PWM timer in 32x and internal SH2 timers
12 void p32x_timers_recalc(void)
14 int cycles = Pico32x.regs[0x32 / 2];
18 cycles = (cycles - 1) & 0x0fff;
20 elprintf(EL_32X|EL_PWM|EL_ANOMALY, "pwm: low cycle value: %d", cycles + 1);
24 pwm_mult = 0x10000 / cycles;
26 frame_samples = OSC_PAL / 7 * 3 / 50 / cycles;
28 frame_samples = OSC_NTSC / 7 * 3 / 60 / cycles;
30 pwm_line_samples = (frame_samples << 16) / scanlines_total;
33 for (i = 0; i < 2; i++) {
34 tmp = PREG8(Pico32xMem->sh2_peri_regs[i], 0x80) & 7;
35 // Sclk cycles per timer tick
41 tmp = OSC_PAL / 7 * 3 / 50 / scanlines_total;
43 tmp = OSC_NTSC / 7 * 3 / 60 / scanlines_total;
44 timer_line_ticks[i] = (tmp << 16) / cycles;
45 elprintf(EL_32X, "timer_line_ticks[%d] = %.3f", i, (double)timer_line_ticks[i] / 0x10000);
49 // PWM irq for every tm samples
50 void p32x_timers_do(int new_line)
53 tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
56 Pico32x.pwm_irq_sample_cnt += pwm_line_samples;
57 if (Pico32x.pwm_irq_sample_cnt >= (tm << 16)) {
58 Pico32x.pwm_irq_sample_cnt -= tm << 16;
59 Pico32x.sh2irqs |= P32XI_PWM;
67 for (i = 0; i < 2; i++) {
68 void *pregs = Pico32xMem->sh2_peri_regs[i];
69 if (PREG8(pregs, 0x80) & 0x20) { // TME
70 cnt = PREG8(pregs, 0x81);
71 cnt += timer_line_ticks[i];
73 int level = PREG8(pregs, 0xe3) >> 4;
74 int vector = PREG8(pregs, 0xe4) & 0x7f;
75 elprintf(EL_32X, "%csh2 WDT irq (%d, %d)", i ? 's' : 'm', level, vector);
76 sh2_internal_irq(&sh2s[i], level, vector);
79 PREG8(pregs, 0x81) = cnt;
84 unsigned int p32x_pwm_read16(unsigned int a)
93 return Pico32x.regs[(0x30 + a) / 2];
98 predict = (pwm_line_samples * Pico.m.scanline) >> 16;
99 elprintf(EL_PWM, "pwm: read status: ptr %d/%d, predict %d",
100 pwm_frame_smp_cnt, (pwm_line_samples * scanlines_total) >> 16, predict);
101 if (pwm_frame_smp_cnt > predict + 3)
103 else if (pwm_frame_smp_cnt == 0 || pwm_frame_smp_cnt < predict - 1)
111 void p32x_pwm_write16(unsigned int a, unsigned int d)
114 if (a == 0) // control
115 Pico32x.regs[0x30 / 2] = d;
116 else if (a == 2) { // cycle
117 Pico32x.regs[0x32 / 2] = d & 0x0fff;
118 p32x_timers_recalc();
119 Pico32x.pwm_irq_sample_cnt = 0; // resets?
125 d = (d - pwm_cycles / 2) * pwm_mult;
128 Pico32xMem->pwm[pwm_ptr * 2] = d;
129 else if (a == 6) // R ch
130 Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
132 Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
134 if (a >= 6) { // R or MONO
136 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
137 elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x", pwm_frame_smp_cnt, pwm_ptr, d);
142 void p32x_pwm_update(int *buf32, int length, int stereo)
149 if (pwm_ptr <= 16) // at least some samples..
152 step = (pwm_ptr << 16) / length; // FIXME: division..
153 pwmb = Pico32xMem->pwm;
157 while (length-- > 0) {
162 pwmb += (p >> 16) * 2;
168 while (length-- > 0) {
172 pwmb += (p >> 16) * 2;
177 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
178 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);