db1d3564 |
1 | #include "../pico_int.h" |
2 | |
3 | static int pwm_line_samples; |
4 | static int pwm_cycles; |
5 | static int pwm_mult; |
6 | static int pwm_ptr; |
7 | int pwm_frame_smp_cnt; |
8 | |
9 | |
10 | void 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) { |
1b3f5844 |
17 | elprintf(EL_32X|EL_PWM|EL_ANOMALY, "pwm: low cycle value: %d", cycles + 1); |
db1d3564 |
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 :( |
be20816c |
32 | void p32x_pwm_irq_check(int new_line) |
db1d3564 |
33 | { |
34 | int tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8; |
35 | if (tm == 0) |
36 | return; // TODO: verify |
37 | |
be20816c |
38 | if (new_line) |
39 | Pico32x.pwm_irq_sample_cnt += pwm_line_samples; |
db1d3564 |
40 | if (Pico32x.pwm_irq_sample_cnt >= (tm << 16)) { |
41 | Pico32x.pwm_irq_sample_cnt -= tm << 16; |
42 | Pico32x.sh2irqs |= P32XI_PWM; |
43 | p32x_update_irls(); |
44 | } |
45 | } |
46 | |
47 | unsigned int p32x_pwm_read16(unsigned int a) |
48 | { |
49 | unsigned int d = 0; |
50 | int predict; |
51 | |
52 | a &= 0x0e; |
53 | switch (a) { |
54 | case 0: // control |
55 | case 2: // cycle |
56 | return Pico32x.regs[(0x30 + a) / 2]; |
57 | |
58 | case 4: // L ch |
59 | case 6: // R ch |
60 | case 8: // MONO |
61 | predict = (pwm_line_samples * Pico.m.scanline) >> 16; |
1b3f5844 |
62 | elprintf(EL_PWM, "pwm: read status: ptr %d/%d, predict %d", |
db1d3564 |
63 | pwm_frame_smp_cnt, (pwm_line_samples * scanlines_total) >> 16, predict); |
64 | if (pwm_frame_smp_cnt > predict + 3) |
65 | d |= P32XP_FULL; |
66 | else if (pwm_frame_smp_cnt == 0 || pwm_frame_smp_cnt < predict - 1) |
67 | d |= P32XP_EMPTY; |
68 | break; |
69 | } |
70 | |
71 | return d; |
72 | } |
73 | |
74 | void p32x_pwm_write16(unsigned int a, unsigned int d) |
75 | { |
76 | a &= 0x0e; |
77 | if (a == 0) // control |
78 | Pico32x.regs[0x30 / 2] = d; |
79 | else if (a == 2) { // cycle |
80 | Pico32x.regs[0x32 / 2] = d & 0x0fff; |
81 | p32x_pwm_refresh(); |
82 | Pico32x.pwm_irq_sample_cnt = 0; // resets? |
83 | } |
84 | else if (a <= 8) { |
85 | d &= 0x0fff; |
86 | if (d > pwm_cycles) |
87 | d = pwm_cycles; |
88 | d = (d - pwm_cycles / 2) * pwm_mult; |
89 | |
90 | if (a < 6) // L ch |
91 | Pico32xMem->pwm[pwm_ptr * 2] = d; |
92 | else if (a == 6) // R ch |
93 | Pico32xMem->pwm[pwm_ptr * 2 + 1] = d; |
94 | else // MONO |
95 | Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d; |
96 | |
97 | if (a >= 6) { // R or MONO |
98 | pwm_frame_smp_cnt++; |
99 | pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1); |
1b3f5844 |
100 | elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x", pwm_frame_smp_cnt, pwm_ptr, d); |
db1d3564 |
101 | } |
102 | } |
103 | } |
104 | |
105 | void p32x_pwm_update(int *buf32, int length, int stereo) |
106 | { |
107 | extern int pwm_ptr; |
108 | short *pwmb; |
109 | int step; |
110 | int p = 0; |
111 | |
112 | if (pwm_ptr <= 16) // at least some samples.. |
113 | return; |
114 | |
115 | step = (pwm_ptr << 16) / length; // FIXME: division.. |
116 | pwmb = Pico32xMem->pwm; |
117 | |
118 | if (stereo) |
119 | { |
120 | while (length-- > 0) { |
121 | *buf32++ += pwmb[0]; |
122 | *buf32++ += pwmb[1]; |
123 | |
124 | p += step; |
125 | pwmb += (p >> 16) * 2; |
126 | p &= 0xffff; |
127 | } |
128 | } |
129 | else |
130 | { |
131 | while (length-- > 0) { |
132 | *buf32++ += pwmb[0]; |
133 | |
134 | p += step; |
135 | pwmb += (p >> 16) * 2; |
136 | p &= 0xffff; |
137 | } |
138 | } |
139 | |
1b3f5844 |
140 | elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d", |
db1d3564 |
141 | pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2); |
142 | |
143 | pwm_ptr = 0; |
144 | } |
145 | |