forked from FIX94/fixNES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_fds.c
343 lines (326 loc) · 6.93 KB
/
audio_fds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (C) 2017 FIX94
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <malloc.h>
#include <string.h>
#include "apu.h"
#include "audio_fds.h"
#include "audio.h"
#include "mem.h"
#include "cpu.h"
//used externally
bool fdsEnabled;
uint8_t fdsOut;
//65536(clock base)*12(ntsc cpu cycle timer)=786432
static const uint32_t fdsClockDiv = 65536*12;
static uint8_t fds_wave[0x40];
static uint8_t fds_modulation[0x40];
static uint8_t fdsCurWavePos, fdsCurModPos;
static uint8_t fdsCurWave;
static uint8_t fdsEnvSpeed;
static uint8_t fdsVolEnvSpeed, fdsVolEnvGain, fdsVolEnvMode;
static uint8_t fdsSweepSpeed, fdsSweepGain, fdsSweepMode;
static uint8_t fdsMasterVol, fdsCurVol;
static int8_t fdsSweepBias;
static uint16_t fdsFreq, fdsModFreq;
static int16_t fdsModChange;
static uint32_t fdsCurMasterClock, fdsCurModClock, fdsCurVolEnvClock, fdsCurSweepClock;
static uint32_t fdsMasterClock, fdsModClock, fdsVolEnvClock, fdsSweepClock;
static bool fdsMasterEnable;
static bool fdsEnvEnabled;
static bool fdsEnvMasterEnable;
static bool fdsSweepEnabled;
static bool fdsModEnabled;
static bool fdsWavWrite;
static void fdsUpdateClockRates()
{
if(fdsFreq && (fdsFreq + fdsModChange))
fdsMasterClock = fdsClockDiv / (fdsFreq + fdsModChange);
else
fdsMasterClock = 0;
if(fdsModFreq)
fdsModClock = fdsClockDiv / fdsModFreq;
else
fdsModClock = 0;
fdsVolEnvClock = 8 * fdsEnvSpeed * (fdsVolEnvSpeed + 1);
fdsSweepClock = 8 * fdsEnvSpeed * (fdsSweepSpeed + 1);
}
void fdsAudioInit()
{
memset(fds_wave, 0, 0x40);
memset(fds_modulation, 0, 0x40);
fdsEnabled = true;
fdsOut = 0;
fdsCurWavePos = 0, fdsCurModPos = 0;
fdsCurWave = 0;
fdsEnvSpeed = 0xE8; //from FDS BIOS
fdsVolEnvSpeed = 0, fdsVolEnvGain = 0, fdsVolEnvMode = 0;
fdsSweepSpeed = 0, fdsSweepGain = 0, fdsSweepMode = 0;
fdsMasterVol = 0, fdsCurVol = 0;
fdsSweepBias = 0;
fdsFreq = 0, fdsModFreq = 0;
fdsModChange = 0;
fdsUpdateClockRates();
fdsCurMasterClock = fdsMasterClock;
fdsCurModClock = fdsModClock;
fdsCurVolEnvClock = fdsVolEnvClock;
fdsCurSweepClock = fdsSweepClock;
fdsMasterEnable = false;
fdsEnvEnabled = false;
fdsEnvMasterEnable = false;
fdsSweepEnabled = false;
fdsModEnabled = false;
fdsWavWrite = false;
}
void fdsAudioCycle()
{
if(fdsWavWrite)
return;
if(fdsCurVol > 0x20)
fdsCurVol = 0x20;
uint16_t tmp = (((fdsCurWave&0x3F)*(fdsCurVol)));
if(fdsMasterVol == 1)
tmp = tmp*20/30;
else if(fdsMasterVol == 2)
tmp = tmp*15/30;
else if(fdsMasterVol == 3)
tmp = tmp*12/30;
fdsOut = (tmp>>5)&0x3F;
}
void fdsAudioClockTimers()
{
if(!fdsMasterEnable)
return;
if(fdsCurVolEnvClock)
fdsCurVolEnvClock--;
else if(fdsEnvEnabled && fdsEnvMasterEnable)
{
fdsCurVolEnvClock = fdsVolEnvClock;
//do env stuff
if(fdsVolEnvMode == 1)
{
if(fdsVolEnvGain < 0x20)
fdsVolEnvGain++;
}
else
{
if(fdsVolEnvGain > 0)
fdsVolEnvGain--;
}
fdsCurVol = fdsVolEnvGain;
if(fdsCurVol > 0x20)
fdsCurVol = 0x20;
}
if(fdsCurSweepClock)
fdsCurSweepClock--;
else if(fdsSweepEnabled)
{
fdsCurSweepClock = fdsSweepClock;
//do sweep stuff
if(fdsSweepMode == 1)
{
if(fdsSweepGain < 0x20)
fdsSweepGain++;
}
else
{
if(fdsSweepGain > 0)
fdsSweepGain--;
}
}
}
void fdsAudioMasterUpdate()
{
if(!fdsMasterEnable)
return;
if(fdsCurMasterClock)
fdsCurMasterClock--;
else if(fdsMasterClock)
{
fdsCurMasterClock = fdsMasterClock;
//do master stuff
fdsCurWave = fds_wave[fdsCurWavePos];
fdsCurWavePos++;
if(fdsCurWavePos >= 0x40)
fdsCurWavePos = 0;
}
if(fdsCurModClock)
fdsCurModClock--;
else if(fdsModEnabled && fdsModClock)
{
fdsCurModClock = fdsModClock;
//do mod stuff
switch((fds_modulation[fdsCurModPos]&7))
{
case 1:
fdsSweepBias++;
break;
case 2:
fdsSweepBias += 2;
break;
case 3:
fdsSweepBias += 4;
break;
case 4:
fdsSweepBias = 0;
break;
case 5:
fdsSweepBias -= 4;
break;
case 6:
fdsSweepBias -= 2;
break;
case 7:
fdsSweepBias -= 1;
break;
default:
break;
}
fdsCurModPos++;
if(fdsCurModPos >= 0x40)
fdsCurModPos = 0;
if(fdsSweepBias & 0x40) //7-bit signed
fdsSweepBias |= 0x80;
else
fdsSweepBias &= ~0x80;
// from https://forums.nesdev.com/viewtopic.php?f=3&t=10233
// 1. multiply bias by gain, lose lowest 4 bits of result but "round" in a strange way
int16_t temp = fdsSweepBias * fdsSweepGain;
uint8_t remainder = temp & 0xF;
temp >>= 4;
if((remainder > 0) && ((temp & 0x80) == 0))
{
if (fdsSweepBias < 0)
temp -= 1;
else
temp += 2;
}
// 2. wrap if a certain range is exceeded
if(temp >= 192)
temp -= 256;
else if(temp < -64)
temp += 256;
// 3. multiply result by pitch, then round to nearest while dropping 6 bits
temp = fdsFreq * temp;
remainder = temp & 0x3F;
temp >>= 6;
if(remainder >= 32)
temp += 1;
// final mod result is in temp
fdsModChange = temp;
fdsUpdateClockRates();
}
}
void fdsAudioSet8(uint8_t reg, uint8_t val)
{
if(reg == 0)
{
fdsEnvEnabled = ((val&0x80) == 0);
fdsVolEnvMode = ((val&0x40) != 0);
if(fdsEnvEnabled)
{
fdsVolEnvSpeed = val&0x3F;
fdsUpdateClockRates();
}
else
{
fdsVolEnvGain = val&0x3F;
fdsCurVol = val&0x3F;
if(fdsCurVol > 0x20)
fdsCurVol = 0x20;
}
}
else if(reg == 2)
{
fdsFreq = ((fdsFreq&~0xFF) | val);
fdsUpdateClockRates();
}
else if(reg == 3)
{
fdsMasterEnable = ((val&0x80) == 0);
fdsEnvMasterEnable = ((val&0x40) == 0);
fdsFreq = (fdsFreq&0xFF) | ((val&0xF)<<8);
if(fdsFreq == 0)
fdsMasterEnable = false;
fdsUpdateClockRates();
}
else if(reg == 4)
{
fdsSweepEnabled = ((val&0x80) == 0);
fdsSweepMode = ((val&0x40) != 0);
if(fdsSweepEnabled)
{
fdsSweepSpeed = val&0x3F;
fdsUpdateClockRates();
}
else
fdsSweepGain = val&0x3F;
}
else if(reg == 5)
{
fdsCurModPos = 0;
fdsSweepBias = (val&0x7F);
if(val&0x40) //7-bit signed
fdsSweepBias |= 0x80;
else
fdsSweepBias &= ~0x80;
}
else if(reg == 6)
{
fdsModFreq = ((fdsModFreq&~0xFF) | val);
fdsUpdateClockRates();
}
else if(reg == 7)
{
fdsModEnabled = ((val&0x80) == 0);
fdsModFreq = (fdsModFreq&0xFF) | ((val&0xF)<<8);
if(fdsModFreq == 0)
fdsModEnabled = false;
if(!fdsModEnabled)
fdsModChange = 0;
fdsUpdateClockRates();
}
else if(reg == 8)
{
memmove(fds_modulation, fds_modulation+2, 62);
fds_modulation[0x3E] = val;
fds_modulation[0x3F] = val;
}
else if(reg == 9)
{
fdsWavWrite = ((val&0x80) != 0);
fdsMasterVol = val&3;
}
else if(reg == 0xA)
{
fdsEnvSpeed = val;
fdsUpdateClockRates();
}
}
uint8_t fdsAudioGet8(uint8_t reg)
{
if(reg == 0)
return fdsVolEnvGain|0x40;
else if(reg == 2)
return fdsSweepGain|0x40;
return 0;
}
void fdsAudioSetWave(uint8_t pos, uint8_t val)
{
if(!fdsWavWrite)
return;
fds_wave[pos] = val;
}
uint8_t fdsAudioGetWave(uint8_t pos)
{
return fds_wave[pos];
}
void fdsAudioLenCycle()
{
}