1 #include "../pico_int.h"
3 static int pwm_line_samples;
10 void p32x_pwm_refresh(void)
12 int cycles = Pico32x.regs[0x32 / 2];
15 cycles = (cycles - 1) & 0x0fff;
17 elprintf(EL_32X|EL_ANOMALY, "pwm: low cycle value: %d", cycles + 1);
21 pwm_mult = 0x10000 / cycles;
23 frame_samples = OSC_PAL / 7 * 3 / 50 / cycles;
25 frame_samples = OSC_NTSC / 7 * 3 / 60 / cycles;
27 pwm_line_samples = (frame_samples << 16) / scanlines_total;
30 // irq for every sample??
31 // FIXME: we need to hit more than once per line :(
32 void p32x_pwm_irq_check(void)
34 int tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
36 return; // TODO: verify
38 Pico32x.pwm_irq_sample_cnt += pwm_line_samples;
39 if (Pico32x.pwm_irq_sample_cnt >= (tm << 16)) {
40 Pico32x.pwm_irq_sample_cnt -= tm << 16;
41 Pico32x.sh2irqs |= P32XI_PWM;
46 unsigned int p32x_pwm_read16(unsigned int a)
55 return Pico32x.regs[(0x30 + a) / 2];
60 predict = (pwm_line_samples * Pico.m.scanline) >> 16;
61 elprintf(EL_32X, "pwm: read status: ptr %d/%d, predict %d",
62 pwm_frame_smp_cnt, (pwm_line_samples * scanlines_total) >> 16, predict);
63 if (pwm_frame_smp_cnt > predict + 3)
65 else if (pwm_frame_smp_cnt == 0 || pwm_frame_smp_cnt < predict - 1)
73 void p32x_pwm_write16(unsigned int a, unsigned int d)
76 if (a == 0) // control
77 Pico32x.regs[0x30 / 2] = d;
78 else if (a == 2) { // cycle
79 Pico32x.regs[0x32 / 2] = d & 0x0fff;
81 Pico32x.pwm_irq_sample_cnt = 0; // resets?
87 d = (d - pwm_cycles / 2) * pwm_mult;
90 Pico32xMem->pwm[pwm_ptr * 2] = d;
91 else if (a == 6) // R ch
92 Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
94 Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
96 if (a >= 6) { // R or MONO
98 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
99 elprintf(EL_32X, "pwm: smp_cnt %d, ptr %d, smp %x", pwm_frame_smp_cnt, pwm_ptr, d);
104 void p32x_pwm_update(int *buf32, int length, int stereo)
111 if (pwm_ptr <= 16) // at least some samples..
114 step = (pwm_ptr << 16) / length; // FIXME: division..
115 pwmb = Pico32xMem->pwm;
119 while (length-- > 0) {
124 pwmb += (p >> 16) * 2;
130 while (length-- > 0) {
134 pwmb += (p >> 16) * 2;
139 elprintf(EL_STATUS, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
140 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);