Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/quicknes/nes_emu/Nes_Oscs.cpp
2 views
1
2
// Nes_Snd_Emu 0.1.7. http://www.slack.net/~ant/
3
4
#include "Nes_Apu.h"
5
6
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
7
can redistribute it and/or modify it under the terms of the GNU Lesser
8
General Public License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version. This
10
module is distributed in the hope that it will be useful, but WITHOUT ANY
11
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
13
more details. You should have received a copy of the GNU Lesser General
14
Public License along with this module; if not, write to the Free Software
15
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
17
#include "blargg_source.h"
18
19
#ifdef BLARGG_ENABLE_OPTIMIZER
20
#include BLARGG_ENABLE_OPTIMIZER
21
#endif
22
23
// Nes_Osc
24
25
void Nes_Osc::clock_length( int halt_mask )
26
{
27
if ( length_counter && !(regs [0] & halt_mask) )
28
length_counter--;
29
}
30
31
void Nes_Envelope::clock_envelope()
32
{
33
int period = regs [0] & 15;
34
if ( reg_written [3] ) {
35
reg_written [3] = false;
36
env_delay = period;
37
envelope = 15;
38
}
39
else if ( --env_delay < 0 ) {
40
env_delay = period;
41
if ( envelope | (regs [0] & 0x20) )
42
envelope = (envelope - 1) & 15;
43
}
44
}
45
46
int Nes_Envelope::volume() const
47
{
48
return length_counter == 0 ? 0 : (regs [0] & 0x10) ? (regs [0] & 15) : envelope;
49
}
50
51
// Nes_Square
52
53
void Nes_Square::clock_sweep( int negative_adjust )
54
{
55
int sweep = regs [1];
56
57
if ( --sweep_delay < 0 )
58
{
59
reg_written [1] = true;
60
61
int period = this->period();
62
int shift = sweep & shift_mask;
63
if ( shift && (sweep & 0x80) && period >= 8 )
64
{
65
int offset = period >> shift;
66
67
if ( sweep & negate_flag )
68
offset = negative_adjust - offset;
69
70
if ( period + offset < 0x800 )
71
{
72
period += offset;
73
// rewrite period
74
regs [2] = period & 0xff;
75
regs [3] = (regs [3] & ~7) | ((period >> 8) & 7);
76
}
77
}
78
}
79
80
if ( reg_written [1] ) {
81
reg_written [1] = false;
82
sweep_delay = (sweep >> 4) & 7;
83
}
84
}
85
86
// TODO: clean up
87
inline nes_time_t Nes_Square::maintain_phase( nes_time_t time, nes_time_t end_time,
88
nes_time_t timer_period )
89
{
90
long remain = end_time - time;
91
if ( remain > 0 )
92
{
93
int count = (remain + timer_period - 1) / timer_period;
94
phase = (phase + count) & (phase_range - 1);
95
time += (long) count * timer_period;
96
}
97
return time;
98
}
99
100
void Nes_Square::run( nes_time_t time, nes_time_t end_time )
101
{
102
const int period = this->period();
103
const int timer_period = (period + 1) * 2;
104
105
if ( !output )
106
{
107
delay = maintain_phase( time + delay, end_time, timer_period ) - end_time;
108
return;
109
}
110
111
int offset = period >> (regs [1] & shift_mask);
112
if ( regs [1] & negate_flag )
113
offset = 0;
114
115
const int volume = this->volume();
116
if ( volume == 0 || period < 8 || (period + offset) >= 0x800 )
117
{
118
if ( last_amp ) {
119
synth.offset( time, -last_amp, output );
120
last_amp = 0;
121
}
122
123
time += delay;
124
time = maintain_phase( time, end_time, timer_period );
125
}
126
else
127
{
128
// handle duty select
129
int duty_select = (regs [0] >> 6) & 3;
130
int duty = 1 << duty_select; // 1, 2, 4, 2
131
int amp = 0;
132
if ( duty_select == 3 ) {
133
duty = 2; // negated 25%
134
amp = volume;
135
}
136
if ( phase < duty )
137
amp ^= volume;
138
139
int delta = update_amp( amp );
140
if ( delta )
141
synth.offset( time, delta, output );
142
143
time += delay;
144
if ( time < end_time )
145
{
146
Blip_Buffer* const output = this->output;
147
const Synth& synth = this->synth;
148
int delta = amp * 2 - volume;
149
int phase = this->phase;
150
151
do {
152
phase = (phase + 1) & (phase_range - 1);
153
if ( phase == 0 || phase == duty ) {
154
delta = -delta;
155
synth.offset_inline( time, delta, output );
156
}
157
time += timer_period;
158
}
159
while ( time < end_time );
160
161
last_amp = (delta + volume) >> 1;
162
this->phase = phase;
163
}
164
}
165
166
delay = time - end_time;
167
}
168
169
// Nes_Triangle
170
171
void Nes_Triangle::clock_linear_counter()
172
{
173
if ( reg_written [3] )
174
linear_counter = regs [0] & 0x7f;
175
else if ( linear_counter )
176
linear_counter--;
177
178
if ( !(regs [0] & 0x80) )
179
reg_written [3] = false;
180
}
181
182
inline int Nes_Triangle::calc_amp() const
183
{
184
int amp = phase_range - phase;
185
if ( amp < 0 )
186
amp = phase - (phase_range + 1);
187
return amp;
188
}
189
190
// TODO: clean up
191
inline nes_time_t Nes_Triangle::maintain_phase( nes_time_t time, nes_time_t end_time,
192
nes_time_t timer_period )
193
{
194
long remain = end_time - time;
195
if ( remain > 0 )
196
{
197
int count = (remain + timer_period - 1) / timer_period;
198
phase = ((unsigned) phase + 1 - count) & (phase_range * 2 - 1);
199
phase++;
200
time += (long) count * timer_period;
201
}
202
return time;
203
}
204
205
void Nes_Triangle::run( nes_time_t time, nes_time_t end_time )
206
{
207
const int timer_period = period() + 1;
208
if ( !output )
209
{
210
time += delay;
211
delay = 0;
212
if ( length_counter && linear_counter && timer_period >= 3 )
213
delay = maintain_phase( time, end_time, timer_period ) - end_time;
214
return;
215
}
216
217
// to do: track phase when period < 3
218
// to do: Output 7.5 on dac when period < 2? More accurate, but results in more clicks.
219
220
int delta = update_amp( calc_amp() );
221
if ( delta )
222
synth.offset( time, delta, output );
223
224
time += delay;
225
if ( length_counter == 0 || linear_counter == 0 || timer_period < 3 )
226
{
227
time = end_time;
228
}
229
else if ( time < end_time )
230
{
231
Blip_Buffer* const output = this->output;
232
233
int phase = this->phase;
234
int volume = 1;
235
if ( phase > phase_range ) {
236
phase -= phase_range;
237
volume = -volume;
238
}
239
240
do {
241
if ( --phase == 0 ) {
242
phase = phase_range;
243
volume = -volume;
244
}
245
else {
246
synth.offset_inline( time, volume, output );
247
}
248
249
time += timer_period;
250
}
251
while ( time < end_time );
252
253
if ( volume < 0 )
254
phase += phase_range;
255
this->phase = phase;
256
last_amp = calc_amp();
257
}
258
delay = time - end_time;
259
}
260
261
// Nes_Dmc
262
263
void Nes_Dmc::reset()
264
{
265
address = 0;
266
dac = 0;
267
buf = 0;
268
bits_remain = 1;
269
bits = 0;
270
buf_full = false;
271
silence = true;
272
next_irq = Nes_Apu::no_irq;
273
irq_flag = false;
274
irq_enabled = false;
275
276
Nes_Osc::reset();
277
period = 0x1ac;
278
}
279
280
void Nes_Dmc::recalc_irq()
281
{
282
nes_time_t irq = Nes_Apu::no_irq;
283
if ( irq_enabled && length_counter )
284
irq = apu->last_dmc_time + delay +
285
((length_counter - 1) * 8 + bits_remain - 1) * nes_time_t (period) + 1;
286
if ( irq != next_irq ) {
287
next_irq = irq;
288
apu->irq_changed();
289
}
290
}
291
292
int Nes_Dmc::count_reads( nes_time_t time, nes_time_t* last_read ) const
293
{
294
if ( last_read )
295
*last_read = time;
296
297
if ( length_counter == 0 )
298
return 0; // not reading
299
300
long first_read = next_read_time();
301
long avail = time - first_read;
302
if ( avail <= 0 )
303
return 0;
304
305
int count = (avail - 1) / (period * 8) + 1;
306
if ( !(regs [0] & loop_flag) && count > length_counter )
307
count = length_counter;
308
309
if ( last_read ) {
310
*last_read = first_read + (count - 1) * (period * 8) + 1;
311
assert( *last_read <= time );
312
assert( count == count_reads( *last_read, NULL ) );
313
assert( count - 1 == count_reads( *last_read - 1, NULL ) );
314
}
315
316
return count;
317
}
318
319
static const short dmc_period_table [2] [16] = {
320
{0x1ac, 0x17c, 0x154, 0x140, 0x11e, 0x0fe, 0x0e2, 0x0d6, // NTSC
321
0x0be, 0x0a0, 0x08e, 0x080, 0x06a, 0x054, 0x048, 0x036},
322
323
{0x18e, 0x161, 0x13c, 0x129, 0x10a, 0x0ec, 0x0d2, 0x0c7, // PAL (totally untested)
324
0x0b1, 0x095, 0x084, 0x077, 0x062, 0x04e, 0x043, 0x032} // to do: verify PAL periods
325
};
326
327
inline void Nes_Dmc::reload_sample()
328
{
329
address = 0x4000 + regs [2] * 0x40;
330
length_counter = regs [3] * 0x10 + 1;
331
}
332
333
static const unsigned char dac_table [128] =
334
{
335
0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,
336
15,15,16,17,18,19,20,20,21,22,23,24,24,25,26,27,
337
27,28,29,30,31,31,32,33,33,34,35,36,36,37,38,38,
338
39,40,41,41,42,43,43,44,45,45,46,47,47,48,48,49,
339
50,50,51,52,52,53,53,54,55,55,56,56,57,58,58,59,
340
59,60,60,61,61,62,63,63,64,64,65,65,66,66,67,67,
341
68,68,69,70,70,71,71,72,72,73,73,74,74,75,75,75,
342
76,76,77,77,78,78,79,79,80,80,81,81,82,82,82,83,
343
};
344
345
void Nes_Dmc::write_register( int addr, int data )
346
{
347
if ( addr == 0 )
348
{
349
period = dmc_period_table [pal_mode] [data & 15];
350
irq_enabled = (data & 0xc0) == 0x80; // enabled only if loop disabled
351
irq_flag &= irq_enabled;
352
recalc_irq();
353
}
354
else if ( addr == 1 )
355
{
356
int old_dac = dac;
357
dac = data & 0x7F;
358
359
// adjust last_amp so that "pop" amplitude will be properly non-linear
360
// with respect to change in dac
361
int faked_nonlinear = dac - (dac_table [dac] - dac_table [old_dac]);
362
if ( !nonlinear )
363
last_amp = faked_nonlinear;
364
}
365
}
366
367
void Nes_Dmc::start()
368
{
369
reload_sample();
370
fill_buffer();
371
recalc_irq();
372
}
373
374
void Nes_Dmc::fill_buffer()
375
{
376
if ( !buf_full && length_counter )
377
{
378
require( prg_reader ); // prg_reader must be set
379
buf = prg_reader( prg_reader_data, 0x8000u + address );
380
address = (address + 1) & 0x7FFF;
381
buf_full = true;
382
if ( --length_counter == 0 )
383
{
384
if ( regs [0] & loop_flag ) {
385
reload_sample();
386
}
387
else {
388
apu->osc_enables &= ~0x10;
389
irq_flag = irq_enabled;
390
next_irq = Nes_Apu::no_irq;
391
apu->irq_changed();
392
}
393
}
394
}
395
}
396
397
void Nes_Dmc::run( nes_time_t time, nes_time_t end_time )
398
{
399
int delta = update_amp( dac );
400
if ( !output )
401
silence = true;
402
else if ( delta )
403
synth.offset( time, delta, output );
404
405
time += delay;
406
if ( time < end_time )
407
{
408
int bits_remain = this->bits_remain;
409
if ( silence && !buf_full )
410
{
411
int count = (end_time - time + period - 1) / period;
412
bits_remain = (bits_remain - 1 + 8 - (count % 8)) % 8 + 1;
413
time += count * period;
414
}
415
else
416
{
417
Blip_Buffer* const output = this->output;
418
const int period = this->period;
419
int bits = this->bits;
420
int dac = this->dac;
421
422
do
423
{
424
if ( !silence )
425
{
426
int step = (bits & 1) * 4 - 2;
427
bits >>= 1;
428
if ( unsigned (dac + step) <= 0x7F ) {
429
dac += step;
430
synth.offset_inline( time, step, output );
431
}
432
}
433
434
time += period;
435
436
if ( --bits_remain == 0 )
437
{
438
bits_remain = 8;
439
if ( !buf_full ) {
440
silence = true;
441
}
442
else {
443
silence = false;
444
bits = buf;
445
buf_full = false;
446
if ( !output )
447
silence = true;
448
fill_buffer();
449
}
450
}
451
}
452
while ( time < end_time );
453
454
this->dac = dac;
455
this->last_amp = dac;
456
this->bits = bits;
457
}
458
this->bits_remain = bits_remain;
459
}
460
delay = time - end_time;
461
}
462
463
// Nes_Noise
464
465
static const short noise_period_table [16] = {
466
0x004, 0x008, 0x010, 0x020, 0x040, 0x060, 0x080, 0x0A0,
467
0x0CA, 0x0FE, 0x17C, 0x1FC, 0x2FA, 0x3F8, 0x7F2, 0xFE4
468
};
469
470
void Nes_Noise::run( nes_time_t time, nes_time_t end_time )
471
{
472
int period = noise_period_table [regs [2] & 15];
473
#if NES_APU_NOISE_LOW_CPU
474
if ( period < 8 )
475
{
476
period = 8;
477
}
478
#endif
479
480
if ( !output )
481
{
482
// TODO: clean up
483
time += delay;
484
delay = time + (end_time - time + period - 1) / period * period - end_time;
485
return;
486
}
487
488
const int volume = this->volume();
489
int amp = (noise & 1) ? volume : 0;
490
int delta = update_amp( amp );
491
if ( delta )
492
synth.offset( time, delta, output );
493
494
time += delay;
495
if ( time < end_time )
496
{
497
const int mode_flag = 0x80;
498
499
if ( !volume )
500
{
501
// round to next multiple of period
502
time += (end_time - time + period - 1) / period * period;
503
504
// approximate noise cycling while muted, by shuffling up noise register
505
// to do: precise muted noise cycling?
506
if ( !(regs [2] & mode_flag) ) {
507
int feedback = (noise << 13) ^ (noise << 14);
508
noise = (feedback & 0x4000) | (noise >> 1);
509
}
510
}
511
else
512
{
513
Blip_Buffer* const output = this->output;
514
515
// using resampled time avoids conversion in synth.offset()
516
blip_resampled_time_t rperiod = output->resampled_duration( period );
517
blip_resampled_time_t rtime = output->resampled_time( time );
518
519
int noise = this->noise;
520
int delta = amp * 2 - volume;
521
const int tap = (regs [2] & mode_flag ? 8 : 13);
522
523
do {
524
int feedback = (noise << tap) ^ (noise << 14);
525
time += period;
526
527
if ( (noise + 1) & 2 ) {
528
// bits 0 and 1 of noise differ
529
delta = -delta;
530
synth.offset_resampled( rtime, delta, output );
531
}
532
533
rtime += rperiod;
534
noise = (feedback & 0x4000) | (noise >> 1);
535
}
536
while ( time < end_time );
537
538
last_amp = (delta + volume) >> 1;
539
this->noise = noise;
540
}
541
}
542
543
delay = time - end_time;
544
}
545
546
547