summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/disc_dev.c
blob: 9764bede8b528b8e3678dc8c7d7d8925a9bb4a87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
/************************************************************************
 *
 *  MAME - Discrete sound system emulation library
 *
 *  Written by Keith Wilkins (mame@dysfunction.demon.co.uk)
 *
 *  (c) K.Wilkins 2000
 *  (c) D.Renaud 2003-2004
 *
 ************************************************************************
 *
 * DSD_555_ASTBL         - NE555 Simulation - Astable mode
 * DSD_555_MSTBL         - NE555 Simulation - Monostable mode
 * DSD_555_CC            - NE555 Constant Current VCO
 * DSD_555_VCO1          - Op-Amp linear ramp based 555 VCO
 * DSD_566               - NE566 Simulation
 *
 ************************************************************************
 *
 * You will notice that the code for a lot of these routines are similar.
 * I tried to make a common charging routine, but there are too many
 * minor differences that affect each module.
 *
 ************************************************************************/

#define DEFAULT_555_CAP_BLEED	RES_M(10)

struct dsd_555_astbl_context
{
	int		use_ctrlv;
	int		output_type;
	int		output_is_ac;
	double	ac_shift;			/* DC shift needed to make waveform ac */
	int		flip_flop;			/* 555 flip/flop output state */
	double	cap_voltage;		/* voltage on cap */
	double	threshold;
	double	trigger;
	double	v_out_high;			/* Logic 1 voltage level */
	double	v_charge;
	double *v_charge_node;		/* point to output of node */
};

struct dsd_555_mstbl_context
{
	int		trig_is_logic;
	int		trig_discharges_cap;
	int		output_type;
	int		output_is_ac;
	double	ac_shift;				/* DC shift needed to make waveform ac */
	int		flip_flop;				/* 555 flip/flop output state */
	double	cap_voltage;			/* voltage on cap */
	double	threshold;
	double	trigger;
	double	v_out_high;				/* Logic 1 voltage level */
	double	v_charge;
};

struct dsd_555_cc_context
{
	unsigned int	type;			/* type of 555cc circuit */
	int				output_type;
	int				output_is_ac;
	double			ac_shift;		/* DC shift needed to make waveform ac */
	int				flip_flop;		/* 555 flip/flop output state */
	double			cap_voltage;	/* voltage on cap */
	double			threshold;
	double			trigger;
	double			v_out_high;		/* Logic 1 voltage level */
	double			v_cc_source;
};

struct dsd_555_vco1_context
{
	int		ctrlv_is_node;
	int		output_type;
	int		output_is_ac;
	double	ac_shift;			/* DC shift needed to make waveform ac */
	int		flip_flop;			/* flip/flop output state */
	double	v_out_high;			/* 555 high voltage */
	double	threshold;			/* falling threshold */
	double	trigger;			/* rising threshold */
	double	i_charge;			/* charge current */
	double	i_discharge;		/* discharge current */
	double	cap_voltage;		/* current capacitor voltage */
};

struct dsd_566_context
{
	int			error;
	unsigned int state[2];			/* keeps track of excess flip_flop changes during the current step */
	int			flip_flop;			/* 566 flip/flop output state */
	double		cap_voltage;		/* voltage on cap */
	double		v_charge;			/* static charge value */
	double	   *v_charge_node;		/* point to charge node */
	double		v_sqr_low;			/* voltage for a squarewave at low */
	double		v_sqr_high;			/* voltage for a squarewave at high */
	double		threshold_low;		/* falling threshold */
	double		threshold_high;		/* rising threshold */
	double		triangle_ac_offset;	/* used to shift a triangle to AC */
};

struct dsd_ls624_context
{
	int			state;
	double		remain;			/* remaining time from last step */
	int			out_type;
};


/************************************************************************
 *
 * DSD_555_ASTBL -  - 555 Astable simulation
 *
 * input[0]    - Reset value
 * input[1]    - R1 value
 * input[2]    - R2 value
 * input[3]    - C value
 * input[4]    - Control Voltage value
 *
 * also passed discrete_555_desc structure
 *
 * Jan 2004, D Renaud.
 ************************************************************************/
#define DSD_555_ASTBL__RESET	(! *(node->input[0]))
#define DSD_555_ASTBL__R1		(*(node->input[1]))
#define DSD_555_ASTBL__R2		(*(node->input[2]))
#define DSD_555_ASTBL__C		(*(node->input[3]))
#define DSD_555_ASTBL__CTRLV	(*(node->input[4]))

