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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
** Nofrendo (c) 1998-2000 Matthew Conte (matt@conte.com)
**
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of version 2 of the GNU Library General
** Public License as published by the Free Software Foundation.
**
** 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
** Library General Public License for more details. To obtain a
** copy of the GNU Library General Public License, write to the Free
** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** Any permitted reproduction of these routines, in whole or in part,
** must bear this legend.
**
**
** mmc5_snd.c
**
** Nintendo MMC5 sound emulation
** $Id: mmc5_snd.c,v 1.1 2003/01/08 07:04:35 tmmm Exp $
*/
#include <string.h>
#include "types.h"
#include "mmc5_snd.h"
#include "nes_apu.h"
/* TODO: encapsulate apu/mmc5 rectangle */
#define APU_OVERSAMPLE
#define APU_VOLUME_DECAY(x) ((x) -= ((x) >> 7))
typedef struct mmc5dac_s
{
int32 output;
boolean enabled;
} mmc5dac_t;
/* look up table madness */
static int32 decay_lut[16];
static int vbl_lut[32];
/* various sound constants for sound emulation */
/* vblank length table used for rectangles, triangle, noise */
static const uint8 vbl_length[32] =
{
5, 127, 10, 1, 19, 2, 40, 3, 80, 4, 30, 5, 7, 6, 13, 7,
6, 8, 12, 9, 24, 10, 48, 11, 96, 12, 36, 13, 8, 14, 16, 15
};
/* ratios of pos/neg pulse for rectangle waves
** 2/16 = 12.5%, 4/16 = 25%, 8/16 = 50%, 12/16 = 75%
** (4-bit adder in rectangles, hence the 16)
*/
static const int duty_lut[4] =
{
2, 4, 8, 12
};
static int32 mmc5_incsize;
static uint8 mul[2];
static mmc5rectangle_t mmc5rect[2];
static mmc5dac_t mmc5dac;
#define MMC5_RECTANGLE_OUTPUT chan->output_vol
static int32 mmc5_rectangle(mmc5rectangle_t *chan)
{
int32 output;
#ifdef APU_OVERSAMPLE
int num_times;
int32 total;
#endif /* APU_OVERSAMPLE */
/* reg0: 0-3=volume, 4=envelope, 5=hold, 6-7=duty cycle
** reg1: 0-2=sweep shifts, 3=sweep inc/dec, 4-6=sweep length, 7=sweep on
** reg2: 8 bits of freq
** reg3: 0-2=high freq, 7-4=vbl length counter
*/
APU_VOLUME_DECAY(chan->output_vol);
if (FALSE == chan->enabled || 0 == chan->vbl_length)
return MMC5_RECTANGLE_OUTPUT;
/* vbl length counter */
if (FALSE == chan->holdnote)
chan->vbl_length--;
/* envelope decay at a rate of (env_delay + 1) / 240 secs */
chan->env_phase -= 4; /* 240/60 */
while (chan->env_phase < 0)
{
chan->env_phase += chan->env_delay;
if (chan->holdnote)
chan->env_vol = (chan->env_vol + 1) & 0x0F;
else if (chan->env_vol < 0x0F)
chan->env_vol++;
}
if (chan->freq < APU_TO_FIXED(4))
return MMC5_RECTANGLE_OUTPUT;
chan->phaseacc -= mmc5_incsize; /* # of cycles per sample */
if (chan->phaseacc >= 0)
return MMC5_RECTANGLE_OUTPUT;
#ifdef APU_OVERSAMPLE
num_times = total = 0;
if (chan->fixed_envelope)
output = chan->volume << 8; /* fixed volume */
else
output = (chan->env_vol ^ 0x0F) << 8;
#endif
while (chan->phaseacc < 0)
{
chan->phaseacc += chan->freq;
chan->adder = (chan->adder + 1) & 0x0F;
#ifdef APU_OVERSAMPLE
if (chan->adder < chan->duty_flip)
total += output;
else
total -= output;
num_times++;
#endif
}
#ifdef APU_OVERSAMPLE
chan->output_vol = total / num_times;
#else
if (chan->fixed_envelope)
output = chan->volume << 8; /* fixed volume */
else
output = (chan->env_vol ^ 0x0F) << 8;
if (0 == chan->adder)
chan->output_vol = output;
else if (chan->adder == chan->duty_flip)
chan->output_vol = -output;
#endif
return MMC5_RECTANGLE_OUTPUT;
}
static uint8 mmc5_read(uint32 address)
{
uint32 retval;
retval = (uint32) (mul[0] * mul[1]);
switch (address)
{
case 0x5205:
return (uint8) retval;
case 0x5206:
return (uint8) (retval >> 8);
default:
return 0xFF;
}
}
/* mix vrcvi sound channels together */
static int32 mmc5_process(void)
{
int32 accum;
accum = mmc5_rectangle(&mmc5rect[0]);
accum += mmc5_rectangle(&mmc5rect[1]);
if (mmc5dac.enabled)
accum += mmc5dac.output;
return accum;
}
/* write to registers */
static void mmc5_write(uint32 address, uint8 value)
{
int chan;
switch (address)
{
/* rectangles */
case MMC5_WRA0:
case MMC5_WRB0:
chan = (address & 4) ? 1 : 0;
mmc5rect[chan].regs[0] = value;
mmc5rect[chan].volume = value & 0x0F;
mmc5rect[chan].env_delay = decay_lut[value & 0x0F];
mmc5rect[chan].holdnote = (value & 0x20) ? TRUE : FALSE;
mmc5rect[chan].fixed_envelope = (value & 0x10) ? TRUE : FALSE;
mmc5rect[chan].duty_flip = duty_lut[value >> 6];
break;
case MMC5_WRA1:
case MMC5_WRB1:
break;
case MMC5_WRA2:
case MMC5_WRB2:
chan = (address & 4) ? 1 : 0;
mmc5rect[chan].regs[2] = value;
if (mmc5rect[chan].enabled)
mmc5rect[chan].freq = APU_TO_FIXED((((mmc5rect[chan].regs[3] & 7) << 8) + value) + 1);
break;
case MMC5_WRA3:
case MMC5_WRB3:
chan = (address & 4) ? 1 : 0;
mmc5rect[chan].regs[3] = value;
if (mmc5rect[chan].enabled)
{
mmc5rect[chan].vbl_length = vbl_lut[value >> 3];
mmc5rect[chan].env_vol = 0;
mmc5rect[chan].freq = APU_TO_FIXED((((value & 7) << 8) + mmc5rect[chan].regs[2]) + 1);
mmc5rect[chan].adder = 0;
}
break;
case MMC5_SMASK:
if (value & 0x01)
mmc5rect[0].enabled = TRUE;
else
{
mmc5rect[0].enabled = FALSE;
mmc5rect[0].vbl_length = 0;
}
if (value & 0x02)
mmc5rect[1].enabled = TRUE;
else
{
mmc5rect[1].enabled = FALSE;
mmc5rect[1].vbl_length = 0;
}
break;
case 0x5010:
if (value & 0x01)
mmc5dac.enabled = TRUE;
else
mmc5dac.enabled = FALSE;
break;
case 0x5011:
mmc5dac.output = (value ^ 0x80) << 8;
break;
case 0x5205:
mul[0] = value;
break;
case 0x5206:
mul[1] = value;
break;
default:
break;
}
}
/* reset state of vrcvi sound channels */
static void mmc5_reset(void)
{
int i;
/* get the phase period from the apu */
mmc5_incsize = apu_getcyclerate();
for (i = 0x5000; i < 0x5008; i++)
mmc5_write(i, 0);
mmc5_write(0x5010, 0);
mmc5_write(0x5011, 0);
}
static void mmc5_init(void)
{
int i;
int num_samples = apu_getcontext()->num_samples;
/* lut used for enveloping and frequency sweeps */
for (i = 0; i < 16; i++)
decay_lut[i] = num_samples * (i + 1);
/* used for note length, based on vblanks and size of audio buffer */
for (i = 0; i < 32; i++)
vbl_lut[i] = vbl_length[i] * num_samples;
}
/* TODO: bleh */
static void mmc5_shutdown(void)
{
}
static apu_memread mmc5_memread[] =
{
{ 0x5205, 0x5206, mmc5_read },
{ -1, -1, NULL }
};
static apu_memwrite mmc5_memwrite[] =
{
{ 0x5000, 0x5015, mmc5_write },
{ 0x5205, 0x5206, mmc5_write },
{ -1, -1, NULL }
};
apuext_t mmc5_ext =
{
mmc5_init,
mmc5_shutdown,
mmc5_reset,
mmc5_process,
mmc5_memread,
mmc5_memwrite
};
/*
** $Log: mmc5_snd.c,v $
** Revision 1.1 2003/01/08 07:04:35 tmmm
** initial import of Nosefart sources
**
** Revision 1.6 2000/07/04 04:51:41 matt
** cleanups
**
** Revision 1.5 2000/07/03 02:18:53 matt
** much better external module exporting
**
** Revision 1.4 2000/06/28 22:03:51 matt
** fixed stupid oversight
**
** Revision 1.3 2000/06/20 20:46:58 matt
** minor cleanups
**
** Revision 1.2 2000/06/20 04:06:16 matt
** migrated external sound definition to apu module
**
** Revision 1.1 2000/06/20 00:06:47 matt
** initial revision
**
*/
|