summaryrefslogtreecommitdiffstats
path: root/apps/codecs/libgme/nes_fds_apu.c
blob: dc0775d5d33c47e7074a14c675a904e9b386482e (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
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
// Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/

#include "nes_fds_apu.h"

/* Copyright (C) 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"

int const fract_range = 65536;

void Fds_init( struct Nes_Fds_Apu* this )
{
	Synth_init( &this->synth );
		
	this->lfo_tempo = lfo_base_tempo;
	Fds_set_output( this, 0, NULL );
	Fds_volume( this, (int)FP_ONE_VOLUME );
	Fds_reset( this );
}

void Fds_reset( struct Nes_Fds_Apu* this )
{
	memset( this->regs_, 0, sizeof this->regs_ );
	memset( this->mod_wave, 0, sizeof this->mod_wave );
	
	this->last_time     = 0;
	this->env_delay     = 0;
	this->sweep_delay   = 0;
	this->wave_pos      = 0;
	this->last_amp      = 0;
	this->wave_fract    = fract_range;
	this->mod_fract     = fract_range;
	this->mod_pos       = 0;
	this->mod_write_pos = 0;
	
	static byte const initial_regs [0x0B] = {
		0x80,       // disable envelope
		0, 0, 0xC0, // disable wave and lfo
		0x80,       // disable sweep
		0, 0, 0x80, // disable modulation
		0, 0, 0xFF  // LFO period // TODO: use 0xE8 as FDS ROM does?
	};
	int i;
	for ( i = 0; i < (int) sizeof initial_regs; i++ )
	{
		// two writes to set both gain and period for envelope registers
		Fds_write_( this, fds_io_addr + fds_wave_size + i, 0 );
		Fds_write_( this, fds_io_addr + fds_wave_size + i, initial_regs [i] );
	}
}

void Fds_write_( struct Nes_Fds_Apu* this, unsigned addr, int data )
{
	unsigned reg = addr - fds_io_addr;
	if ( reg < fds_io_size )
	{
		if ( reg < fds_wave_size )
		{
			if ( *regs_nes (this, 0x4089) & 0x80 )
				this->regs_ [reg] = data & fds_wave_sample_max;
		}
		else
		{
			this->regs_ [reg] = data;
			switch ( addr )
			{
			case 0x4080:
				if ( data & 0x80 )
					this->env_gain = data & 0x3F;
				else
					this->env_speed = (data & 0x3F) + 1;
				break;
			
			case 0x4084:
				if ( data & 0x80 )
					this->sweep_gain = data & 0x3F;
				else
					this->sweep_speed = (data & 0x3F) + 1;
				break;
			
			case 0x4085:
				this->mod_pos = this->mod_write_pos;
				*regs_nes (this, 0x4085) = data & 0x7F;
				break;
			
			case 0x4088:
				if ( *regs_nes (this, 0x4087) & 0x80 )
				{
					int pos = this->mod_write_pos;
					data &= 0x07;
					this->mod_wave [pos    ] = data;
					this->mod_wave [pos + 1] = data;
					this->mod_write_pos = (pos     + 2) & (fds_wave_size - 1);
					this->mod_pos       = (this->mod_pos + 2) & (fds_wave_size - 1);
				}
				break;
			}
		}
	}
}

void Fds_set_tempo( struct Nes_Fds_Apu* this, int t )
{
	this->lfo_tempo = lfo_base_tempo;
	if ( t != (int)FP_ONE_TEMPO )
	{
		this->lfo_tempo = (int) ((lfo_base_tempo * FP_ONE_TEMPO) / t);
		if ( this->lfo_tempo <= 0 )
			this->lfo_tempo = 1;
	}
}

void Fds_run_until( struct Nes_Fds_Apu* this, blip_time_t final_end_time )
{
	int const wave_freq = (*regs_nes (this, 0x4083) & 0x0F) * 0x100 + *regs_nes (this, 0x4082);
	struct Blip_Buffer* const output_ = this->output_;
	if ( wave_freq && output_ && !((*regs_nes (this, 0x4089) | *regs_nes (this, 0x4083)) & 0x80) )
	{
		Blip_set_modified( output_ );
		
		// master_volume
		#define MVOL_ENTRY( percent ) (fds_master_vol_max * percent + 50) / 100
		static unsigned char const master_volumes [4] = {
			MVOL_ENTRY( 100 ), MVOL_ENTRY( 67 ), MVOL_ENTRY( 50 ), MVOL_ENTRY( 40 )
		};
		int const master_volume = master_volumes [*regs_nes (this, 0x4089) & 0x03];
		
		// lfo_period
		blip_time_t lfo_period = *regs_nes (this, 0x408A) * this->lfo_tempo;
		if ( *regs_nes (this, 0x4083) & 0x40 )
			lfo_period = 0;
		
		// sweep setup
		blip_time_t sweep_time = this->last_time + this->sweep_delay;
		blip_time_t const sweep_period = lfo_period * this->sweep_speed;
		if ( !sweep_period || *regs_nes (this, 0x4084) & 0x80 )
			sweep_time = final_end_time;
		
		// envelope setup
		blip_time_t env_time = this->last_time + this->env_delay;
		blip_time_t const env_period = lfo_period * this->env_speed;
		if ( !env_period || *regs_nes (this, 0x4080) & 0x80 )
			env_time = final_end_time;
		
		// modulation
		int mod_freq = 0;
		if ( !(*regs_nes (this, 0x4087) & 0x80) )
			mod_freq = (*regs_nes (this, 0x4087) & 0x0F) * 0x100 + *regs_nes (this, 0x4086);
		
		blip_time_t end_time = this->last_time;
		do
		{
			// sweep
			if ( sweep_time <= end_time )
			{
				sweep_time += sweep_period;
				int mode = *regs_nes (this, 0x4084) >> 5 & 2;
				int new_sweep_gain = this->sweep_gain + mode - 1;
				if ( (unsigned) new_sweep_gain <= (unsigned) 0x80 >> mode )
					this->sweep_gain = new_sweep_gain;
				else
					*regs_nes (this, 0x4084) |= 0x80; // optimization only
			}
			
			// envelope
			if ( env_time <= end_time )
			{
				env_time += env_period;
				int mode = *regs_nes (this, 0x4080) >> 5 & 2;
				int new_env_gain = this->env_gain + mode - 1;
				if ( (unsigned) new_env_gain <= (unsigned) 0x80 >> mode )
					this->env_gain = new_env_gain;
				else
					*regs_nes (this, 0x4080) |= 0x80; // optimization only
			}
			
			// new end_time
			blip_time_t const start_time = end_time;
			end_time = final_end_time;
			if ( end_time > env_time   ) end_time = env_time;
			if ( end_time > sweep_time ) end_time = sweep_time;
			
			// frequency modulation
			int freq = wave_freq;
			if ( mod_freq )
			{
				// time of next modulation clock
				blip_time_t mod_time = start_time + (this->mod_fract + mod_freq - 1) / mod_freq;
				if ( end_time > mod_time )
					end_time = mod_time;
				
				// run modulator up to next clock and save old sweep_bias
				int sweep_bias = *regs_nes (this, 0x4085);
				this->mod_fract -= (end_time - start_time) * mod_freq;
				if ( this->mod_fract <= 0 )
				{
					this->mod_fract += fract_range;
					check( (unsigned) this->mod_fract <= fract_range );
					
					static short const mod_table [8] = { 0, +1, +2, +4, 0, -4, -2, -1 };
					int mod = this->mod_wave [this->mod_pos];
					this->mod_pos = (this->mod_pos + 1) & (fds_wave_size - 1);
					int new_sweep_bias = (sweep_bias + mod_table [mod]) & 0x7F;
					if ( mod == 4 )
						new_sweep_bias = 0;
					*regs_nes (this, 0x4085) = new_sweep_bias;
				}
				
				// apply frequency modulation
				sweep_bias = (sweep_bias ^ 0x40) - 0x40;
				int factor = sweep_bias * this->sweep_gain;
				int extra = factor & 0x0F;
				factor >>= 4;
				if ( extra )
				{
					factor--;
					if ( sweep_bias >= 0 )
						factor += 3;
				}
				if ( factor > 193 ) factor -= 258;
				if ( factor < -64 ) factor += 256;
				freq += (freq * factor) >> 6;
				if ( freq <= 0 )
					continue;
			}
			
			// wave
			int wave_fract = this->wave_fract;
			blip_time_t delay = (wave_fract + freq - 1) / freq;
			blip_time_t time = start_time + delay;
			
			if ( time <= end_time )
			{
				// at least one wave clock within start_time...end_time
				
				blip_time_t const min_delay = fract_range / freq;
				int wave_pos = this->wave_pos;
				
				int volume = this->env_gain;
				if ( volume > fds_vol_max )
					volume = fds_vol_max;
				volume *= master_volume;
				
				int const min_fract = min_delay * freq;
				
				do
				{
					// clock wave
					int amp = this->regs_ [wave_pos] * volume;
					wave_pos = (wave_pos + 1) & (fds_wave_size - 1);
					int delta = amp - this->last_amp;
					if ( delta )
					{
						this->last_amp = amp;
						Synth_offset_inline( &this->synth, time, delta, output_ );
					}
					
					wave_fract += fract_range - delay * freq;
					check( unsigned (fract_range - wave_fract) < freq );
					
					// delay until next clock
					delay = min_delay;
					if ( wave_fract > min_fract )
						delay++;
					check( delay && delay == (wave_fract + freq - 1) / freq );
					
					time += delay;
				}
				while ( time <= end_time ); // TODO: using < breaks things, but <= is wrong
				
				this->wave_pos = wave_pos;
			}
			this->wave_fract = wave_fract - (end_time - (time - delay)) * freq;
			check( this->wave_fract > 0 );
		}
		while ( end_time < final_end_time );
		
		this->env_delay   = env_time   - final_end_time; check( env_delay >= 0 );
		this->sweep_delay = sweep_time - final_end_time; check( sweep_delay >= 0 );
	}
	this->last_time = final_end_time;
}