~ubuntu-branches/ubuntu/lucid/sdlmame/lucid

« back to all changes in this revision

Viewing changes to src/emu/sound/disc_flt.c

  • Committer: Bazaar Package Importer
  • Author(s): Cesare Falco
  • Date: 2009-11-03 17:10:15 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20091103171015-6hop4ory5lxnumpn
Tags: 0.135-0ubuntu1
* New upstream release - Closes (LP: #403212)
* debian/watch: unstable releases are no longer detected
* mame.ini: added the cheat subdirectories to cheatpath so zipped
  cheatfiles will be searched too
* renamed crsshair subdirectory to crosshair to reflect upstream change
* mame.ini: renamed references to crosshair subdirectory (see above)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 */
67
68
};
68
69
 
 
70
struct dst_rc_circuit_1_context
 
71
{
 
72
        double v_cap;
 
73
        double v_charge_1_2;
 
74
        double v_drop;
 
75
        double exp_1;
 
76
        double exp_1_2;
 
77
        double exp_2;
 
78
};
 
79
 
69
80
struct dst_rcdisc_context
70
81
{
71
82
        int state;
72
83
        double t;           /* time */
73
84
        double exponent0;
74
85
        double exponent1;
 
86
        double v_cap;           /* rcdisc5 */
 
87
        double v_diode;         /* rcdisc3 */
75
88
};
76
89
 
77
90
struct dst_rcdisc_mod_context
94
107
 
95
108
struct dst_rcfilter_context
96
109
{
 
110
        double  vCap;
 
111
        double  rc;
97
112
        double  exponent;
98
 
        double  vCap;
 
113
        UINT8   has_rc_nodes;
99
114
};
100
115
 
101
116
struct dst_rcfilter_sw_context
105
120
        double  exp0;   /* fast case bit 0 */
106
121
        double  exp1;   /* fast case bit 1 */
107
122
        double  factor; /* fast case */
 
123
        double  f1[16];
 
124
        double  f2[16];
108
125
};
109
126
 
110
127
struct dst_rcintegrate_context
133
150
 * input[4]    - Voltage reference. Usually 0V.
134
151
 *
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)
141
157
 
142
158
static DISCRETE_STEP(dst_crfilter)
143
159
{
144
160
        struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
145
161
 
146
 
        if(DST_CRFILTER__ENABLE)
147
 
        {
148
 
                node->output[0] = DST_CRFILTER__IN - context->vCap;
149
 
                context->vCap += ((DST_CRFILTER__IN - DST_CRFILTER__VREF) - context->vCap) * context->exponent;
150
 
        }
151
 
        else
152
 
        {
153
 
                node->output[0] = 0;
154
 
        }
 
162
        if (UNEXPECTED(context->has_rc_nodes))
 
163
        {
 
164
                double rc = DST_CRFILTER__R * DST_CRFILTER__C;
 
165
                if (rc != context->rc)
 
166
                {
 
167
                        context->rc = rc;
 
168
                        context->exponent = RC_CHARGE_EXP(rc);
 
169
                }
 
170
        }
 
171
 
 
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;
155
175
}
156
176
 
157
177
static DISCRETE_RESET(dst_crfilter)
158
178
{
159
179
        struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
160
180
 
161
 
        context->exponent = RC_CHARGE_EXP(DST_CRFILTER__R * DST_CRFILTER__C);
162
 
        context->vCap   = 0;
 
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);
 
184
        context->vCap = 0;
163
185
        node->output[0] = DST_CRFILTER__IN;
164
186
}
165
187
 
174
196
 * input[3]    - Filter type (initialization only)
175
197
 *
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)
181
203
 
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)
184
206
{
185
207
        double den, w, two_over_T;
186
208
 
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;
191
213
 
192
214
        den = w + two_over_T;
193
215
        *a1 = (w - two_over_T)/den;
202
224
        }
203
225
        else
204
226
        {
205
 
                discrete_log("calculate_filter1_coefficients() - Invalid filter type for 1st order filter.");
 
227
                discrete_log(disc_info, "calculate_filter1_coefficients() - Invalid filter type for 1st order filter.");
206
228
        }
207
229
}
208
230
 
227
249
{
228
250
        struct dss_filter1_context *context = (struct dss_filter1_context *)node->context;
229
251
 
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;
232
254
}
233
255
 
243
265
 * input[4]    - Filter type (initialization only)
