undo push-model SDL2 sound backend, use SDL1 approach
[rofl0r-gnuboy.git] / sound.c
blob365002191fe4031ddef91ece3b826106c9bcdd31
1 #include <string.h>
4 #include "defs.h"
5 #include "pcm.h"
6 #include "sound.h"
7 #include "cpu.h"
8 #include "hw.h"
9 #include "regs.h"
10 #include "rc.h"
11 #include "noise.h"
12 #include "sys.h"
14 const static byte dmgwave[16] =
16 0xac, 0xdd, 0xda, 0x48,
17 0x36, 0x02, 0xcf, 0x16,
18 0x2c, 0x04, 0xe5, 0x2c,
19 0xac, 0xdd, 0xda, 0x48
22 const static byte cgbwave[16] =
24 0x00, 0xff, 0x00, 0xff,
25 0x00, 0xff, 0x00, 0xff,
26 0x00, 0xff, 0x00, 0xff,
27 0x00, 0xff, 0x00, 0xff,
30 const static byte sqwave[4][8] =
32 { 0, 0,-1, 0, 0, 0, 0, 0 },
33 { 0,-1,-1, 0, 0, 0, 0, 0 },
34 { -1,-1,-1,-1, 0, 0, 0, 0 },
35 { -1, 0, 0,-1,-1,-1,-1,-1 }
38 const static int freqtab[8] =
40 (1<<14)*2,
41 (1<<14),
42 (1<<14)/2,
43 (1<<14)/3,
44 (1<<14)/4,
45 (1<<14)/5,
46 (1<<14)/6,
47 (1<<14)/7
50 struct snd snd;
52 #define RATE (snd.rate)
53 #define WAVE (snd.wave) /* ram.hi+0x30 */
54 #define S1 (snd.ch[0])
55 #define S2 (snd.ch[1])
56 #define S3 (snd.ch[2])
57 #define S4 (snd.ch[3])
59 rcvar_t sound_exports[] =
61 RCV_END
65 static void s1_freq_d(int d)
67 if (RATE > (d<<4)) S1.freq = 0;
68 else S1.freq = (RATE << 17)/d;
71 static void s1_freq()
73 s1_freq_d(2048 - (((R_NR14&7)<<8) + R_NR13));
76 static void s2_freq()
78 int d = 2048 - (((R_NR24&7)<<8) + R_NR23);
79 if (RATE > (d<<4)) S2.freq = 0;
80 else S2.freq = (RATE << 17)/d;
83 static void s3_freq()
85 int d = 2048 - (((R_NR34&7)<<8) + R_NR33);
86 if (RATE > (d<<3)) S3.freq = 0;
87 else S3.freq = (RATE << 21)/d;
90 static void s4_freq()
92 S4.freq = (freqtab[R_NR43&7] >> (R_NR43 >> 4)) * RATE;
93 if (S4.freq >> 18) S4.freq = 1<<18;
96 void sound_dirty()
98 S1.swlen = ((R_NR10>>4) & 7) << 14;
99 S1.len = (64-(R_NR11&63)) << 13;
100 S1.envol = R_NR12 >> 4;
101 S1.endir = (R_NR12>>3) & 1;
102 S1.endir |= S1.endir - 1;
103 S1.enlen = (R_NR12 & 7) << 15;
104 s1_freq();
105 S2.len = (64-(R_NR21&63)) << 13;
106 S2.envol = R_NR22 >> 4;
107 S2.endir = (R_NR22>>3) & 1;
108 S2.endir |= S2.endir - 1;
109 S2.enlen = (R_NR22 & 7) << 15;
110 s2_freq();
111 S3.len = (256-R_NR31) << 20;
112 s3_freq();
113 S4.len = (64-(R_NR41&63)) << 13;
114 S4.envol = R_NR42 >> 4;
115 S4.endir = (R_NR42>>3) & 1;
116 S4.endir |= S4.endir - 1;
117 S4.enlen = (R_NR42 & 7) << 15;
118 s4_freq();
121 void sound_off()
123 memset(&S1, 0, sizeof S1);
124 memset(&S2, 0, sizeof S2);
125 memset(&S3, 0, sizeof S3);
126 memset(&S4, 0, sizeof S4);
127 R_NR10 = 0x80;
128 R_NR11 = 0xBF;
129 R_NR12 = 0xF3;
130 R_NR14 = 0xBF;
131 R_NR21 = 0x3F;
132 R_NR22 = 0x00;
133 R_NR24 = 0xBF;
134 R_NR30 = 0x7F;
135 R_NR31 = 0xFF;
136 R_NR32 = 0x9F;
137 R_NR33 = 0xBF;
138 R_NR41 = 0xFF;
139 R_NR42 = 0x00;
140 R_NR43 = 0x00;
141 R_NR44 = 0xBF;
142 R_NR50 = 0x77;
143 R_NR51 = 0xF3;
144 R_NR52 = 0xF1;
145 sound_dirty();
148 void sound_reset()
150 memset(&snd, 0, sizeof snd);
151 if (pcm.hz) snd.rate = (1<<21) / pcm.hz;
152 else snd.rate = 0;
153 memcpy(WAVE, hw.cgb ? cgbwave : dmgwave, 16);
154 memcpy(ram.hi+0x30, WAVE, 16);
155 sound_off();
159 void sound_mix()
161 int s, l, r, f, n;
163 if (!RATE || cpu.snd < RATE) return;
165 for (; cpu.snd >= RATE; cpu.snd -= RATE)
167 l = r = 0;
169 if (S1.on)
171 s = sqwave[R_NR11>>6][(S1.pos>>18)&7] & S1.envol;
172 S1.pos += S1.freq;
173 if ((R_NR14 & 64) && ((S1.cnt += RATE) >= S1.len))
174 S1.on = 0;
175 if (S1.enlen && (S1.encnt += RATE) >= S1.enlen)
177 S1.encnt -= S1.enlen;
178 S1.envol += S1.endir;
179 if (S1.envol < 0) S1.envol = 0;
180 if (S1.envol > 15) S1.envol = 15;
182 if (S1.swlen && (S1.swcnt += RATE) >= S1.swlen)
184 S1.swcnt -= S1.swlen;
185 f = S1.swfreq;
186 n = (R_NR10 & 7);
187 if (R_NR10 & 8) f -= (f >> n);
188 else f += (f >> n);
189 if (f > 2047)
190 S1.on = 0;
191 else
193 S1.swfreq = f;
194 R_NR13 = f;
195 R_NR14 = (R_NR14 & 0xF8) | (f>>8);
196 s1_freq_d(2048 - f);
199 s <<= 2;
200 if (R_NR51 & 1) r += s;
201 if (R_NR51 & 16) l += s;
204 if (S2.on)
206 s = sqwave[R_NR21>>6][(S2.pos>>18)&7] & S2.envol;
207 S2.pos += S2.freq;
208 if ((R_NR24 & 64) && ((S2.cnt += RATE) >= S2.len))
209 S2.on = 0;
210 if (S2.enlen && (S2.encnt += RATE) >= S2.enlen)
212 S2.encnt -= S2.enlen;
213 S2.envol += S2.endir;
214 if (S2.envol < 0) S2.envol = 0;
215 if (S2.envol > 15) S2.envol = 15;
217 s <<= 2;
218 if (R_NR51 & 2) r += s;
219 if (R_NR51 & 32) l += s;
222 if (S3.on)
224 s = WAVE[(S3.pos>>22) & 15];
225 if (S3.pos & (1<<21)) s &= 15;
226 else s >>= 4;
227 s -= 8;
228 S3.pos += S3.freq;
229 if ((R_NR34 & 64) && ((S3.cnt += RATE) >= S3.len))
230 S3.on = 0;
231 if (R_NR32 & 96) s <<= (3 - ((R_NR32>>5)&3));
232 else s = 0;
233 if (R_NR51 & 4) r += s;
234 if (R_NR51 & 64) l += s;
237 if (S4.on)
239 if (R_NR43 & 8) s = 1 & (noise7[
240 (S4.pos>>20)&15] >> (7-((S4.pos>>17)&7)));
241 else s = 1 & (noise15[
242 (S4.pos>>20)&4095] >> (7-((S4.pos>>17)&7)));
243 s = (-s) & S4.envol;
244 S4.pos += S4.freq;
245 if ((R_NR44 & 64) && ((S4.cnt += RATE) >= S4.len))
246 S4.on = 0;
247 if (S4.enlen && (S4.encnt += RATE) >= S4.enlen)
249 S4.encnt -= S4.enlen;
250 S4.envol += S4.endir;
251 if (S4.envol < 0) S4.envol = 0;
252 if (S4.envol > 15) S4.envol = 15;
254 s += s << 1;
255 if (R_NR51 & 8) r += s;
256 if (R_NR51 & 128) l += s;
259 l *= (R_NR50 & 0x07);
260 r *= ((R_NR50 & 0x70)>>4);
261 l >>= 4;
262 r >>= 4;
264 if (l > 127) l = 127;
265 else if (l < -128) l = -128;
266 if (r > 127) r = 127;
267 else if (r < -128) r = -128;
269 if (pcm.buf)
271 if (pcm.pos >= pcm.len)
272 pcm_submit();
273 if (pcm.stereo)
275 pcm.buf[pcm.pos++] = l+128;
276 pcm.buf[pcm.pos++] = r+128;
278 else pcm.buf[pcm.pos++] = ((l+r)>>1)+128;
281 R_NR52 = (R_NR52&0xf0) | S1.on | (S2.on<<1) | (S3.on<<2) | (S4.on<<3);
286 byte sound_read(byte r)
288 sound_mix();
289 /* printf("read %02X: %02X\n", r, REG(r)); */
290 return REG(r);
293 void s1_init()
295 S1.swcnt = 0;
296 S1.swfreq = ((R_NR14&7)<<8) + R_NR13;
297 S1.envol = R_NR12 >> 4;
298 S1.endir = (R_NR12>>3) & 1;
299 S1.endir |= S1.endir - 1;
300 S1.enlen = (R_NR12 & 7) << 15;
301 if (!S1.on) S1.pos = 0;
302 S1.on = 1;
303 S1.cnt = 0;
304 S1.encnt = 0;
307 void s2_init()
309 S2.envol = R_NR22 >> 4;
310 S2.endir = (R_NR22>>3) & 1;
311 S2.endir |= S2.endir - 1;
312 S2.enlen = (R_NR22 & 7) << 15;
313 if (!S2.on) S2.pos = 0;
314 S2.on = 1;
315 S2.cnt = 0;
316 S2.encnt = 0;
319 void s3_init()
321 int i;
322 if (!S3.on) S3.pos = 0;
323 S3.cnt = 0;
324 S3.on = R_NR30 >> 7;
325 if (S3.on) for (i = 0; i < 16; i++)
326 ram.hi[i+0x30] = 0x13 ^ ram.hi[i+0x31];
329 void s4_init()
331 S4.envol = R_NR42 >> 4;
332 S4.endir = (R_NR42>>3) & 1;
333 S4.endir |= S4.endir - 1;
334 S4.enlen = (R_NR42 & 7) << 15;
335 S4.on = 1;
336 S4.pos = 0;
337 S4.cnt = 0;
338 S4.encnt = 0;
342 void sound_write(byte r, byte b)
344 #if 0
345 static void *timer;
346 if (!timer) timer = sys_timer();
347 printf("write %02X: %02X @ %d\n", r, b, sys_elapsed(timer));
348 #endif
350 if (!(R_NR52 & 128) && r != RI_NR52) return;
351 if ((r & 0xF0) == 0x30)
353 if (S3.on) sound_mix();
354 if (!S3.on)
355 WAVE[r-0x30] = ram.hi[r] = b;
356 return;
358 sound_mix();
359 switch (r)
361 case RI_NR10:
362 R_NR10 = b;
363 S1.swlen = ((R_NR10>>4) & 7) << 14;
364 S1.swfreq = ((R_NR14&7)<<8) + R_NR13;
365 break;
366 case RI_NR11:
367 R_NR11 = b;
368 S1.len = (64-(R_NR11&63)) << 13;
369 break;
370 case RI_NR12:
371 R_NR12 = b;
372 S1.envol = R_NR12 >> 4;
373 S1.endir = (R_NR12>>3) & 1;
374 S1.endir |= S1.endir - 1;
375 S1.enlen = (R_NR12 & 7) << 15;
376 break;
377 case RI_NR13:
378 R_NR13 = b;
379 s1_freq();
380 break;
381 case RI_NR14:
382 R_NR14 = b;
383 s1_freq();
384 if (b & 128) s1_init();
385 break;
386 case RI_NR21:
387 R_NR21 = b;
388 S2.len = (64-(R_NR21&63)) << 13;
389 break;
390 case RI_NR22:
391 R_NR22 = b;
392 S2.envol = R_NR22 >> 4;
393 S2.endir = (R_NR22>>3) & 1;
394 S2.endir |= S2.endir - 1;
395 S2.enlen = (R_NR22 & 7) << 15;
396 break;
397 case RI_NR23:
398 R_NR23 = b;
399 s2_freq();
400 break;
401 case RI_NR24:
402 R_NR24 = b;
403 s2_freq();
404 if (b & 128) s2_init();
405 break;
406 case RI_NR30:
407 R_NR30 = b;
408 if (!(b & 128)) S3.on = 0;
409 break;
410 case RI_NR31:
411 R_NR31 = b;
412 S3.len = (256-R_NR31) << 13;
413 break;
414 case RI_NR32:
415 R_NR32 = b;
416 break;
417 case RI_NR33:
418 R_NR33 = b;
419 s3_freq();
420 break;
421 case RI_NR34:
422 R_NR34 = b;
423 s3_freq();
424 if (b & 128) s3_init();
425 break;
426 case RI_NR41:
427 R_NR41 = b;
428 S4.len = (64-(R_NR41&63)) << 13;
429 break;
430 case RI_NR42:
431 R_NR42 = b;
432 S4.envol = R_NR42 >> 4;
433 S4.endir = (R_NR42>>3) & 1;
434 S4.endir |= S4.endir - 1;
435 S4.enlen = (R_NR42 & 7) << 15;
436 break;
437 case RI_NR43:
438 R_NR43 = b;
439 s4_freq();
440 break;
441 case RI_NR44:
442 R_NR44 = b;
443 if (b & 128) s4_init();
444 break;
445 case RI_NR50:
446 R_NR50 = b;
447 break;
448 case RI_NR51:
449 R_NR51 = b;
450 break;
451 case RI_NR52:
452 R_NR52 = b;
453 if (!(R_NR52 & 128))
454 sound_off();
455 break;
456 default:
457 return;