forked from libretro/libretro-fceumm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fds_apu.c
322 lines (283 loc) · 7.25 KB
/
fds_apu.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
#ifdef TARGET_GNW
#include "build/config.h"
#endif
#if !defined(TARGET_GNW) || (defined(TARGET_GNW) && defined(ENABLE_EMULATOR_NES) && FORCE_NOFRENDO == 0)
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2002 Xodnizel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Begin FDS sound */
#include <string.h>
#include "fceu-types.h"
#include "x6502.h"
#include "fceu.h"
#include "fceu-sound.h"
#include "fceu-state.h"
#ifndef TARGET_GNW
#define FDSClock (1789772.7272727272727272 / 2)
#else
#define FDSClock (1789000 / 2) // this value allow to reduce audio glitches on G&W with FDS Games
#endif
typedef struct {
int64 cycles; /* Cycles per PCM sample */
int64 count; /* Cycle counter */
int64 envcount; /* Envelope cycle counter */
uint32 b19shiftreg60;
uint32 b24adder66;
uint32 b24latch68;
uint32 b17latch76;
int32 clockcount; /* Counter to divide frequency by 8. */
uint8 b8shiftreg88; /* Modulation register. */
uint8 amplitude[2]; /* Current amplitudes. */
uint8 speedo[2];
uint8 mwcount;
uint8 mwstart;
uint8 mwave[0x20]; /* Modulation waveform */
uint8 cwave[0x40]; /* Game-defined waveform(carrier) */
uint8 SPSG[0xB];
} FDSSOUND;
static FDSSOUND fdso;
#define SPSG fdso.SPSG
#define b19shiftreg60 fdso.b19shiftreg60
#define b24adder66 fdso.b24adder66
#define b24latch68 fdso.b24latch68
#define b17latch76 fdso.b17latch76
#define b8shiftreg88 fdso.b8shiftreg88
#define clockcount fdso.clockcount
#define amplitude fdso.amplitude
#define speedo fdso.speedo
void FDSSoundStateAdd(void) {
AddExState(fdso.cwave, 64, 0, "WAVE");
AddExState(fdso.mwave, 32, 0, "MWAV");
AddExState(amplitude, 2, 0, "AMPL");
AddExState(SPSG, 0xB, 0, "SPSG");
AddExState(&b8shiftreg88, 1, 0, "B88");
AddExState(&clockcount, 4, 1, "CLOC");
AddExState(&b19shiftreg60, 4, 1, "B60");
AddExState(&b24adder66, 4, 1, "B66");
AddExState(&b24latch68, 4, 1, "B68");
AddExState(&b17latch76, 4, 1, "B76");
}
DECLFR(FDSSRead) {
switch (A & 0xF) {
case 0x0: return(amplitude[0] | (X.DB & 0xC0));
case 0x2: return(amplitude[1] | (X.DB & 0xC0));
}
return(X.DB);
}
static void RenderSound(void);
#ifndef TARGET_GNW
static void RenderSoundHQ(void);
#endif
DECLFW(FDSSWrite) {
if (FSettings.SndRate) {
#ifndef TARGET_GNW
if (FSettings.soundq >= 1)
RenderSoundHQ();
else
#endif
RenderSound();
}
A -= 0x4080;
switch (A) {
case 0x0:
case 0x4:
if (V & 0x80)
amplitude[(A & 0xF) >> 2] = V & 0x3F;
break;
case 0x7:
b17latch76 = 0;
SPSG[0x5] = 0;
break;
case 0x8:
b17latch76 = 0;
fdso.mwave[SPSG[0x5] & 0x1F] = V & 0x7;
SPSG[0x5] = (SPSG[0x5] + 1) & 0x1F;
break;
}
SPSG[A] = V;
}
/* $4080 - Fundamental wave amplitude data register 92
* $4082 - Fundamental wave frequency data register 58
* $4083 - Same as $4082($4083 is the upper 4 bits).
*
* $4084 - Modulation amplitude data register 78
* $4086 - Modulation frequency data register 72
* $4087 - Same as $4086($4087 is the upper 4 bits)
*/
static void DoEnv() {
int x;
for (x = 0; x < 2; x++)
if (!(SPSG[x << 2] & 0x80) && !(SPSG[0x3] & 0x40)) {
static int counto[2] = { 0, 0 };
if (counto[x] <= 0) {
if (!(SPSG[x << 2] & 0x80)) {
if (SPSG[x << 2] & 0x40) {
if (amplitude[x] < 0x3F)
amplitude[x]++;
} else {
if (amplitude[x] > 0)
amplitude[x]--;
}
}
counto[x] = (SPSG[x << 2] & 0x3F);
} else
counto[x]--;
}
}
DECLFR(FDSWaveRead) {
return(fdso.cwave[A & 0x3f] | (X.DB & 0xC0));
}
DECLFW(FDSWaveWrite) {
if (SPSG[0x9] & 0x80)
fdso.cwave[A & 0x3f] = V & 0x3F;
}
static int ta;
static INLINE void ClockRise(void) {
if (!clockcount) {
ta++;
b19shiftreg60 = (SPSG[0x2] | ((SPSG[0x3] & 0xF) << 8));
b17latch76 = (SPSG[0x6] | ((SPSG[0x07] & 0xF) << 8)) + b17latch76;
if (!(SPSG[0x7] & 0x80)) {
int t = fdso.mwave[(b17latch76 >> 13) & 0x1F] & 7;
int t2 = amplitude[1];
int adj = 0;
if ((t & 3)) {
if ((t & 4))
adj -= (t2 * ((4 - (t & 3))));
else
adj += (t2 * ((t & 3)));
}
adj *= 2;
if (adj > 0x7F) adj = 0x7F;
if (adj < -0x80) adj = -0x80;
b8shiftreg88 = 0x80 + adj;
} else {
b8shiftreg88 = 0x80;
}
} else {
b19shiftreg60 <<= 1;
b8shiftreg88 >>= 1;
}
b24adder66 = (b24latch68 + b19shiftreg60) & 0x1FFFFFF;
}
static INLINE void ClockFall(void) {
if ((b8shiftreg88 & 1))
b24latch68 = b24adder66;
clockcount = (clockcount + 1) & 7;
}
static INLINE int32 FDSDoSound(void) {
fdso.count += fdso.cycles;
if (fdso.count >= ((int64)1 << 40)) {
dogk:
fdso.count -= (int64)1 << 40;
ClockRise();
ClockFall();
fdso.envcount--;
if (fdso.envcount <= 0) {
fdso.envcount += SPSG[0xA] * 3;
DoEnv();
}
}
if (fdso.count >= 32768) goto dogk;
/* Might need to emulate applying the amplitude to the waveform a bit better... */
{
int k = amplitude[0];
if (k > 0x20) k = 0x20;
return (fdso.cwave[b24latch68 >> 19] * k) * 4 / ((SPSG[0x9] & 0x3) + 2);
}
}
static int32 FBC = 0;
static void RenderSound(void) {
int32 end, start;
int32 x;
start = FBC;
end = (SOUNDTS << 16) / soundtsinc;
if (end <= start)
return;
FBC = end;
if (!(SPSG[0x9] & 0x80))
for (x = start; x < end; x++) {
uint32 t = FDSDoSound();
t += t >> 1;
t >>= 4;
Wave[x >> 4] += t; /* (t>>2)-(t>>3); */ /* >>3; */
}
}
#ifndef TARGET_GNW
static void RenderSoundHQ(void) {
uint32 x;
if (!(SPSG[0x9] & 0x80))
for (x = FBC; x < SOUNDTS; x++) {
uint32 t = FDSDoSound();
t += t >> 1;
WaveHi[x] += t; /* (t<<2)-(t<<1); */
}
FBC = SOUNDTS;
}
static void HQSync(int32 ts) {
FBC = ts;
}
#endif
void FDSSound(int c) {
RenderSound();
FBC = c;
}
static void FDS_ESI(void) {
if (FSettings.SndRate) {
#ifndef TARGET_GNW
if (FSettings.soundq >= 1) {
fdso.cycles = (int64)1 << 39;
} else
#endif
{
fdso.cycles = ((int64)1 << 40) * FDSClock;
fdso.cycles /= FSettings.SndRate * 16;
}
}
#ifndef FCEU_LOW_RAM
SetReadHandler(0x4040, 0x407f, FDSWaveRead);
SetWriteHandler(0x4040, 0x407f, FDSWaveWrite);
SetWriteHandler(0x4080, 0x408A, FDSSWrite);
SetReadHandler(0x4090, 0x4092, FDSSRead);
#endif
}
void FDSSoundReset(void) {
memset(&fdso, 0, sizeof(fdso));
FDS_ESI();
#ifndef TARGET_GNW
GameExpSound.HiSync = HQSync;
GameExpSound.HiFill = RenderSoundHQ;
#endif
GameExpSound.Fill = FDSSound;
GameExpSound.RChange = FDS_ESI;
}
uint8 FDSSoundRead(uint32 A) {
if (A >= 0x4040 && A < 0x4080) return FDSWaveRead(A);
if (A >= 0x4090 && A < 0x4093) return FDSSRead(A);
return X.DB;
}
void FDSSoundWrite(uint32 A, uint8 V) {
if (A >= 0x4040 && A < 0x4080) FDSWaveWrite(A, V);
else if (A >= 0x4080 && A < 0x408B) FDSSWrite(A, V);
}
void FDSSoundPower(void) {
FDSSoundReset();
FDSSoundStateAdd();
}
#endif