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_PWM|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(int new_line)
34 int tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
36 return; // TODO: verify
39 Pico32x.pwm_irq_sample_cnt += pwm_line_samples;
40 if (Pico32x.pwm_irq_sample_cnt >= (tm << 16)) {
41 Pico32x.pwm_irq_sample_cnt -= tm << 16;
42 Pico32x.sh2irqs |= P32XI_PWM;
47 unsigned int p32x_pwm_read16(unsigned int a)
56 return Pico32x.regs[(0x30 + a) / 2];
61 predict = (pwm_line_samples * Pico.m.scanline) >> 16;
62 elprintf(EL_PWM, "pwm: read status: ptr %d/%d, predict %d",
63 pwm_frame_smp_cnt, (pwm_line_samples * scanlines_total) >> 16, predict);
64 if (pwm_frame_smp_cnt > predict + 3)
66 else if (pwm_frame_smp_cnt == 0 || pwm_frame_smp_cnt < predict - 1)
74 void p32x_pwm_write16(unsigned int a, unsigned int d)
77 if (a == 0) // control
78 Pico32x.regs[0x30 / 2] = d;
79 else if (a == 2) { // cycle
80 Pico32x.regs[0x32 / 2] = d & 0x0fff;
82 Pico32x.pwm_irq_sample_cnt = 0; // resets?
88 d = (d - pwm_cycles / 2) * pwm_mult;
91 Pico32xMem->pwm[pwm_ptr * 2] = d;
92 else if (a == 6) // R ch
93 Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
95 Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
97 if (a >= 6) { // R or MONO
99 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
100 elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x", pwm_frame_smp_cnt, pwm_ptr, d);
105 void p32x_pwm_update(int *buf32, int length, int stereo)
112 if (pwm_ptr <= 16) // at least some samples..
115 step = (pwm_ptr << 16) / length; // FIXME: division..
116 pwmb = Pico32xMem->pwm;
120 while (length-- > 0) {
125 pwmb += (p >> 16) * 2;
131 while (length-- > 0) {
135 pwmb += (p >> 16) * 2;
140 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
141 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);