static void dsd_555_astbl_step(node_description *node)
{
	const  discrete_555_desc     *info    = node->custom;
	struct dsd_555_astbl_context *context = node->context;

	int		count_f = 0;
	int		count_r = 0;
	double	dt;								/* change in time */
	double	x_time  = 0;					/* time since change happened */
	double	t_rc    = 0;					/* RC time constant */
	double	v_cap   = context->cap_voltage;	/* Current voltage on capacitor, before dt */
	double	v_cap_next = 0;					/* Voltage on capacitor, after dt */
	double	v_charge;

	if(DSD_555_ASTBL__RESET)
	{
		/* We are in RESET */
		node->output[0]      = 0;
		context->flip_flop   = 1;
		context->cap_voltage = 0;
		return;
	}

	/* Check: if the Control Voltage node is connected. */
	if (context->use_ctrlv)
	{
		/* If CV is less then .25V, the circuit will oscillate way out of range.
         * So we will just ignore it when it happens. */
		if (DSD_555_ASTBL__CTRLV < .25) return;
		/* If it is a node then calculate thresholds based on Control Voltage */
		context->threshold = DSD_555_ASTBL__CTRLV;
		context->trigger   = DSD_555_ASTBL__CTRLV / 2.0;
		/* Since the thresholds may have changed we need to update the FF */
		if (v_cap >= context->threshold)
		{
			context->flip_flop = 0;
			count_f++;
		}
		else
		if (v_cap <= context->trigger)
		{
			context->flip_flop = 1;
			count_r++;
		}
	}

	/* get the v_charge and update each step if it is a node */
	if (context->v_charge_node != NULL)
	{
		v_charge = *context->v_charge_node;
		if (info->options & DISC_555_ASTABLE_HAS_FAST_CHARGE_DIODE) v_charge -= 0.5;
	}
	else
		v_charge = context->v_charge;


	/* Calculate future capacitor voltage.
     * ref@ http://www.physics.rutgers.edu/ugrad/205/capacitance.html
     * The formulas from the ref pages have been modified to reflect that we are stepping the change.
     * dt = time of sample (1/sample frequency)
     * VC = Voltage across capacitor
     * VC' = Future voltage across capacitor
     * Vc = Voltage change
     * Vr = is the voltage across the resistor.  For charging it is Vcc - VC.  Discharging it is VC - 0.
     * R = R1+R2 (for charging)  R = R2 for discharging.
     * Vc = Vr*(1-exp(-dt/(R*C)))
     * VC' = VC + Vc (for charging) VC' = VC - Vc for discharging.
     *
     * We will also need to calculate the amount of time we overshoot the thresholds
     * dt = amount of time we overshot
     * Vc = voltage change overshoot
     * dt = R*C(log(1/(1-(Vc/Vr))))
     */

	dt = discrete_current_context->sample_time;

	/* Sometimes a switching network is used to setup the capacitance.
     * These may select no capacitor, causing oscillation to stop.
     */
	if (DSD_555_ASTBL__C == 0)
	{
		context->flip_flop = 1;
		/* The voltage goes high because the cap circuit is open. */
		v_cap_next = v_charge;
		v_cap      = v_charge;
		context->cap_voltage = 0;
	}
	else
	{
		/* Keep looping until all toggling in time sample is used up. */
		do
		{
			if (context->flip_flop)
			{
				if (DSD_555_ASTBL__R1 == 0)
				{
					/* Oscillation disabled because there is no longer any charge resistor. */
					/* Bleed the cap due to circuit losses. */
					t_rc       = DEFAULT_555_CAP_BLEED * DSD_555_ASTBL__C;
					v_cap_next = v_cap - (v_cap * (1.0 - exp(-(dt / t_rc ))));
					dt = 0;
				}
				else
				{
					/* Charging */
					/* Use quick charge if specified. */
					t_rc       = (DSD_555_ASTBL__R1 + ((info->options & DISC_555_ASTABLE_HAS_FAST_CHARGE_DIODE) ? 0 : DSD_555_ASTBL__R2)) * DSD_555_ASTBL__C;
					v_cap_next = v_cap + ((v_charge - v_cap) * (1.0 - exp(-(dt / t_rc ))));
					dt = 0;

					/* has it charged past upper limit? */
					if (v_cap_next >= context->threshold)
					{
						/* calculate the overshoot time */
						dt     = t_rc  * log(1.0 / (1.0 - ((v_cap_next - context->threshold) / (v_charge - v_cap))));
						x_time = dt;
						v_cap  = context->threshold;
						context->flip_flop = 0;
						count_f++;
					}
				}
			}
			else
			{
				/* Discharging */
				if(DSD_555_ASTBL__R2 != 0)
				{
					t_rc       = DSD_555_ASTBL__R2 * DSD_555_ASTBL__C;
					v_cap_next = v_cap - (v_cap * (1 - exp(-(dt / t_rc ))));
					dt = 0;
				}
				else
				{
					/* no discharge resistor so we imediately discharge */
					v_cap_next = context->trigger;
				}

				/* has it discharged past lower limit? */
				if (v_cap_next <= context->trigger)
				{
					/* calculate the overshoot time */
					if (dt == 0)
						dt = t_rc  * log(1.0 / (1.0 - ((context->trigger - v_cap_next) / v_cap)));
					x_time = dt;
					v_cap  = context->trigger;
					context->flip_flop = 1;
					count_r++;
				}
			}
		} while(dt);

		context->cap_voltage = v_cap_next;
	}

	/* Convert last switch time to a ratio */
	x_time = x_time / discrete_current_context->sample_time;

	switch (context->output_type)
	{
		case DISC_555_OUT_SQW:
			node->output[0] = context->flip_flop * context->v_out_high + context->ac_shift;
			break;
		case DISC_555_OUT_CAP:
			node->output[0] = v_cap_next;
			/* Fake it to AC if needed */
			if (context->output_is_ac)
				node->output[0] -= context->threshold * 3.0 /4.0;
			break;
		case DISC_555_OUT_ENERGY:
			if (x_time == 0) x_time = 1.0;
			node->output[0]  = context->v_out_high * (context->flip_flop ? x_time : (1.0 - x_time));
			node->output[0] += context->ac_shift;
			break;
		case DISC_555_OUT_LOGIC_X:
			node->output[0] = context->flip_flop + x_time;
			break;
		case DISC_555_OUT_COUNT_F_X:
			node->output[0] = count_f ? count_f + x_time : count_f;
			break;
		case DISC_555_OUT_COUNT_R_X:
			node->output[0] =  count_r ? count_r + x_time : count_r;
			break;
		case DISC_555_OUT_COUNT_F:
			node->output[0] = count_f;
			break;
		case DISC_555_OUT_COUNT_R:
			node->output[0] = count_r;
			break;
	}
}

static void dsd_555_astbl_reset(node_description *node)
{
	const  discrete_555_desc     *info    = node->custom;
	struct dsd_555_astbl_context *context = node->context;
	node_description *v_charge_node;

	context->use_ctrlv   = (node->input_is_node >> 4) & 1;
	context->output_type = info->options & DISC_555_OUT_MASK;

	/* Use the defaults or supplied values. */
	context->v_out_high = (info->v_out_high == DEFAULT_555_HIGH) ? info->v_pos - 1.2 : info->v_out_high;

	/* setup v_charge or node */
	v_charge_node = discrete_find_node(NULL, info->v_charge);
	if (v_charge_node)
		context->v_charge_node = &(v_charge_node->output[NODE_CHILD_NODE_NUM(info->v_charge)]);
	else
	{
		context->v_charge   = (info->v_charge == DEFAULT_555_CHARGE) ? info->v_pos : info->v_charge;
		context->v_charge_node = NULL;

		if (info->options & DISC_555_ASTABLE_HAS_FAST_CHARGE_DIODE) context->v_charge -= 0.5;
	}

	if ((DSD_555_ASTBL__CTRLV != -1) && !context->use_ctrlv)
	{
		/* Setup based on supplied Control Voltage static value */
		context->threshold = DSD_555_ASTBL__CTRLV;
		context->trigger   = DSD_555_ASTBL__CTRLV / 2.0;
	}
	else
	{
		/* Setup based on v_pos power source */
		context->threshold = info->v_pos * 2.0 / 3.0;
		context->trigger   = info->v_pos / 3.0;
	}

	context->output_is_ac = info->options & DISC_555_OUT_AC;
	/* Calculate DC shift needed to make squarewave waveform AC */
	context->ac_shift = context->output_is_ac ? -context->v_out_high / 2.0 : 0;

	context->flip_flop = 1;
	context->cap_voltage = 0;

	/* Step to set the output */
	dsd_555_astbl_step(node);
}


/************************************************************************
 *
 * DSD_555_MSTBL - 555 Monostable simulation
 *
 * input[0]    - Reset value
 * input[1]    - Trigger input
 * input[2]    - R2 value
 * input[3]    - C value
 *
 * also passed discrete_555_desc structure
 *
 * Oct 2004, D Renaud.
 ************************************************************************/