244
266
 *
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)
251
273
 
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)
255
278
{
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;
261
284
 
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;
266
289
 
267
290
        den = two_over_T_squared + d*w*two_over_T + w_squared;
287
310
        }
288
311
        else
289
312
        {
290
 
                discrete_log("calculate_filter2_coefficients() - Invalid filter type for 2nd order filter.");
 
313
                discrete_log(disc_info, "calculate_filter2_coefficients() - Invalid filter type for 2nd order filter.");
291
314
        }
292
315
}
293
316
 
315
338
{
316
339
        struct dss_filter2_context *context = (struct dss_filter2_context *)node->context;
317
340
 
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;
335
358
 *
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)
342
365
 
343
366
static DISCRETE_STEP(dst_op_amp_filt)
344
367
{
358
381
                {
359
382
                        /* Millman the input voltages. */
360
383
                        i  = context->iFixed;
361
 
                        i += (DST_OP_AMP_FILT__INP1 - context->vRef) / info->r1;
362
 
                        if (info->r2 != 0)
363
 
                                i += (DST_OP_AMP_FILT__INP2 - context->vRef) / info->r2;
 
384
                        switch (context->type)
 
385
                        {
 
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;
 
388
                                        if (info->r2 != 0)
 
389
                                                i += (context->vP - DST_OP_AMP_FILT__INP2) / info->r2;
 
390
                                        if (info->r3 != 0)
 
391
                                                i += (context->vN - DST_OP_AMP_FILT__INP2) / info->r3;
 
392
                                        break;
 
393
                                default:
 
394
                                        i += (DST_OP_AMP_FILT__INP1 - context->vRef) / info->r1;
 
395
                                        if (info->r2 != 0)
 
396
                                                i += (DST_OP_AMP_FILT__INP2 - context->vRef) / info->r2;
 
397
                                        break;
 
398
                        }
364
399
                        v = i * context->rTotal;
365
400
                }
366
401
 
371
406
                                node->output[0] = context->vC1 * context->gain + info->vRef;
372
407
                                break;
373
408
 
 
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;
 
412
                                break;
 
413
 
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;
468
508
        switch (context->type)
469
509
        {
470
510
                case DISC_OP_AMP_FILTER_IS_LOW_PASS_1:
 
511
                case DISC_OP_AMP_FILTER_IS_LOW_PASS_1_A:
471
512
                        context->exponentC1 = RC_CHARGE_EXP(info->rF * info->c1);
472
513
                        context->exponentC2 =  0;
473
514
                        break;
480
521
                        context->exponentC2 = RC_CHARGE_EXP(context->rTotal * info->c2);
481
522
                        break;
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);
 
524
                        if (info->r2 == 0)
 
525
                                context->rTotal = info->r1;
 
526
                        else
 
527
                                context->rTotal = RES_2_PARALLEL(info->r1, info->r2);
484
528
                case DISC_OP_AMP_FILTER_IS_BAND_PASS_1M:
485
529
                {
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);
489
533
 
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;
523
567
 
524
568
/************************************************************************
525
569
 *
 
570
 * DST_RC_CIRCUIT_1 - RC charge/discharge circuit
 
571
 *
 
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)
 
577
 
 
578
#define CD4066_R_ON     270
 
579
 
 
580
static DISCRETE_STEP( dst_rc_circuit_1 )
 
581
{
 
582
        struct dst_rc_circuit_1_context *context = (struct dst_rc_circuit_1_context *)node->context;
 
583
 
 
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 */
 
588
                        node->output[0] = 0;
 
589
                else
 
590
                {
 
591
                        /* cap is discharged */
 
592
                        context->v_cap -= context->v_cap * context->exp_2;
 
593
                        node->output[0] = context->v_cap * context->v_drop;
 
594
                }
 
595
        else
 
596
                if (DST_RC_CIRCUIT_1__IN1 == 0)
 
597
                {
 
598
                        /* cap is charged */
 
599
                        context->v_cap += (5.0 - context->v_cap) * context->exp_1;
 
600
                        /* output is pulled to ground */
 
601
                        node->output[0] = 0;
 
602
                }
 
603
                else
 
604
                {
 
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;
 
608
                }
 
609
}
 
610
 
 
611
static DISCRETE_RESET( dst_rc_circuit_1 )
 
612
{
 
613
        struct dst_rc_circuit_1_context *context = (struct dst_rc_circuit_1_context *)node->context;
 
614
 
 
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;
 
618
        context->v_cap = 0;
 
619
 
 
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);
 
