summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Nes_Vrc7_Apu.cpp
blob: 033061d6adcddc7830690c3c196e143418f7661b (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
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
#include "Nes_Vrc7_Apu.h"

#include "ym2413.h"
#include <string.h>

#include "blargg_source.h"

int const period = 36; // NES CPU clocks per FM clock

Nes_Vrc7_Apu::Nes_Vrc7_Apu()
{
	opll = 0;
}

blargg_err_t Nes_Vrc7_Apu::init()
{
	CHECK_ALLOC( opll = ym2413_init( 3579545, 3579545 / 72, 1 ) );
	
	set_output( 0 );
	volume( 1.0 );
	reset();
	return 0;
}

Nes_Vrc7_Apu::~Nes_Vrc7_Apu()
{
	if ( opll )
		ym2413_shutdown( opll );
}

void Nes_Vrc7_Apu::set_output( Blip_Buffer* buf )
{
	for ( int i = 0; i < osc_count; ++i )
		oscs [i].output = buf;
	output_changed();
}

void Nes_Vrc7_Apu::output_changed()
{
	mono.output = oscs [0].output;
	for ( int i = osc_count; --i; )
	{
		if ( mono.output != oscs [i].output )
		{
			mono.output = 0;
			break;
		}
	}
	
	if ( mono.output )
	{
		for ( int i = osc_count; --i; )
		{
			mono.last_amp += oscs [i].last_amp;
			oscs [i].last_amp = 0;
		}
	}
}

void Nes_Vrc7_Apu::reset()
{
	addr      = 0;
	next_time = 0;
	mono.last_amp = 0;
	
	for ( int i = osc_count; --i >= 0; )
	{
		Vrc7_Osc& osc = oscs [i];
		osc.last_amp = 0;
		for ( int j = 0; j < 3; ++j )
			osc.regs [j] = 0;
	}

	ym2413_reset_chip( opll );
}

void Nes_Vrc7_Apu::write_reg( int data )
{
	addr = data;
}

void Nes_Vrc7_Apu::write_data( blip_time_t time, int data )
{
	int type = (addr >> 4) - 1;
	int chan = addr & 15;
	if ( (unsigned) type < 3 && chan < osc_count )
		oscs [chan].regs [type] = data;
	
	if ( time > next_time )
		run_until( time );
	ym2413_write( opll, 0, addr );
	ym2413_write( opll, 1, data );
}

void Nes_Vrc7_Apu::end_frame( blip_time_t time )
{
	if ( time > next_time )
		run_until( time );
	
	next_time -= time;
	assert( next_time >= 0 );
	
	for ( int i = osc_count; --i >= 0; )
	{
		Blip_Buffer* output = oscs [i].output;
		if ( output )
			output->set_modified();
	}
}

void Nes_Vrc7_Apu::save_snapshot( vrc7_snapshot_t* out ) const
{
	out->latch = addr;
	out->delay = next_time;
	for ( int i = osc_count; --i >= 0; )
	{
		for ( int j = 0; j < 3; ++j )
			out->regs [i] [j] = oscs [i].regs [j];
	}
	memcpy( out->inst, ym2413_get_inst0( opll ), 8 );
}

void Nes_Vrc7_Apu::load_snapshot( vrc7_snapshot_t const& in )
{
	assert( offsetof (vrc7_snapshot_t,delay) == 28 - 1 );
	
	reset();
	next_time = in.delay;
	write_reg( in.latch );
	int i;
	for ( i = 0; i < osc_count; ++i )
	{
		for ( int j = 0; j < 3; ++j )
			oscs [i].regs [j] = in.regs [i] [j];
	}

	for ( i = 0; i < 8; ++i )
	{
		ym2413_write( opll, 0, i );
		ym2413_write( opll, 1, in.inst [i] );
	}

	for ( i = 0; i < 3; ++i )
	{
		for ( int j = 0; j < 6; ++j )
		{
			ym2413_write( opll, 0, 0x10 + i * 0x10 + j );
			ym2413_write( opll, 1, oscs [j].regs [i] );
		}
	}
}

void Nes_Vrc7_Apu::run_until( blip_time_t end_time )
{
	require( end_time > next_time );

	blip_time_t time = next_time;
	void* opll = this->opll; // cache
	Blip_Buffer* const mono_output = mono.output;
	if ( mono_output )
	{
		// optimal case
		do
		{
			ym2413_advance_lfo( opll );
			int amp = 0;
			for ( int i = 0; i < osc_count; i++ )
				amp += ym2413_calcch( opll, i );
			ym2413_advance( opll );
			int delta = amp - mono.last_amp;
			if ( delta )
			{
				mono.last_amp = amp;
				synth.offset_inline( time, delta, mono_output );
			}
			time += period;
		}
		while ( time < end_time );
	}
	else
	{
		mono.last_amp = 0;
		do
		{
			ym2413_advance_lfo( opll );
			for ( int i = 0; i < osc_count; ++i )
			{
				Vrc7_Osc& osc = oscs [i];
				if ( osc.output )
				{
					int amp = ym2413_calcch( opll, i );
					int delta = amp - osc.last_amp;
					if ( delta )
					{
						osc.last_amp = amp;
						synth.offset( time, delta, osc.output );
					}
				}
			}
			ym2413_advance( opll );
			time += period;
		}
		while ( time < end_time );
	}
	next_time = time;
}