#define DSD_555_MSTBL__RESET	(! *(node->input[0]))
#define DSD_555_MSTBL__TRIGGER	(*(node->input[1]))
#define DSD_555_MSTBL__R		(*(node->input[2]))
#define DSD_555_MSTBL__C		(*(node->input[3]))

static void dsd_555_mstbl_step(node_description *node)
{
	const  discrete_555_desc     *info    = node->custom;
	struct dsd_555_mstbl_context *context = node->context;

	double v_cap;			/* Current voltage on capacitor, before dt */
	double v_cap_next = 0;	/* Voltage on capacitor, after dt */

	if(DSD_555_MSTBL__RESET)
	{
		/* We are in RESET */
		node->output[0]     = 0;
		context->flip_flop  = 0;
		context->cap_voltage = 0;
	}
	else
	{
		int trigger;

		if (context->trig_is_logic)
			trigger = !DSD_555_MSTBL__TRIGGER;
		else
			trigger = DSD_555_MSTBL__TRIGGER < context->trigger;

		if (context->trig_discharges_cap && trigger)
			context->cap_voltage = 0;

		if (!context->flip_flop)
		{
			/* Wait for trigger */
			if (trigger)
				context->flip_flop = 1;
		}
		else
		{
			v_cap = context->cap_voltage;

			/* Sometimes a switching network is used to setup the capacitance.
             * These may select 'no' capacitor, causing oscillation to stop.
             */
			if (DSD_555_MSTBL__C == 0)
			{
				context->flip_flop = 0;
				/* The voltage goes high because the cap circuit is open. */
				v_cap_next = info->v_pos;
				v_cap      = info->v_pos;
				context->cap_voltage = 0;
			}
			else
			{
				/* Charging */
				v_cap_next = v_cap + ((info->v_pos - v_cap) * (1.0 - exp(-(discrete_current_context->sample_time / (DSD_555_MSTBL__R * DSD_555_MSTBL__C)))));

				/* Has it charged past upper limit? */
				/* If trigger is still enabled, then we keep charging,
                 * regardless of threshold. */
				if ((v_cap_next >= context->threshold) && !trigger)
				{
					v_cap_next = 0;
					v_cap      = context->threshold;
					context->flip_flop = 0;
				}
			}

			context->cap_voltage = v_cap_next;

			switch (info->options & DISC_555_OUT_MASK)
			{
				case DISC_555_OUT_SQW:
					node->output[0] = context->flip_flop * context->v_out_high;
					/* Fake it to AC if needed */
					if (context->output_is_ac)
						node->output[0] -= context->v_out_high / 2.0;
					break;
				case DISC_555_OUT_CAP:
					node->output[0] = v_cap_next;
					/* Fake it to AC if needed */
					if (context->output_is_ac)
						node->output[0] -= context->threshold * 3.0 /4.0;
					break;
			}
		}
	}
}

static void dsd_555_mstbl_reset(node_description *node)
{
	const  discrete_555_desc     *info    = node->custom;
	struct dsd_555_mstbl_context *context = node->context;

	context->output_type = info->options & DISC_555_OUT_MASK;
	if ((context->output_type == DISC_555_OUT_COUNT_F) || (context->output_type == DISC_555_OUT_COUNT_R))
	{
		discrete_log("Invalid Output type in NODE_%d.\n", node->node - NODE_00);
		context->output_type = DISC_555_OUT_SQW;
	}

	/* Use the defaults or supplied values. */
	context->v_out_high = (info->v_out_high == DEFAULT_555_HIGH) ? info->v_pos - 1.2 : info->v_out_high;
	context->v_charge   = (info->v_charge   == DEFAULT_555_CHARGE) ? info->v_pos : info->v_charge;

	/* Setup based on v_pos power source */
	context->threshold = info->v_pos * 2.0 / 3.0;
	context->trigger   = info->v_pos / 3.0;

	context->output_is_ac = info->options & DISC_555_OUT_AC;
	/* Calculate DC shift needed to make squarewave waveform AC */
	context->ac_shift     = context->output_is_ac ? -context->v_out_high / 2.0 : 0;

	context->trig_is_logic       = (info->options & DISC_555_TRIGGER_IS_VOLTAGE) ? 0: 1;
	context->trig_discharges_cap = (info->options & DISC_555_TRIGGER_DISCHARGES_CAP) ? 1: 0;

	context->flip_flop   = 0;
	context->cap_voltage = 0;

	node->output[0] = 0;
}


/************************************************************************
 *
 * DSD_555_CC - Usage of node_description values
 *
 * input[0]    - Reset input value
 * input[1]    - Voltage input for Constant current source.
 * input[2]    - R value to set CC current.
 * input[3]    - C value
 * input[4]    - rBias value
 * input[5]    - rGnd value
 * input[6]    - rDischarge value
 *
 * also passed discrete_555_cc_desc structure
 *
 * Mar 2004, D Renaud.
 ************************************************************************/
#define DSD_555_CC__RESET	(! *(node->input[0]))
#define DSD_555_CC__VIN		(*(node->input[1]))
#define DSD_555_CC__R		(*(node->input[2]))
#define DSD_555_CC__C		(*(node->input[3]))
#define DSD_555_CC__RBIAS	(*(node->input[4]))
#define DSD_555_CC__RGND	(*(node->input[5]))
#define DSD_555_CC__RDIS	(*(node->input[6]))