627
 
 
628
        /* starts at 0 until cap starts charging */
 
629
        node->output[0] = 0;
 
630
}
 
631
 
 
632
/************************************************************************
 
633
 *
526
634
 * DST_RCDISC -   Usage of node_description values for RC discharge
527
635
 *                (inverse slope of DST_RCFILTER)
528
636
 *
532
640
 * input[3]    - Capacitor Value (initialization only)
533
641
 *
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)
539
647
 
540
648
static DISCRETE_STEP(dst_rcdisc)
541
649
{
556
664
                        if (DST_RCDISC__ENABLE)
557
665
                        {
558
666
                                node->output[0] = DST_RCDISC__IN * exp(context->t / context->exponent0);
559
 
                                context->t += discrete_current_context->sample_time;
 
667
                                context->t += node->info->sample_time;
560
668
                        } else
561
669
                        {
562
670
                                context->state = 0;
589
697
 * input[5]    - Capacitor Value (initialization only)
590
698
 *
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)
598
706
 
599
707
static DISCRETE_STEP(dst_rcdisc2)
600
708
{
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)
635
744
 *
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)
642
752
 
643
753
static DISCRETE_STEP(dst_rcdisc3)
644
754
{
651
761
        if(DST_RCDISC3__ENABLE)
652
762
        {
653
763
                diff = DST_RCDISC3__IN - node->output[0];
654
 
                if( diff > 0 )
655
 
                {
656
 
                        diff = diff - (diff * context->exponent0);
657
 
                } else if( diff < 0)
658
 
                {
659
 
                        if(diff < -0.5)
660
 
                                diff = diff - (diff * context->exponent1);
661
 
                        else
662
 
                                diff = diff - (diff * context->exponent0);
 
764
                if (context->v_diode > 0)
 
765
                {
 
766
                        if (diff > 0)
 
767
                        {
 
768
                                diff = diff * context->exponent0;
 
769
                        }
 
770
                        else if (diff < -context->v_diode)
 
771
                        {
 
772
                                diff = diff * context->exponent1;
 
773
                        }
 
774
                        else
 
775
                        {
 
776
                                diff = diff * context->exponent0;
 
777
                        }
 
778
                }
 
779
                else
 
780
                {
 
781
                        if (diff < 0)
 
782
                        {
 
783
                                diff = diff * context->exponent0;
 
784
                        }
 
785
                        else if (diff > -context->v_diode)
 
786
                        {
 
787
                                diff = diff * context->exponent1;
 
788
                        }
 
789
                        else
 
790
                        {
 
791
                                diff = diff * context->exponent0;
 
792
                        }
663
793
                }
664
794
                node->output[0] += diff;
665
795
        }
677
807
 
678
808
        context->state = 0;
679
809
        context->t = 0;
 
810
        context->v_diode = DST_RCDISC3__DJV;
680
811
        context->exponent0 = RC_CHARGE_EXP(DST_RCDISC3__R1 * DST_RCDISC3__C);
681
812
        context->exponent1 = RC_CHARGE_EXP(RES_2_PARALLEL(DST_RCDISC3__R1, DST_RCDISC3__R2) * DST_RCDISC3__C);
682
813
}
695
826
 * input[4]    - circuit type (initialization only)
696
827
 *
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)
706
837
 
707
838
static DISCRETE_STEP(dst_rcdisc4)
708
839
{
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))
742
873
        {
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));
744
875
                return;
745
876
        }
746
877
        if (DST_RCDISC4__VP < 3)
747
878
        {
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));
749
880
                return;
750
881
        }
751
882
        if (DST_RCDISC4__TYPE < 1 || DST_RCDISC4__TYPE > 3)
752
883
        {
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));
754
885
                return;
755
886
        }
756
887
 
811
942
 * input[3]    - Capacitor Value (initialization only)
812
943
 *
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)
818
949
 
819
950
static DISCRETE_STEP( dst_rcdisc5)
820
951
{
824
955
 
825
956
        /* Exponential based in difference between input/output   */
826
957
 
 
958
    u = DST_RCDISC5__IN - 0.7; /* Diode drop */
 
959
        if( u < 0)
 
960
                u = 0;
 
961
 
 
962
        diff = u - context->v_cap;
 
963
 
827
964
        if(DST_RCDISC5__ENABLE)
