| 1 | // This file is part of reSID, a MOS6581 SID emulator engine.cxx . . $ `<file>/src/resid-fp/filterfp.h`/ |
| 2 | // Copyright (C) 2004 Dag Lem <[email protected]> |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | // --------------------------------------------------------------------------- |
| 18 | // Filter distortion code written by Antti S. Lankila 2007 - 2009. |
| 19 | |
| 20 | #ifndef __FILTERFP_H__ |
| 21 | #define __FILTERFP_H__cxx . . $ `src/resid-fp/filterfp.h:21:9`! |
| 22 | |
| 23 | #define M_LN2cxx . . $ `src/resid-fp/filterfp.h:23:9`! 0.69314718055994530942 /* log_e 2 */ |
| 24 | #define M_PIcxx . . $ `src/resid-fp/filterfp.h:24:9`! 3.14159265358979323846 /* pi */ |
| 25 | |
| 26 | #include <math.h> |
| 27 | #include "siddefsfp.h"cxx . . $ `<file>/src/resid-fp/siddefsfp.h`/ |
| 28 | |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | // The SID filter is modeled with a two-integrator-loop biquadratic filter, |
| 31 | // which has been confirmed by Bob Yannes to be the actual circuit used in |
| 32 | // the SID chip. |
| 33 | // |
| 34 | // Measurements show that excellent emulation of the SID filter is achieved, |
| 35 | // except when high resonance is combined with high sustain levels. |
| 36 | // In this case the SID op-amps are performing less than ideally and are |
| 37 | // causing some peculiar behavior of the SID filter. This however seems to |
| 38 | // have more effect on the overall amplitude than on the color of the sound. |
| 39 | // |
| 40 | // The theory for the filter circuit can be found in "Microelectric Circuits" |
| 41 | // by Adel S. Sedra and Kenneth C. Smith. |
| 42 | // The circuit is modeled based on the explanation found there except that |
| 43 | // an additional inverter is used in the feedback from the bandpass output, |
| 44 | // allowing the summer op-amp to operate in single-ended mode. This yields |
| 45 | // inverted filter outputs with levels independent of Q, which corresponds with |
| 46 | // the results obtained from a real SID. |
| 47 | // |
| 48 | // We have been able to model the summer and the two integrators of the circuit |
| 49 | // to form components of an IIR filter. |
| 50 | // Vhp is the output of the summer, Vbp is the output of the first integrator, |
| 51 | // and Vlp is the output of the second integrator in the filter circuit. |
| 52 | // |
| 53 | // According to Bob Yannes, the active stages of the SID filter are not really |
| 54 | // op-amps. Rather, simple NMOS inverters are used. By biasing an inverter |
| 55 | // into its region of quasi-linear operation using a feedback resistor from |
| 56 | // input to output, a MOS inverter can be made to act like an op-amp for |
| 57 | // small signals centered around the switching threshold. |
| 58 | // |
| 59 | // Qualified guesses at SID filter schematics are depicted below. |
| 60 | // |
| 61 | // SID filter |
| 62 | // ---------- |
| 63 | // |
| 64 | // ----------------------------------------------- |
| 65 | // | | |
| 66 | // | ---Rq-- | |
| 67 | // | | | | |
| 68 | // | ------------<A]-----R1--------- | |
| 69 | // | | | | |
| 70 | // | | ---C---| ---C---| |
| 71 | // | | | | | | |
| 72 | // | --R1-- ---R1-- |---Rs--| |---Rs--| |
| 73 | // | | | | | | | | |
| 74 | // ----R1--|-----[A>--|--R-----[A>--|--R-----[A>--| |
| 75 | // | | | | |
| 76 | // vi -----R1-- | | | |
| 77 | // |
| 78 | // vhp vbp vlp |
| 79 | // |
| 80 | // |
| 81 | // vi - input voltage |
| 82 | // vhp - highpass output |
| 83 | // vbp - bandpass output |
| 84 | // vlp - lowpass output |
| 85 | // [A> - op-amp |
| 86 | // R1 - summer resistor |
| 87 | // Rq - resistor array controlling resonance (4 resistors) |
| 88 | // R - NMOS FET voltage controlled resistor controlling cutoff frequency |
| 89 | // Rs - shunt resitor |
| 90 | // C - capacitor |
| 91 | // |
| 92 | // |
| 93 | // |
| 94 | // SID integrator |
| 95 | // -------------- |
| 96 | // |
| 97 | // V+ |
| 98 | // |
| 99 | // | |
| 100 | // | |
| 101 | // -----| |
| 102 | // | | |
| 103 | // | ||-- |
| 104 | // -|| |
| 105 | // ---C--- ||-> |
| 106 | // | | | |
| 107 | // |---Rs-----------|---- vo |
| 108 | // | | |
| 109 | // | ||-- |
| 110 | // vi ---- -----|------------|| |
| 111 | // | ^ | ||-> |
| 112 | // |___| | | |
| 113 | // ----- | | |
| 114 | // | | | |
| 115 | // |---R2-- | |
| 116 | // | |
| 117 | // R1 V- |
| 118 | // | |
| 119 | // | |
| 120 | // |
| 121 | // Vw |
| 122 | // |
| 123 | // ---------------------------------------------------------------------------- |
| 124 | class FilterFPcxx . . $ FilterFP# |
| 125 | { |
| 126 | public: |
| 127 | FilterFPcxx . . $ FilterFP#FilterFP(49f6e7a06ebc5aa8).(); |
| 128 | |
| 129 | void enable_filtercxx . . $ FilterFP#enable_filter(b45ddcbc8423892c).(bool enable); |
| 130 | void set_chip_modelcxx . . $ FilterFP#set_chip_model(cea143c94d31b99f).(chip_modelcxx . . $ chip_model# model); |
| 131 | void set_distortion_propertiescxx . . $ FilterFP#set_distortion_properties(bd1040fe176e0786).(float, float, float); |
| 132 | void set_type3_propertiescxx . . $ FilterFP#set_type3_properties(7e9a355db6148521).(float, float, float, float); |
| 133 | void set_type4_propertiescxx . . $ FilterFP#set_type4_properties(5102ef758adc0ae7).(float, float); |
| 134 | void set_clock_frequencycxx . . $ FilterFP#set_clock_frequency(9364e6e7bff7660).(float); |
| 135 | void set_nonlinearitycxx . . $ FilterFP#set_nonlinearity(9364e6e7bff7660).(float); |
| 136 | |
| 137 | inline |
| 138 | float clockcxx . . $ FilterFP#clock(95d5df56d1097f86).(float voice1, float voice2, float voice3, |
| 139 | float ext_in); |
| 140 | void resetcxx . . $ FilterFP#reset(49f6e7a06ebc5aa8).(); |
| 141 | |
| 142 | // Write registers. |
| 143 | void writeFC_LOcxx . . $ FilterFP#writeFC_LO(cb2f890c2fdad230).(reg8cxx . . $ reg8#); |
| 144 | void writeFC_HIcxx . . $ FilterFP#writeFC_HI(cb2f890c2fdad230).(reg8cxx . . $ reg8#); |
| 145 | void writeRES_FILTcxx . . $ FilterFP#writeRES_FILT(cb2f890c2fdad230).(reg8cxx . . $ reg8#); |
| 146 | void writeMODE_VOLcxx . . $ FilterFP#writeMODE_VOL(cb2f890c2fdad230).(reg8cxx . . $ reg8#); |
| 147 | |
| 148 | private: |
| 149 | void set_Qcxx . . $ FilterFP#set_Q(49f6e7a06ebc5aa8).(); |
| 150 | void set_w0cxx . . $ FilterFP#set_w0(49f6e7a06ebc5aa8).(); |
| 151 | float type3_w0cxx . . $ FilterFP#type3_w0(fbcf3211dfe1bfd1).(const float dist); |
| 152 | float type4_w0cxx . . $ FilterFP#type4_w0(9b8c9e39889c762c).(); |
| 153 | void calculate_helperscxx . . $ FilterFP#calculate_helpers(49f6e7a06ebc5aa8).(); |
| 154 | void nuke_denormalscxx . . $ FilterFP#nuke_denormals(49f6e7a06ebc5aa8).(); |
| 155 | float waveshaper1cxx . . $ FilterFP#waveshaper1(fbcf3211dfe1bfd1).(float value); |
| 156 | float waveshaper2cxx . . $ FilterFP#waveshaper2(fbcf3211dfe1bfd1).(float value); |
| 157 | |
| 158 | // Filter enabled. |
| 159 | bool enabledcxx . . $ FilterFP#enabled.; |
| 160 | |
| 161 | // 6581/8580 filter model (XXX: we should specialize in separate classes) |
| 162 | chip_modelcxx . . $ chip_model# modelcxx . . $ FilterFP#model.; |
| 163 | |
| 164 | // Filter cutoff frequency. |
| 165 | reg12cxx . . $ reg12# fccxx . . $ FilterFP#fc.; |
| 166 | |
| 167 | // Filter resonance. |
| 168 | reg8cxx . . $ reg8# rescxx . . $ FilterFP#res.; |
| 169 | |
| 170 | // Selects which inputs to route through filter. |
| 171 | reg8cxx . . $ reg8# filtcxx . . $ FilterFP#filt.; |
| 172 | |
| 173 | // Switch voice 3 off. |
| 174 | reg8cxx . . $ reg8# voice3offcxx . . $ FilterFP#voice3off.; |
| 175 | |
| 176 | // Highpass, bandpass, and lowpass filter modes. |
| 177 | reg8cxx . . $ reg8# hp_bp_lpcxx . . $ FilterFP#hp_bp_lp.; |
| 178 | |
| 179 | // Output master volume. |
| 180 | reg4cxx . . $ reg4# volcxx . . $ FilterFP#vol.; |
| 181 | float volfcxx . . $ FilterFP#volf.; /* avoid integer-to-float conversion at output */ |
| 182 | |
| 183 | // clock |
| 184 | float clock_frequencycxx . . $ FilterFP#clock_frequency.; |
| 185 | |
| 186 | /* Distortion params for Type3 */ |
| 187 | float attenuationcxx . . $ FilterFP#attenuation., distortion_nonlinearitycxx . . $ FilterFP#distortion_nonlinearity., intermixing_leakscxx . . $ FilterFP#intermixing_leaks.; |
| 188 | |
| 189 | /* Type3 params. */ |
| 190 | float type3_baseresistancecxx . . $ FilterFP#type3_baseresistance., type3_offsetcxx . . $ FilterFP#type3_offset., type3_steepnesscxx . . $ FilterFP#type3_steepness., type3_minimumfetresistancecxx . . $ FilterFP#type3_minimumfetresistance.; |
| 191 | |
| 192 | /* Type4 params */ |
| 193 | float type4_kcxx . . $ FilterFP#type4_k., type4_bcxx . . $ FilterFP#type4_b.; |
| 194 | |
| 195 | // State of filter. |
| 196 | float Vhpcxx . . $ FilterFP#Vhp., Vbpcxx . . $ FilterFP#Vbp., Vlpcxx . . $ FilterFP#Vlp.; |
| 197 | |
| 198 | /* Resonance/Distortion/Type3/Type4 helpers. */ |
| 199 | float type4_w0_cachecxx . . $ FilterFP#type4_w0_cache., _1_div_Qcxx . . $ FilterFP#_1_div_Q., type3_fc_kink_expcxx . . $ FilterFP#type3_fc_kink_exp., distortion_CTcxx . . $ FilterFP#distortion_CT.; |
| 200 | |
| 201 | float nonlinearitycxx . . $ FilterFP#nonlinearity.; |
| 202 | |
| 203 | friend class SIDFPcxx . . $ SIDFP#; |
| 204 | }; |
| 205 | |
| 206 | // ---------------------------------------------------------------------------- |
| 207 | // Inline functions. |
| 208 | // The following functions are defined inline because they are called every |
| 209 | // time a sample is calculated. |
| 210 | // ---------------------------------------------------------------------------- |
| 211 | |
| 212 | const float sidcaps_6581cxx . . $ sidcaps_6581. = 470e-12f; |
| 213 | const float FC_TO_OSCcxx . . $ FC_TO_OSC. = 512.f; |
| 214 | |
| 215 | inline |
| 216 | static float fastexpcxx . . $ fastexp(fbcf3211dfe1bfd1).(float val) { |
| 217 | typedef unioncxx . . $ fastexp(fbcf3211dfe1bfd1).$anonymous_type_0# { |
| 218 | int icxx . . $ fastexp(fbcf3211dfe1bfd1).$anonymous_type_0#i.; |
| 219 | float fcxx . . $ fastexp(fbcf3211dfe1bfd1).$anonymous_type_0#f.; |
| 220 | } convcxx . . $ fastexp(fbcf3211dfe1bfd1).conv#; |
| 221 | |
| 222 | convcxx . . $ fastexp(fbcf3211dfe1bfd1).conv# tmp; |
| 223 | |
| 224 | /* single precision fp has 1 + 8 + 23 bits, exponent bias is 127. |
| 225 | * It therefore follows that we need to shift left by 23 bits, and to |
| 226 | * calculate exp(x) instead of pow(2, x) we divide the power by ln(2). */ |
| 227 | const float a = static_cast<float>((1 << 23) / M_LN2cxx . . $ `<external>/11263917310783833665/math.h:1287:10`!); |
| 228 | /* The other factor corrects for the exponent bias so that 2^0 = 1. */ |
| 229 | const float b = static_cast<float>((1 << 23) * 127); |
| 230 | /* According to "A Fast, Compact Approximation of the Exponential Function" |
| 231 | * by Nicol N. Schraudolph, 60801.48 yields the minimum RMS error for the |
| 232 | * piecewise-linear approximation when using doubles (20 bits residual). |
| 233 | * We have 23 bits, so we scale this value by 8. */ |
| 234 | const float c = 60801.48f * 8.f; |
| 235 | |
| 236 | /* Parenthesis are important: C standard disallows folding subtraction. |
| 237 | * Unfortunately GCC appears to generate a write to memory rather than |
| 238 | * handle this conversion entirely in registers. */ |
| 239 | tmp.icxx . . $ fastexp(fbcf3211dfe1bfd1).$anonymous_type_0#i. = static_cast<int>(a * val + (b - c + 0.5f)); |
| 240 | return tmp.fcxx . . $ fastexp(fbcf3211dfe1bfd1).$anonymous_type_0#f.; |
| 241 | } |
| 242 | |
| 243 | inline |
| 244 | float FilterFPcxx . . $ FilterFP#::type3_w0cxx . . $ FilterFP#type3_w0(fbcf3211dfe1bfd1).(const float dist) |
| 245 | { |
| 246 | float fetresistance = type3_fc_kink_expcxx . . $ FilterFP#type3_fc_kink_exp.; |
| 247 | if (dist > 0) { |
| 248 | fetresistance *= fastexpcxx . . $ fastexp(fbcf3211dfe1bfd1).(dist * type3_steepnesscxx . . $ FilterFP#type3_steepness.); |
| 249 | } |
| 250 | const float dynamic_resistance = type3_minimumfetresistancecxx . . $ FilterFP#type3_minimumfetresistance. + fetresistance; |
| 251 | |
| 252 | /* 2 parallel resistors */ |
| 253 | const float _1_div_resistance = (type3_baseresistancecxx . . $ FilterFP#type3_baseresistance. + dynamic_resistance) / (type3_baseresistancecxx . . $ FilterFP#type3_baseresistance. * dynamic_resistance); |
| 254 | /* 1.f / (clock * caps * resistance) */ |
| 255 | return distortion_CTcxx . . $ FilterFP#distortion_CT. * _1_div_resistance; |
| 256 | } |
| 257 | |
| 258 | inline |
| 259 | float FilterFPcxx . . $ FilterFP#::type4_w0cxx . . $ FilterFP#type4_w0(9b8c9e39889c762c).() |
| 260 | { |
| 261 | const float freq = type4_kcxx . . $ FilterFP#type4_k. * fccxx . . $ FilterFP#fc. + type4_bcxx . . $ FilterFP#type4_b.; |
| 262 | return 2.f * static_cast<float>(M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!) * freq / clock_frequencycxx . . $ FilterFP#clock_frequency.; |
| 263 | } |
| 264 | |
| 265 | inline float FilterFPcxx . . $ FilterFP#::waveshaper1cxx . . $ FilterFP#waveshaper1(fbcf3211dfe1bfd1).(float value) |
| 266 | { |
| 267 | if (value > distortion_nonlinearitycxx . . $ FilterFP#distortion_nonlinearity.) { |
| 268 | value -= (value - distortion_nonlinearitycxx . . $ FilterFP#distortion_nonlinearity.) * 0.5f; |
| 269 | } |
| 270 | return value; |
| 271 | } |
| 272 | |
| 273 | // ---------------------------------------------------------------------------- |
| 274 | // SID clocking - 1 cycle. |
| 275 | // ---------------------------------------------------------------------------- |
| 276 | inline |
| 277 | float FilterFPcxx . . $ FilterFP#::clockcxx . . $ FilterFP#clock(95d5df56d1097f86).(float voice1, |
| 278 | float voice2, |
| 279 | float voice3, |
| 280 | float ext_in) |
| 281 | { |
| 282 | float Vi = 0.f, Vf = 0.f; |
| 283 | |
| 284 | // Route voices into or around filter. |
| 285 | ((filtcxx . . $ FilterFP#filt. & 1) ? Vi : Vf) += voice1; |
| 286 | ((filtcxx . . $ FilterFP#filt. & 2) ? Vi : Vf) += voice2; |
| 287 | // NB! Voice 3 is not silenced by voice3off if it is routed through |
| 288 | // the filter. |
| 289 | if (filtcxx . . $ FilterFP#filt. & 4) { |
| 290 | Vi += voice3; |
| 291 | } else if (! voice3offcxx . . $ FilterFP#voice3off.) { |
| 292 | Vf += voice3; |
| 293 | } |
| 294 | ((filtcxx . . $ FilterFP#filt. & 8) ? Vi : Vf) += ext_in; |
| 295 | |
| 296 | if (hp_bp_lpcxx . . $ FilterFP#hp_bp_lp. & 1) { |
| 297 | Vf += Vlpcxx . . $ FilterFP#Vlp.; |
| 298 | } |
| 299 | if (hp_bp_lpcxx . . $ FilterFP#hp_bp_lp. & 2) { |
| 300 | Vf += Vbpcxx . . $ FilterFP#Vbp.; |
| 301 | } |
| 302 | if (hp_bp_lpcxx . . $ FilterFP#hp_bp_lp. & 4) { |
| 303 | Vf += Vhpcxx . . $ FilterFP#Vhp.; |
| 304 | } |
| 305 | |
| 306 | if (modelcxx . . $ FilterFP#model. == MOS6581cxx . . $ MOS6581.) { |
| 307 | Vlpcxx . . $ FilterFP#Vlp. -= Vbpcxx . . $ FilterFP#Vbp. * type3_w0cxx . . $ FilterFP#type3_w0(fbcf3211dfe1bfd1).(Vbpcxx . . $ FilterFP#Vbp.); |
| 308 | Vbpcxx . . $ FilterFP#Vbp. -= Vhpcxx . . $ FilterFP#Vhp. * type3_w0cxx . . $ FilterFP#type3_w0(fbcf3211dfe1bfd1).(Vhpcxx . . $ FilterFP#Vhp.); |
| 309 | Vhpcxx . . $ FilterFP#Vhp. = (Vbpcxx . . $ FilterFP#Vbp. * _1_div_Qcxx . . $ FilterFP#_1_div_Q. - Vlpcxx . . $ FilterFP#Vlp. - Vi * 0.85f) * attenuationcxx . . $ FilterFP#attenuation.; |
| 310 | |
| 311 | /* output strip mixing to filter state */ |
| 312 | if (hp_bp_lpcxx . . $ FilterFP#hp_bp_lp. & 1) { |
| 313 | Vlpcxx . . $ FilterFP#Vlp. += Vf * intermixing_leakscxx . . $ FilterFP#intermixing_leaks.; |
| 314 | } |
| 315 | if (hp_bp_lpcxx . . $ FilterFP#hp_bp_lp. & 2) { |
| 316 | Vbpcxx . . $ FilterFP#Vbp. += Vf * intermixing_leakscxx . . $ FilterFP#intermixing_leaks.; |
| 317 | } |
| 318 | if (hp_bp_lpcxx . . $ FilterFP#hp_bp_lp. & 4) { |
| 319 | Vhpcxx . . $ FilterFP#Vhp. += Vf * intermixing_leakscxx . . $ FilterFP#intermixing_leaks.; |
| 320 | } |
| 321 | |
| 322 | /* saturate. This is likely the output inverter saturation. */ |
| 323 | Vf *= volfcxx . . $ FilterFP#volf.; |
| 324 | Vf = waveshaper1cxx . . $ FilterFP#waveshaper1(fbcf3211dfe1bfd1).(Vf); |
| 325 | } else { |
| 326 | /* On the 8580, BP appears mixed in phase with the rest. */ |
| 327 | Vlpcxx . . $ FilterFP#Vlp. += Vbpcxx . . $ FilterFP#Vbp. * type4_w0_cachecxx . . $ FilterFP#type4_w0_cache.; |
| 328 | Vbpcxx . . $ FilterFP#Vbp. += Vhpcxx . . $ FilterFP#Vhp. * type4_w0_cachecxx . . $ FilterFP#type4_w0_cache.; |
| 329 | Vhpcxx . . $ FilterFP#Vhp. = -Vbpcxx . . $ FilterFP#Vbp. * _1_div_Qcxx . . $ FilterFP#_1_div_Q. - Vlpcxx . . $ FilterFP#Vlp. - Vi; |
| 330 | Vf *= volfcxx . . $ FilterFP#volf.; |
| 331 | } |
| 332 | |
| 333 | return Vf; |
| 334 | } |
| 335 | |
| 336 | inline |
| 337 | void FilterFPcxx . . $ FilterFP#::nuke_denormalscxx . . $ FilterFP#nuke_denormals(49f6e7a06ebc5aa8).() |
| 338 | { |
| 339 | /* We only need this for systems that don't do -msse and -mfpmath=sse */ |
| 340 | if (Vbpcxx . . $ FilterFP#Vbp. > -1e-12f && Vbpcxx . . $ FilterFP#Vbp. < 1e-12f) |
| 341 | Vbpcxx . . $ FilterFP#Vbp. = 0; |
| 342 | if (Vlpcxx . . $ FilterFP#Vlp. > -1e-12f && Vlpcxx . . $ FilterFP#Vlp. < 1e-12f) |
| 343 | Vlpcxx . . $ FilterFP#Vlp. = 0; |
| 344 | } |
| 345 | |
| 346 | #endif // not __FILTER_H__ |