static void dsd_555_cc_step(node_description *node)
{
	const  discrete_555_cc_desc *info    = node->custom;
	struct dsd_555_cc_context   *context = node->context;

	int		count_f  = 0;
	int		count_r  = 0;
	double	i;					/* Charging current created by vIn */
	double	r_charge = 0;		/* Equivalent charging resistor */
	double	r_discharge = 0;	/* Equivalent discharging resistor */
	double	vi     = 0;			/* Equivalent voltage from current source */
	double	v_bias = 0;			/* Equivalent voltage from bias voltage */
	double	v      = 0;			/* Equivalent voltage total from current source and bias circuit if used */
	double	dt;					/* change in time */
	double	x_time = 0;			/* time since change happened */
	double	t_rc ;				/* RC time constant */
	double	v_cap;				/* Current voltage on capacitor, before dt */
	double	v_cap_next = 0;		/* Voltage on capacitor, after dt */
	double	v_vcharge_limit;	/* vIn and the junction voltage limit the max charging voltage from i */
	double	r_temp;				/* play thing */


	if (DSD_555_CC__RESET)
	{
		/* We are in RESET */
		node->output[0]      = 0;
		context->flip_flop   = 1;
		context->cap_voltage = 0;
		return;
	}

	dt    = discrete_current_context->sample_time;	/* Change in time */
	v_cap = context->cap_voltage;	/* Set to voltage before change */
	v_vcharge_limit = DSD_555_CC__VIN + info->v_cc_junction;	/* the max v_cap can be and still be charged by i */
	/* Calculate charging current */
	i = (context->v_cc_source - v_vcharge_limit) / DSD_555_CC__R;
	if ( i < 0) i = 0;

	if (info->options & DISCRETE_555_CC_TO_CAP)
	{
		vi = i * DSD_555_CC__RDIS;
	}
	else
	switch (context->type)	/* see dsd_555_cc_reset for descriptions */
	{
		case 1:
			r_discharge = DSD_555_CC__RDIS;
		case 0:
			break;
		case 3:
			r_discharge = (DSD_555_CC__RDIS * DSD_555_CC__RGND) / (DSD_555_CC__RDIS + DSD_555_CC__RGND);
		case 2:
			r_charge = DSD_555_CC__RGND;
			vi       = i * r_charge;
			break;
		case 4:
			r_charge = DSD_555_CC__RBIAS;
			vi       = i * r_charge;
			v_bias   = info->v_pos;
			break;
		case 5:
			r_charge = DSD_555_CC__RBIAS + DSD_555_CC__RDIS;
			vi       = i * DSD_555_CC__RBIAS;
			v_bias   = info->v_pos;
			r_discharge = DSD_555_CC__RDIS;
			break;
		case 6:
			r_charge = (DSD_555_CC__RBIAS * DSD_555_CC__RGND) / (DSD_555_CC__RBIAS + DSD_555_CC__RGND);
			vi       = i * r_charge;
			v_bias   = info->v_pos * (DSD_555_CC__RGND / (DSD_555_CC__RBIAS + DSD_555_CC__RGND));
			break;
		case 7:
			r_temp   = DSD_555_CC__RBIAS + DSD_555_CC__RDIS;
			r_charge = (r_temp * DSD_555_CC__RGND) / (r_temp + DSD_555_CC__RGND);
			r_temp  += DSD_555_CC__RGND;
			r_temp   = DSD_555_CC__RGND / r_temp;	/* now has voltage divider ratio, not resistance */
			vi       = i * DSD_555_CC__RBIAS * r_temp;
			v_bias   = info->v_pos * r_temp;
			r_discharge = (DSD_555_CC__RGND * DSD_555_CC__RDIS) / (DSD_555_CC__RGND + DSD_555_CC__RDIS);
			break;
	}

	/* Keep looping until all toggling in time sample is used up. */
	do
	{
		if (context->type <= 1)
		{
			/* Standard constant current charge */
			if (context->flip_flop)
			{
				if (i == 0)
				{
					/* No charging current, so we have to discharge the cap
                     * due to cap and circuit losses.
                     */
					t_rc       = DEFAULT_555_CAP_BLEED * DSD_555_CC__C;
					v_cap_next = v_cap - (v_cap * (1.0 - exp(-(dt / t_rc ))));
					dt = 0;
				}
				else
				{
					/* Charging */
					/* iC=C*dv/dt  works out to dv=iC*dt/C */
					v_cap_next = v_cap + (i * dt / DSD_555_CC__C);
					/* Yes, if the cap voltage has reached the max voltage it can,
                     * and the 555 threshold has not been reached, then oscillation stops.
                     * This is the way the actual electronics works.
                     * This is why you never play with the pots after being factory adjusted
                     * to work in the proper range. */
					if (v_cap_next > v_vcharge_limit) v_cap_next = v_vcharge_limit;
					dt = 0;

					/* has it charged past upper limit? */
					if (v_cap_next >= context->threshold)
					{
						/* calculate the overshoot time */
						dt     = DSD_555_CC__C * (v_cap_next - context->threshold) / i;
						x_time = dt;
						v_cap  = context->threshold;
						context->flip_flop = 0;
						count_f++;
					}
				}
			}
			else if (DSD_555_CC__RDIS)
			{
				/* Discharging */
				t_rc  = DSD_555_CC__RDIS * DSD_555_CC__C;

				if (info->options & DISCRETE_555_CC_TO_CAP)
				{
					/* Asteroids - Special Case */
					/* Charging in discharge mode */
					/* If the cap voltage is past the current source charging limit
                     * then only the bias voltage will charge the cap. */
					v          = (v_cap < v_vcharge_limit) ? vi : v_vcharge_limit;
					v_cap_next = v_cap + ((v - v_cap) * (1.0 - exp(-(dt / t_rc ))));
				}
				else
				{
					v_cap_next = v_cap - (v_cap * (1.0 - exp(-(dt / t_rc ))));
				}

				dt = 0;
				/* has it discharged past lower limit? */
				if (v_cap_next <= context->trigger)
				{
					dt     = t_rc  * log(1.0 / (1.0 - ((context->trigger - v_cap_next) / v_cap)));
					x_time = dt;
					v_cap  = context->trigger;
					context->flip_flop = 1;
					count_r++;
				}
			}
			else	/* Immediate discharge. No change in dt. */
			{
				x_time = dt;
				v_cap  = context->trigger;
				context->flip_flop = 1;
				count_r++;
			}
		}
		else
		{
			/* The constant current gets changed to a voltage due to a load resistor. */
			if (context->flip_flop)
			{
				if ((i == 0) && (DSD_555_CC__RBIAS == 0))
				{
					/* No charging current, so we have to discharge the cap
                     * due to rGnd.
                     */
					t_rc       = DSD_555_CC__RGND * DSD_555_CC__C;
					v_cap_next = v_cap - (v_cap * (1.0 - exp(-(dt / t_rc ))));
					dt = 0;
				}
				else
				{
					/* Charging */
					/* If the cap voltage is past the current source charging limit
                     * then only the bias voltage will charge the cap. */
					v = v_bias;
					if (v_cap < v_vcharge_limit) v += vi;
					else if (context->type <= 3) v = v_vcharge_limit;

					t_rc       = r_charge * DSD_555_CC__C;
					v_cap_next = v_cap + ((v - v_cap) * (1.0 - exp(-(dt / t_rc ))));
					dt         = 0;

					/* has it charged past upper limit? */
					if (v_cap_next >= context->threshold)
					{
						/* calculate the overshoot time */
						dt     = t_rc  * log(1.0 / (1.0 - ((v_cap_next - context->threshold) / (v - v_cap))));
						x_time = dt;
						v_cap  = context->threshold;
						context->flip_flop = 0;
						count_f++;
					}
				}
			}
			else /* Discharging */
			if (r_discharge)
			{
				t_rc       = r_discharge * DSD_555_CC__C;
				v_cap_next = v_cap - (v_cap * (1.0 - exp(-(dt / t_rc ))));
				dt = 0;

				/* has it discharged past lower limit? */
				if (v_cap_next <= context->trigger)
				{
					/* calculate the overshoot time */
					dt     = t_rc  * log(1.0 / (1.0 - ((context->trigger - v_cap_next) / v_cap)));
					x_time = dt;
					v_cap  = context->trigger;
					context->flip_flop = 1;
					count_r++;
				}
			}
			else	/* Immediate discharge. No change in dt. */
			{
				x_time = dt;
				v_cap  = context->trigger;
				context->flip_flop = 1;
				count_r++;
			}
		}
	} while(dt);

	context->cap_voltage = v_cap_next;

	/* Convert last switch time to a ratio */
	x_time = x_time / discrete_current_context->sample_time;

	switch (context->output_type)
	{
		case DISC_555_OUT_SQW:
			if (count_r && (~context->type & 0x01))
			{
				/* There has been an immediate discharge, so keep low for 1 sample. */
				node->output[0] = 0;
			}
			else
				node->output[0] = context->flip_flop * context->v_out_high;
			/* Fake it to AC if needed */
			node->output[0] += context->ac_shift;
			break;
		case DISC_555_OUT_CAP:
			node->output[0] = v_cap_next + context->ac_shift;
			break;
		case DISC_555_OUT_ENERGY:
			if (x_time == 0) x_time = 1.0;
			node->output[0]  = context->v_out_high * (context->flip_flop ? x_time : (1.0 - x_time));
			node->output[0] += context->ac_shift;
			break;
		case DISC_555_OUT_LOGIC_X:
			node->output[0] = context->flip_flop + x_time;
			break;
		case DISC_555_OUT_COUNT_F_X:
			node->output[0] = count_f ? count_f + x_time : count_f;
			break;
		case DISC_555_OUT_COUNT_R_X:
			node->output[0] =  count_r ? count_r + x_time : count_r;
			break;
		case DISC_555_OUT_COUNT_F:
			node->output[0] = count_f;
			break;
		case DISC_555_OUT_COUNT_R:
			node->output[0] = count_r;
			break;
	}
}

