Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/wonderswan/sound.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*/
17
18
#include "system.h"
19
#include <cstring>
20
21
22
namespace MDFN_IEN_WSWAN
23
{
24
#define MK_SAMPLE_CACHE \
25
{ \
26
int sample; \
27
sample = (((ram[((SampleRAMPos << 6) + (sample_pos[ch] >> 1) + (ch << 4)) ] >> ((sample_pos[ch] & 1) ? 4 : 0)) & 0x0F)) - 0x8; \
28
sample_cache[ch][0] = sample * ((volume[ch] >> 4) & 0x0F); \
29
sample_cache[ch][1] = sample * ((volume[ch] >> 0) & 0x0F); \
30
}
31
32
#define MK_SAMPLE_CACHE_NOISE \
33
{ \
34
int sample; \
35
sample = ((nreg & 1) ? 0xF : 0x0) - 0x8; \
36
sample_cache[ch][0] = sample * ((volume[ch] >> 4) & 0x0F); \
37
sample_cache[ch][1] = sample * ((volume[ch] >> 0) & 0x0F); \
38
}
39
40
41
#define SYNCSAMPLE(wt) \
42
{ \
43
int32 left = sample_cache[ch][0], right = sample_cache[ch][1]; \
44
WaveSynth.offset_inline(wt, left - last_val[ch][0], sbuf[0]); \
45
WaveSynth.offset_inline(wt, right - last_val[ch][1], sbuf[1]); \
46
last_val[ch][0] = left; \
47
last_val[ch][1] = right; \
48
}
49
50
#define SYNCSAMPLE_NOISE(wt) \
51
{ \
52
int32 left = sample_cache[ch][0], right = sample_cache[ch][1]; \
53
NoiseSynth.offset_inline(wt, left - last_val[ch][0], sbuf[0]); \
54
NoiseSynth.offset_inline(wt, right - last_val[ch][1], sbuf[1]); \
55
last_val[ch][0] = left; \
56
last_val[ch][1] = right; \
57
}
58
59
void Sound::Update()
60
{
61
int32 run_time;
62
const uint8 *ram = sys->memory.wsRAM;
63
const uint32 current_ts = sys->cpu.timestamp;
64
65
//printf("%d\n", v30mz_timestamp);
66
//printf("%02x %02x\n", control, noise_control);
67
run_time = current_ts - last_ts;
68
69
for(unsigned int ch = 0; ch < 4; ch++)
70
{
71
// Channel is disabled?
72
if(!(control & (1 << ch)))
73
continue;
74
75
if(ch == 1 && (control & 0x20)) // Direct D/A mode?
76
{
77
int32 neoval = (volume[ch] - 0x80) * voice_volume;
78
79
VoiceSynth.offset(current_ts, neoval - last_v_val, sbuf[0]);
80
VoiceSynth.offset(current_ts, neoval - last_v_val, sbuf[1]);
81
82
last_v_val = neoval;
83
}
84
else if(ch == 2 && (control & 0x40) && sweep_value) // Sweep
85
{
86
uint32 tmp_pt = 2048 - period[ch];
87
uint32 meow_timestamp = current_ts - run_time;
88
uint32 tmp_run_time = run_time;
89
90
while(tmp_run_time)
91
{
92
int32 sub_run_time = tmp_run_time;
93
94
if(sub_run_time > sweep_8192_divider)
95
sub_run_time = sweep_8192_divider;
96
97
sweep_8192_divider -= sub_run_time;
98
if(sweep_8192_divider <= 0)
99
{
100
sweep_8192_divider += 8192;
101
sweep_counter--;
102
if(sweep_counter <= 0)
103
{
104
sweep_counter = sweep_step + 1;
105
period[ch] = (period[ch] + (int8)sweep_value) & 0x7FF;
106
}
107
}
108
109
meow_timestamp += sub_run_time;
110
if(tmp_pt > 4)
111
{
112
period_counter[ch] -= sub_run_time;
113
while(period_counter[ch] <= 0)
114
{
115
sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F;
116
117
MK_SAMPLE_CACHE;
118
SYNCSAMPLE(meow_timestamp + period_counter[ch]);
119
period_counter[ch] += tmp_pt;
120
}
121
}
122
tmp_run_time -= sub_run_time;
123
}
124
}
125
else if(ch == 3 && (noise_control & 0x10)) //(control & 0x80)) // Noise
126
{
127
uint32 tmp_pt = 2048 - period[ch];
128
129
period_counter[ch] -= run_time;
130
while(period_counter[ch] <= 0)
131
{
132
static const uint8 stab[8] = { 14, 10, 13, 4, 8, 6, 9, 11 };
133
nreg = ((nreg << 1) | ((1 ^ (nreg >> 7) ^ (nreg >> stab[noise_control & 0x7])) & 1)) & 0x7FFF;
134
135
if(control & 0x80)
136
{
137
MK_SAMPLE_CACHE_NOISE;
138
SYNCSAMPLE_NOISE(current_ts + period_counter[ch]);
139
}
140
else if(tmp_pt > 4)
141
{
142
sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F;
143
MK_SAMPLE_CACHE;
144
SYNCSAMPLE(current_ts + period_counter[ch]);
145
}
146
period_counter[ch] += tmp_pt;
147
}
148
}
149
else
150
{
151
uint32 tmp_pt = 2048 - period[ch];
152
153
if(tmp_pt > 4)
154
{
155
period_counter[ch] -= run_time;
156
while(period_counter[ch] <= 0)
157
{
158
sample_pos[ch] = (sample_pos[ch] + 1) & 0x1F;
159
160
MK_SAMPLE_CACHE;
161
SYNCSAMPLE(current_ts + period_counter[ch]); // - period_counter[ch]);
162
period_counter[ch] += tmp_pt;
163
}
164
}
165
}
166
}
167
168
{
169
int32 tmphv = HyperVoice;
170
171
if(tmphv - last_hv_val)
172
{
173
WaveSynth.offset_inline(current_ts, tmphv - last_hv_val, sbuf[0]);
174
WaveSynth.offset_inline(current_ts, tmphv - last_hv_val, sbuf[1]);
175
last_hv_val = tmphv;
176
}
177
}
178
last_ts = current_ts;
179
}
180
181
void Sound::Write(uint32 A, uint8 V)
182
{
183
Update();
184
185
if(A >= 0x80 && A <= 0x87)
186
{
187
int ch = (A - 0x80) >> 1;
188
189
if(A & 1)
190
period[ch] = (period[ch] & 0x00FF) | ((V & 0x07) << 8);
191
else
192
period[ch] = (period[ch] & 0x0700) | ((V & 0xFF) << 0);
193
}
194
else if(A >= 0x88 && A <= 0x8B)
195
{
196
volume[A - 0x88] = V;
197
}
198
else if(A == 0x8C)
199
sweep_value = V;
200
else if(A == 0x8D)
201
{
202
sweep_step = V;
203
sweep_counter = sweep_step + 1;
204
sweep_8192_divider = 8192;
205
}
206
else if(A == 0x8E)
207
{
208
if(V & 0x8)
209
nreg = 0;
210
noise_control = V & 0x17;
211
//printf("NOISECONTROL: %02x\n", V);
212
}
213
else if(A == 0x90)
214
{
215
for(int n = 0; n < 4; n++)
216
{
217
if(!(control & (1 << n)) && (V & (1 << n)))
218
{
219
period_counter[n] = 0;
220
sample_pos[n] = 0x1F;
221
}
222
}
223
control = V;
224
//printf("Sound Control: %02x\n", V);
225
}
226
else if(A == 0x91)
227
{
228
output_control = V & 0xF;
229
//printf("%02x, %02x\n", V, (V >> 1) & 3);
230
}
231
else if(A == 0x92)
232
nreg = (nreg & 0xFF00) | (V << 0);
233
else if(A == 0x93)
234
nreg = (nreg & 0x00FF) | ((V & 0x7F) << 8);
235
else if(A == 0x94)
236
{
237
voice_volume = V & 0xF;
238
//printf("%02x\n", V);
239
}
240
else switch(A)
241
{
242
case 0x8F: SampleRAMPos = V; break;
243
case 0x95: HyperVoice = V; break; // Pick a port, any port?!
244
//default: printf("%04x:%02x\n", A, V); break;
245
}
246
Update();
247
}
248
249
uint8 Sound::Read(uint32 A)
250
{
251
Update();
252
253
if(A >= 0x80 && A <= 0x87)
254
{
255
int ch = (A - 0x80) >> 1;
256
257
if(A & 1)
258
return period[ch] >> 8;
259
else
260
return (uint8)period[ch];
261
}
262
else if(A >= 0x88 && A <= 0x8B)
263
return(volume[A - 0x88]);
264
else switch(A)
265
{
266
default: /*printf("SoundRead: %04x\n", A);*/ return(0);
267
case 0x8C: return(sweep_value);
268
case 0x8D: return(sweep_step);
269
case 0x8E: return(noise_control);
270
case 0x8F: return(SampleRAMPos);
271
case 0x90: return(control);
272
case 0x91: return(output_control | 0x80);
273
case 0x92: return((nreg >> 0) & 0xFF);
274
case 0x93: return((nreg >> 8) & 0xFF);
275
case 0x94: return(voice_volume);
276
}
277
}
278
279
280
int32 Sound::Flush(int16 *SoundBuf, const int32 MaxSoundFrames)
281
{
282
int32 FrameCount = 0;
283
284
Update();
285
286
if(SoundBuf)
287
{
288
for(int y = 0; y < 2; y++)
289
{
290
sbuf[y]->end_frame(sys->cpu.timestamp);
291
FrameCount = sbuf[y]->read_samples(SoundBuf + y, MaxSoundFrames, true);
292
}
293
}
294
295
last_ts = 0;
296
297
return(FrameCount);
298
}
299
300
// Call before wsRAM is updated
301
void Sound::CheckRAMWrite(uint32 A)
302
{
303
if((A >> 6) == SampleRAMPos)
304
Update();
305
}
306
307
Sound::Sound()
308
{
309
for(int i = 0; i < 2; i++)
310
{
311
sbuf[i] = new Blip_Buffer();
312
313
sbuf[i]->set_sample_rate(0 ? 0 : 44100, 60);
314
sbuf[i]->clock_rate((long)(3072000));
315
sbuf[i]->bass_freq(20);
316
}
317
318
double eff_volume = 0.1; //TOOLOUD 1.0 / 4;
319
320
WaveSynth.volume(eff_volume);
321
NoiseSynth.volume(eff_volume);
322
VoiceSynth.volume(eff_volume);
323
324
SetRate(44100);
325
}
326
327
Sound::~Sound()
328
{
329
for(int i = 0; i < 2; i++)
330
{
331
if(sbuf[i])
332
{
333
delete sbuf[i];
334
sbuf[i] = nullptr;
335
}
336
}
337
338
}
339
340
bool Sound::SetRate(uint32 rate)
341
{
342
for(int i = 0; i < 2; i++)
343
sbuf[i]->set_sample_rate(rate?rate:44100, 60);
344
345
return(TRUE);
346
}
347
348
void Sound::Reset()
349
{
350
std::memset(period, 0, sizeof(period));
351
std::memset(volume, 0, sizeof(volume));
352
voice_volume = 0;
353
sweep_step = 0;
354
sweep_value = 0;
355
noise_control = 0;
356
control = 0;
357
output_control = 0;
358
359
sweep_8192_divider = 8192;
360
sweep_counter = 0;
361
SampleRAMPos = 0;
362
std::memset(period_counter, 0, sizeof(period_counter));
363
std::memset(sample_pos, 0, sizeof(sample_pos));
364
nreg = 0;
365
366
std::memset(sample_cache, 0, sizeof(sample_cache));
367
std::memset(last_val, 0, sizeof(last_val));
368
last_v_val = 0;
369
370
HyperVoice = 0;
371
last_hv_val = 0;
372
373
for(int y = 0; y < 2; y++)
374
sbuf[y]->clear();
375
}
376
377
SYNCFUNC(Sound)
378
{
379
// don't need to save any of the blip stuff
380
381
NSS(period);
382
NSS(volume);
383
NSS(voice_volume);
384
385
NSS(sweep_step);
386
NSS(sweep_value);
387
NSS(noise_control);
388
NSS(control);
389
NSS(output_control);
390
391
NSS(sweep_8192_divider);
392
NSS(sweep_counter);
393
NSS(SampleRAMPos);
394
395
NSS(sample_cache);
396
397
NSS(last_v_val);
398
399
NSS(HyperVoice);
400
NSS(last_hv_val);
401
402
NSS(period_counter);
403
NSS(last_val);
404
NSS(sample_pos);
405
NSS(nreg);
406
NSS(last_ts);
407
}
408
}
409
410