aboutsummaryrefslogtreecommitdiff
path: root/audio/softsynth/sid.h
blob: d07d25bc47f46d9af10785f8f42fd8bd4a0d7c65 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 *  This file is based on reSID, a MOS6581 SID emulator engine.
 *  Copyright (C) 2004  Dag Lem <resid@nimrod.no>
 */

#ifndef AUDIO_SOFTSYNTH_SID_H
#define AUDIO_SOFTSYNTH_SID_H

// Inlining on/off.
#define RESID_INLINE inline

namespace Resid {

// We could have used the smallest possible data type for each SID register,
// however this would give a slower engine because of data type conversions.
// An int is assumed to be at least 32 bits (necessary in the types reg24,
// cycle_count, and sound_sample). GNU does not support 16-bit machines
// (GNU Coding Standards: Portability between CPUs), so this should be
// a valid assumption.

typedef unsigned int reg4;
typedef unsigned int reg8;
typedef unsigned int reg12;
typedef unsigned int reg16;
typedef unsigned int reg24;

typedef int cycle_count;
typedef int sound_sample;
typedef sound_sample fc_point[2];


class WaveformGenerator {
public:
	WaveformGenerator();

	void set_sync_source(WaveformGenerator *);

	void updateClock(cycle_count delta_t);
	void synchronize();
	void reset();

	void writeFREQ_LO(reg8);
	void writeFREQ_HI(reg8);
	void writePW_LO(reg8);
	void writePW_HI(reg8);
	void writeCONTROL_REG(reg8);
	reg8 readOSC();

	// 12-bit waveform output.
	reg12 output();

protected:
	const WaveformGenerator* sync_source;
	WaveformGenerator* sync_dest;

	// Tell whether the accumulator MSB was set high on this cycle.
	bool msb_rising;

	reg24 accumulator;
	reg24 shift_register;

	// Fout  = (Fn*Fclk/16777216)Hz
	reg16 freq;
	// PWout = (PWn/40.95)%
	reg12 pw;

	// The control register right-shifted 4 bits; used for output function
	// table lookup.
	reg8 waveform;

	// The remaining control register bits.
	reg8 test;
	reg8 ring_mod;
	reg8 sync;
	// The gate bit is handled by the EnvelopeGenerator.

	// 16 possible combinations of waveforms.
	reg12 output____();
	reg12 output___T();
	reg12 output__S_();
	reg12 output__ST();
	reg12 output_P__();
	reg12 output_P_T();
	reg12 output_PS_();
	reg12 output_PST();
	reg12 outputN___();
	reg12 outputN__T();
	reg12 outputN_S_();
	reg12 outputN_ST();
	reg12 outputNP__();
	reg12 outputNP_T();
	reg12 outputNPS_();
	reg12 outputNPST();

	// Sample data for combinations of waveforms.
	static const reg8 wave6581__ST[];
	static const reg8 wave6581_P_T[];
	static const reg8 wave6581_PS_[];
	static const reg8 wave6581_PST[];

	friend class Voice;
	friend class SID;
};

class Filter {
public:
	Filter();

	void enable_filter(bool enable);

	void updateClock(cycle_count delta_t,
		sound_sample voice1, sound_sample voice2, sound_sample voice3);
	void reset();

	// Write registers.
	void writeFC_LO(reg8);
	void writeFC_HI(reg8);
	void writeRES_FILT(reg8);
	void writeMODE_VOL(reg8);

	// SID audio output (16 bits).
	sound_sample output();

protected:
	void set_w0();
	void set_Q();

	// Filter enabled.
	bool enabled;

	// Filter cutoff frequency.
	reg12 fc;

	// Filter resonance.
	reg8 res;

	// Selects which inputs to route through filter.
	reg8 filt;

	// Switch voice 3 off.
	reg8 voice3off;

	// Highpass, bandpass, and lowpass filter modes.
	reg8 hp_bp_lp;

	// Output master volume.
	reg4 vol;