static void dsd_555_cc_reset(node_description *node)
{
	const  discrete_555_cc_desc *info    = node->custom;
	struct dsd_555_cc_context   *context = node->context;

	context->flip_flop   = 1;
	context->cap_voltage = 0;

	context->output_type = info->options & DISC_555_OUT_MASK;

	/* Use the defaults or supplied values. */
	context->v_out_high  = (info->v_out_high  == DEFAULT_555_HIGH) ? info->v_pos - 1.2 : info->v_out_high;
	context->v_cc_source = (info->v_cc_source == DEFAULT_555_CC_SOURCE) ? info->v_pos : info->v_cc_source;

	/* Setup based on v_pos power source */
	context->threshold = info->v_pos * 2.0 / 3.0;
	context->trigger   = info->v_pos / 3.0;

	context->output_is_ac = info->options & DISC_555_OUT_AC;
	/* Calculate DC shift needed to make squarewave waveform AC */
	context->ac_shift     = context->output_is_ac ? -context->v_out_high / 2.0 : 0;

	/* There are 8 different types of basic oscillators
     * depending on the resistors used.  We will determine
     * the type of circuit at reset, because the ciruit type
     * is constant.
     */
	context->type = (DSD_555_CC__RDIS > 0) | ((DSD_555_CC__RGND  > 0) << 1) | ((DSD_555_CC__RBIAS  > 0) << 2);
	/*
     * TYPES:
     * Note: These are equivalent circuits shown without the 555 circuitry.
     *       See the schematic in src\sound\discrete.h for full hookup info.
     *
     * DISCRETE_555_CC_TO_DISCHARGE_PIN
     * When the CC source is connected to the discharge pin, it allows the
     * circuit to charge when the 555 is in charge mode.  But when in discharge
     * mode, the CC source is grounded, disabling it's effect.
     *
     * [0]
     * No resistors.  Straight constant current charge of capacitor.
     * When there is not any charge current, the cap will bleed off.
     * Once the lower threshold(trigger) is reached, the output will
     * go high but the cap will continue to discharge due to losses.
     *   .------+---> cap_voltage      CHARGING:
     *   |      |                 dv (change in voltage) compared to dt (change in time in seconds).
     * .---.   ---                dv = i * dt / C; where i is current in amps and C is capacitance in farads.
     * | i |   --- C              cap_voltage = cap_voltage + dv
     * '---'    |
     *   |      |               DISCHARGING:
     *  gnd    gnd                instantaneous
     *
     * [1]
     * Same as type 1 but with rDischarge.  rDischarge has no effect on the charge rate because
     * of the constant current source i.
     * When there is not any charge current, the cap will bleed off.
     * Once the lower threshold(trigger) is reached, the output will
     * go high but the cap will continue to discharge due to losses.
     *   .----ZZZ-----+---> cap_voltage      CHARGING:
     *   | rDischarge |                 dv (change in voltage) compared to dt (change in time in seconds).
     * .---.         ---                dv = i * dt / C; where i is current in amps and C is capacitance in farads.
     * | i |         --- C              cap_voltage = cap_voltage + dv
     * '---'          |
     *   |            |               DISCHARGING:
     *  gnd          gnd                thru rDischarge
     *
     * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     * !!!!! IMPORTANT NOTE ABOUT TYPES 3 - 7 !!!!!
     * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     *
     * From here on in all the circuits have either an rBias or rGnd resistor.
     * This converts the constant current into a voltage source.
     * So all the remaining circuit types will be converted to this circuit.
     * When discharging, rBias is out of the equation because the 555 is grounding the circuit
     * after that point.
     *
     * .------------.     Rc                  Rc is the equivilent circuit resistance.
     * |     v      |----ZZZZ---+---> cap_voltage    v  is the equivilent circuit voltage.
     * |            |           |
     * '------------'          ---            Then the standard RC charging formula applies.
     *       |                 --- C
     *       |                  |             NOTE: All the following types are converted to Rc and v values.
     *      gnd                gnd
     *
     * [2]
     * When there is not any charge current, the cap will bleed off.
     * Once the lower threshold(trigger) is reached, the output will
     * go high but the cap will continue to discharge due to rGnd.
     *   .-------+------+------> cap_voltage         CHARGING:
     *   |       |      |                       v = vi = i * rGnd
     * .---.    ---     Z                       Rc = rGnd
     * | i |    --- C   Z rGnd
     * '---'     |      |                     DISCHARGING:
     *   |       |      |                       instantaneous
     *  gnd     gnd    gnd
     *
     * [3]
     * When there is not any charge current, the cap will bleed off.
     * Once the lower threshold(trigger) is reached, the output will
     * go high but the cap will continue to discharge due to rGnd.
     *   .----ZZZ-----+------+------> cap_voltage    CHARGING:
     *   | rDischarge |      |                  v = vi = i * rGnd
     * .---.         ---     Z                  Rc = rGnd
     * | i |         --- C   Z rGnd
     * '---'          |      |                DISCHARGING:
     *   |            |      |                  thru rDischarge || rGnd  ( || means in parallel)
     *  gnd          gnd    gnd
     *
     * [4]
     *     .---ZZZ---+------------+-------------> cap_voltage      CHARGING:
     *     |  rBias  |            |                           Rc = rBias
     * .-------.   .---.         ---                          vi = i * rBias
     * | vBias |   | i |         --- C                        v = vBias + vi
     * '-------'   '---'          |
     *     |         |            |                         DISCHARGING:
     *    gnd       gnd          gnd                          instantaneous
     *
     * [5]
     *     .---ZZZ---+----ZZZ-----+-------------> cap_voltage      CHARGING:
     *     |  rBias  | rDischarge |                           Rc = rBias + rDischarge
     * .-------.   .---.         ---                          vi = i * rBias
     * | vBias |   | i |         --- C                        v = vBias + vi
     * '-------'   '---'          |
     *     |         |            |                         DISCHARGING:
     *    gnd       gnd          gnd                          thru rDischarge
     *
     * [6]
     *     .---ZZZ---+------------+------+------> cap_voltage      CHARGING:
     *     |  rBias  |            |      |                    Rc = rBias || rGnd
     * .-------.   .---.         ---     Z                    vi = i * Rc
     * | vBias |   | i |         --- C   Z rGnd               v = vBias * (rGnd / (rBias + rGnd)) + vi
     * '-------'   '---'          |      |
     *     |         |            |      |                  DISCHARGING:
     *    gnd       gnd          gnd    gnd                   instantaneous
     *
     * [7]
     *     .---ZZZ---+----ZZZ-----+------+------> cap_voltage      CHARGING:
     *     |  rBias  | rDischarge |      |                    Rc = (rBias + rDischarge) || rGnd
     * .-------.   .---.         ---     Z                    vi = i * rBias * (rGnd / (rBias + rDischarge + rGnd))
     * | vBias |   | i |         --- C   Z rGnd               v = vBias * (rGnd / (rBias + rDischarge + rGnd)) + vi
     * '-------'   '---'          |      |
     *     |         |            |      |                  DISCHARGING:
     *    gnd       gnd          gnd    gnd                   thru rDischarge || rGnd
     */

    /*
     * DISCRETE_555_CC_TO_CAP
     *
     * When the CC source is connected to the capacitor, it allows the
     * current to charge the cap while it is in discharge mode, slowing the
     * discharge.  So in charge mode it charges linearly from the constant
     * current cource.  But when in discharge mode it behaves like circuit
     * type 2 above.
     *   .-------+------+------> cap_voltage         CHARGING:
     *   |       |      |                       dv = i * dt / C
     * .---.    ---     Z                       cap_voltage = cap_voltage + dv
     * | i |    --- C   Z rDischarge
     * '---'     |      |                     DISCHARGING:
     *   |       |      |                       v = vi = i * rGnd
     *  gnd     gnd   discharge                 Rc = rDischarge
     */

	/* Step to set the output */
	dsd_555_cc_step(node);
}


