32x: rework scheduling/timing
[picodrive.git] / pico / 32x / pwm.c
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
10 static int pwm_cycle_counter;
11 static int pwm_cycles;
12 static int pwm_mult;
13 static int pwm_ptr;
14
15 static int pwm_smp_cnt;
16 static int pwm_smp_expect;
17
18 static int timer_cycles[2];
19 static int timer_tick_cycles[2];
20
21 // timers. This includes PWM timer in 32x and internal SH2 timers
22 void 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
49 void p32x_timers_do(unsigned int cycles)
50 {
51   int cnt, i;
52
53   cycles *= 3;
54
55   pwm_cycle_counter += cycles;
56   while (pwm_cycle_counter > pwm_cycles) {
57     pwm_cycle_counter -= pwm_cycles;
58     pwm_smp_expect++;
59   }
60
61   // WDT timers
62   for (i = 0; i < 2; i++) {
63     void *pregs = Pico32xMem->sh2_peri_regs[i];
64     if (PREG8(pregs, 0x80) & 0x20) { // TME
65       timer_cycles[i] += cycles;
66       cnt = PREG8(pregs, 0x81);
67       while (timer_cycles[i] >= timer_tick_cycles[i]) {
68         timer_cycles[i] -= timer_tick_cycles[i];
69         cnt++;
70       }
71       if (cnt >= 0x100) {
72         int level = PREG8(pregs, 0xe3) >> 4;
73         int vector = PREG8(pregs, 0xe4) & 0x7f;
74         elprintf(EL_32X, "%csh2 WDT irq (%d, %d)",
75           i ? 's' : 'm', level, vector);
76         sh2_internal_irq(&sh2s[i], level, vector);
77         cnt &= 0xff;
78       }
79       PREG8(pregs, 0x81) = cnt;
80     }
81   }
82 }
83
84 void p32x_pwm_schedule(unsigned int now)
85 {
86   int tm;
87
88   if (Pico32x.emu_flags & P32XF_PWM_PEND)
89     return; // already scheduled
90   if (Pico32x.sh2irqs & P32XI_PWM)
91     return; // previous not acked
92   if (!((Pico32x.sh2irq_mask[0] | Pico32x.sh2irq_mask[1]) & 1))
93     return; // masked by everyone
94
95   tm = (Pico32x.regs[0x30 / 2] & 0x0f00) >> 8;
96   tm = ((tm - 1) & 0x0f) + 1;
97   p32x_event_schedule(P32X_EVENT_PWM, now, pwm_cycles * tm / 3);
98   Pico32x.emu_flags |= P32XF_PWM_PEND;
99 }
100
101 unsigned int p32x_pwm_read16(unsigned int a)
102 {
103   unsigned int d = 0;
104   int diff;
105
106   a &= 0x0e;
107   switch (a) {
108     case 0: // control
109     case 2: // cycle
110       return Pico32x.regs[(0x30 + a) / 2];
111
112     case 4: // L ch
113     case 6: // R ch
114     case 8: // MONO
115       diff = pwm_smp_cnt - pwm_smp_expect;
116       elprintf(EL_PWM, "pwm: read status: ptr %d/%d %d",
117         pwm_smp_cnt, pwm_smp_expect, diff);
118       if (diff > 3)
119         d |= P32XP_FULL;
120       else if (diff < 0)
121         d |= P32XP_EMPTY;
122       break;
123   }
124
125   return d;
126 }
127
128 void p32x_pwm_write16(unsigned int a, unsigned int d)
129 {
130   a &= 0x0e;
131   if (a == 0) // control
132     Pico32x.regs[0x30 / 2] = d;
133   else if (a == 2) { // cycle
134     Pico32x.regs[0x32 / 2] = d & 0x0fff;
135     p32x_timers_recalc();
136     Pico32x.pwm_irq_sample_cnt = 0; // resets?
137   }
138   else if (a <= 8) {
139     d &= 0x0fff;
140     if (d > pwm_cycles)
141       d = pwm_cycles;
142     d = (d - pwm_cycles / 2) * pwm_mult;
143
144     if       (a < 6) // L ch
145       Pico32xMem->pwm[pwm_ptr * 2] = d;
146     else if (a == 6) // R ch
147       Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
148     else             // MONO
149       Pico32xMem->pwm[pwm_ptr * 2] = Pico32xMem->pwm[pwm_ptr * 2 + 1] = d;
150
151     if (a >= 6) { // R or MONO
152       pwm_smp_cnt++;
153       pwm_ptr = (pwm_ptr + 1) & (PWM_BUFF_LEN - 1);
154       elprintf(EL_PWM, "pwm: smp_cnt %d, ptr %d, smp %x",
155           pwm_smp_cnt, pwm_ptr, d);
156     }
157   }
158 }
159
160 void p32x_pwm_update(int *buf32, int length, int stereo)
161 {
162   short *pwmb;
163   int step;
164   int p = 0;
165
166   if (pwm_ptr <= 16) // at least some samples..
167     return;
168
169   step = (pwm_ptr << 16) / length; // FIXME: division..
170   pwmb = Pico32xMem->pwm;
171
172   if (stereo)
173   {
174     while (length-- > 0) {
175       *buf32++ += pwmb[0];
176       *buf32++ += pwmb[1];
177
178       p += step;
179       pwmb += (p >> 16) * 2;
180       p &= 0xffff;
181     }
182   }
183   else
184   {
185     while (length-- > 0) {
186       *buf32++ += pwmb[0];
187
188       p += step;
189       pwmb += (p >> 16) * 2;
190       p &= 0xffff;
191     }
192   }
193
194   elprintf(EL_PWM, "pwm_update: pwm_ptr %d, len %d, step %04x, done %d",
195     pwm_ptr, length, step, (pwmb - Pico32xMem->pwm) / 2);
196
197   pwm_ptr = 0;
198 }
199
200 // vim:shiftwidth=2:ts=2:expandtab