828
965
        {
829
 
            u = DST_RCDISC5__IN - 0.7; /* Diode drop */
830
 
                if( u < 0)
831
 
                        u = 0;
832
 
 
833
 
                diff = u - node->output[0];
834
 
 
835
966
                if(diff < 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;
 
968
 
 
969
                context->v_cap += diff;
 
970
                node->output[0] = context->v_cap;
839
971
        }
840
972
        else
841
973
        {
 
974
                if(diff > 0)
 
975
                        context->v_cap = u;
 
976
 
842
977
                node->output[0] = 0;
843
978
        }
844
979
}
851
986
 
852
987
        context->state = 0;
853
988
        context->t = 0;
 
989
        context->v_cap = 0;
854
990
        context->exponent0 = RC_CHARGE_EXP(DST_RCDISC5__R * DST_RCDISC5__C);
855
991
}
856
992
 
870
1006
 * input[8]    - Voltage Value (initialization only)
871
1007
 *
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)
881
1017
 
882
1018
static DISCRETE_STEP(dst_rcdisc_mod)
883
1019
{
963
1099
 * input[4]    - Voltage reference. Usually 0V.
964
1100
 *
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)
971
1106
 
972
1107
static DISCRETE_STEP(dst_rcfilter)
973
1108
{
974
1109
        struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
975
1110
 
 
1111
        if (UNEXPECTED(context->has_rc_nodes))
 
1112
        {
 
1113
                double rc = DST_RCFILTER__R * DST_RCFILTER__C;
 
1114
                if (rc != context->rc)
 
1115
                {
 
1116
                        context->rc = rc;
 
1117
                        context->exponent = RC_CHARGE_EXP(rc);
 
1118
                }
 
1119
        }
 
1120
 
976
1121
        /************************************************************************/
977
1122
        /* Next Value = PREV + (INPUT_VALUE - PREV)*(1-(EXP(-TIMEDELTA/RC)))    */
978
1123
        /************************************************************************/
979
1124
 
980
 
        if(DST_RCFILTER__ENABLE)
981
 
        {
982
 
                context->vCap += ((DST_RCFILTER__VIN - DST_RCFILTER__VREF - context->vCap) * context->exponent);
983
 
                node->output[0] = context->vCap + DST_RCFILTER__VREF;
984
 
        }
985
 
        else
986
 
        {
987
 
                node->output[0] = 0;
988
 
        }
 
1125
        context->vCap += ((DST_RCFILTER__VIN - node->output[0]) * context->exponent);
 
1126
        node->output[0] = context->vCap + DST_RCFILTER__VREF;
 
1127
}
 
1128
 
 
1129
static DISCRETE_STEP(dst_rcfilter_fast)
 
1130
{
 
1131
        struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
 
1132
        node->output[0] += ((DST_RCFILTER__VIN - node->output[0]) * context->exponent);
989
1133
}
990
1134
 
991
1135
static DISCRETE_RESET(dst_rcfilter)
992
1136
{
993
1137
        struct dst_rcfilter_context *context = (struct dst_rcfilter_context *)node->context;
994
1138
 
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);
998
1146
}
999
1147
 
1000
1148
/************************************************************************
1008
1156
 * input[4]    - Voltage reference. Usually 0V.
1009
1157
 *
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)
1016
1164
 
1017
 
#define CD4066_ON_RES  270
1018
 
#define DST_RCFILTER_SW_ITERATIONS      (10)
 
1165
/* 74HC4066 : 15
 
1166
 * 74VHC4066 : 15
 
1167
 * UTC4066 : 270 @ 5VCC, 80 @ 15VCC
 
1168
 * CD4066BC : 270 (Fairchild)
 
1169
 *
 
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.
 
1172
 */
 
1173
#define CD4066_ON_RES (40)
1019
1174
 