/************************************************************************
 *
 * DSD_555_VCO1 - Usage of node_description values
 *
 * input[0]    - Reset input value
 * input[1]    - Modulation Voltage (Vin1)
 * input[2]    - Control Voltage (Vin2)
 *
 * also passed discrete_5555_vco1_desc structure
 *
 * Apr 2006, D Renaud.
 ************************************************************************/
#define DSD_555_VCO1__RESET	(*(node->input[0]))	/* reset active low */
#define DSD_555_VCO1__VIN1	(*(node->input[1]))
#define DSD_555_VCO1__VIN2	(*(node->input[2]))

static void dsd_555_vco1_step(node_description *node)
{
	const  discrete_555_vco1_desc *info    = node->custom;
	struct dsd_555_vco1_context   *context = node->context;

	int		count_f = 0;
	int		count_r = 0;
	double	dt;				/* change in time */
	double	x_time  = 0;	/* time since change happened */
	double	v_cap;			/* Current voltage on capacitor, before dt */
	double	v_cap_next = 0;	/* Voltage on capacitor, after dt */

	dt    = discrete_current_context->sample_time;	/* Change in time */
	v_cap = context->cap_voltage;

	/* Check: if the Control Voltage node is connected. */
	if (context->ctrlv_is_node && DSD_555_VCO1__RESET)	/* reset active low */
	{
		/* If CV is less then .25V, the circuit will oscillate way out of range.
         * So we will just ignore it when it happens. */
		if (DSD_555_VCO1__VIN2 < .25) return;
		/* If it is a node then calculate thresholds based on Control Voltage */
		context->threshold = DSD_555_VCO1__VIN2;
		context->trigger   = DSD_555_VCO1__VIN2 / 2.0;
		/* Since the thresholds may have changed we need to update the FF */
		if (v_cap >= context->threshold)
		{
			x_time = dt;
			context->flip_flop = 0;
			count_f++;
		}
		else
		if (v_cap <= context->trigger)
		{
			x_time = dt;
			context->flip_flop = 1;
			count_r++;
		}
	}

	/* Keep looping until all toggling in time sample is used up. */
	do
	{
		if (context->flip_flop)
		{
			/* if we are in reset then toggle f/f and discharge */
			if (!DSD_555_VCO1__RESET)	/* reset active low */
			{
				context->flip_flop = 0;
				count_f++;
			}
			else
			{
				/* Charging */
				/* iC=C*dv/dt  works out to dv=iC*dt/C */
				v_cap_next = v_cap + (context->i_charge * dt / info->c);
				dt         = 0;

				/* has it charged past upper limit? */
				if (v_cap_next >= context->threshold)
				{
					/* calculate the overshoot time */
					dt     = info->c * (v_cap_next - context->threshold) / context->i_charge;
					v_cap  = context->threshold;
					x_time = dt;
					context->flip_flop = 0;
					count_f++;
				}
			}
		}
		else
		{
			/* Discharging */
			/* iC=C*dv/dt  works out to dv=iC*dt/C */
			v_cap_next = v_cap - (context->i_discharge * dt / info->c);

			/* if we are in reset, then the cap can discharge to 0 */
			if (!DSD_555_VCO1__RESET)	/* reset active low */
			{
				if (v_cap_next < 0) v_cap_next = 0;
				dt = 0;
			}
			else
			{
				/* if we are out of reset and the cap voltage is less then
                 * the lower threshold, toggle f/f and start charging */
				if (v_cap <= context->trigger)
				{
					if (context->flip_flop == 0)
					{
						/* don't need to track x_time here */
						context->flip_flop = 1;
						count_r++;
					}
				}
				else
				{
					dt = 0;
					/* has it discharged past lower limit? */
					if (v_cap_next <= context->trigger)
					{
						/* calculate the overshoot time */
						dt     = info->c * (v_cap_next - context->trigger) / context->i_discharge;
						v_cap  = context->trigger;
						x_time = dt;
						context->flip_flop = 1;
						count_r++;
					}
				}
			}
		}
	} while(dt);

	context->cap_voltage = v_cap_next;

	/* Convert last switch time to a ratio.  No x_time in reset. */
	x_time = x_time / discrete_current_context->sample_time;
	if (!DSD_555_VCO1__RESET) x_time = 0;

	switch (context->output_type)
	{
		case DISC_555_OUT_SQW:
			node->output[0] = context->flip_flop * context->v_out_high + context->ac_shift;
			break;
		case DISC_555_OUT_CAP:
			node->output[0] = v_cap_next;
			/* Fake it to AC if needed */
			if (context->output_is_ac)
				node->output[0] -= context->threshold * 3.0 /4.0;
			break;
		case DISC_555_OUT_ENERGY:
			if (x_time == 0) x_time = 1.0;
			node->output[0]  = context->v_out_high * (context->flip_flop ? x_time : (1.0 - x_time));
			node->output[0] += context->ac_shift;
			break;
		case DISC_555_OUT_LOGIC_X:
			node->output[0] = context->flip_flop + x_time;
			break;
		case DISC_555_OUT_COUNT_F_X:
			node->output[0] = count_f ? count_f + x_time : count_f;
			break;
		case DISC_555_OUT_COUNT_R_X:
			node->output[0] =  count_r ? count_r + x_time : count_r;
			break;
		case DISC_555_OUT_COUNT_F:
			node->output[0] = count_f;
			break;
		case DISC_555_OUT_COUNT_R:
			node->output[0] = count_r;
			break;
	}
}

