Debian Codegraph @

src/engine/platform/sound/nes_nsfplay/5e01_apu.cpp

raw
1 //
2 // I5E01 2A03
3 //
4 #include <assert.h>
5 #include "5e01_apu.h"
6 #include "common.h"
7
8 namespace xgm
9 {
10 void I5E01_APU::sweep_sqr(int i)
11 {
12 int shifted = freq[i] >> sweep_amount[i];
13 if (i == 0 && sweep_mode[i]) shifted += 1;
14 sfreq[i] = freq[i] + (sweep_mode[i] ? -shifted : shifted);
15 //DEBUG_OUT("shifted[%d] = %d (%d >> %d)\n",i,shifted,freq[i],sweep_amount[i]);
16 }
17
18 void I5E01_APU::FrameSequence(int s)
19 {
20 //DEBUG_OUT("FrameSequence(%d)\n",s);
21
22 if (s > 3) return; // no operation in step 4
23
24 // 240hz clock
25 for (int i = 0; i < 2; ++i)
26 {
27 bool divider = false;
28 if (envelope_write[i])
29 {
30 envelope_write[i] = false;
31 envelope_counter[i] = 15;
32 envelope_div[i] = 0;
33 }
34 else
35 {
36 ++envelope_div[i];
37 if (envelope_div[i] > envelope_div_period[i])
38 {
39 divider = true;
40 envelope_div[i] = 0;
41 }
42 }
43 if (divider)
44 {
45 if (envelope_loop[i] && envelope_counter[i] == 0)
46 envelope_counter[i] = 15;
47 else if (envelope_counter[i] > 0)
48 --envelope_counter[i];
49 }
50 }
51
52 // 120hz clock
53 if ((s & 1) == 0)
54 for (int i = 0; i < 2; ++i)
55 {
56 if (!envelope_loop[i] && (length_counter[i] > 0))
57 --length_counter[i];
58
59 if (sweep_enable[i])
60 {
61 //DEBUG_OUT("Clock sweep: %d\n", i);
62
63 --sweep_div[i];
64 if (sweep_div[i] <= 0)
65 {
66 sweep_sqr(i); // calculate new sweep target
67
68 //DEBUG_OUT("sweep_div[%d] (0/%d)\n",i,sweep_div_period[i]);
69 //DEBUG_OUT("freq[%d]=%d > sfreq[%d]=%d\n",i,freq[i],i,sfreq[i]);
70
71 if (freq[i] >= 8 && sfreq[i] < 0x800 && sweep_amount[i] > 0) // update frequency if appropriate
72 {
73 freq[i] = sfreq[i] < 0 ? 0 : sfreq[i];
74 }
75 sweep_div[i] = sweep_div_period[i] + 1;
76
77 //DEBUG_OUT("freq[%d]=%d\n",i,freq[i]);
78 }
79
80 if (sweep_write[i])
81 {
82 sweep_div[i] = sweep_div_period[i] + 1;
83 sweep_write[i] = false;
84 }
85 }
86 }
87
88 }
89
90 int I5E01_APU::calc_sqr(int i, unsigned int clocks)
91 {
92 static const short sqrtbl[4][16] = {
93 {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
94 {0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
95 {0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
96 {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}
97 };
98
99 scounter[i] -= clocks;
100 while (scounter[i] < 0)
101 {
102 sphase[i] = (sphase[i] + 1) & 15;
103 scounter[i] += freq[i] + 1;
104 }
105
106 int ret = 0;
107 if (length_counter[i] > 0 &&
108 freq[i] >= 8 &&
109 sfreq[i] < 0x800
110 )
111 {
112 int v = envelope_disable[i] ? volume[i] : envelope_counter[i];
113 ret = sqrtbl[duty[i]][sphase[i]] ? v : 0;
114 }
115
116 return ret;
117 }
118
119 bool I5E01_APU::Read(unsigned int adr, unsigned int& val, unsigned int id)
120 {
121 if (0x4100 <= adr && adr < 0x4108)
122 {
123 val |= reg[adr & 0x7];
124 return true;
125 }
126 else if (adr == 0x4115)
127 {
128 val |= (length_counter[1] ? 2 : 0) | (length_counter[0] ? 1 : 0);
129 return true;
130 }
131 else
132 return false;
133 }
134
135 void I5E01_APU::Tick(unsigned int clocks)
136 {
137 out[0] = calc_sqr(0, clocks);
138 out[1] = calc_sqr(1, clocks);
139 }
140
141 // 生成される波形の振幅は0-8191
142 unsigned int I5E01_APU::Render(int b[2])
143 {
144 out[0] = (mask & 1) ? 0 : out[0];
145 out[1] = (mask & 2) ? 0 : out[1];
146
147 int m[2];
148
149 if (option[OPT_NONLINEAR_MIXER])
150 {
151 int voltage = square_table[out[0] + out[1]];
152 m[0] = out[0] << 6;
153 m[1] = out[1] << 6;
154 int ref = m[0] + m[1];
155 if (ref > 0)
156 {
157 m[0] = (m[0] * voltage) / ref;
158 m[1] = (m[1] * voltage) / ref;
159 }
160 else
161 {
162 m[0] = voltage;
163 m[1] = voltage;
164 }
165 }
166 else
167 {
168 m[0] = (out[0] * square_linear) / 15;
169 m[1] = (out[1] * square_linear) / 15;
170 }
171
172 b[0] = m[0] * sm[0][0];
173 b[0] += m[1] * sm[0][1];
174 b[0] >>= 7;
175
176 b[1] = m[0] * sm[1][0];
177 b[1] += m[1] * sm[1][1];
178 b[1] >>= 7;
179
180 return 2;
181 }
182
183 I5E01_APU::I5E01_APU()
184 {
185 SetClock(DEFAULT_CLOCK);
186 SetRate(DEFAULT_RATE);
187 option[OPT_UNMUTE_ON_RESET] = true;
188 option[OPT_PHASE_REFRESH] = true;
189 option[OPT_NONLINEAR_MIXER] = true;
190 option[OPT_DUTY_SWAP] = false;
191 option[OPT_NEGATE_SWEEP_INIT] = false;
192
193 square_table[0] = 0;
194 for (int i = 1;i < 32;i++)
195 square_table[i] = (int)((8192.0 * 95.88) / (8128.0 / i + 100));
196
197 square_linear = square_table[15]; // match linear scale to one full volume square of nonlinear
198
199 for (int c = 0;c < 2;++c)
200 for (int t = 0;t < 2;++t)
201 sm[c][t] = 128;
202 }
203
204 I5E01_APU::~I5E01_APU()
205 {
206 }
207
208 void I5E01_APU::Reset()
209 {
210 int i;
211 gclock = 0;
212 mask = 0;
213
214 for (int i = 0; i < 2; ++i)
215 {
216 scounter[i] = 0;
217 sphase[i] = 0;
218 duty[i] = 0;
219 volume[i] = 0;
220 freq[i] = 0;
221 sfreq[i] = 0;
222 sweep_enable[i] = 0;
223 sweep_mode[i] = 0;
224 sweep_write[i] = 0;
225 sweep_div_period[i] = 0;
226 sweep_div[i] = 1;
227 sweep_amount[i] = 0;
228 envelope_disable[i] = 0;
229 envelope_loop[i] = 0;
230 envelope_write[i] = 0;
231 envelope_div_period[i] = 0;
232 envelope_div[0] = 0;
233 envelope_counter[i] = 0;
234 length_counter[i] = 0;
235 enable[i] = 0;
236 }
237
238 for (i = 0x4100; i < 0x4108; i++)
239 Write(i, 0);
240
241 Write(0x4115, 0);
242 if (option[OPT_UNMUTE_ON_RESET])
243 Write(0x4115, 0x0f);
244 if (option[OPT_NEGATE_SWEEP_INIT])
245 {
246 Write(0x4101, 0x08);
247 Write(0x4105, 0x08);
248 }
249
250 for (i = 0; i < 2; i++)
251 out[i] = 0;
252
253 SetRate(rate);
254 }
255
256 void I5E01_APU::SetOption(int id, int val)
257 {
258 if (id < OPT_END) option[id] = val;
259 }
260
261 void I5E01_APU::SetClock(double c)
262 {
263 clock = c;
264 }
265
266 void I5E01_APU::SetRate(double r)
267 {
268 rate = r ? r : DEFAULT_RATE;
269 }
270
271 void I5E01_APU::SetStereoMix(int trk, short mixl, short mixr)
272 {
273 if (trk < 0) return;
274 if (trk > 1) return;
275 sm[0][trk] = mixl;
276 sm[1][trk] = mixr;
277 }
278
279 double I5E01_APU::GetFrequencyPulse1() const // // !!
280 {
281 if (!(length_counter[0] > 0 &&
282 freq[0] >= 8 &&
283 sfreq[0] < 0x800))
284 return 0.0;
285 return clock / 16 / (freq[0] + 1);
286 }
287
288 double I5E01_APU::GetFrequencyPulse2() const // // !!
289 {
290 if (!(length_counter[1] > 0 &&
291 freq[1] >= 8 &&
292 sfreq[1] < 0x800))
293 return 0.0;
294 return clock / 16 / (freq[1] + 1);
295 }
296
297 bool I5E01_APU::Write(unsigned int adr, unsigned int val, unsigned int id)
298 {
299 int ch;
300
301 // hack
302 adr+=0x100;
303
304 static const unsigned char length_table[32] = {
305 0x0A, 0xFE,
306 0x14, 0x02,
307 0x28, 0x04,
308 0x50, 0x06,
309 0xA0, 0x08,
310 0x3C, 0x0A,
311 0x0E, 0x0C,
312 0x1A, 0x0E,
313 0x0C, 0x10,
314 0x18, 0x12,
315 0x30, 0x14,
316 0x60, 0x16,
317 0xC0, 0x18,
318 0x48, 0x1A,
319 0x10, 0x1C,
320 0x20, 0x1E
321 };
322
323 if (0x4100 <= adr && adr < 0x4108)
324 {
325 //DEBUG_OUT("$%04X = %02X\n",adr,val);
326
327 adr &= 0xf;
328 ch = adr >> 2;
329 switch (adr)
330 {
331 case 0x0:
332 case 0x4:
333 volume[ch] = val & 15;
334 envelope_disable[ch] = (val >> 4) & 1;
335 envelope_loop[ch] = (val >> 5) & 1;
336 envelope_div_period[ch] = (val & 15);
337 duty[ch] = (val >> 6) & 3;
338 if (option[OPT_DUTY_SWAP])
339 {
340 if (duty[ch] == 1) duty[ch] = 2;
341 else if (duty[ch] == 2) duty[ch] = 1;
342 }
343 break;
344
345 case 0x1:
346 case 0x5:
347 sweep_enable[ch] = (val >> 7) & 1;
348 sweep_div_period[ch] = (((val >> 4) & 7));
349 sweep_mode[ch] = (val >> 3) & 1;
350 sweep_amount[ch] = val & 7;
351 sweep_write[ch] = true;
352 sweep_sqr(ch);
353 break;
354
355 case 0x2:
356 case 0x6:
357 freq[ch] = val | (freq[ch] & 0x700);
358 sweep_sqr(ch);
359 break;
360
361 case 0x3:
362 case 0x7:
363 freq[ch] = (freq[ch] & 0xFF) | ((val & 0x7) << 8);
364 if (option[OPT_PHASE_REFRESH])
365 sphase[ch] = 0;
366 envelope_write[ch] = true;
367 if (enable[ch])
368 {
369 length_counter[ch] = length_table[(val >> 3) & 0x1f];
370 }
371 sweep_sqr(ch);
372 break;
373
374 default:
375 return false;
376 }
377 reg[adr] = val;
378 return true;
379 }
380 else if (adr == 0x4115)
381 {
382 enable[0] = (val & 1) ? true : false;
383 enable[1] = (val & 2) ? true : false;
384
385 if (!enable[0])
386 length_counter[0] = 0;
387 if (!enable[1])
388 length_counter[1] = 0;
389
390 reg[adr - 0x4100] = val;
391 return true;
392 }
393
394 // 4017 is handled in nes_dmc.cpp
395 //else if (adr == 0x4017)
396 //{
397 //}
398
399 return false;
400 }
401 } // namespace xgm;