1 //  ---------------------------------------------------------------------------
2 //  This file is part of reSID, a MOS6581 SID emulator engine.
3 //  Copyright (C) 2004  Dag Lem <resid@nimrod.no>
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 2 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //  ---------------------------------------------------------------------------
19 
20 #ifndef __EXTFILTFP_H__
21 #define __EXTFILTFP_H__
22 
23 #include <math.h>
24 
25 #include "siddefsfp.h"
26 
27 // ----------------------------------------------------------------------------
28 // The audio output stage in a Commodore 64 consists of two STC networks,
29 // a low-pass filter with 3-dB frequency 16kHz followed by a high-pass
30 // filter with 3-dB frequency 16Hz (the latter provided an audio equipment
31 // input impedance of 1kOhm).
32 // The STC networks are connected with a BJT supposedly meant to act as
33 // a unity gain buffer, which is not really how it works. A more elaborate
34 // model would include the BJT, however DC circuit analysis yields BJT
35 // base-emitter and emitter-base impedances sufficiently low to produce
36 // additional low-pass and high-pass 3dB-frequencies in the order of hundreds
37 // of kHz. This calls for a sampling frequency of several MHz, which is far
38 // too high for practical use.
39 // ----------------------------------------------------------------------------
40 class ExternalFilterFP
41 {
42 public:
43   ExternalFilterFP();
44 
45   void set_clock_frequency(float);
46 
47   inline void clock(float Vi);
48   void reset();
49 
50   // Audio output (20 bits).
51   inline float output();
52 
53 private:
54   inline void nuke_denormals();
55 
56   // State of filters.
57   float Vlp; // lowpass
58   float Vhp; // highpass
59 
60   // Cutoff frequencies.
61   float w0lp;
62   float w0hp;
63 
64 friend class SIDFP;
65 };
66 
67 // ----------------------------------------------------------------------------
68 // SID clocking - 1 cycle.
69 // ----------------------------------------------------------------------------
70 inline
clock(float Vi)71 void ExternalFilterFP::clock(float Vi)
72 {
73   float dVlp = w0lp * (Vi - Vlp);
74   float dVhp = w0hp * (Vlp - Vhp);
75   Vlp += dVlp;
76   Vhp += dVhp;
77 }
78 
79 // ----------------------------------------------------------------------------
80 // Audio output (19.5 bits).
81 // ----------------------------------------------------------------------------
82 inline
output()83 float ExternalFilterFP::output()
84 {
85   return Vlp - Vhp;
86 }
87 
88 inline
nuke_denormals()89 void ExternalFilterFP::nuke_denormals()
90 {
91     if (Vhp > -1e-12f && Vhp < 1e-12f)
92         Vhp = 0;
93     if (Vlp > -1e-12f && Vlp < 1e-12f)
94         Vlp = 0;
95 }
96 
97 #endif // not __EXTFILTFP_H__
98