static void dsd_555_vco1_reset(node_description *node)
{
	const  discrete_555_vco1_desc *info    = node->custom;
	struct dsd_555_vco1_context   *context = node->context;

	double v_ratio_r3, v_ratio_r4_1, r_in_1;

	context->output_type  = info->options & DISC_555_OUT_MASK;
	context->output_is_ac = info->options & DISC_555_OUT_AC;

	/* Setup op-amp parameters */

	/* The voltage at op-amp +in is always a fixed ratio of the modulation voltage. */
	v_ratio_r3 = info->r3 / (info->r2 + info->r3);			/* +in voltage */
	/* The voltage at op-amp -in is 1 of 2 fixed ratios of the modulation voltage,
     * based on the 555 Flip-Flop state. */
	/* If the FF is 0, then only R1 is connected allowing the full modulation volatge to pass. */
	/* v_ratio_r4_0 = 1 */
	/* If the FF is 1, then R1 & R4 make a voltage divider similar to R2 & R3 */
	v_ratio_r4_1 = info->r4 / (info->r1 + info->r4);		/* -in voltage */
	/* the input resistance to the op amp depends on the FF state */
	/* r_in_0 = info->r1 when FF = 0 */
	r_in_1 = 1.0 / (1.0 / info->r1 + 1.0 / info->r4);	/* input resistance when r4 switched in */

	/* Now that we know the voltages entering the op amp and the resistance for the
     * FF states, we can predetermine the ratios for the charge/discharge currents. */
	context->i_discharge = (1 - v_ratio_r3) / info->r1;
	context->i_charge    = (v_ratio_r3 - v_ratio_r4_1) / r_in_1;

	/* the cap starts off discharged */
	context->cap_voltage = 0;

	/* Setup 555 parameters */

	/* There is no charge on the cap so the 555 goes high at init. */
	context->flip_flop     = 1;
	context->ctrlv_is_node = (node->input_is_node >> 2) & 1;
	context->v_out_high    = (info->v_out_high == DEFAULT_555_HIGH) ? info->v_pos - 1.2 : info->v_out_high;

	/* Calculate 555 thresholds.
     * If the Control Voltage is a node, then the thresholds will be calculated each step.
     * If the Control Voltage is a fixed voltage, then the thresholds will be calculated
     * from that.  Otherwise we will use thresholds based on v_pos. */
	if (!context->ctrlv_is_node && (DSD_555_VCO1__VIN2 != -1))
	{
		/* Setup based on supplied Control Voltage static value */
		context->threshold = DSD_555_VCO1__VIN2;
		context->trigger   = DSD_555_VCO1__VIN2 / 2.0;
	}
	else
	{
		/* Setup based on v_pos power source */
		context->threshold = info->v_pos * 2.0 / 3.0;
		context->trigger   = info->v_pos / 3.0;
	}

	/* Calculate DC shift needed to make squarewave waveform AC */
	context->ac_shift = context->output_is_ac ? -context->v_out_high / 2.0 : 0;
}

/************************************************************************
 *
 * DSD_566 - Usage of node_description values
 *
 * input[0]    - Enable input value
 * input[1]    - Modulation Voltage
 * input[2]    - R value
 * input[3]    - C value
 *
 * also passed discrete_566_desc structure
 *
 * Mar 2004, D Renaud.
 ************************************************************************/
#define DSD_566__ENABLE	(*(node->input[0]))
#define DSD_566__VMOD	(*(node->input[1]))
#define DSD_566__R		(*(node->input[2]))
#define DSD_566__C		(*(node->input[3]))

