-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextfilt.h
169 lines (141 loc) · 5.16 KB
/
extfilt.h
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
// ---------------------------------------------------------------------------
// This file is part of reSID, a MOS6581 SID emulator engine.
// Copyright (C) 2010 Dag Lem <[email protected]>
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ---------------------------------------------------------------------------
#ifndef RESID_EXTFILT_H
#define RESID_EXTFILT_H
#include "resid-config.h"
namespace reSID
{
// ----------------------------------------------------------------------------
// The audio output stage in a Commodore 64 consists of two STC networks,
// a low-pass filter with 3-dB frequency 16kHz followed by a high-pass
// filter with 3-dB frequency 16Hz (the latter provided an audio equipment
// input impedance of 1kOhm).
// The STC networks are connected with a BJT supposedly meant to act as
// a unity gain buffer, which is not really how it works. A more elaborate
// model would include the BJT, however DC circuit analysis yields BJT
// base-emitter and emitter-base impedances sufficiently low to produce
// additional low-pass and high-pass 3dB-frequencies in the order of hundreds
// of kHz. This calls for a sampling frequency of several MHz, which is far
// too high for practical use.
//
// 9/12V
// -----+
// audio| 10k |
// +----+---R---+--------+-----(K) +-----
// out | | | | | |audio
// -----+ R 1k C 1000 | | 10 uF |
// | | pF +-C----+-----C-----+ 1K
// 470 | |
// GND GND pF R 1K | amp
// | +-----
//
// GND
//
// ----------------------------------------------------------------------------
class ExternalFilter
{
public:
ExternalFilter();
void enable_filter(bool enable);
void clock(short Vi);
void clock(cycle_count delta_t, short Vi);
void reset();
// Audio output (16 bits).
int output();
protected:
// Filter enabled.
bool enabled;
// State of filters (27 bits).
int Vlp; // lowpass
int Vhp; // highpass
// Cutoff frequencies.
int w0lp_1_s7;
int w0hp_1_s17;
friend class SID;
};
// ----------------------------------------------------------------------------
// Inline functions.
// The following functions are defined inline because they are called every
// time a sample is calculated.
// ----------------------------------------------------------------------------
#if RESID_INLINING || defined(RESID_EXTFILT_CC)
// ----------------------------------------------------------------------------
// SID clocking - 1 cycle.
// ----------------------------------------------------------------------------
RESID_INLINE
void ExternalFilter::clock(short Vi)
{
// This is handy for testing.
if (unlikely(!enabled)) {
// Vo = Vlp - Vhp;
Vlp = Vi << 11;
Vhp = 0;
return;
}
// Calculate filter outputs.
// Vlp = Vlp + w0lp*(Vi - Vlp)*delta_t;
// Vhp = Vhp + w0hp*(Vlp - Vhp)*delta_t;
// Vo = Vlp - Vhp;
int dVlp = w0lp_1_s7*int((unsigned(Vi) << 11) - unsigned(Vlp)) >> 7;
int dVhp = w0hp_1_s17*(Vlp - Vhp) >> 17;
Vlp += dVlp;
Vhp += dVhp;
}
// ----------------------------------------------------------------------------
// SID clocking - delta_t cycles.
// ----------------------------------------------------------------------------
RESID_INLINE
void ExternalFilter::clock(cycle_count delta_t, short Vi)
{
// This is handy for testing.
if (unlikely(!enabled)) {
// Vo = Vlp - Vhp;
Vlp = Vi << 11;
Vhp = 0;
return;
}
// Maximum delta cycles for the external filter to work satisfactorily
// is approximately 8.
cycle_count delta_t_flt = 8;
while (delta_t) {
if (unlikely(delta_t < delta_t_flt)) {
delta_t_flt = delta_t;
}
// Calculate filter outputs.
// Vlp = Vlp + w0lp*(Vi - Vlp)*delta_t;
// Vhp = Vhp + w0hp*(Vlp - Vhp)*delta_t;
// Vo = Vlp - Vhp;
int dVlp = (w0lp_1_s7*delta_t_flt >> 3)*((Vi << 11) - Vlp) >> 4;
int dVhp = (w0hp_1_s17*delta_t_flt >> 3)*(Vlp - Vhp) >> 14;
Vlp += dVlp;
Vhp += dVhp;
delta_t -= delta_t_flt;
}
}
// ----------------------------------------------------------------------------
// Audio output (16 bits).
// ----------------------------------------------------------------------------
RESID_INLINE
int ExternalFilter::output()
{
return (Vlp - Vhp) >> 11;
}
#endif // RESID_INLINING || defined(RESID_EXTFILT_CC)
} // namespace reSID
#endif // not RESID_EXTFILT_H