12
12
* DST_FILTER1 - Generic 1st order filter
13
13
* DST_FILTER2 - Generic 2nd order filter
14
14
* DST_OP_AMP_FILT - Op Amp filter circuits
15
* DST_RC_CIRCUIT_1 - RC charge/discharge circuit
15
16
* DST_RCDISC - Simple discharging RC
16
17
* DST_RCDISC2 - Simple charge R1/C, discharge R0/C
17
18
* DST_RCDISC3 - Simple charge R1/c, discharge R0*R1/(R0+R1)/C
66
67
double b0,b1,b2; /* digital filter coefficients, numerator */
70
struct dst_rc_circuit_1_context
69
80
struct dst_rcdisc_context
72
83
double t; /* time */
86
double v_cap; /* rcdisc5 */
87
double v_diode; /* rcdisc3 */
77
90
struct dst_rcdisc_mod_context
133
150
* input[4] - Voltage reference. Usually 0V.
135
152
************************************************************************/
136
#define DST_CRFILTER__ENABLE (*(node->input[0]))
137
#define DST_CRFILTER__IN (*(node->input[1]))
138
#define DST_CRFILTER__R (*(node->input[2]))
139
#define DST_CRFILTER__C (*(node->input[3]))
140
#define DST_CRFILTER__VREF (*(node->input[4]))
153
#define DST_CRFILTER__IN DISCRETE_INPUT(0)
154
#define DST_CRFILTER__R DISCRETE_INPUT(1)
155
#define DST_CRFILTER__C DISCRETE_INPUT(2)
156
#define DST_CRFILTER__VREF DISCRETE_INPUT(3)
142
158
static DISCRETE_STEP(dst_crfilter)
144
160
struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
146
if(DST_CRFILTER__ENABLE)
148
node->output[0] = DST_CRFILTER__IN - context->vCap;
149
context->vCap += ((DST_CRFILTER__IN - DST_CRFILTER__VREF) - context->vCap) * context->exponent;
162
if (UNEXPECTED(context->has_rc_nodes))
164
double rc = DST_CRFILTER__R * DST_CRFILTER__C;
165
if (rc != context->rc)
168
context->exponent = RC_CHARGE_EXP(rc);
172
node->output[0] = DST_CRFILTER__IN - context->vCap;
173
//context->vCap += ((DST_CRFILTER__IN - context->vRef) - context->vCap) * context->exponent;
174
context->vCap += (node->output[0] - DST_CRFILTER__VREF) * context->exponent;
157
177
static DISCRETE_RESET(dst_crfilter)
159
179
struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
161
context->exponent = RC_CHARGE_EXP(DST_CRFILTER__R * DST_CRFILTER__C);
181
context->has_rc_nodes = node->input_is_node & 0x6;
182
context->rc = DST_CRFILTER__R * DST_CRFILTER__C;
183
context->exponent = RC_CHARGE_EXP(context->rc);
163
185
node->output[0] = DST_CRFILTER__IN;
174
196
* input[3] - Filter type (initialization only)
176
198
************************************************************************/
177
#define DST_FILTER1__ENABLE (*(node->input[0]))
178
#define DST_FILTER1__IN (*(node->input[1]))
179
#define DST_FILTER1__FREQ (*(node->input[2]))
180
#define DST_FILTER1__TYPE (*(node->input[3]))
199
#define DST_FILTER1__ENABLE DISCRETE_INPUT(0)
200
#define DST_FILTER1__IN DISCRETE_INPUT(1)
201
#define DST_FILTER1__FREQ DISCRETE_INPUT(2)
202
#define DST_FILTER1__TYPE DISCRETE_INPUT(3)
182
static void calculate_filter1_coefficients(double fc, double type,
204
static void calculate_filter1_coefficients(const discrete_info *disc_info, double fc, double type,
183
205
double *a1, double *b0, double *b1)
185
207
double den, w, two_over_T;
187
209
/* calculate digital filter coefficents */
188
210
/*w = 2.0*M_PI*fc; no pre-warping */
189
w = discrete_current_context->sample_rate*2.0*tan(M_PI*fc/discrete_current_context->sample_rate); /* pre-warping */
190
two_over_T = 2.0*discrete_current_context->sample_rate;
211
w = disc_info->sample_rate*2.0*tan(M_PI*fc/disc_info->sample_rate); /* pre-warping */
212
two_over_T = 2.0*disc_info->sample_rate;
192
214
den = w + two_over_T;
193
215
*a1 = (w - two_over_T)/den;
228
250
struct dss_filter1_context *context = (struct dss_filter1_context *)node->context;
230
calculate_filter1_coefficients(DST_FILTER1__FREQ, DST_FILTER1__TYPE, &context->a1, &context->b0, &context->b1);
252
calculate_filter1_coefficients(node->info, DST_FILTER1__FREQ, DST_FILTER1__TYPE, &context->a1, &context->b0, &context->b1);
231
253
node->output[0] = 0;
243
265
* input[4] - Filter type (initialization only)
245
267
************************************************************************/
246
#define DST_FILTER2__ENABLE (*(node->input[0]))
247
#define DST_FILTER2__IN (*(node->input[1]))
248
#define DST_FILTER2__FREQ (*(node->input[2]))
249
#define DST_FILTER2__DAMP (*(node->input[3]))
250
#define DST_FILTER2__TYPE (*(node->input[4]))
268
#define DST_FILTER2__ENABLE DISCRETE_INPUT(0)
269
#define DST_FILTER2__IN DISCRETE_INPUT(1)
270
#define DST_FILTER2__FREQ DISCRETE_INPUT(2)
271
#define DST_FILTER2__DAMP DISCRETE_INPUT(3)
272
#define DST_FILTER2__TYPE DISCRETE_INPUT(4)
252
static void calculate_filter2_coefficients(double fc, double d, double type,
274
static void calculate_filter2_coefficients(const discrete_info *disc_info,
275
double fc, double d, double type,
253
276
double *a1, double *a2,
254
277
double *b0, double *b1, double *b2)
256
279
double w; /* cutoff freq, in radians/sec */
257
280
double w_squared;
258
281
double den; /* temp variable */
259
double two_over_T = 2*discrete_current_context->sample_rate;
282
double two_over_T = 2 * disc_info->sample_rate;
260
283
double two_over_T_squared = two_over_T * two_over_T;
262
285
/* calculate digital filter coefficents */
263
286
/*w = 2.0*M_PI*fc; no pre-warping */
264
w = discrete_current_context->sample_rate * 2.0 * tan(M_PI * fc / discrete_current_context->sample_rate); /* pre-warping */
287
w = disc_info->sample_rate * 2.0 * tan(M_PI * fc / disc_info->sample_rate); /* pre-warping */
265
288
w_squared = w * w;
267
290
den = two_over_T_squared + d*w*two_over_T + w_squared;
316
339
struct dss_filter2_context *context = (struct dss_filter2_context *)node->context;
318
calculate_filter2_coefficients(DST_FILTER2__FREQ, DST_FILTER2__DAMP, DST_FILTER2__TYPE,
341
calculate_filter2_coefficients(node->info, DST_FILTER2__FREQ, DST_FILTER2__DAMP, DST_FILTER2__TYPE,
319
342
&context->a1, &context->a2,
320
343
&context->b0, &context->b1, &context->b2);
321
344
node->output[0] = 0;
336
359
* Mar 2004, D Renaud.
337
360
************************************************************************/
338
#define DST_OP_AMP_FILT__ENABLE (*(node->input[0]))
339
#define DST_OP_AMP_FILT__INP1 (*(node->input[1]))
340
#define DST_OP_AMP_FILT__INP2 (*(node->input[2]))
341
#define DST_OP_AMP_FILT__TYPE (*(node->input[3]))
361
#define DST_OP_AMP_FILT__ENABLE DISCRETE_INPUT(0)
362
#define DST_OP_AMP_FILT__INP1 DISCRETE_INPUT(1)
363
#define DST_OP_AMP_FILT__INP2 DISCRETE_INPUT(2)
364
#define DST_OP_AMP_FILT__TYPE DISCRETE_INPUT(3)
343
366
static DISCRETE_STEP(dst_op_amp_filt)
359
382
/* Millman the input voltages. */
360
383
i = context->iFixed;
361
i += (DST_OP_AMP_FILT__INP1 - context->vRef) / info->r1;
363
i += (DST_OP_AMP_FILT__INP2 - context->vRef) / info->r2;
384
switch (context->type)
386
case DISC_OP_AMP_FILTER_IS_LOW_PASS_1_A:
387
i += (DST_OP_AMP_FILT__INP1 - DST_OP_AMP_FILT__INP2) / info->r1;
389
i += (context->vP - DST_OP_AMP_FILT__INP2) / info->r2;
391
i += (context->vN - DST_OP_AMP_FILT__INP2) / info->r3;
394
i += (DST_OP_AMP_FILT__INP1 - context->vRef) / info->r1;
396
i += (DST_OP_AMP_FILT__INP2 - context->vRef) / info->r2;
364
399
v = i * context->rTotal;
371
406
node->output[0] = context->vC1 * context->gain + info->vRef;
409
case DISC_OP_AMP_FILTER_IS_LOW_PASS_1_A:
410
context->vC1 += (v - context->vC1) * context->exponentC1;
411
node->output[0] = context->vC1 * context->gain + DST_OP_AMP_FILT__INP2;
374
414
case DISC_OP_AMP_FILTER_IS_HIGH_PASS_1:
375
415
node->output[0] = (v - context->vC1) * context->gain + info->vRef;
376
416
context->vC1 += (v - context->vC1) * context->exponentC1;
480
521
context->exponentC2 = RC_CHARGE_EXP(context->rTotal * info->c2);
482
523
case DISC_OP_AMP_FILTER_IS_BAND_PASS_1M | DISC_OP_AMP_IS_NORTON:
483
context->rTotal = 1.0 / (1.0 / info->r1 + 1.0 / info->r2);
525
context->rTotal = info->r1;
527
context->rTotal = RES_2_PARALLEL(info->r1, info->r2);
484
528
case DISC_OP_AMP_FILTER_IS_BAND_PASS_1M:
486
530
double fc = 1.0 / (2 * M_PI * sqrt(context->rTotal * info->rF * info->c1 * info->c2));
487
531
double d = (info->c1 + info->c2) / sqrt(info->rF / context->rTotal * info->c1 * info->c2);
488
532
double gain = -info->rF / context->rTotal * info->c2 / (info->c1 + info->c2);
490
calculate_filter2_coefficients(fc, d, DISC_FILTER_BANDPASS,
534
calculate_filter2_coefficients(node->info, fc, d, DISC_FILTER_BANDPASS,
491
535
&context->a1, &context->a2,
492
536
&context->b0, &context->b1, &context->b2);
493
537
context->b0 *= gain;
524
568
/************************************************************************
570
* DST_RC_CIRCUIT_1 - RC charge/discharge circuit
572
************************************************************************/
573
#define DST_RC_CIRCUIT_1__IN0 DISCRETE_INPUT(0)
574
#define DST_RC_CIRCUIT_1__IN1 DISCRETE_INPUT(1)
575
#define DST_RC_CIRCUIT_1__R DISCRETE_INPUT(2)
576
#define DST_RC_CIRCUIT_1__C DISCRETE_INPUT(3)
578
#define CD4066_R_ON 270
580
static DISCRETE_STEP( dst_rc_circuit_1 )
582
struct dst_rc_circuit_1_context *context = (struct dst_rc_circuit_1_context *)node->context;
584
if (DST_RC_CIRCUIT_1__IN0 == 0)
585
if (DST_RC_CIRCUIT_1__IN1 == 0)
586
/* cap is floating and does not change charge */
587
/* output is pulled to ground */
591
/* cap is discharged */
592
context->v_cap -= context->v_cap * context->exp_2;
593
node->output[0] = context->v_cap * context->v_drop;
596
if (DST_RC_CIRCUIT_1__IN1 == 0)
599
context->v_cap += (5.0 - context->v_cap) * context->exp_1;
600
/* output is pulled to ground */
605
/* cap is charged slightly less */
606
context->v_cap += (context->v_charge_1_2 - context->v_cap) * context->exp_1_2;
607
node->output[0] = context->v_cap * context->v_drop;
611
static DISCRETE_RESET( dst_rc_circuit_1 )
613
struct dst_rc_circuit_1_context *context = (struct dst_rc_circuit_1_context *)node->context;
615
/* the charging voltage across the cap based on in2*/
616
context->v_drop = RES_VOLTAGE_DIVIDER(CD4066_R_ON, CD4066_R_ON + DST_RC_CIRCUIT_1__R);
617
context->v_charge_1_2 = 5.0 * context->v_drop;
620
/* precalculate charging exponents */
621
/* discharge cap - in1 = 0, in2 = 1*/
622
context->exp_2 = RC_CHARGE_EXP((CD4066_R_ON + DST_RC_CIRCUIT_1__R) * DST_RC_CIRCUIT_1__C);
623
/* charge cap - in1 = 1, in2 = 0 */
624
context->exp_1 = RC_CHARGE_EXP(CD4066_R_ON * DST_RC_CIRCUIT_1__C);
625
/* charge cap - in1 = 1, in2 = 1 */
626
context->exp_1_2 = RC_CHARGE_EXP(RES_2_PARALLEL(CD4066_R_ON, CD4066_R_ON + DST_RC_CIRCUIT_1__R) * DST_RC_CIRCUIT_1__C);
628
/* starts at 0 until cap starts charging */
632
/************************************************************************
526
634
* DST_RCDISC - Usage of node_description values for RC discharge
527
635
* (inverse slope of DST_RCFILTER)
532
640
* input[3] - Capacitor Value (initialization only)
534
642
************************************************************************/
535
#define DST_RCDISC__ENABLE (*(node->input[0]))
536
#define DST_RCDISC__IN (*(node->input[1]))
537
#define DST_RCDISC__R (*(node->input[2]))
538
#define DST_RCDISC__C (*(node->input[3]))
643
#define DST_RCDISC__ENABLE DISCRETE_INPUT(0)
644
#define DST_RCDISC__IN DISCRETE_INPUT(1)
645
#define DST_RCDISC__R DISCRETE_INPUT(2)
646
#define DST_RCDISC__C DISCRETE_INPUT(3)
540
648
static DISCRETE_STEP(dst_rcdisc)
589
697
* input[5] - Capacitor Value (initialization only)
591
699
************************************************************************/
592
#define DST_RCDISC2__ENABLE (*(node->input[0]))
593
#define DST_RCDISC2__IN0 (*(node->input[1]))
594
#define DST_RCDISC2__R0 (*(node->input[2]))
595
#define DST_RCDISC2__IN1 (*(node->input[3]))
596
#define DST_RCDISC2__R1 (*(node->input[4]))
597
#define DST_RCDISC2__C (*(node->input[5]))
700
#define DST_RCDISC2__ENABLE DISCRETE_INPUT(0)
701
#define DST_RCDISC2__IN0 DISCRETE_INPUT(1)
702
#define DST_RCDISC2__R0 DISCRETE_INPUT(2)
703
#define DST_RCDISC2__IN1 DISCRETE_INPUT(3)
704
#define DST_RCDISC2__R1 DISCRETE_INPUT(4)
705
#define DST_RCDISC2__C DISCRETE_INPUT(5)
599
707
static DISCRETE_STEP(dst_rcdisc2)
632
740
* input[2] - Resistor0 value (initialization only)
633
741
* input[4] - Resistor1 value (initialization only)
634
742
* input[5] - Capacitor Value (initialization only)
743
* input[6] - Diode Junction voltage (initialization only)
636
745
************************************************************************/
637
#define DST_RCDISC3__ENABLE (*(node->input[0]))
638
#define DST_RCDISC3__IN (*(node->input[1]))
639
#define DST_RCDISC3__R1 (*(node->input[2]))
640
#define DST_RCDISC3__R2 (*(node->input[3]))
641
#define DST_RCDISC3__C (*(node->input[4]))
746
#define DST_RCDISC3__ENABLE DISCRETE_INPUT(0)
747
#define DST_RCDISC3__IN DISCRETE_INPUT(1)
748
#define DST_RCDISC3__R1 DISCRETE_INPUT(2)
749
#define DST_RCDISC3__R2 DISCRETE_INPUT(3)
750
#define DST_RCDISC3__C DISCRETE_INPUT(4)
751
#define DST_RCDISC3__DJV DISCRETE_INPUT(5)
643
753
static DISCRETE_STEP(dst_rcdisc3)
651
761
if(DST_RCDISC3__ENABLE)
653
763
diff = DST_RCDISC3__IN - node->output[0];
656
diff = diff - (diff * context->exponent0);
660
diff = diff - (diff * context->exponent1);
662
diff = diff - (diff * context->exponent0);
764
if (context->v_diode > 0)
768
diff = diff * context->exponent0;
770
else if (diff < -context->v_diode)
772
diff = diff * context->exponent1;
776
diff = diff * context->exponent0;
783
diff = diff * context->exponent0;
785
else if (diff > -context->v_diode)
787
diff = diff * context->exponent1;
791
diff = diff * context->exponent0;
664
794
node->output[0] += diff;
695
826
* input[4] - circuit type (initialization only)
697
828
************************************************************************/
698
#define DST_RCDISC4__ENABLE (*(node->input[0]))
699
#define DST_RCDISC4__IN (*(node->input[1]))
700
#define DST_RCDISC4__R1 (*(node->input[2]))
701
#define DST_RCDISC4__R2 (*(node->input[3]))
702
#define DST_RCDISC4__R3 (*(node->input[4]))
703
#define DST_RCDISC4__C1 (*(node->input[5]))
704
#define DST_RCDISC4__VP (*(node->input[6]))
705
#define DST_RCDISC4__TYPE (*(node->input[7]))
829
#define DST_RCDISC4__ENABLE DISCRETE_INPUT(0)
830
#define DST_RCDISC4__IN DISCRETE_INPUT(1)
831
#define DST_RCDISC4__R1 DISCRETE_INPUT(2)
832
#define DST_RCDISC4__R2 DISCRETE_INPUT(3)
833
#define DST_RCDISC4__R3 DISCRETE_INPUT(4)
834
#define DST_RCDISC4__C1 DISCRETE_INPUT(5)
835
#define DST_RCDISC4__VP DISCRETE_INPUT(6)
836
#define DST_RCDISC4__TYPE DISCRETE_INPUT(7)
707
838
static DISCRETE_STEP(dst_rcdisc4)
740
871
/* some error checking. */
741
872
if (DST_RCDISC4__R1 <= 0 || DST_RCDISC4__R2 <= 0 || DST_RCDISC4__C1 <= 0 || (DST_RCDISC4__R3 <= 0 && context->type == 1))
743
discrete_log("Invalid component values in NODE_%d.\n", node->node - NODE_00);
874
discrete_log(node->info, "Invalid component values in NODE_%d.\n", NODE_BLOCKINDEX(node));
746
877
if (DST_RCDISC4__VP < 3)
748
discrete_log("vP must be >= 3V in NODE_%d.\n", node->node - NODE_00);
879
discrete_log(node->info, "vP must be >= 3V in NODE_%d.\n", NODE_BLOCKINDEX(node));
751
882
if (DST_RCDISC4__TYPE < 1 || DST_RCDISC4__TYPE > 3)
753
discrete_log("Invalid circuit type in NODE_%d.\n", node->node - NODE_00);
884
discrete_log(node->info, "Invalid circuit type in NODE_%d.\n", NODE_BLOCKINDEX(node));
811
942
* input[3] - Capacitor Value (initialization only)
813
944
************************************************************************/
814
#define DST_RCDISC5__ENABLE (*(node->input[0]))
815
#define DST_RCDISC5__IN (*(node->input[1]))
816
#define DST_RCDISC5__R (*(node->input[2]))
817
#define DST_RCDISC5__C (*(node->input[3]))
945
#define DST_RCDISC5__ENABLE DISCRETE_INPUT(0)
946
#define DST_RCDISC5__IN DISCRETE_INPUT(1)
947
#define DST_RCDISC5__R DISCRETE_INPUT(2)
948
#define DST_RCDISC5__C DISCRETE_INPUT(3)
819
950
static DISCRETE_STEP( dst_rcdisc5)
825
956
/* Exponential based in difference between input/output */
958
u = DST_RCDISC5__IN - 0.7; /* Diode drop */
962
diff = u - context->v_cap;
827
964
if(DST_RCDISC5__ENABLE)
829
u = DST_RCDISC5__IN - 0.7; /* Diode drop */
833
diff = u - node->output[0];
836
//diff = diff - (diff * exp(discrete_current_context->sample_time / context->exponent0));
837
diff = -node->output[0] + (node->output[0] * context->exponent0);
838
node->output[0] += diff;
967
diff = diff * context->exponent0;
969
context->v_cap += diff;
970
node->output[0] = context->v_cap;
842
977
node->output[0] = 0;
870
1006
* input[8] - Voltage Value (initialization only)
872
1008
************************************************************************/
873
#define DST_RCDISC_MOD__IN1 (*(node->input[0]))
874
#define DST_RCDISC_MOD__IN2 (*(node->input[1]))
875
#define DST_RCDISC_MOD__R1 (*(node->input[2]))
876
#define DST_RCDISC_MOD__R2 (*(node->input[3]))
877
#define DST_RCDISC_MOD__R3 (*(node->input[4]))
878
#define DST_RCDISC_MOD__R4 (*(node->input[5]))
879
#define DST_RCDISC_MOD__C (*(node->input[6]))
880
#define DST_RCDISC_MOD__VP (*(node->input[7]))
1009
#define DST_RCDISC_MOD__IN1 DISCRETE_INPUT(0)
1010
#define DST_RCDISC_MOD__IN2 DISCRETE_INPUT(1)
1011
#define DST_RCDISC_MOD__R1 DISCRETE_INPUT(2)
1012
#define DST_RCDISC_MOD__R2 DISCRETE_INPUT(3)
1013
#define DST_RCDISC_MOD__R3 DISCRETE_INPUT(4)
1014
#define DST_RCDISC_MOD__R4 DISCRETE_INPUT(5)
1015
#define DST_RCDISC_MOD__C DISCRETE_INPUT(6)
1016
#define DST_RCDISC_MOD__VP DISCRETE_INPUT(7)
882
1018
static DISCRETE_STEP(dst_rcdisc_mod)
963
1099
* input[4] - Voltage reference. Usually 0V.
965
1101
************************************************************************/
966
#define DST_RCFILTER__ENABLE (*(node->input[0]))
967
#define DST_RCFILTER__VIN (*(node->input[1]))
968
#define DST_RCFILTER__R (*(node->input[2]))
969
#define DST_RCFILTER__C (*(node->input[3]))
970
#define DST_RCFILTER__VREF (*(node->input[4]))
1102
#define DST_RCFILTER__VIN DISCRETE_INPUT(0)
1103
#define DST_RCFILTER__R DISCRETE_INPUT(1)
1104
#define DST_RCFILTER__C DISCRETE_INPUT(2)
1105
#define DST_RCFILTER__VREF DISCRETE_INPUT(3)
972
1107
static DISCRETE_STEP(dst_rcfilter)
974
1109
struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
1111
if (UNEXPECTED(context->has_rc_nodes))
1113
double rc = DST_RCFILTER__R * DST_RCFILTER__C;
1114
if (rc != context->rc)
1117
context->exponent = RC_CHARGE_EXP(rc);
976
1121
/************************************************************************/
977
1122
/* Next Value = PREV + (INPUT_VALUE - PREV)*(1-(EXP(-TIMEDELTA/RC))) */
978
1123
/************************************************************************/
980
if(DST_RCFILTER__ENABLE)
982
context->vCap += ((DST_RCFILTER__VIN - DST_RCFILTER__VREF - context->vCap) * context->exponent);
983
node->output[0] = context->vCap + DST_RCFILTER__VREF;
1125
context->vCap += ((DST_RCFILTER__VIN - node->output[0]) * context->exponent);
1126
node->output[0] = context->vCap + DST_RCFILTER__VREF;
1129
static DISCRETE_STEP(dst_rcfilter_fast)
1131
struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
1132
node->output[0] += ((DST_RCFILTER__VIN - node->output[0]) * context->exponent);
991
1135
static DISCRETE_RESET(dst_rcfilter)
993
1137
struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
995
context->exponent = RC_CHARGE_EXP(DST_RCFILTER__R * DST_RCFILTER__C);
1139
context->has_rc_nodes = node->input_is_node & 0x6;
1140
context->rc = DST_RCFILTER__R * DST_RCFILTER__C;
1141
context->exponent = RC_CHARGE_EXP(context->rc);
996
1142
context->vCap = 0;
997
1143
node->output[0] = 0;
1144
if (!context->has_rc_nodes && DST_RCFILTER__VREF == 0)
1145
node->step = DISCRETE_STEP_NAME(dst_rcfilter_fast);
1000
1148
/************************************************************************
1008
1156
* input[4] - Voltage reference. Usually 0V.
1010
1158
************************************************************************/
1011
#define DST_RCFILTER_SW__ENABLE (*(node->input[0]))
1012
#define DST_RCFILTER_SW__VIN (*(node->input[1]))
1013
#define DST_RCFILTER_SW__SWITCH (*(node->input[2]))
1014
#define DST_RCFILTER_SW__R (*(node->input[3]))
1015
#define DST_RCFILTER_SW__C(x) (*(node->input[4+x]))
1159
#define DST_RCFILTER_SW__ENABLE DISCRETE_INPUT(0)
1160
#define DST_RCFILTER_SW__VIN DISCRETE_INPUT(1)
1161
#define DST_RCFILTER_SW__SWITCH DISCRETE_INPUT(2)
1162
#define DST_RCFILTER_SW__R DISCRETE_INPUT(3)
1163
#define DST_RCFILTER_SW__C(x) DISCRETE_INPUT(4+x)
1017
#define CD4066_ON_RES 270
1018
#define DST_RCFILTER_SW_ITERATIONS (10)
1167
* UTC4066 : 270 @ 5VCC, 80 @ 15VCC
1168
* CD4066BC : 270 (Fairchild)
1170
* The choice below makes scramble sound about "right". For future error reports,
1171
* we need the exact type of switch and at which voltage (5, 12?) it is operated.
1173
#define CD4066_ON_RES (40)
1020
1175
// FIXME: This needs optimization !
1021
1176
static DISCRETE_STEP(dst_rcfilter_sw)
1023
1178
struct dst_rcfilter_sw_context *context = (struct dst_rcfilter_sw_context *)node->context;
1026
1181
int bits = (int)DST_RCFILTER_SW__SWITCH;
1027
double us = 0, rs = 0;
1029
1184
if (DST_RCFILTER_SW__ENABLE)
1042
1197
node->output[0] = context->vCap[1] + (DST_RCFILTER_SW__VIN - context->vCap[1]) * context->factor;
1045
for (j = 0; j < DST_RCFILTER_SW_ITERATIONS; j++)
1047
for (i = 0; i < 4; i++)
1049
if (( bits & (1 << i)) != 0)
1051
us += context->vCap[i];
1052
rs += DST_RCFILTER_SW__R;
1055
node->output[0] = RES_VOLTAGE_DIVIDER(rs, CD4066_ON_RES) * DST_RCFILTER_SW__VIN + DST_RCFILTER_SW__R / (CD4066_ON_RES + rs) * us;
1056
for (i = 0; i < 4; i++)
1058
if (( bits & (1 << i)) != 0)
1060
context->vCap[i] += (node->output[0] - context->vCap[i]) * context->exp[i];
1200
for (i = 0; i < 4; i++)
1202
if (( bits & (1 << i)) != 0)
1203
us += context->vCap[i];
1205
node->output[0] = context->f1[bits] * DST_RCFILTER_SW__VIN + context->f2[bits] * us;
1206
for (i = 0; i < 4; i++)
1208
if (( bits & (1 << i)) != 0)
1209
context->vCap[i] += (node->output[0] - context->vCap[i]) * context->exp[i];
1074
1221
struct dst_rcfilter_sw_context *context = (struct dst_rcfilter_sw_context *)node->context;
1078
1225
for (i = 0; i < 4; i++)
1080
1227
context->vCap[i] = 0;
1081
context->exp[i] = RC_CHARGE_EXP(((double) DST_RCFILTER_SW_ITERATIONS) * CD4066_ON_RES * DST_RCFILTER_SW__C(i));
1228
context->exp[i] = RC_CHARGE_EXP( CD4066_ON_RES * DST_RCFILTER_SW__C(i));
1231
for (bits=0; bits < 15; bits++)
1235
for (i = 0; i < 4; i++)
1237
if (( bits & (1 << i)) != 0)
1238
rs += DST_RCFILTER_SW__R;
1240
context->f1[bits] = RES_VOLTAGE_DIVIDER(rs, CD4066_ON_RES);
1241
context->f2[bits] = DST_RCFILTER_SW__R / (CD4066_ON_RES + rs);
1083
1245
/* fast cases */
1084
1246
context->exp0 = RC_CHARGE_EXP((CD4066_ON_RES + DST_RCFILTER_SW__R) * DST_RCFILTER_SW__C(0));
1085
1247
context->exp1 = RC_CHARGE_EXP((CD4066_ON_RES + DST_RCFILTER_SW__R) * DST_RCFILTER_SW__C(1));
1086
1248
context->factor = RES_VOLTAGE_DIVIDER(DST_RCFILTER_SW__R, CD4066_ON_RES);
1087
1250
node->output[0] = 0;
1101
1264
* input[5] - Capacitor Value (initialization only)
1103
1266
************************************************************************/
1104
#define DST_RCINTEGRATE__IN1 (*(node->input[0]))
1105
#define DST_RCINTEGRATE__R1 (*(node->input[1]))
1106
#define DST_RCINTEGRATE__R2 (*(node->input[2]))
1107
#define DST_RCINTEGRATE__R3 (*(node->input[3]))
1108
#define DST_RCINTEGRATE__C (*(node->input[4]))
1109
#define DST_RCINTEGRATE__VP (*(node->input[5]))
1110
#define DST_RCINTEGRATE__TYPE (*(node->input[6]))
1267
#define DST_RCINTEGRATE__IN1 DISCRETE_INPUT(0)
1268
#define DST_RCINTEGRATE__R1 DISCRETE_INPUT(1)
1269
#define DST_RCINTEGRATE__R2 DISCRETE_INPUT(2)
1270
#define DST_RCINTEGRATE__R3 DISCRETE_INPUT(3)
1271
#define DST_RCINTEGRATE__C DISCRETE_INPUT(4)
1272
#define DST_RCINTEGRATE__VP DISCRETE_INPUT(5)
1273
#define DST_RCINTEGRATE__TYPE DISCRETE_INPUT(6)
1112
1275
/* Ebers-Moll large signal model
1227
1390
* 2008, couriersud
1228
1391
************************************************************************/
1229
#define DST_SALLEN_KEY__ENABLE (*(node->input[0]))
1230
#define DST_SALLEN_KEY__INP0 (*(node->input[1]))
1231
#define DST_SALLEN_KEY__TYPE (*(node->input[2]))
1392
#define DST_SALLEN_KEY__ENABLE DISCRETE_INPUT(0)
1393
#define DST_SALLEN_KEY__INP0 DISCRETE_INPUT(1)
1394
#define DST_SALLEN_KEY__TYPE DISCRETE_INPUT(2)
1233
1396
static DISCRETE_STEP(dst_sallen_key)
1267
1430
fatalerror("Unknown sallen key filter type");
1270
calculate_filter2_coefficients(freq, 1.0 / q, DISC_FILTER_LOWPASS,
1433
calculate_filter2_coefficients(node->info, freq, 1.0 / q, DISC_FILTER_LOWPASS,
1271
1434
&context->a1, &context->a2,
1272
1435
&context->b0, &context->b1, &context->b2);
1273
1436
node->output[0] = 0;
1287
1450
* input[3] - Capacitor Value (initialization only)
1289
1452
************************************************************************/
1290
#define DST_RCFILTERN__ENABLE (*(node->input[0]))
1291
#define DST_RCFILTERN__IN (*(node->input[1]))
1292
#define DST_RCFILTERN__R (*(node->input[2]))
1293
#define DST_RCFILTERN__C (*(node->input[3]))
1453
#define DST_RCFILTERN__ENABLE DISCRETE_INPUT(0)
1454
#define DST_RCFILTERN__IN DISCRETE_INPUT(1)
1455
#define DST_RCFILTERN__R DISCRETE_INPUT(2)
1456
#define DST_RCFILTERN__C DISCRETE_INPUT(3)
1295
1458
static DISCRETE_RESET(dst_rcfilterN)
1320
1483
* input[3] - Capacitor Value (initialization only)
1322
1485
************************************************************************/
1323
#define DST_RCDISCN__ENABLE (*(node->input[0]))
1324
#define DST_RCDISCN__IN (*(node->input[1]))
1325
#define DST_RCDISCN__R (*(node->input[2]))
1326
#define DST_RCDISCN__C (*(node->input[3]))
1486
#define DST_RCDISCN__ENABLE DISCRETE_INPUT(0)
1487
#define DST_RCDISCN__IN DISCRETE_INPUT(1)
1488
#define DST_RCDISCN__R DISCRETE_INPUT(2)
1489
#define DST_RCDISCN__C DISCRETE_INPUT(3)
1328
1491
static DISCRETE_RESET(dst_rcdiscN)
1377
1540
* input[5] - Capacitor Value (initialization only)
1379
1542
************************************************************************/
1380
#define DST_RCDISC2N__ENABLE (*(node->input[0]))
1381
#define DST_RCDISC2N__IN0 (*(node->input[1]))
1382
#define DST_RCDISC2N__R0 (*(node->input[2]))
1383
#define DST_RCDISC2N__IN1 (*(node->input[3]))
1384
#define DST_RCDISC2N__R1 (*(node->input[4]))
1385
#define DST_RCDISC2N__C (*(node->input[5]))
1543
#define DST_RCDISC2N__ENABLE DISCRETE_INPUT(0)
1544
#define DST_RCDISC2N__IN0 DISCRETE_INPUT(1)
1545
#define DST_RCDISC2N__R0 DISCRETE_INPUT(2)
1546
#define DST_RCDISC2N__IN1 DISCRETE_INPUT(3)
1547
#define DST_RCDISC2N__R1 DISCRETE_INPUT(4)
1548
#define DST_RCDISC2N__C DISCRETE_INPUT(5)
1387
1550
struct dss_rcdisc2_context
1415
1578
f1 = 1.0 / (2 * M_PI * DST_RCDISC2N__R0 * DST_RCDISC2N__C);
1416
1579
f2 = 1.0 / (2 * M_PI * DST_RCDISC2N__R1 * DST_RCDISC2N__C);
1418
calculate_filter1_coefficients(f1, DISC_FILTER_LOWPASS, &context->a1_0, &context->b0_0, &context->b1_0);
1419
calculate_filter1_coefficients(f2, DISC_FILTER_LOWPASS, &context->a1_1, &context->b0_1, &context->b1_1);
1581
calculate_filter1_coefficients(node->info, f1, DISC_FILTER_LOWPASS, &context->a1_0, &context->b0_0, &context->b1_0);
1582
calculate_filter1_coefficients(node->info, f2, DISC_FILTER_LOWPASS, &context->a1_1, &context->b0_1, &context->b1_1);
1421
1584
/* Initialize the object */
1422
1585
node->output[0] = 0;