static void dsd_566_step(node_description *node)
{
	const  discrete_566_desc *info    = node->custom;
	struct dsd_566_context   *context = node->context;

	double i;				/* Charging current created by vIn */
	double dt;				/* change in time */
	double v_cap;			/* Current voltage on capacitor, before dt */
	double v_cap_next = 0;	/* Voltage on capacitor, after dt */
	double v_charge;

	if (DSD_566__ENABLE && !context->error)
	{
		dt    = discrete_current_context->sample_time;	/* Change in time */
		v_cap = context->cap_voltage;	/* Set to voltage before change */

		/* get the v_charge and update each step if it is a node */
		if (context->v_charge_node != NULL)
		{
			v_charge  = *context->v_charge_node;
			v_charge -= info->v_neg;
		}
		else
			v_charge = context->v_charge;

		/* Calculate charging current */
		i = (v_charge - DSD_566__VMOD) / DSD_566__R;

		/* Keep looping until all toggling in time sample is used up. */
		do
		{
			if (context->flip_flop)
			{
				/* Discharging */
				v_cap_next = v_cap - (i * dt / DSD_566__C);
				dt         = 0;

				/* has it discharged past lower limit? */
				if (v_cap_next <= context->threshold_low)
				{
					if (v_cap_next < context->threshold_low)
					{
						/* calculate the overshoot time */
						dt = DSD_566__C * (context->threshold_low - v_cap_next) / i;
					}
					v_cap = context->threshold_low;
					context->flip_flop = 0;
					/*
                     * If the sampling rate is too low and the desired frequency is too high
                     * then we will start getting too many outputs that can't catch up.  We will
                     * limit this to 3.  The output is already incorrect because of the low sampling,
                     * but at least this way it can recover.
                     */
					context->state[0] = (context->state[0] + 1) & 0x03;
				}
			}
			else
			{
				/* Charging */
				/* iC=C*dv/dt  works out to dv=iC*dt/C */
				v_cap_next = v_cap + (i * dt / DSD_566__C);
				dt         = 0;
				/* Yes, if the cap voltage has reached the max voltage it can,
                 * and the 566 threshold has not been reached, then oscillation stops.
                 * This is the way the actual electronics works.
                 * This is why you never play with the pots after being factory adjusted
                 * to work in the proper range. */
				if (v_cap_next > DSD_566__VMOD) v_cap_next = DSD_566__VMOD;

				/* has it charged past upper limit? */
				if (v_cap_next >= context->threshold_high)
				{
					if (v_cap_next > context->threshold_high)
					{
						/* calculate the overshoot time */
						dt = DSD_566__C * (v_cap_next - context->threshold_high) / i;
					}
					v_cap = context->threshold_high;
					context->flip_flop = 1;
					context->state[1] = (context->state[1] + 1) & 0x03;
				}
			}
		} while(dt);

		context->cap_voltage = v_cap_next;

		switch (info->options & DISC_566_OUT_MASK)
		{
			case DISC_566_OUT_SQUARE:
			case DISC_566_OUT_LOGIC:
				/* use up any output states */
				if (node->output[0] && context->state[0])
				{
					node->output[0] = 0;
					context->state[0]--;
				}
				else if (!node->output[0] && context->state[1])
				{
					node->output[0] = 1;
					context->state[1]--;
				}
				else
				{
					node->output[0] = context->flip_flop;
				}
				if ((info->options & DISC_566_OUT_MASK) != DISC_566_OUT_LOGIC)
					node->output[0] = context->flip_flop ? context->v_sqr_high : context->v_sqr_low;
				break;
			case DISC_566_OUT_TRIANGLE:
				/* we can ignore any unused states when
                 * outputting the cap voltage */
				node->output[0] = v_cap_next;
				if (info->options & DISC_566_OUT_AC)
					node->output[0] -= context->triangle_ac_offset;
				break;
		}
	}
	else
		node->output[0] = 0;
}

static void dsd_566_reset(node_description *node)
{
	const  discrete_566_desc *info    = node->custom;
	struct dsd_566_context   *context = node->context;
	node_description *v_charge_node;

	double	v_diff, temp;

	context->error = 0;
	if (info->v_neg >= info->v_pos)
	{
		logerror("[v_neg >= v_pos] - NODE_%d DISABLED!\n", node->node - NODE_00);
		context->error = 1;
		return;
	}

	/* setup v_charge or node */
	v_charge_node = discrete_find_node(NULL, info->v_charge);
	if (v_charge_node)
		context->v_charge_node = &(v_charge_node->output[NODE_CHILD_NODE_NUM(info->v_charge)]);
	else
	{
		context->v_charge  = (info->v_charge == DEFAULT_566_CHARGE) ? info->v_pos : info->v_charge;
		context->v_charge -= info->v_neg;
		context->v_charge_node = NULL;
	}

	v_diff = info->v_pos - info->v_neg;
	context->flip_flop   = 0;
	context->cap_voltage = 0;
	context->state[0]    = 0;
	context->state[1]    = 0;

	/* The data sheets are useless for this IC.  I will have to get my hands on a chip
     * to make real measurements.  For now this should work fine for 12V. */
	context->threshold_high = v_diff / 2 + info->v_neg;
	context->threshold_low  = context->threshold_high - (0.2 * v_diff);
	context->v_sqr_high     = info->v_pos - 0.6;
	context->v_sqr_low      = context->threshold_high;

	if (info->options & DISC_566_OUT_AC)
	{
		temp = (context->v_sqr_high - context->v_sqr_low) / 2;
		context->v_sqr_high =  temp;
		context->v_sqr_low  = -temp;
		context->triangle_ac_offset = context->threshold_high - (0.1 * v_diff);
	}

	/* Step the output */
	dsd_566_step(node);
}

/************************************************************************
 *
 * DSD_LS624 - Usage of node_description values
 *
 * input[0]    - Enable input value
 * input[1]    - Modulation Voltage
 * input[2]    - Range Voltage
 * input[3]    - C value
 * input[4]    - Output type
 *
 * Dec 2007, Couriersud
 ************************************************************************/
#define DSD_LS624__ENABLE	(*(node->input[0]))
#define DSD_LS624__VMOD		(*(node->input[1]))
#define DSD_LS624__VRNG		(*(node->input[2]))
#define DSD_LS624__C		(*(node->input[3]))
#define DSD_LS624__OUTTYPE	(*(node->input[4]))

/*
 * These formulas are derived from diagrams in the datasheet!
 * They are not based on any law. The function is not
 * described anywhere.
 */

#define LS624_F1(x)			(0.19 + 20.0/90.0*(x))
#define LS624_T(_C, _R, _F)		(-600.0 * (_C) * log(1.0-LS624_F1(_R)*0.12/LS624_F1(_F)))

static void dsd_ls624_step(node_description *node)
{
	struct dsd_ls624_context *context = node->context;

	if (DSD_LS624__ENABLE)
	{
		double	dt;	/* change in time */
		double	sample_t;
		double	t;
		int		lst, cntf=0, cntr=0;

		sample_t = discrete_current_context->sample_time;	/* Change in time */
		dt  = LS624_T(DSD_LS624__C, DSD_LS624__VRNG, DSD_LS624__VMOD);
		dt  = 16 * dt;
		t   = context->remain;
		lst = context->state;
		while (t + dt < sample_t)
		{
			context->state = (1-context->state);
			if (context->state)
				cntr++;
			else
				cntf++;
			t += dt;
		}
		context->remain = t - sample_t;

		switch (context->out_type)
		{
			case DISC_LS624_OUT_ENERGY:
				node->output[0] = ((double) lst) * (1.0+context->remain/sample_t) - ((double) context->state) * context->remain/sample_t;
				break;
			case DISC_LS624_OUT_LOGIC:
				node->output[0] = context->state;
				break;
			case DISC_LS624_OUT_COUNT_F:
				node->output[0] = cntf;
				break;
			case DISC_LS624_OUT_COUNT_R:
				node->output[0] = cntr;
				break;
		}

	}
	else
		node->output[0] = 0;
}

static void dsd_ls624_reset(node_description *node)
{
	struct dsd_ls624_context *context = node->context;

	context->remain   = 0;
	context->state    = 0;
	context->out_type = DSD_LS624__OUTTYPE;

	/* Step the output */
	dsd_ls624_step(node);
}