summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/nes_fme7_apu.c
blob: 3e47e0b17c5b891182e5b58bca7d6f166517daeb (plain)
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
// Game_Music_Emu 0.5.5. http://www.slack.net/~ant/

#include "nes_fme7_apu.h"

#include <string.h>

/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module 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 Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

#include "blargg_source.h"

void Fme7_init( struct Nes_Fme7_Apu* this )
{
	Synth_init( &this->synth );
	
	Fme7_output( this, NULL );
	Fme7_volume( this, (int)FP_ONE_VOLUME );
	Fme7_reset( this );
}

void Fme7_reset( struct Nes_Fme7_Apu* this )
{
	this->last_time = 0;
	
	int i;
	for ( i = 0; i < fme7_osc_count; i++ )
		this->oscs [i].last_amp = 0;
	
	this->latch = 0;
	memset( this->regs, 0, sizeof this->regs);
	memset( this->phases, 0, sizeof this->phases );
	memset( this->delays, 0, sizeof this->delays );
}

static unsigned char const amp_table [16] =
{
	#define ENTRY( n ) (unsigned char) (n * amp_range + 0.5)
	ENTRY(0.0000), ENTRY(0.0078), ENTRY(0.0110), ENTRY(0.0156),
	ENTRY(0.0221), ENTRY(0.0312), ENTRY(0.0441), ENTRY(0.0624),
	ENTRY(0.0883), ENTRY(0.1249), ENTRY(0.1766), ENTRY(0.2498),
	ENTRY(0.3534), ENTRY(0.4998), ENTRY(0.7070), ENTRY(1.0000)
	#undef ENTRY
};

void Fme7_run_until( struct Nes_Fme7_Apu* this, blip_time_t end_time )
{
	require( end_time >= this->last_time );
	
	int index;
	for ( index = 0; index < fme7_osc_count; index++ )
	{
		int mode = this->regs [7] >> index;
		int vol_mode = this->regs [010 + index];
		int volume = amp_table [vol_mode & 0x0F];
		
		struct Blip_Buffer* const osc_output = this->oscs [index].output;
		if ( !osc_output )
			continue;
		
		// check for unsupported mode
		#ifndef NDEBUG
			if ( (mode & 011) <= 001 && vol_mode & 0x1F )
				debug_printf( "FME7 used unimplemented sound mode: %02X, vol_mode: %02X\n",
						mode, vol_mode & 0x1F );
		#endif
		
		if ( (mode & 001) | (vol_mode & 0x10) )
			volume = 0; // noise and envelope aren't supported
		
		// period
		int const period_factor = 16;
		unsigned period = (this->regs [index * 2 + 1] & 0x0F) * 0x100 * period_factor +
				this->regs [index * 2] * period_factor;
		if ( period < 50 ) // around 22 kHz
		{
			volume = 0;
			if ( !period ) // on my AY-3-8910A, period doesn't have extra one added
				period = period_factor;
		}
		
		// current amplitude
		int amp = volume;
		if ( !this->phases [index] )
			amp = 0;
		
		{
			int delta = amp - this->oscs [index].last_amp;
			if ( delta )
			{
				this->oscs [index].last_amp = amp;
				Blip_set_modified( osc_output );
				Synth_offset( &this->synth, this->last_time, delta, osc_output );
			}
		}
		
		blip_time_t time = this->last_time + this->delays [index];
		if ( time < end_time )
		{
			int delta = amp * 2 - volume;
			Blip_set_modified( osc_output );
			if ( volume )
			{
				do
				{
					delta = -delta;
					Synth_offset_inline( &this->synth, time, delta, osc_output );
					time += period;
				}
				while ( time < end_time );
				
				this->oscs [index].last_amp = (delta + volume) >> 1;
				this->phases [index] = (delta > 0);
			}
			else
			{
				// maintain phase when silent
				int count = (end_time - time + period - 1) / period;
				this->phases [index] ^= count & 1;
				time += count * period;
			}
		}
		
		this->delays [index] = time - end_time;
	}
	
	this->last_time = end_time;
}