forked from vgmrips/vgmplay-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnp_nes_apu.c
511 lines (425 loc) · 10.7 KB
/
np_nes_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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//
// NES 2A03
//
// Ported from NSFPlay 2.2 to VGMPlay (including C++ -> C conversion)
// by Valley Bell on 24 September 2013
// Updated to NSFPlay 2.3 on 26 September 2013
// (Note: Encoding is UTF-8)
//#include <assert.h>
#include <stdlib.h>
#include <string.h> // for memset
#include <stddef.h> // for NULL
#include "mamedef.h"
#include "../stdbool.h"
#include "np_nes_apu.h"
// Master Clock: 21477272 (NTSC)
// APU Clock = Master Clock / 12
#define DEFAULT_CLOCK 1789772.0 // not sure if this shouldn't be 1789772,667 instead
#define DEFAULT_RATE 44100
/** Upper half of APU **/
enum
{
OPT_UNMUTE_ON_RESET=0,
OPT_NONLINEAR_MIXER,
OPT_PHASE_REFRESH,
OPT_DUTY_SWAP,
OPT_END
};
enum
{
SQR0_MASK = 1,
SQR1_MASK = 2,
};
// Note: For increased speed, I'll inline all of NSFPlay's Counter member functions.
#define COUNTER_SHIFT 24
typedef struct _Counter Counter;
struct _Counter
{
double ratio;
UINT32 val, step;
};
#define COUNTER_setcycle(cntr, s) (cntr).step = (UINT32)((cntr).ratio / (s + 1))
#define COUNTER_iup(cntr) (cntr).val += (cntr).step
#define COUNTER_value(cntr) ((cntr).val >> COUNTER_SHIFT)
#define COUNTER_init(cntr, clk, rate) \
{ \
(cntr).ratio = (1 << COUNTER_SHIFT) * (1.0 * clk / rate); \
(cntr).step = (UINT32)((cntr).ratio + 0.5); \
(cntr).val = 0; \
}
typedef struct _NES_APU NES_APU;
struct _NES_APU
{
int option[OPT_END]; // 各種オプション
int mask;
INT32 sm[2][2];
UINT32 gclock;
UINT8 reg[0x20];
INT32 out[2];
double rate, clock;
INT32 square_table[32]; // nonlinear mixer
int scounter[2]; // frequency divider
int sphase[2]; // phase counter
int duty[2];
int volume[2];
int freq[2];
int sfreq[2];
bool sweep_enable[2];
bool sweep_mode[2];
bool sweep_write[2];
int sweep_div_period[2];
int sweep_div[2];
int sweep_amount[2];
bool envelope_disable[2];
bool envelope_loop[2];
bool envelope_write[2];
int envelope_div_period[2];
int envelope_div[2];
int envelope_counter[2];
int length_counter[2];
bool enable[2];
Counter tick_count;
UINT32 tick_last;
};
static void sweep_sqr(NES_APU* apu, int ch); // calculates target sweep frequency
static INT32 calc_sqr(NES_APU* apu, int ch, UINT32 clocks);
static void Tick(NES_APU* apu, UINT32 clocks);
static void sweep_sqr(NES_APU* apu, int i)
{
int shifted = apu->freq[i] >> apu->sweep_amount[i];
if (i == 0 && apu->sweep_mode[i]) shifted += 1;
apu->sfreq[i] = apu->freq[i] + (apu->sweep_mode[i] ? -shifted : shifted);
//DEBUG_OUT("shifted[%d] = %d (%d >> %d)\n",i,shifted,apu->freq[i],apu->sweep_amount[i]);
}
void NES_APU_np_FrameSequence(void* chip, int s)
{
NES_APU* apu = (NES_APU*)chip;
int i;
//DEBUG_OUT("FrameSequence(%d)\n",s);
if (s > 3) return; // no operation in step 4
// 240hz clock
for (i=0; i < 2; ++i)
{
bool divider = false;
if (apu->envelope_write[i])
{
apu->envelope_write[i] = false;
apu->envelope_counter[i] = 15;
apu->envelope_div[i] = 0;
}
else
{
++apu->envelope_div[i];
if (apu->envelope_div[i] > apu->envelope_div_period[i])
{
divider = true;
apu->envelope_div[i] = 0;
}
}
if (divider)
{
if (apu->envelope_loop[i] && apu->envelope_counter[i] == 0)
apu->envelope_counter[i] = 15;
else if (apu->envelope_counter[i] > 0)
--apu->envelope_counter[i];
}
}
// 120hz clock
if ((s&1) == 0)
for (i=0; i < 2; ++i)
{
if (!apu->envelope_loop[i] && (apu->length_counter[i] > 0))
--apu->length_counter[i];
if (apu->sweep_enable[i])
{
//DEBUG_OUT("Clock sweep: %d\n", i);
--apu->sweep_div[i];
if (apu->sweep_div[i] <= 0)
{
sweep_sqr(apu, i); // calculate new sweep target
//DEBUG_OUT("sweep_div[%d] (0/%d)\n",i,apu->sweep_div_period[i]);
//DEBUG_OUT("freq[%d]=%d > sfreq[%d]=%d\n",i,apu->freq[i],i,apu->sfreq[i]);
if (apu->freq[i] >= 8 && apu->sfreq[i] < 0x800 && apu->sweep_amount[i] > 0) // update frequency if appropriate
{
apu->freq[i] = apu->sfreq[i] < 0 ? 0 : apu->sfreq[i];
if (apu->scounter[i] > apu->freq[i]) apu->scounter[i] = apu->freq[i];
}
apu->sweep_div[i] = apu->sweep_div_period[i] + 1;
//DEBUG_OUT("freq[%d]=%d\n",i,apu->freq[i]);
}
if (apu->sweep_write[i])
{
apu->sweep_div[i] = apu->sweep_div_period[i] + 1;
apu->sweep_write[i] = false;
}
}
}
}
static INT32 calc_sqr(NES_APU* apu, int i, UINT32 clocks)
{
static const INT16 sqrtbl[4][16] = {
{0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
INT32 ret = 0;
apu->scounter[i] += clocks;
while (apu->scounter[i] > apu->freq[i])
{
apu->sphase[i] = (apu->sphase[i] + 1) & 15;
apu->scounter[i] -= (apu->freq[i] + 1);
}
//INT32 ret = 0;
if (apu->length_counter[i] > 0 &&
apu->freq[i] >= 8 &&
apu->sfreq[i] < 0x800
)
{
int v = apu->envelope_disable[i] ? apu->volume[i] : apu->envelope_counter[i];
ret = sqrtbl[apu->duty[i]][apu->sphase[i]] ? v : 0;
}
return ret;
}
bool NES_APU_np_Read(void* chip, UINT32 adr, UINT32* val)
{
NES_APU* apu = (NES_APU*)chip;
if (0x4000 <= adr && adr < 0x4008)
{
*val |= apu->reg[adr&0x7];
return true;
}
else if(adr==0x4015)
{
*val |= (apu->length_counter[1]?2:0)|(apu->length_counter[0]?1:0);
return true;
}
else
return false;
}
static void Tick(NES_APU* apu, UINT32 clocks)
{
apu->out[0] = calc_sqr(apu, 0, clocks);
apu->out[1] = calc_sqr(apu, 1, clocks);
}
// 生成される波形の振幅は0-8191
UINT32 NES_APU_np_Render(void* chip, INT32 b[2])
{
NES_APU* apu = (NES_APU*)chip;
INT32 m[2];
COUNTER_iup(apu->tick_count);
Tick(apu, (COUNTER_value(apu->tick_count) - apu->tick_last) & 0xFF);
apu->tick_last = COUNTER_value(apu->tick_count);
apu->out[0] = (apu->mask & 1) ? 0 : apu->out[0];
apu->out[1] = (apu->mask & 2) ? 0 : apu->out[1];
if(apu->option[OPT_NONLINEAR_MIXER])
{
INT32 voltage;
INT32 ref;
voltage = apu->square_table[apu->out[0] + apu->out[1]];
m[0] = apu->out[0] << 6;
m[1] = apu->out[1] << 6;
ref = m[0] + m[1];
if (ref > 0)
{
m[0] = (m[0] * voltage) / ref;
m[1] = (m[1] * voltage) / ref;
}
else
{
m[0] = voltage;
m[1] = voltage;
}
}
else
{
m[0] = apu->out[0] << 6;
m[1] = apu->out[1] << 6;
}
// Shifting is (x-2) to match the volume of MAME's NES APU sound core
b[0] = m[0] * apu->sm[0][0];
b[0] += m[1] * apu->sm[0][1];
b[0] >>= 7-2; // was 7, but is now 8 for bipolar square
b[1] = m[0] * apu->sm[1][0];
b[1] += m[1] * apu->sm[1][1];
b[1] >>= 7-2; // see above
return 2;
}
void* NES_APU_np_Create(int clock, int rate)
{
NES_APU* apu;
int i, c, t;
apu = (NES_APU*)malloc(sizeof(NES_APU));
if (apu == NULL)
return NULL;
memset(apu, 0x00, sizeof(NES_APU));
//NES_APU_np_SetClock(apu, DEFAULT_CLOCK);
//NES_APU_np_SetRate(apu, DEFAULT_RATE);
NES_APU_np_SetClock(apu, clock);
NES_APU_np_SetRate(apu, rate);
apu->option[OPT_UNMUTE_ON_RESET] = true;
apu->option[OPT_PHASE_REFRESH] = true;
apu->option[OPT_NONLINEAR_MIXER] = true;
apu->option[OPT_DUTY_SWAP] = false;
apu->square_table[0] = 0;
for(i=1;i<32;i++)
apu->square_table[i]=(INT32)((8192.0*95.88)/(8128.0/i+100));
for(c=0;c<2;++c)
for(t=0;t<2;++t)
apu->sm[c][t] = 128;
return apu;
}
void NES_APU_np_Destroy(void* chip)
{
free(chip);
}
void NES_APU_np_Reset(void* chip)
{
NES_APU* apu = (NES_APU*)chip;
int i;
apu->gclock = 0;
apu->mask = 0;
apu->scounter[0] = 0;
apu->scounter[1] = 0;
apu->sphase[0] = 0;
apu->sphase[0] = 0;
apu->sweep_div[0] = 1;
apu->sweep_div[1] = 1;
apu->envelope_div[0] = 0;
apu->envelope_div[1] = 0;
apu->length_counter[0] = 0;
apu->length_counter[1] = 0;
apu->envelope_counter[0] = 0;
apu->envelope_counter[1] = 0;
for (i = 0x4000; i < 0x4008; i++)
NES_APU_np_Write(apu, i, 0);
NES_APU_np_Write(apu, 0x4015, 0);
if (apu->option[OPT_UNMUTE_ON_RESET])
NES_APU_np_Write(apu, 0x4015, 0x0f);
for (i = 0; i < 2; i++)
apu->out[i] = 0;
NES_APU_np_SetRate(apu, apu->rate);
}
void NES_APU_np_SetOption(void* chip, int id, int val)
{
NES_APU* apu = (NES_APU*)chip;
if(id<OPT_END) apu->option[id] = val;
}
void NES_APU_np_SetClock(void* chip, double c)
{
NES_APU* apu = (NES_APU*)chip;
apu->clock = c;
}
void NES_APU_np_SetRate(void* chip, double r)
{
NES_APU* apu = (NES_APU*)chip;
apu->rate = r ? r : DEFAULT_RATE;
COUNTER_init(apu->tick_count, apu->clock, apu->rate);
apu->tick_last = 0;
}
void NES_APU_np_SetMask(void* chip, int m)
{
NES_APU* apu = (NES_APU*)chip;
apu->mask = m;
}
void NES_APU_np_SetStereoMix(void* chip, int trk, INT16 mixl, INT16 mixr)
{
NES_APU* apu = (NES_APU*)chip;
if (trk < 0) return;
if (trk > 1) return;
apu->sm[0][trk] = mixl;
apu->sm[1][trk] = mixr;
}
bool NES_APU_np_Write(void* chip, UINT32 adr, UINT32 val)
{
NES_APU* apu = (NES_APU*)chip;
int ch;
static const UINT8 length_table[32] = {
0x0A, 0xFE,
0x14, 0x02,
0x28, 0x04,
0x50, 0x06,
0xA0, 0x08,
0x3C, 0x0A,
0x0E, 0x0C,
0x1A, 0x0E,
0x0C, 0x10,
0x18, 0x12,
0x30, 0x14,
0x60, 0x16,
0xC0, 0x18,
0x48, 0x1A,
0x10, 0x1C,
0x20, 0x1E
};
if (0x4000 <= adr && adr < 0x4008)
{
//DEBUG_OUT("$%04X = %02X\n",adr,val);
adr &= 0xf;
ch = adr >> 2;
switch (adr)
{
case 0x0:
case 0x4:
apu->volume[ch] = val & 15;
apu->envelope_disable[ch] = (val >> 4) & 1;
apu->envelope_loop[ch] = (val >> 5) & 1;
apu->envelope_div_period[ch] = (val & 15);
apu->duty[ch] = (val >> 6) & 3;
if (apu->option[OPT_DUTY_SWAP])
{
if (apu->duty[ch] == 1) apu->duty[ch] = 2;
else if (apu->duty[ch] == 2) apu->duty[ch] = 1;
}
break;
case 0x1:
case 0x5:
apu->sweep_enable[ch] = (val >> 7) & 1;
apu->sweep_div_period[ch] = (((val >> 4) & 7));
apu->sweep_mode[ch] = (val >> 3) & 1;
apu->sweep_amount[ch] = val & 7;
apu->sweep_write[ch] = true;
sweep_sqr(apu, ch);
break;
case 0x2:
case 0x6:
apu->freq[ch] = val | (apu->freq[ch] & 0x700) ;
sweep_sqr(apu, ch);
if (apu->scounter[ch] > apu->freq[ch]) apu->scounter[ch] = apu->freq[ch];
break;
case 0x3:
case 0x7:
apu->freq[ch] = (apu->freq[ch] & 0xFF) | ((val & 0x7) << 8) ;
if (apu->option[OPT_PHASE_REFRESH])
apu->sphase[ch] = 0;
apu->envelope_write[ch] = true;
if (apu->enable[ch])
{
apu->length_counter[ch] = length_table[(val >> 3) & 0x1f];
}
sweep_sqr(apu, ch);
if (apu->scounter[ch] > apu->freq[ch]) apu->scounter[ch] = apu->freq[ch];
break;
default:
return false;
}
apu->reg[adr] = val;
return true;
}
else if (adr == 0x4015)
{
apu->enable[0] = (val & 1) ? true : false;
apu->enable[1] = (val & 2) ? true : false;
if (!apu->enable[0])
apu->length_counter[0] = 0;
if (!apu->enable[1])
apu->length_counter[1] = 0;
apu->reg[adr-0x4000] = val;
return true;
}
// 4017 is handled in np_nes_dmc.c
//else if (adr == 0x4017)
//{
//}
return false;
}