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] =
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
[] =
65 static void s1_freq_d(int d
)
67 if (RATE
> (d
<<4)) S1
.freq
= 0;
68 else S1
.freq
= (RATE
<< 17)/d
;
73 s1_freq_d(2048 - (((R_NR14
&7)<<8) + R_NR13
));
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
;
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
;
92 S4
.freq
= (freqtab
[R_NR43
&7] >> (R_NR43
>> 4)) * RATE
;
93 if (S4
.freq
>> 18) S4
.freq
= 1<<18;
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;
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;
111 S3
.len
= (256-R_NR31
) << 20;
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;
123 memset(&S1
, 0, sizeof S1
);
124 memset(&S2
, 0, sizeof S2
);
125 memset(&S3
, 0, sizeof S3
);
126 memset(&S4
, 0, sizeof S4
);
150 memset(&snd
, 0, sizeof snd
);
151 if (pcm
.hz
) snd
.rate
= (1<<21) / pcm
.hz
;
153 memcpy(WAVE
, hw
.cgb
? cgbwave
: dmgwave
, 16);
154 memcpy(ram
.hi
+0x30, WAVE
, 16);
163 if (!RATE
|| cpu
.snd
< RATE
) return;
165 for (; cpu
.snd
>= RATE
; cpu
.snd
-= RATE
)
171 s
= sqwave
[R_NR11
>>6][(S1
.pos
>>18)&7] & S1
.envol
;
173 if ((R_NR14
& 64) && ((S1
.cnt
+= RATE
) >= S1
.len
))
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
;
187 if (R_NR10
& 8) f
-= (f
>> n
);
195 R_NR14
= (R_NR14
& 0xF8) | (f
>>8);
200 if (R_NR51
& 1) r
+= s
;
201 if (R_NR51
& 16) l
+= s
;
206 s
= sqwave
[R_NR21
>>6][(S2
.pos
>>18)&7] & S2
.envol
;
208 if ((R_NR24
& 64) && ((S2
.cnt
+= RATE
) >= S2
.len
))
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;
218 if (R_NR51
& 2) r
+= s
;
219 if (R_NR51
& 32) l
+= s
;
224 s
= WAVE
[(S3
.pos
>>22) & 15];
225 if (S3
.pos
& (1<<21)) s
&= 15;
229 if ((R_NR34
& 64) && ((S3
.cnt
+= RATE
) >= S3
.len
))
231 if (R_NR32
& 96) s
<<= (3 - ((R_NR32
>>5)&3));
233 if (R_NR51
& 4) r
+= s
;
234 if (R_NR51
& 64) l
+= s
;
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)));
245 if ((R_NR44
& 64) && ((S4
.cnt
+= RATE
) >= S4
.len
))
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;
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);
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;
271 if (pcm
.pos
>= pcm
.len
)
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
)
289 /* printf("read %02X: %02X\n", r, REG(r)); */
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;
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;
322 if (!S3
.on
) S3
.pos
= 0;
325 if (S3
.on
) for (i
= 0; i
< 16; i
++)
326 ram
.hi
[i
+0x30] = 0x13 ^ ram
.hi
[i
+0x31];
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;
342 void sound_write(byte r
, byte b
)
346 if (!timer
) timer
= sys_timer();
347 printf("write %02X: %02X @ %d\n", r
, b
, sys_elapsed(timer
));
350 if (!(R_NR52
& 128) && r
!= RI_NR52
) return;
351 if ((r
& 0xF0) == 0x30)
353 if (S3
.on
) sound_mix();
355 WAVE
[r
-0x30] = ram
.hi
[r
] = b
;
363 S1
.swlen
= ((R_NR10
>>4) & 7) << 14;
364 S1
.swfreq
= ((R_NR14
&7)<<8) + R_NR13
;
368 S1
.len
= (64-(R_NR11
&63)) << 13;
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;
384 if (b
& 128) s1_init();
388 S2
.len
= (64-(R_NR21
&63)) << 13;
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;
404 if (b
& 128) s2_init();
408 if (!(b
& 128)) S3
.on
= 0;
412 S3
.len
= (256-R_NR31
) << 13;
424 if (b
& 128) s3_init();
428 S4
.len
= (64-(R_NR41
&63)) << 13;
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;
443 if (b
& 128) s4_init();