1020
1175
// FIXME: This needs optimization !
1021
1176
static DISCRETE_STEP(dst_rcfilter_sw)
1022
1177
{
1023
1178
        struct dst_rcfilter_sw_context *context = (struct dst_rcfilter_sw_context *)node->context;
1024
1179
 
1025
 
        int i,j ;
 
1180
        int i;
1026
1181
        int bits = (int)DST_RCFILTER_SW__SWITCH;
1027
 
        double us = 0, rs = 0;
 
1182
        double us = 0;
1028
1183
 
1029
1184
        if (DST_RCFILTER_SW__ENABLE)
1030
1185
        {
1042
1197
                        node->output[0] = context->vCap[1] + (DST_RCFILTER_SW__VIN - context->vCap[1]) * context->factor;
1043
1198
                        break;
1044
1199
                default:
1045
 
                        for (j = 0; j < DST_RCFILTER_SW_ITERATIONS; j++)
1046
 
                        {
1047
 
                                for (i = 0; i < 4; i++)
1048
 
                                {
1049
 
                                        if (( bits & (1 << i)) != 0)
1050
 
                                        {
1051
 
                                                us += context->vCap[i];
1052
 
                                                rs += DST_RCFILTER_SW__R;
1053
 
                                        }
1054
 
                                }
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++)
1057
 
                                {
1058
 
                                        if (( bits & (1 << i)) != 0)
1059
 
                                        {
1060
 
                                                context->vCap[i] += (node->output[0] - context->vCap[i]) * context->exp[i];
1061
 
                                        }
1062
 
                                }
 
1200
                        for (i = 0; i < 4; i++)
 
1201
                        {
 
1202
                                if (( bits & (1 << i)) != 0)
 
1203
                                        us += context->vCap[i];
 
1204
                        }
 
1205
                        node->output[0] = context->f1[bits] * DST_RCFILTER_SW__VIN + context->f2[bits]  * us;
 
1206
                        for (i = 0; i < 4; i++)
 
1207
                        {
 
1208
                                if (( bits & (1 << i)) != 0)
 
1209
                                        context->vCap[i] += (node->output[0] - context->vCap[i]) * context->exp[i];
1063
1210
                        }
1064
1211
                }
1065
1212
        }
1073
1220
{
1074
1221
        struct dst_rcfilter_sw_context *context = (struct dst_rcfilter_sw_context *)node->context;
1075
1222
 
1076
 
        int i;
 
1223
        int i, bits;
1077
1224
 
1078
1225
        for (i = 0; i < 4; i++)
1079
1226
        {
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));
1082
 
        }
 
1228
                context->exp[i] = RC_CHARGE_EXP( CD4066_ON_RES * DST_RCFILTER_SW__C(i));
 
1229
        }
 
1230
 
 
1231
        for (bits=0; bits < 15; bits++)
 
1232
        {
 
1233
                double rs = 0;
 
1234
 
 
1235
                for (i = 0; i < 4; i++)
 
1236
                {
 
1237
                        if (( bits & (1 << i)) != 0)
 
1238
                                rs += DST_RCFILTER_SW__R;
 
1239
                }
 
1240
                context->f1[bits] = RES_VOLTAGE_DIVIDER(rs, CD4066_ON_RES);
 
1241
                context->f2[bits] = DST_RCFILTER_SW__R / (CD4066_ON_RES + rs);
 
1242
        }
 
1243
 
 
1244
 
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);
 
1249
 
1087
1250
        node->output[0] = 0;
1088
1251
}
1089
1252
 
1101
1264
 * input[5]    - Capacitor Value (initialization only)
1102
1265
 *
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)
1111
1274
 
1112
1275
/* Ebers-Moll large signal model
1113
1276
 * Couriersud:
1191
1354
        struct dst_rcintegrate_context *context = (struct dst_rcintegrate_context *)node->context;
1192
1355
 
1193
1356
        double r;
1194
 
        double dt = discrete_current_context->sample_time;
 
1357
        double dt = node->info->sample_time;
1195
1358
 
1196
1359
        context->type = DST_RCINTEGRATE__TYPE;
1197
1360
 
1226
1389
 *
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)
1232
1395
 
1233
1396
static DISCRETE_STEP(dst_sallen_key)
1234
1397
{
1267
1430
                        fatalerror("Unknown sallen key filter type");
1268
1431
        }
1269
1432
 
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)
1288
1451
 *
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)
1294
1457
 
1295
1458
static DISCRETE_RESET(dst_rcfilterN)
1296
1459
{
1320
1483
 * input[3]    - Capacitor Value (initialization only)
1321
1484
 *
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)
1327
1490
 
1328
1491
static DISCRETE_RESET(dst_rcdiscN)
1329
1492
{
1377
1540
 * input[5]    - Capacitor Value (initialization only)
1378
1541
 *
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)
1386
1549
 
1387
1550
struct dss_rcdisc2_context
1388
1551
{
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);
1417
1580
 
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);
1420
1583
 
1421
1584
        /* Initialize the object */
1422
1585
        node->output[0] = 0;