	// Mixer DC offset.
	sound_sample mixer_DC;

	// State of filter.
	sound_sample Vhp; // highpass
	sound_sample Vbp; // bandpass
	sound_sample Vlp; // lowpass
	sound_sample Vnf; // not filtered

	// Cutoff frequency, resonance.
	sound_sample w0, w0_ceil_1, w0_ceil_dt;
	sound_sample _1024_div_Q;

	// Cutoff frequency tables.
	// FC is an 11 bit register.
	sound_sample f0_6581[2048];
	sound_sample* f0;
	static fc_point f0_points_6581[];
	fc_point* f0_points;
	int f0_count;

	friend class SID;
};

class EnvelopeGenerator {
public:
	EnvelopeGenerator();

	enum State { ATTACK, DECAY_SUSTAIN, RELEASE };

	void updateClock(cycle_count delta_t);
	void reset();

	void writeCONTROL_REG(reg8);
	void writeATTACK_DECAY(reg8);
	void writeSUSTAIN_RELEASE(reg8);
	reg8 readENV();

	// 8-bit envelope output.
	reg8 output();

protected:
	reg16 rate_counter;
	reg16 rate_period;
	reg8 exponential_counter;
	reg8 exponential_counter_period;
	reg8 envelope_counter;
	bool hold_zero;

	reg4 attack;
	reg4 decay;
	reg4 sustain;
	reg4 release;

	reg8 gate;

	State state;

	// Lookup table to convert from attack, decay, or release value to rate
	// counter period.
	static reg16 rate_counter_period[];

	// The 16 selectable sustain levels.
	static reg8 sustain_level[];

	friend class SID;
};

class ExternalFilter {
public:
	ExternalFilter();

	void enable_filter(bool enable);
	void set_sampling_parameter(double pass_freq);

	void updateClock(cycle_count delta_t, sound_sample Vi);
	void reset();

	// Audio output (20 bits).
	sound_sample output();

protected:
	// Filter enabled.
	bool enabled;

	// Maximum mixer DC offset.
	sound_sample mixer_DC;

	// State of filters.
	sound_sample Vlp; // lowpass
	sound_sample Vhp; // highpass
	sound_sample Vo;

	// Cutoff frequencies.
	sound_sample w0lp;
	sound_sample w0hp;

	friend class SID;
};

class Voice {
public:
	Voice();

	void set_sync_source(Voice *);
	void reset();

	void writeCONTROL_REG(reg8);

	// Amplitude modulated waveform output.
	// Range [-2048*255, 2047*255].
	sound_sample output() {
		// Multiply oscillator output with envelope output.
		return (wave.output() - wave_zero)*envelope.output() + voice_DC;
	}

protected:
	WaveformGenerator wave;
	EnvelopeGenerator envelope;

	// Waveform D/A zero level.
	sound_sample wave_zero;

	// Multiplying D/A DC offset.
	sound_sample voice_DC;

	friend class SID;
};


class SID {
public:
	SID();
	~SID();

	void enable_filter(bool enable);
	void enable_external_filter(bool enable);
	bool set_sampling_parameters(double clock_freq,
		double sample_freq, double pass_freq = -1,
		double filter_scale = 0.97);

	void updateClock(cycle_count delta_t);
	int updateClock(cycle_count& delta_t, short* buf, int n, int interleave = 1);
	void reset();

	// Read/write registers.
	reg8 read(reg8 offset);
	void write(reg8 offset, reg8 value);

	// 16-bit output (AUDIO OUT).
	int output();

protected:
	Voice voice[3];
	Filter filter;
	ExternalFilter extfilt;

	reg8 bus_value;
	cycle_count bus_value_ttl;

	double clock_frequency;

	// Fixpoint constants.
	static const int FIXP_SHIFT;
	static const int FIXP_MASK;

	// Sampling variables.
	cycle_count cycles_per_sample;
	cycle_count sample_offset;
	short sample_prev;
};

}

#endif // not AUDIO_SOFTSYNTH_SID_H