drc: rework invalidation yet more
[picodrive.git] / pico / 32x / pwm.c
... / ...
CommitLineData
1/*
2 * PicoDrive
3 * (C) notaz, 2009,2010
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
8#include "../pico_int.h"
9
10static int pwm_cycle_counter;
11static int pwm_cycles;
12static int pwm_mult;
13static int pwm_ptr;
14
15static int pwm_smp_cnt;
16static int pwm_smp_expect;
17
18static int timer_cycles[2];
19static int timer_tick_cycles[2];
20
21// timers. This includes PWM timer in 32x and internal SH2 timers
22void p32x_timers_recalc(void)
23{
24 int cycles = Pico32x.regs[0x32 / 2];
25 int tmp, i;
26
27 cycles = (cycles - 1) & 0x0fff;
28 if (cycles < 500) {
29 elprintf(EL_32X|EL_PWM|EL_ANOMALY, "pwm: low cycle value: %d", cycles + 1);
30 cycles = 500;
31 }
32 pwm_cycles = cycles;
33 pwm_mult = 0x10000 / cycles;
34
35 // SH2 timer step
36 for (i = 0; i < 2; i++) {
37 tmp = PREG8(Pico32xMem->sh2_peri_regs[i], 0x80) & 7;
38 // Sclk cycles per timer tick
39 if (tmp)
40 cycles = 0x20 << tmp;
41 else
42 cycles = 2;
43 timer_tick_cycles[i] = cycles;
44 elprintf(EL_32X, "WDT cycles[%d] = %d", i, cycles);
45 }
46}
47
48// PWM irq for every tm samples
49void p32x_timers_do(unsigned int cycles)
50{
51 int cnt, i;
52
53 cycles *= 3;
54
55 // since we run things in async fashion, allow pwm to lag behind
56 // but don't allow our "queue" to be infinite
57 cnt = pwm_smp_expect - pwm_smp_cnt;
58 if (cnt <= 0 || cnt * pwm_cycles < OSC_NTSC/7*3 / 60 / 2) {
59 pwm_cycle_counter += cycles;
60 while (pwm_cycle_counter > pwm_cycles) {
61 pwm_cycle_counter -= pwm_cycles;
62 pwm_smp_expect++;
63 }
64 }
65
66 // WDT timers
67 for (i = 0; i < 2; i++) {
68 void *pregs = Pico32xMem->sh2_peri_regs[i];
69 if (PREG8(pregs, 0x80) & 0x20) { // TME
70 timer_cycles[i] += cycles;
71 cnt = PREG8(pregs, 0x81);
72 while (timer_cycles[i] >= timer_tick_cycles[i]) {
73 timer_cycles[i] -= timer_tick_cycles[i];
74 cnt++;
75 }
76 if (cnt >= 0x100) {
77 int level = PREG8(pregs, 0xe3) >> 4;
78 int vector = PREG8(pregs, 0xe4) & 0x7f;
79 elprintf(EL_32X, "%csh2 WDT irq (%d, %d)",
80 i ? 's' : 'm', level, vector);
81 sh2_internal_irq(&sh2s[i], level, vector);
82 cnt &= 0xff;
83 }
84 PREG8(pregs, 0x81) = cnt;
85 }
86 }
87}
88
89void p32x_pwm_schedule(unsigned int now)
90{
91 int tm;
92
93 if (Pico32x.emu_flags & P32XF_PWM_PEND)
94 return; // already scheduled
95 if (Pico32x.sh2irqs & P32XI_PWM)
96 return; // previous not acked
97 if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
98 return; // masked by everyone
99
100 tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
101 tm = ((tm - 1) & 0x0f) + 1;
102 p32x_event_schedule(P32X_EVENT_PWM, now, pwm_cycles * tm / 3);
103 Pico32x.emu_flags |= P32XF_PWM_PEND;
104}
105
106unsigned int p32x_pwm_read16(unsigned int a)
107{
108 unsigned int d = 0;
109 int diff;
110
111 a &= 0x0e;
112 switch (a) {
113 case 0: // control
114 case 2: // cycle
115 return Pico32x.regs[(0x30 + a) / 2];
116
117 case 4: // L ch
118 case 6: // R ch
119 case 8: // MONO
120 diff = pwm_smp_cnt - pwm_smp_expect;
121 elprintf(EL_PWM, "pwm: read status: ptr %d/%d %d",
122 pwm_smp_cnt, pwm_smp_expect, diff);
123 if (diff > 3)
124 d |= P32XP_FULL;
125 else if (diff < 0)
126 d |= P32XP_EMPTY;
127 break;
128 }
129
130 return d;
131}
132
133void p32x_pwm_write16(unsigned int a, unsigned int d)
134{
135 a &= 0x0e;
136 if (a == 0) // control
137 Pico32x.regs[0x30 / 2] = d;
138 else if (a == 2) { // cycle
139 Pico32x.regs[0x32 / 2] = d & 0x0fff;
140 p32x_timers_recalc();
141 Pico32x.pwm_irq_sample_cnt = 0; // resets?
142 }
143 else if (a <= 8) {
144 d &= 0x0fff;
145 if (d > pwm_cycles)
146 d = pwm_cycles;
147 d = (d - pwm_cycles / 2) * pwm_mult;
148
149 if (a < 6) // L ch
150 Pico32xMem->pwm[pwm_ptr * 2] = d;
151 else if (a == 6) // R ch
152 Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
153 else // MONO
154 Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
155
156 if (a >= 6) { // R or MONO
157 pwm_smp_cnt++;
158 pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
159 elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x",
160 pwm_smp_cnt, pwm_ptr, d);
161 }
162 }
163}
164
165void p32x_pwm_update(int *buf32, int length, int stereo)
166{
167 short *pwmb;
168 int step;
169 int p = 0;
170
171 if (pwm_ptr <= 16) // at least some samples..
172 return;
173
174 step = (pwm_ptr << 16) / length; // FIXME: division..
175 pwmb = Pico32xMem->pwm;
176
177 if (stereo)
178 {
179 while (length-- > 0) {
180 *buf32++ += pwmb[0];
181 *buf32++ += pwmb[1];
182
183 p += step;
184 pwmb += (p >> 16) * 2;
185 p &= 0xffff;
186 }
187 }
188 else
189 {
190 while (length-- > 0) {
191 *buf32++ += pwmb[0];
192
193 p += step;
194 pwmb += (p >> 16) * 2;
195 p &= 0xffff;
196 }
197 }
198
199 elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
200 pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
201
202 pwm_ptr = 0;
203}
204
205// vim:shiftwidth=2:ts=2:expandtab