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)
54 if (PicoOpt & POPT_EN_PWM)
56 tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
59 Pico32x.pwm_irq_sample_cnt += pwm_line_samples;
60 if (Pico32x.pwm_irq_sample_cnt >= (tm << 16)) {
61 Pico32x.pwm_irq_sample_cnt -= tm << 16;
62 Pico32x.sh2irqs |= P32XI_PWM;
71 for (i = 0; i < 2; i++) {
72 void *pregs = Pico32xMem->sh2_peri_regs[i];
73 if (PREG8(pregs, 0x80) & 0x20) { // TME
74 cnt = PREG8(pregs, 0x81);
75 cnt += timer_line_ticks[i];
77 int level = PREG8(pregs, 0xe3) >> 4;
78 int vector = PREG8(pregs, 0xe4) & 0x7f;
79 elprintf(EL_32X, "%csh2 WDT irq (%d, %d)", i ? 's' : 'm', level, vector);
80 sh2_internal_irq(&sh2s[i], level, vector);
83 PREG8(pregs, 0x81) = cnt;
88 unsigned int p32x_pwm_read16(unsigned int a)
97 return Pico32x.regs[(0x30 + a) / 2];
102 predict = (pwm_line_samples * Pico.m.scanline) >> 16;
103 elprintf(EL_PWM, "pwm: read status: ptr %d/%d, predict %d",
104 pwm_frame_smp_cnt, (pwm_line_samples * scanlines_total) >> 16, predict);
105 if (pwm_frame_smp_cnt > predict + 3)
107 else if (pwm_frame_smp_cnt == 0 || pwm_frame_smp_cnt < predict - 1)
115 void p32x_pwm_write16(unsigned int a, unsigned int d)
118 if (a == 0) // control
119 Pico32x.regs[0x30 / 2] = d;
120 else if (a == 2) { // cycle
121 Pico32x.regs[0x32 / 2] = d & 0x0fff;
122 p32x_timers_recalc();
123 Pico32x.pwm_irq_sample_cnt = 0; // resets?
129 d = (d - pwm_cycles / 2) * pwm_mult;
132 Pico32xMem->pwm[pwm_ptr * 2] = d;
133 else if (a == 6) // R ch
134 Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
136 Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
138 if (a >= 6) { // R or MONO
140 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
141 elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x", pwm_frame_smp_cnt, pwm_ptr, d);
146 void p32x_pwm_update(int *buf32, int length, int stereo)
153 if (pwm_ptr <= 16) // at least some samples..
156 step = (pwm_ptr << 16) / length; // FIXME: division..
157 pwmb = Pico32xMem->pwm;
161 while (length-- > 0) {
166 pwmb += (p >> 16) * 2;
172 while (length-- > 0) {
176 pwmb += (p >> 16) * 2;
181 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
182 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);