32x: preliminary PWM implementation. 32x opts in menu
[picodrive.git] / pico / 32x / pwm.c
CommitLineData
db1d3564 1#include "../pico_int.h"
2
3static int pwm_line_samples;
4static int pwm_cycles;
5static int pwm_mult;
6static int pwm_ptr;
7int pwm_frame_smp_cnt;
8
9
10void p32x_pwm_refresh(void)
11{
12 int cycles = Pico32x.regs[0x32 / 2];
13 int frame_samples;
14
15 cycles = (cycles - 1) & 0x0fff;
16 if (cycles < 500) {
17 elprintf(EL_32X|EL_ANOMALY, "pwm: low cycle value: %d", cycles + 1);
18 cycles = 500;
19 }
20 pwm_cycles = cycles;
21 pwm_mult = 0x10000 / cycles;
22 if (Pico.m.pal)
23 frame_samples = OSC_PAL / 7 * 3 / 50 / cycles;
24 else
25 frame_samples = OSC_NTSC / 7 * 3 / 60 / cycles;
26
27 pwm_line_samples = (frame_samples << 16) / scanlines_total;
28}
29
30// irq for every sample??
31// FIXME: we need to hit more than once per line :(
32void p32x_pwm_irq_check(void)
33{
34 int tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
35 if (tm == 0)
36 return; // TODO: verify
37
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;
42 p32x_update_irls();
43 }
44}
45
46unsigned int p32x_pwm_read16(unsigned int a)
47{
48 unsigned int d = 0;
49 int predict;
50
51 a &= 0x0e;
52 switch (a) {
53 case 0: // control
54 case 2: // cycle
55 return Pico32x.regs[(0x30 + a) / 2];
56
57 case 4: // L ch
58 case 6: // R ch
59 case 8: // MONO
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)
64 d |= P32XP_FULL;
65 else if (pwm_frame_smp_cnt == 0 || pwm_frame_smp_cnt < predict - 1)
66 d |= P32XP_EMPTY;
67 break;
68 }
69
70 return d;
71}
72
73void p32x_pwm_write16(unsigned int a, unsigned int d)
74{
75 a &= 0x0e;
76 if (a == 0) // control
77 Pico32x.regs[0x30 / 2] = d;
78 else if (a == 2) { // cycle
79 Pico32x.regs[0x32 / 2] = d & 0x0fff;
80 p32x_pwm_refresh();
81 Pico32x.pwm_irq_sample_cnt = 0; // resets?
82 }
83 else if (a <= 8) {
84 d &= 0x0fff;
85 if (d > pwm_cycles)
86 d = pwm_cycles;
87 d = (d - pwm_cycles / 2) * pwm_mult;
88
89 if (a < 6) // L ch
90 Pico32xMem->pwm[pwm_ptr * 2] = d;
91 else if (a == 6) // R ch
92 Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
93 else // MONO
94 Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
95
96 if (a >= 6) { // R or MONO
97 pwm_frame_smp_cnt++;
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);
100 }
101 }
102}
103
104void p32x_pwm_update(int *buf32, int length, int stereo)
105{
106 extern int pwm_ptr;
107 short *pwmb;
108 int step;
109 int p = 0;
110
111 if (pwm_ptr <= 16) // at least some samples..
112 return;
113
114 step = (pwm_ptr << 16) / length; // FIXME: division..
115 pwmb = Pico32xMem->pwm;
116
117 if (stereo)
118 {
119 while (length-- > 0) {
120 *buf32++ += pwmb[0];
121 *buf32++ += pwmb[1];
122
123 p += step;
124 pwmb += (p >> 16) * 2;
125 p &= 0xffff;
126 }
127 }
128 else
129 {
130 while (length-- > 0) {
131 *buf32++ += pwmb[0];
132
133 p += step;
134 pwmb += (p >> 16) * 2;
135 p &= 0xffff;
136 }
137 }
138
139 elprintf(EL_STATUS, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
140 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
141
142 pwm_ptr = 0;
143}
144