summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/disc_wav.c
blob: d29f887ff90bd26ba147e385f3716ef722ab0501 (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
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
/************************************************************************
 *
 *  MAME - Discrete sound system emulation library
 *
 *  Written by Keith Wilkins (mame@esplexo.co.uk)
 *
 *  (c) K.Wilkins 2000
 *
 ************************************************************************
 *
 * DSS_COUNTER        - External clock Binary Counter
 * DSS_LFSR_NOISE     - Linear Feedback Shift Register Noise
 * DSS_NOISE          - Noise Source - Random source
 * DSS_NOTE           - Note/tone generator
 * DSS_OP_AMP_OSC     - Op Amp oscillator circuits
 * DSS_SAWTOOTHWAVE   - Sawtooth waveform generator
 * DSS_SCHMITT_OSC    - Schmitt Feedback Oscillator
 * DSS_SINEWAVE       - Sinewave generator source code
 * DSS_SQUAREWAVE     - Squarewave generator source code
 * DSS_SQUAREWFIX     - Squarewave generator - fixed frequency
 * DSS_SQUAREWAVE2    - Squarewave generator - by tOn/tOff
 * DSS_TRIANGLEWAVE   - Triangle waveform generator
 *
 ************************************************************************/

struct dss_adsr_context
{
	double phase;
};

struct dss_counter_context
{
	int		clock_type;
	int		out_type;
	int		is_7492;
	int		last;		// Last clock state
	int		count;		// current count
	double	t_clock;	// fixed counter clock in seconds
	double	t_left;		// time unused during last sample in seconds
};

struct dss_lfsr_context
{
	unsigned int	lfsr_reg;
	int				last;		// Last clock state
	double			t_clock;	// fixed counter clock in seconds
	double			t_left;		// time unused during last sample in seconds
	double			sampleStep;
	double			shiftStep;
	double			t;
	UINT8			reset_on_high;
	UINT8			invert_output;
	UINT8			out_is_f0;
};

struct dss_noise_context
{
	double phase;
};

struct dss_note_context
{
	int		clock_type;
	int		out_type;
	int		last;		// Last clock state
	double	t_clock;	// fixed counter clock in seconds
	double	t_left;		// time unused during last sample in seconds
	int		max1;		// Max 1 Count stored as int for easy use.
	int		max2;		// Max 2 Count stored as int for easy use.
	int		count1;		// current count1
	int		count2;		// current count2
};

struct dss_op_amp_osc_context
{
	const double *r1;		// pointers to resistor values
	const double *r2;
	const double *r3;
	const double *r4;
	const double *r5;
	const double *r6;
	const double *r7;
	const double *r8;
	int		type;
	UINT8	flip_flop;		// flip/flop output state
	UINT8	flip_flopXOR;	// flip_flop ^ flip_flopXOR, 0 = discharge, 1 = charge
	UINT8	is_squarewave;
	double	high_out_V;
	double	thresholdLow;	// falling threshold
	double	thresholdHigh;	// rising threshold
	double	vCap;			// current capacitor voltage
	double	rTotal;			// all input resistors in parallel
	double	iFixed;			// fixed current at the input
	double	temp1;			// Multi purpose
	double	temp2;			// Multi purpose
	double	temp3;			// Multi purpose
};

struct dss_sawtoothwave_context
{
	double	phase;
	int		type;
};

struct dss_schmitt_osc_context
{
	double	ratioIn;		// ratio of total charging voltage that comes from the input
	double	ratioFeedback;	// ratio of total charging voltage that comes from the feedback
	double	vCap;			// current capacitor voltage
	double	rc;				// r*c
	double	exponent;
	int		state;			// state of the output
	int		enable_type;
	UINT8	input_is_voltage;
};

struct dss_sinewave_context
{
	double phase;
};

struct dss_squarewave_context
{
	double phase;
	double trigger;
};

struct dss_squarewfix_context
{
	int		flip_flop;
	double	sampleStep;
	double	tLeft;
	double	tOff;
	double	tOn;
};

struct dss_trianglewave_context
{
	double phase;
};

#define DSS_INV_TAB_SIZE	500

struct dss_inverter_osc_context
{
	double	w;
	double  wc;
	double	vCap;
	double  vG2_old;
	double	Rp;
	double  R1;
	double  R2;
	double  C;
	double	tf_a;
	double	tf_b;
	double  tf_tab[DSS_INV_TAB_SIZE];
};

/************************************************************************
 *
 * DSS_COUNTER - External clock Binary Counter
 *
 * input0    - Enable input value
 * input1    - Reset input (active high)
 * input2    - Clock Input
 * input3    - Max count
 * input4    - Direction - 0=down, 1=up
 * input5    - Reset Value
 * input6    - Clock type
 *
 * Jan 2004, D Renaud.
 ************************************************************************/
#define DSS_COUNTER__ENABLE		(*(node->input[0]))
#define DSS_COUNTER__RESET		(*(node->input[1]))
#define DSS_COUNTER__CLOCK		(*(node->input[2]))
#define DSS_COUNTER__MAX		(*(node->input[3]))
#define DSS_COUNTER__DIR		(*(node->input[4]))
#define DSS_COUNTER__INIT		(*(node->input[5]))
#define DSS_COUNTER__CLOCK_TYPE	(*(node->input[6]))

static const int disc_7492_count[6] = {0x00, 0x01, 0x02, 0x04, 0x05, 0x06};

static void dss_counter_step(node_description *node)
{
	struct dss_counter_context *context = node->context;
	double cycles;
	int clock = 0, last_count, inc = 0;
	int max = DSS_COUNTER__MAX;
	double xTime = 0;

	if (context->clock_type == DISC_CLK_IS_FREQ)
	{
		/* We need to keep clocking the internal clock even if disabled. */
		cycles = (context->t_left + discrete_current_context->sample_time) / context->t_clock;
		inc = (int)cycles;
		context->t_left = (cycles - inc) * context->t_clock;
		if (inc) xTime = context->t_left / discrete_current_context->sample_time;
	}
	else
	{
		clock = (int)DSS_COUNTER__CLOCK;
		xTime = DSS_COUNTER__CLOCK - clock;
	}


	/* If reset enabled then set output to the reset value.  No xTime in reset. */
	if (DSS_COUNTER__RESET)
	{
		context->count = DSS_COUNTER__INIT;
		node->output[0] = context->is_7492 ? 0 : context->count;
		return;
	}

	/*
     * Only count if module is enabled.
     * This has the effect of holding the output at it's current value.
     */
	if (DSS_COUNTER__ENABLE)
	{
		last_count = context->count;

		switch (context->clock_type)
		{
			case DISC_CLK_ON_F_EDGE:
			case DISC_CLK_ON_R_EDGE:
				/* See if the clock has toggled to the proper edge */
				clock = (clock != 0);
				if (context->last != clock)
				{
					context->last = clock;
					if (context->clock_type == clock)
					{
						/* Toggled */
						inc = 1;
					}
				}
				break;

		case DISC_CLK_BY_COUNT:
				/* Clock number of times specified. */
				inc = clock;
				break;
		}

		for (clock = 0; clock < inc; clock++)
		{
			context->count += DSS_COUNTER__DIR ? 1 : -1; // up/down
			if (context->count < 0) context->count = max;
			if (context->count > max) context->count = 0;
		}

		node->output[0] = context->is_7492 ? disc_7492_count[context->count] : context->count;

		if (context->count != last_count)
		{
			/* the xTime is only output if the output changed. */
			switch (context->out_type)
			{
				case DISC_OUT_IS_ENERGY:
					if (xTime != 0)
						node->output[0] = (context->count > last_count) ? (last_count + xTime) : (last_count - xTime);
					break;
				case DISC_OUT_HAS_XTIME:
					node->output[0] += xTime;
					break;
			}
		}
	}
	else
		node->output[0] = context->count;
}

static void dss_counter_reset(node_description *node)
{
	struct dss_counter_context *context = node->context;

	context->clock_type = (int)DSS_COUNTER__CLOCK_TYPE;
	if (context->clock_type == DISC_COUNTER_IS_7492)
	{
		context->clock_type = DISC_CLK_ON_F_EDGE;
		context->is_7492 = 1;
	}
	else
		context->is_7492 = 0;
	if ((context->clock_type < DISC_CLK_ON_F_EDGE) || (context->clock_type > DISC_CLK_IS_FREQ))
		discrete_log("Invalid clock type passed in NODE_%d\n", NODE_INDEX(node->node));
	context->last = 0;
	if (context->clock_type == DISC_CLK_IS_FREQ) context->t_clock = 1.0 / DSS_COUNTER__CLOCK;
	context->t_left = 0;
	context->count = DSS_COUNTER__INIT; /* count starts at reset value */
	node->output[0] = DSS_COUNTER__INIT;
}


/************************************************************************
 *
 * DSS_LFSR_NOISE - Usage of node_description values for LFSR noise gen
 *
 * input0    - Enable input value
 * input1    - Register reset
 * input2    - Clock Input
 * input3    - Amplitude input value
 * input4    - Input feed bit
 * input5    - Bias
 *
 * also passed dss_lfsr_context structure
 *
 ************************************************************************/
#define DSS_LFSR_NOISE__ENABLE	(*(node->input[0]))
#define DSS_LFSR_NOISE__RESET	(*(node->input[1]))
#define DSS_LFSR_NOISE__CLOCK	(*(node->input[2]))
#define DSS_LFSR_NOISE__AMP		(*(node->input[3]))
#define DSS_LFSR_NOISE__FEED	(*(node->input[4]))
#define DSS_LFSR_NOISE__BIAS	(*(node->input[5]))

int	dss_lfsr_function(int myfunc,int in0,int in1,int bitmask)
{
	int retval;

	in0&=bitmask;
	in1&=bitmask;

	switch(myfunc)
	{
		case DISC_LFSR_XOR:
			retval=in0^in1;
			break;
		case DISC_LFSR_OR:
			retval=in0|in1;
			break;
		case DISC_LFSR_AND:
			retval=in0&in1;
			break;
		case DISC_LFSR_XNOR:
			retval=in0^in1;
			retval=retval^bitmask;	/* Invert output */
			break;
		case DISC_LFSR_NOR:
			retval=in0|in1;
			retval=retval^bitmask;	/* Invert output */
			break;
		case DISC_LFSR_NAND:
			retval=in0&in1;
			retval=retval^bitmask;	/* Invert output */
			break;
		case DISC_LFSR_IN0:
			retval=in0;
			break;
		case DISC_LFSR_IN1:
			retval=in1;
			break;
		case DISC_LFSR_NOT_IN0:
			retval=in0^bitmask;
			break;
		case DISC_LFSR_NOT_IN1:
			retval=in1^bitmask;
			break;
		case DISC_LFSR_REPLACE:
			retval=in0&~in1;
			retval=retval|in1;
			break;
		case DISC_LFSR_XOR_INV_IN0:
			retval = in0^bitmask; /* invert in0 */
			retval = retval^in1;  /* xor in1 */
			break;
		case DISC_LFSR_XOR_INV_IN1:
			retval = in1^bitmask; /* invert in1 */
			retval = retval^in0;  /* xor in0 */
			break;
		default:
			discrete_log("dss_lfsr_function - Invalid function type passed");
			retval=0;
			break;
	}
	return retval;
}

/* reset prototype so that it can be used in init function */
static void dss_lfsr_reset(node_description *node);

static void dss_lfsr_step(node_description *node)
{
	const discrete_lfsr_desc *lfsr_desc = node->custom;
	struct dss_lfsr_context *context = node->context;
	double cycles;
	int clock, inc = 0;
	int fb0,fb1,fbresult;

	if (lfsr_desc->clock_type == DISC_CLK_IS_FREQ)
	{
		/* We need to keep clocking the internal clock even if disabled. */
		cycles = (context->t_left + discrete_current_context->sample_time) / context->t_clock;
		inc = (int)cycles;
		context->t_left = (cycles - inc) * context->t_clock;
	}

	/* Reset everything if necessary */
	if((DSS_LFSR_NOISE__RESET ? 1 : 0) == context->reset_on_high)
	{
		dss_lfsr_reset(node);
		return;
	}

	switch (lfsr_desc->clock_type)
	{
		case DISC_CLK_ON_F_EDGE:
		case DISC_CLK_ON_R_EDGE:
			/* See if the clock has toggled to the proper edge */
			clock = (DSS_LFSR_NOISE__CLOCK != 0);
			if (context->last != clock)
			{
				context->last = clock;
				if (lfsr_desc->clock_type == clock)
				{
					/* Toggled */
					inc = 1;
				}
			}
			break;

		case DISC_CLK_BY_COUNT:
			/* Clock number of times specified. */
			inc = (int)DSS_LFSR_NOISE__CLOCK;
			break;
	}

	for (clock = 0; clock < inc; clock++)
	{
		/* Fetch the last feedback result */
		fbresult=((context->lfsr_reg)>>(lfsr_desc->bitlength))&0x01;

		/* Stage 2 feedback combine fbresultNew with infeed bit */
		fbresult=dss_lfsr_function(lfsr_desc->feedback_function1,fbresult,((DSS_LFSR_NOISE__FEED)?0x01:0x00),0x01);

		/* Stage 3 first we setup where the bit is going to be shifted into */
		fbresult=fbresult*lfsr_desc->feedback_function2_mask;
		/* Then we left shift the register, */
		context->lfsr_reg=(context->lfsr_reg)<<1;
		/* Now move the fbresult into the shift register and mask it to the bitlength */
		context->lfsr_reg=dss_lfsr_function(lfsr_desc->feedback_function2,fbresult, (context->lfsr_reg), ((1<<(lfsr_desc->bitlength))-1));

		/* Now get and store the new feedback result */
		/* Fetch the feedback bits */
		fb0=((context->lfsr_reg)>>(lfsr_desc->feedback_bitsel0))&0x01;
		fb1=((context->lfsr_reg)>>(lfsr_desc->feedback_bitsel1))&0x01;
		/* Now do the combo on them */
		fbresult=dss_lfsr_function(lfsr_desc->feedback_function0,fb0,fb1,0x01);
		context->lfsr_reg=dss_lfsr_function(DISC_LFSR_REPLACE,(context->lfsr_reg), fbresult<<(lfsr_desc->bitlength), ((2<<(lfsr_desc->bitlength))-1));

		/* Now select the output bit */
		if (context->out_is_f0)
			node->output[0] = fbresult & 0x01;
		else
			node->output[0]=((context->lfsr_reg)>>(lfsr_desc->output_bit))&0x01;

		/* Final inversion if required */
		if(context->invert_output) node->output[0]=(node->output[0])?0.0:1.0;

		/* Gain stage */
		node->output[0]=(node->output[0])?(DSS_LFSR_NOISE__AMP)/2:-(DSS_LFSR_NOISE__AMP)/2;
		/* Bias input as required */
		node->output[0]=node->output[0]+DSS_LFSR_NOISE__BIAS;
	}

	if(!DSS_LFSR_NOISE__ENABLE)
	{
		node->output[0]=0;
	}
}

static void dss_lfsr_reset(node_description *node)
{
	const discrete_lfsr_desc *lfsr_desc = node->custom;
	struct dss_lfsr_context *context = node->context;
	int fb0,fb1,fbresult;

	context->reset_on_high = (lfsr_desc->flags & DISC_LFSR_FLAG_RESET_TYPE_H) ? 1 : 0;
	context->invert_output = lfsr_desc->flags & DISC_LFSR_FLAG_OUT_INVERT;
	context->out_is_f0 = (lfsr_desc->flags & DISC_LFSR_FLAG_OUTPUT_F0) ? 1 : 0;

	if ((lfsr_desc->clock_type < DISC_CLK_ON_F_EDGE) || (lfsr_desc->clock_type > DISC_CLK_IS_FREQ))
		discrete_log("Invalid clock type passed in NODE_%d\n", NODE_INDEX(node->node));
	context->last = (DSS_COUNTER__CLOCK != 0);
	if (lfsr_desc->clock_type == DISC_CLK_IS_FREQ) context->t_clock = 1.0 / DSS_LFSR_NOISE__CLOCK;
	context->t_left = 0;

	context->lfsr_reg=lfsr_desc->reset_value;

	/* Now get and store the new feedback result */
	/* Fetch the feedback bits */
	fb0=((context->lfsr_reg)>>(lfsr_desc->feedback_bitsel0))&0x01;
	fb1=((context->lfsr_reg)>>(lfsr_desc->feedback_bitsel1))&0x01;
	/* Now do the combo on them */
	fbresult=dss_lfsr_function(lfsr_desc->feedback_function0,fb0,fb1,0x01);
	context->lfsr_reg=dss_lfsr_function(DISC_LFSR_REPLACE,(context->lfsr_reg), fbresult<<(lfsr_desc->bitlength), ((2<<(lfsr_desc->bitlength))-1));

	/* Now select and setup the output bit */
	node->output[0]=((context->lfsr_reg)>>(lfsr_desc->output_bit))&0x01;

	/* Final inversion if required */
	if(lfsr_desc->flags&DISC_LFSR_FLAG_OUT_INVERT) node->output[0]=(node->output[0])?0.0:1.0;

	/* Gain stage */
	node->output[0]=(node->output[0])?(DSS_LFSR_NOISE__AMP)/2:-(DSS_LFSR_NOISE__AMP)/2;
	/* Bias input as required */
	node->output[0]=node->output[0]+DSS_LFSR_NOISE__BIAS;
}


/************************************************************************
 *
 * DSS_NOISE - Usage of node_description values for white nose generator
 *
 * input0    - Enable input value
 * input1    - Noise sample frequency
 * input2    - Amplitude input value
 * input3    - DC Bias value
 *
 ************************************************************************/
#define DSS_NOISE__ENABLE	(*(node->input[0]))
#define DSS_NOISE__FREQ		(*(node->input[1]))
#define DSS_NOISE__AMP		(*(node->input[2]))
#define DSS_NOISE__BIAS		(*(node->input[3]))

static void dss_noise_step(node_description *node)
{
	struct dss_noise_context *context = node->context;

	if(DSS_NOISE__ENABLE)
	{
		/* Only sample noise on rollover to next cycle */
		if(context->phase > (2.0*M_PI))
		{
			/* GCC's rand returns a RAND_MAX value of 0x7fff */
			int newval = (mame_rand(Machine) & 0x7fff) - 16384;

			/* make sure the peak to peak values are the amplitude */
			node->output[0] = DSS_NOISE__AMP / 2;
			if (newval > 0)
				node->output[0] *= ((double)newval / 16383);
			else
				node->output[0] *= ((double)newval / 16384);

			/* Add DC Bias component */
			node->output[0] += DSS_NOISE__BIAS;
		}
	}
	else
	{
		node->output[0] = 0;
	}

	/* Keep the new phasor in the 2Pi range.*/
	context->phase = fmod(context->phase, 2.0*M_PI);

	/* The enable input only curtails output, phase rotation still occurs. */
	/* We allow the phase to exceed 2Pi here, so we can tell when to sample the noise. */
	context->phase += ((2.0*M_PI * DSS_NOISE__FREQ) / discrete_current_context->sample_rate);
}


static void dss_noise_reset(node_description *node)
{
	struct dss_noise_context *context = node->context;

	context->phase=0;
	dss_noise_step(node);
}


/************************************************************************
 *
 * DSS_NOTE - Note/tone generator
 *
 * input0    - Enable input value
 * input1    - Clock Input
 * input2    - data value
 * input3    - Max count 1
 * input4    - Max count 2
 * input5    - Clock type
 *
 * Mar 2004, D Renaud.
 ************************************************************************/
 #define DSS_NOTE__ENABLE		(*(node->input[0]))
 #define DSS_NOTE__CLOCK		(*(node->input[1]))
 #define DSS_NOTE__DATA			(*(node->input[2]))
 #define DSS_NOTE__MAX1			(*(node->input[3]))
 #define DSS_NOTE__MAX2			(*(node->input[4]))
 #define DSS_NOTE__CLOCK_TYPE	(*(node->input[5]))

static void dss_note_step(node_description *node)
{
	struct dss_note_context *context = node->context;

	double cycles;
	int clock = 0, last_count2, inc = 0;
	double xTime = 0;

	if (context->clock_type == DISC_CLK_IS_FREQ)
	{
		/* We need to keep clocking the internal clock even if disabled. */
		cycles = (context->t_left + discrete_current_context->sample_time) / context->t_clock;
		inc = (int)cycles;
		context->t_left = (cycles - inc) * context->t_clock;
		if (inc) xTime = context->t_left / discrete_current_context->sample_time;
	}
	else
	{
		/* Seperate clock info from xTime info. */
		clock = (int)DSS_NOTE__CLOCK;
		xTime = DSS_NOTE__CLOCK - clock;
	}

	if (DSS_NOTE__ENABLE)
	{
		last_count2 = context->count2;

		switch (context->clock_type)
		{
			case DISC_CLK_ON_F_EDGE:
			case DISC_CLK_ON_R_EDGE:
				/* See if the clock has toggled to the proper edge */
				clock = (clock != 0);
				if (context->last != clock)
				{
					context->last = clock;
					if (context->clock_type == clock)
					{
						/* Toggled */
						inc = 1;
					}
				}
				break;

			case DISC_CLK_BY_COUNT:
				/* Clock number of times specified. */
				inc = clock;
				break;
		}

		/* Count output as long as the data loaded is not already equal to max 1 count. */
		if (DSS_NOTE__DATA != DSS_NOTE__MAX1)
		{
			for (clock = 0; clock < inc; clock++)
			{
				context->count1++;
				if (context->count1 > context->max1)
				{
					/* Max 1 count reached.  Load Data into counter. */
					context->count1 = (int)DSS_NOTE__DATA;
					context->count2 += 1;
					if (context->count2 > context->max2) context->count2 = 0;
				}
			}
		}

		node->output[0] = context->count2;
		if (context->count2 != last_count2)
		{
			/* the xTime is only output if the output changed. */
			switch (context->out_type)
			{
				case DISC_OUT_IS_ENERGY:
					if (xTime != 0)
						node->output[0] = (context->count2 > last_count2) ? (last_count2 + xTime) : (last_count2 - xTime);
					break;
				case DISC_OUT_HAS_XTIME:
					node->output[0] += xTime;
					break;
			}
		}
	}
	else
		node->output[0] = 0;
}

static void dss_note_reset(node_description *node)
{
	struct dss_note_context *context = node->context;

	context->clock_type = (int)DSS_NOTE__CLOCK_TYPE & DISC_CLK_MASK;
	context->out_type =  (int)DSS_NOTE__CLOCK_TYPE & DISC_OUT_MASK;
	context->last = (DSS_NOTE__CLOCK != 0);
	if (context->clock_type == DISC_CLK_IS_FREQ) context->t_clock = 1.0 / DSS_NOTE__CLOCK;
	context->t_left = 0;

	context->count1 = (int)DSS_NOTE__DATA;
	context->count2 = 0;
	context->max1 = (int)DSS_NOTE__MAX1;
	context->max2 = (int)DSS_NOTE__MAX2;
	node->output[0] = 0;
}

/************************************************************************
 *
 * DSS_OP_AMP_OSC - Op Amp Oscillators
 *
 * input0    - Enable input value
 * input1    - vMod1 (if needed)
 * input2    - vMod2 (if needed)
 *
 * also passed discrete_op_amp_osc_info structure
 *
 * Mar 2004, D Renaud.
 ************************************************************************/
#define DSS_OP_AMP_OSC__ENABLE	(*(node->input[0]))
#define DSS_OP_AMP_OSC__VMOD1	(*(node->input[1]))
#define DSS_OP_AMP_OSC__VMOD2	(*(node->input[2]))

/* The inputs on a norton op-amp are (info->vP - OP_AMP_NORTON_VBE) */
/* which is the same as the output high voltage.  We will define them */
/* the same to save a calculation step */
#define DSS_OP_AMP_OSC_NORTON_VP_IN		context->high_out_V

static void dss_op_amp_osc_step(node_description *node)
{
	const discrete_op_amp_osc_info *info = node->custom;
	struct dss_op_amp_osc_context *context = node->context;


	double i;			// Charging current created by vIn
	double v = 0;		// all input voltages mixed
	double dt;			// change in time
	double vC;			// Current voltage on capacitor, before dt
	double vCnext = 0;	// Voltage on capacitor, after dt
	double iCharge[2] = {0};
	UINT8 force_charge = 0;
	UINT8 enable = DSS_OP_AMP_OSC__ENABLE;

	dt = discrete_current_context->sample_time;	// Change in time
	vC = context->vCap;	// Set to voltage before change

	/* work out the charge currents for the VCOs. */
	switch (context->type)
	{
		case DISC_OP_AMP_OSCILLATOR_VCO_1:
			/* Work out the charge rates. */
			i = DSS_OP_AMP_OSC__VMOD1 * context->temp1;		// i is not a current.  It is being used as a temp variable.
			iCharge[0] = (DSS_OP_AMP_OSC__VMOD1 - i) / info->r1;
			iCharge[1] = (i - (DSS_OP_AMP_OSC__VMOD1 * context->temp2)) / context->temp3;
			break;

		case DISC_OP_AMP_OSCILLATOR_1 | DISC_OP_AMP_IS_NORTON:
		{
			/* resistors can be nodes, so everything needs updating */
			double i1, i2;
			/* Work out the charge rates. */
			iCharge[0] = DSS_OP_AMP_OSC_NORTON_VP_IN / *context->r1;
			iCharge[1] = (context->high_out_V - OP_AMP_NORTON_VBE) / *context->r2 - iCharge[0];
			/* Work out the Inverting Schmitt thresholds. */
			i1 = DSS_OP_AMP_OSC_NORTON_VP_IN / *context->r5;
			i2 = (0.0 - OP_AMP_NORTON_VBE) / *context->r4;
			context->thresholdLow = (i1 + i2) * *context->r3 + OP_AMP_NORTON_VBE;
			i2 = (context->high_out_V - OP_AMP_NORTON_VBE) / *context->r4;
			context->thresholdHigh = (i1 + i2) * *context->r3 + OP_AMP_NORTON_VBE;
			break;
		}

		case DISC_OP_AMP_OSCILLATOR_VCO_1 | DISC_OP_AMP_IS_NORTON:
			/* Millman the input voltages. */
			if (info->r7 == 0)
			{
				/* No r7 means that the modulation circuit is fed directly into the circuit. */
				v = DSS_OP_AMP_OSC__VMOD1;
			}
			else
			{
				/* we need to mix any bias and all modulation voltages together. */
				i = context->iFixed;
				i += DSS_OP_AMP_OSC__VMOD1 / info->r7;
				if (info->r8 != 0)
					i += DSS_OP_AMP_OSC__VMOD2 / info->r8;
				v = i * context->rTotal;
			}

			/* Work out the charge rates. */
			v -= OP_AMP_NORTON_VBE;
			iCharge[0] = v / info->r1;
			iCharge[1] = v / info->r2 - iCharge[0];

			/* use the real enable circuit */
			force_charge = !enable;
			enable = 1;
			break;

		case DISC_OP_AMP_OSCILLATOR_VCO_2 | DISC_OP_AMP_IS_NORTON:
			/* Work out the charge rates. */
			i = DSS_OP_AMP_OSC__VMOD1 / info->r1;
			iCharge[0] = i - context->temp1;
			iCharge[1] = context->temp2 - i;
			/* if the negative pin current is less then the positive pin current, */
			/* then the osc is disabled and the cap keeps charging */
			if (iCharge[0] < 0)
			{
				force_charge = 1;
				iCharge[0] *= -1;
			}
			break;

		case DISC_OP_AMP_OSCILLATOR_VCO_3 | DISC_OP_AMP_IS_NORTON:
			/* we need to mix any bias and all modulation voltages together. */
			iCharge[0] = context->iFixed;
			v = DSS_OP_AMP_OSC__VMOD1 - OP_AMP_NORTON_VBE;
			iCharge[0] += v / info->r1;
			if (info->r6 != 0)
			{
				v = DSS_OP_AMP_OSC__VMOD2 - OP_AMP_NORTON_VBE;
				iCharge[0] += v / info->r6;
			}
			iCharge[1] = context->temp1 - iCharge[0];
			break;
	}

	if (enable)
	{
		int toggled = 0;
		/* Keep looping until all toggling in time sample is used up. */
		do
		{
			if ((context->flip_flop ^ context->flip_flopXOR) || force_charge)
			{
				/* Charging */
				/* iC=C*dv/dt  works out to dv=iC*dt/C */
				vCnext = vC + (iCharge[1] * dt / info->c);
				dt = 0;

				/* has it charged past upper limit? */
				if (vCnext > context->thresholdHigh)
				{
					context->flip_flop = context->flip_flopXOR;
					toggled++;
					if (force_charge)
					{
						/* we need to keep charging the cap to the max thereby disabling the circuit */
						if (vCnext > context->high_out_V)
							vCnext = context->high_out_V;
					}
					else
					{
						/* calculate the overshoot time */
						dt = info->c * (vCnext - context->thresholdHigh) / iCharge[1];
						vC = context->thresholdHigh;
					}
				}
			}
			else
			{
				/* Discharging */
				vCnext = vC - (iCharge[0] * dt / info->c);
				dt = 0;

				/* has it discharged past lower limit? */
				if (vCnext < context->thresholdLow)
				{
					context->flip_flop = !context->flip_flopXOR;
					toggled++;
					/* calculate the overshoot time */
					dt = info->c * (context->thresholdLow - vCnext) / iCharge[0];
					vC = context->thresholdLow;
				}
			}
		} while(dt);

		context->vCap = vCnext;

		if (context->is_squarewave)
		{
			if (toggled == 2)
				/* Some oscillators have rapid rise or fall times causing 1 part of the */
				/* squarewave to happen in the sample time causing it to be missed. */
				/* If we toggle 2 states we force the missed output for 1 sample. */
				/* If more then 2 states happen, there is no hope, the sample rate is just too low. */
				node->output[0] = context->high_out_V * (context->flip_flop ? 0 : 1);
			else
				node->output[0] = context->high_out_V * context->flip_flop;
		}
		else
			node->output[0] = context->vCap;
	}
	else
	{
			/* we will just output 0 for oscillators that have no real enable. */
			node->output[0] = 0;
	}
}

static void dss_op_amp_osc_reset(node_description *node)
{
	const discrete_op_amp_osc_info *info = node->custom;
	struct dss_op_amp_osc_context *context = node->context;
	const double *r_info_ptr;
	const double **r_context_ptr;
	int loop;
	node_description *r_node;

	double i1 = 0;	// inverting input current
	double i2 = 0;	// non-inverting input current

	/* link to resistor static or node values */
	r_info_ptr = &info->r1;
	r_context_ptr = &context->r1;
	for (loop = 0; loop < 8; loop ++)
	{
		if IS_VALUE_A_NODE(*r_info_ptr)
		{
			r_node = discrete_find_node(NULL, *r_info_ptr);
			*r_context_ptr = &(r_node->output[NODE_CHILD_NODE_NUM((int)*r_info_ptr)]);
		}
		else
			*r_context_ptr = r_info_ptr;
		r_info_ptr++;
		r_context_ptr++;
	}

	context->is_squarewave = (info->type & DISC_OP_AMP_OSCILLATOR_OUT_SQW);
	context->type = info->type & DISC_OP_AMP_OSCILLATOR_TYPE_MASK;

	switch (context->type)
	{
		case DISC_OP_AMP_OSCILLATOR_VCO_1:
			/* The charge rates vary depending on vMod so they are not precalculated. */
			/* Charges while FlipFlop High */
			context->flip_flopXOR = 0;
			/* Work out the Non-inverting Schmitt thresholds. */
			context->temp1 = (info->vP / 2) / info->r4;
			context->temp2 = (info->vP - OP_AMP_VP_RAIL_OFFSET) / info->r3;
			context->temp3 = 1.0 / (1.0 / info->r3 + 1.0 / info->r4);
			context->thresholdLow = context->temp1 * context->temp3;
			context->thresholdHigh = (context->temp1 + context->temp2) * context->temp3;
			/* There is no charge on the cap so the schmitt goes high at init. */
			context->flip_flop = 1;
			/* Setup some commonly used stuff */
			context->temp1 = info->r5 / (info->r2 + info->r5);			// voltage ratio across r5
			context->temp2 = info->r6 / (info->r1 + info->r6);			// voltage ratio across r6
			context->temp3 = 1.0 / (1.0 / info->r1 + 1.0 / info->r6);	// input resistance when r6 switched in
			break;

		case DISC_OP_AMP_OSCILLATOR_1 | DISC_OP_AMP_IS_NORTON:
			/* Charges while FlipFlop High */
			context->flip_flopXOR = 0;
			/* There is no charge on the cap so the schmitt inverter goes high at init. */
			context->flip_flop = 1;
			break;

		case DISC_OP_AMP_OSCILLATOR_VCO_1 | DISC_OP_AMP_IS_NORTON:
			/* Charges while FlipFlop Low */
			context->flip_flopXOR = 1;
			/* There is no charge on the cap so the schmitt goes low at init. */
			context->flip_flop = 0;
			/* The charge rates vary depending on vMod so they are not precalculated. */
			/* But we can precalculate the fixed currents. */
			context->iFixed = 0;
			if (info->r6 != 0) context->iFixed += info->vP / info->r6;
			context->iFixed += OP_AMP_NORTON_VBE / info->r1;
			context->iFixed += OP_AMP_NORTON_VBE / info->r2;
			/* Work out the input resistance to be used later to calculate the Millman voltage. */
			context->rTotal = 1.0 / info->r1 + 1.0 / info->r2 + 1.0 / info->r7;
			if (info->r6) context->rTotal += 1.0 / info->r6;
			if (info->r8) context->rTotal += 1.0 / info->r8;
			context->rTotal = 1.0 / context->rTotal;
			/* Work out the Non-inverting Schmitt thresholds. */
			i1 = (info->vP - OP_AMP_NORTON_VBE) / info->r5;
			i2 = (info->vP - OP_AMP_NORTON_VBE - OP_AMP_NORTON_VBE) / info->r4;
			context->thresholdLow = (i1 - i2) * info->r3 + OP_AMP_NORTON_VBE;
			i2 = (0.0 - OP_AMP_NORTON_VBE) / info->r4;
			context->thresholdHigh = (i1 - i2) * info->r3 + OP_AMP_NORTON_VBE;
			break;

		case DISC_OP_AMP_OSCILLATOR_VCO_2 | DISC_OP_AMP_IS_NORTON:
			/* Charges while FlipFlop High */
			context->flip_flopXOR = 0;
			/* There is no charge on the cap so the schmitt inverter goes high at init. */
			context->flip_flop = 1;
			/* Work out the charge rates. */
			context->temp1 = (info->vP - OP_AMP_NORTON_VBE) / info->r2;
			context->temp2 = (info->vP - OP_AMP_NORTON_VBE) * (1.0 / info->r2 + 1.0 / info->r6);
			/* Work out the Inverting Schmitt thresholds. */
			i1 = (info->vP - OP_AMP_NORTON_VBE) / info->r5;
			i2 = (0.0 - OP_AMP_NORTON_VBE) / info->r4;
			context->thresholdLow = (i1 + i2) * info->r3 + OP_AMP_NORTON_VBE;
			i2 = (info->vP - OP_AMP_NORTON_VBE - OP_AMP_NORTON_VBE) / info->r4;
			context->thresholdHigh = (i1 + i2) * info->r3 + OP_AMP_NORTON_VBE;
			break;

		case DISC_OP_AMP_OSCILLATOR_VCO_3 | DISC_OP_AMP_IS_NORTON:
			/* Charges while FlipFlop High */
			context->flip_flopXOR = 0;
			/* There is no charge on the cap so the schmitt inverter goes high at init. */
			context->flip_flop = 1;
			/* Work out the charge rates. */
			/* The charge rates vary depending on vMod so they are not precalculated. */
			/* But we can precalculate the fixed currents. */
			if (info->r7 != 0) context->iFixed = (info->vP - OP_AMP_NORTON_VBE) / info->r7;
			context->temp1 = (info->vP - OP_AMP_NORTON_VBE - OP_AMP_NORTON_VBE) / info->r2;
			/* Work out the Inverting Schmitt thresholds. */
			i1 = (info->vP - OP_AMP_NORTON_VBE) / info->r5;
			i2 = (0.0 - OP_AMP_NORTON_VBE) / info->r4;
			context->thresholdLow = (i1 + i2) * info->r3 + OP_AMP_NORTON_VBE;
			i2 = (info->vP - OP_AMP_NORTON_VBE - OP_AMP_NORTON_VBE) / info->r4;
			context->thresholdHigh = (i1 + i2) * info->r3 + OP_AMP_NORTON_VBE;
			break;
	}

	context->high_out_V = info->vP - ((context->type & DISC_OP_AMP_IS_NORTON) ? OP_AMP_NORTON_VBE : OP_AMP_VP_RAIL_OFFSET);
	context->vCap = 0;

	dss_op_amp_osc_step(node);
}


/************************************************************************
 *
 * DSS_SAWTOOTHWAVE - Usage of node_description values for step function
 *
 * input0    - Enable input value
 * input1    - Frequency input value
 * input2    - Amplitde input value
 * input3    - DC Bias Value
 * input4    - Gradient
 * input5    - Initial Phase
 *
 ************************************************************************/
#define DSS_SAWTOOTHWAVE__ENABLE	(*(node->input[0]))
#define DSS_SAWTOOTHWAVE__FREQ		(*(node->input[1]))
#define DSS_SAWTOOTHWAVE__AMP		(*(node->input[2]))
#define DSS_SAWTOOTHWAVE__BIAS		(*(node->input[3]))
#define DSS_SAWTOOTHWAVE__GRAD		(*(node->input[4]))
#define DSS_SAWTOOTHWAVE__PHASE		(*(node->input[5]))

static void dss_sawtoothwave_step(node_description *node)
{
	struct dss_sawtoothwave_context *context = node->context;

	if(DSS_SAWTOOTHWAVE__ENABLE)
	{
		node->output[0]=(context->type==0)?context->phase*(DSS_SAWTOOTHWAVE__AMP/(2.0*M_PI)):DSS_SAWTOOTHWAVE__AMP-(context->phase*(DSS_SAWTOOTHWAVE__AMP/(2.0*M_PI)));
		node->output[0]-=DSS_SAWTOOTHWAVE__AMP/2.0;
		/* Add DC Bias component */
		node->output[0]=node->output[0]+DSS_SAWTOOTHWAVE__BIAS;
	}
	else
	{
		node->output[0]=0;
	}

	/* Work out the phase step based on phase/freq & sample rate */
	/* The enable input only curtails output, phase rotation     */
	/* still occurs                                              */
	/*     phase step = 2Pi/(output period/sample period)        */
	/*                    boils out to                           */
	/*     phase step = (2Pi*output freq)/sample freq)           */
	/* Also keep the new phasor in the 2Pi range.                */
	context->phase=fmod((context->phase+((2.0*M_PI*DSS_SAWTOOTHWAVE__FREQ)/discrete_current_context->sample_rate)),2.0*M_PI);
}

static void dss_sawtoothwave_reset(node_description *node)
{
	struct dss_sawtoothwave_context *context = node->context;
	double start;

	/* Establish starting phase, convert from degrees to radians */
	start=(DSS_SAWTOOTHWAVE__PHASE/360.0)*(2.0*M_PI);
	/* Make sure its always mod 2Pi */
	context->phase=fmod(start,2.0*M_PI);

	/* Invert gradient depending on sawtooth type /|/|/|/|/| or |\|\|\|\|\ */
	context->type=(DSS_SAWTOOTHWAVE__GRAD)?1:0;

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


/************************************************************************
 *
 * DSS_SCHMITT_OSC - Schmitt feedback oscillator
 *
 * input0    - Enable input value
 * input1    - Vin
 * input2    - Amplitude
 *
 * also passed discrete_schmitt_osc_disc structure
 *
 * Mar 2004, D Renaud.
 ************************************************************************/
#define DSS_SCHMITT_OSC__ENABLE	(int)(*(node->input[0]))
#define DSS_SCHMITT_OSC__VIN	(*(node->input[1]))
#define DSS_SCHMITT_OSC__AMP	(*(node->input[2]))

static void dss_schmitt_osc_step(node_description *node)
{
	const discrete_schmitt_osc_desc *info = node->custom;
	struct dss_schmitt_osc_context *context = node->context;

	double supply, vCap, new_vCap, t, exponent;

	/* We will always oscillate.  The enable just affects the output. */
	vCap = context->vCap;
	exponent = context->exponent;

	/* Keep looping until all toggling in time sample is used up. */
	do
	{
		t = 0;
		/* The charging voltage to the cap is the sum of the input voltage and the gate
         * output voltage in the ratios determined by their resistors in a divider network.
         * The input voltage is selectable as straight voltage in or logic level that will
         * use vGate as its voltage.  Note that ratioIn is just the ratio of the total
         * voltage and needs to be multipled by the input voltage.  ratioFeedback has
         * already been multiplied by vGate to save time because that voltage never changes. */
		supply = context->input_is_voltage ? context->ratioIn * DSS_SCHMITT_OSC__VIN : (DSS_SCHMITT_OSC__VIN ? context->ratioIn * info->vGate : 0);
		supply += (context->state ? context->ratioFeedback : 0);
		new_vCap = vCap + ((supply - vCap) * exponent);
		if (context->state)
		{
			/* Charging */
			/* has it charged past upper limit? */
			if (new_vCap > info->trshRise)
			{
				/* calculate the overshoot time */
				t = context->rc * log(1.0 / (1.0 - ((new_vCap - info->trshRise) / (info->vGate - vCap))));
				/* calculate new exponent because of reduced time */
				exponent = 1.0 - exp(-t / context->rc);
				vCap = new_vCap = info->trshRise;
				context->state = 0;
			}
		}
		else
		{
			/* Discharging */
			/* has it discharged past lower limit? */
			if (new_vCap < info->trshFall)
			{
				/* calculate the overshoot time */
				t = context->rc * log(1.0 / (1.0 - ((info->trshFall - new_vCap) / vCap)));
				/* calculate new exponent because of reduced time */
				exponent = 1.0 - exp(-t / context->rc);
				vCap = new_vCap = info->trshFall;
				context->state = 1;
			}
		}
	} while(t);

	context->vCap = new_vCap;

	switch (context->enable_type)
	{
		case DISC_SCHMITT_OSC_ENAB_IS_AND:
			node->output[0] = DSS_SCHMITT_OSC__ENABLE && context->state;
			break;
		case DISC_SCHMITT_OSC_ENAB_IS_NAND:
			node->output[0] = !(DSS_SCHMITT_OSC__ENABLE && context->state);
			break;
		case DISC_SCHMITT_OSC_ENAB_IS_OR:
			node->output[0] = DSS_SCHMITT_OSC__ENABLE || context->state;
			break;
		case DISC_SCHMITT_OSC_ENAB_IS_NOR:
			node->output[0] = !(DSS_SCHMITT_OSC__ENABLE || context->state);
			break;
	}
	node->output[0] *= DSS_SCHMITT_OSC__AMP;
}

static void dss_schmitt_osc_reset(node_description *node)
{
	const discrete_schmitt_osc_desc *info = node->custom;
	struct dss_schmitt_osc_context *context = node->context;
	double rSource;

	context->enable_type = info->options & DISC_SCHMITT_OSC_ENAB_MASK;
	context->input_is_voltage = (info->options & DISC_SCHMITT_OSC_IN_IS_VOLTAGE) ? 1 : 0;

	/* The 2 resistors make a voltage divider, so their ratios add together
     * to make the charging voltage. */
	context->ratioIn = info->rFeedback / (info->rIn + info->rFeedback);
	context->ratioFeedback = info->rIn / (info->rIn + info->rFeedback) * info->vGate;

	/* The voltage source resistance works out to the 2 resistors in parallel.
     * So use this for the RC charge constant. */
	rSource = 1.0 / ((1.0 / info->rIn) + (1.0 / info->rFeedback));
	context->rc = rSource * info->c;
	context->exponent = -1.0 / (context->rc  * discrete_current_context->sample_rate);
	context->exponent = 1.0 - exp(context->exponent);

	/* Cap is at 0V on power up.  Causing output to be high. */
	context->vCap = 0;
	context->state = 1;

	node->output[0] = info->options ? 0 : DSS_SCHMITT_OSC__AMP;
}


/************************************************************************
 *
 * DSS_SINEWAVE - Usage of node_description values for step function
 *
 * input0    - Enable input value
 * input1    - Frequency input value
 * input2    - Amplitude input value
 * input3    - DC Bias
 * input4    - Starting phase
 *
 ************************************************************************/
#define DSS_SINEWAVE__ENABLE	(*(node->input[0]))
#define DSS_SINEWAVE__FREQ		(*(node->input[1]))
#define DSS_SINEWAVE__AMPL		(*(node->input[2]))
#define DSS_SINEWAVE__BIAS		(*(node->input[3]))
#define DSS_SINEWAVE__PHASE		(*(node->input[4]))

static void dss_sinewave_step(node_description *node)
{
	struct dss_sinewave_context *context = node->context;

	/* Set the output */
	if(DSS_SINEWAVE__ENABLE)
	{
		node->output[0]=(DSS_SINEWAVE__AMPL/2.0) * sin(context->phase);
		/* Add DC Bias component */
		node->output[0]=node->output[0]+DSS_SINEWAVE__BIAS;
	}
	else
	{
		node->output[0]=0;
	}

	/* Work out the phase step based on phase/freq & sample rate */
	/* The enable input only curtails output, phase rotation     */
	/* still occurs                                              */
	/*     phase step = 2Pi/(output period/sample period)        */
	/*                    boils out to                           */
	/*     phase step = (2Pi*output freq)/sample freq)           */
	/* Also keep the new phasor in the 2Pi range.                */
	context->phase=fmod((context->phase+((2.0*M_PI*DSS_SINEWAVE__FREQ)/discrete_current_context->sample_rate)),2.0*M_PI);
}

static void dss_sinewave_reset(node_description *node)
{
	struct dss_sinewave_context *context = node->context;
	double start;

	/* Establish starting phase, convert from degrees to radians */
	start=(DSS_SINEWAVE__PHASE/360.0)*(2.0*M_PI);
	/* Make sure its always mod 2Pi */
	context->phase=fmod(start,2.0*M_PI);
	/* Step the output to make it correct */
	dss_sinewave_step(node);
}


/************************************************************************
 *
 * DSS_SQUAREWAVE - Usage of node_description values for step function
 *
 * input0    - Enable input value
 * input1    - Frequency input value
 * input2    - Amplitude input value
 * input3    - Duty Cycle
 * input4    - DC Bias level
 * input5    - Start Phase
 *
 ************************************************************************/
#define DSS_SQUAREWAVE__ENABLE	(*(node->input[0]))
#define DSS_SQUAREWAVE__FREQ	(*(node->input[1]))
#define DSS_SQUAREWAVE__AMP		(*(node->input[2]))
#define DSS_SQUAREWAVE__DUTY	(*(node->input[3]))
#define DSS_SQUAREWAVE__BIAS	(*(node->input[4]))
#define DSS_SQUAREWAVE__PHASE	(*(node->input[5]))

static void dss_squarewave_step(node_description *node)
{
	struct dss_squarewave_context *context = node->context;

	/* Establish trigger phase from duty */
	context->trigger=((100-DSS_SQUAREWAVE__DUTY)/100)*(2.0*M_PI);

	/* Set the output */
	if(DSS_SQUAREWAVE__ENABLE)
	{
		if(context->phase>context->trigger)
			node->output[0]=(DSS_SQUAREWAVE__AMP/2.0);
		else
			node->output[0]=-(DSS_SQUAREWAVE__AMP/2.0);

		/* Add DC Bias component */
		node->output[0]=node->output[0]+DSS_SQUAREWAVE__BIAS;
	}
	else
	{
		node->output[0]=0;
	}

	/* Work out the phase step based on phase/freq & sample rate */
	/* The enable input only curtails output, phase rotation     */
	/* still occurs                                              */
	/*     phase step = 2Pi/(output period/sample period)        */
	/*                    boils out to                           */
	/*     phase step = (2Pi*output freq)/sample freq)           */
	/* Also keep the new phasor in the 2Pi range.                */
	context->phase=fmod((context->phase+((2.0*M_PI*DSS_SQUAREWAVE__FREQ)/discrete_current_context->sample_rate)),2.0*M_PI);
}

static void dss_squarewave_reset(node_description *node)
{
	struct dss_squarewave_context *context = node->context;
	double start;

	/* Establish starting phase, convert from degrees to radians */
	start=(DSS_SQUAREWAVE__PHASE/360.0)*(2.0*M_PI);
	/* Make sure its always mod 2Pi */
	context->phase=fmod(start,2.0*M_PI);

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

/************************************************************************
 *
 * DSS_SQUAREWFIX - Usage of node_description values for step function
 *
 * input0    - Enable input value
 * input1    - Frequency input value
 * input2    - Amplitude input value
 * input3    - Duty Cycle
 * input4    - DC Bias level
 * input5    - Start Phase
 *
 ************************************************************************/
#define DSS_SQUAREWFIX__ENABLE	(*(node->input[0]))
#define DSS_SQUAREWFIX__FREQ	(*(node->input[1]))
#define DSS_SQUAREWFIX__AMP		(*(node->input[2]))
#define DSS_SQUAREWFIX__DUTY	(*(node->input[3]))
#define DSS_SQUAREWFIX__BIAS	(*(node->input[4]))
#define DSS_SQUAREWFIX__PHASE	(*(node->input[5]))

static void dss_squarewfix_step(node_description *node)
{
	struct dss_squarewfix_context *context = node->context;

	context->tLeft -= context->sampleStep;

	/* The enable input only curtails output, phase rotation still occurs */
	while (context->tLeft <= 0)
	{
		context->flip_flop = context->flip_flop ? 0 : 1;
		context->tLeft += context->flip_flop ? context->tOn : context->tOff;
	}

	if(DSS_SQUAREWFIX__ENABLE)
	{
		/* Add gain and DC Bias component */

		context->tOff = 1.0 / DSS_SQUAREWFIX__FREQ;	/* cycle time */
		context->tOn = context->tOff * (DSS_SQUAREWFIX__DUTY / 100.0);
		context->tOff -= context->tOn;

		node->output[0] = (context->flip_flop ? DSS_SQUAREWFIX__AMP / 2.0 : -(DSS_SQUAREWFIX__AMP / 2.0)) + DSS_SQUAREWFIX__BIAS;
	}
	else
	{
		node->output[0]=0;
	}
}

static void dss_squarewfix_reset(node_description *node)
{
	struct dss_squarewfix_context *context = node->context;

	context->sampleStep = 1.0 / discrete_current_context->sample_rate;
	context->flip_flop = 1;

	/* Do the intial time shift and convert freq to off/on times */
	context->tOff = 1.0 / DSS_SQUAREWFIX__FREQ;	/* cycle time */
	context->tLeft = DSS_SQUAREWFIX__PHASE / 360.0;	/* convert start phase to % */
	context->tLeft = context->tLeft - (int)context->tLeft;	/* keep % between 0 & 1 */
	context->tLeft = (context->tLeft < 0) ? 1.0 + context->tLeft : context->tLeft;	/* if - then flip to + phase */
	context->tLeft *= context->tOff;
	context->tOn = context->tOff * (DSS_SQUAREWFIX__DUTY / 100.0);
	context->tOff -= context->tOn;

	context->tLeft = -context->tLeft;

	/* toggle output and work out intial time shift */
	while (context->tLeft <= 0)
	{
		context->flip_flop = context->flip_flop ? 0 : 1;
		context->tLeft += context->flip_flop ? context->tOn : context->tOff;
	}

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


/************************************************************************
 *
 * DSS_SQUAREWAVE2 - Usage of node_description values
 *
 * input0    - Enable input value
 * input1    - Amplitude input value
 * input2    - OFF Time
 * input3    - ON Time
 * input4    - DC Bias level
 * input5    - Initial Time Shift
 *
 ************************************************************************/
#define DSS_SQUAREWAVE2__ENABLE	(*(node->input[0]))
#define DSS_SQUAREWAVE2__AMP	(*(node->input[1]))
#define DSS_SQUAREWAVE2__T_OFF	(*(node->input[2]))
#define DSS_SQUAREWAVE2__T_ON	(*(node->input[3]))
#define DSS_SQUAREWAVE2__BIAS	(*(node->input[4]))
#define DSS_SQUAREWAVE2__SHIFT	(*(node->input[5]))

static void dss_squarewave2_step(node_description *node)
{
	struct dss_squarewave_context *context = node->context;
	double newphase;

	if(DSS_SQUAREWAVE2__ENABLE)
	{
		/* Establish trigger phase from time periods */
		context->trigger=(DSS_SQUAREWAVE2__T_OFF / (DSS_SQUAREWAVE2__T_OFF + DSS_SQUAREWAVE2__T_ON)) * (2.0 * M_PI);

		/* Work out the phase step based on phase/freq & sample rate */
		/* The enable input only curtails output, phase rotation     */
		/* still occurs                                              */

		/*     phase step = 2Pi/(output period/sample period)        */
		/*                    boils out to                           */
		/*     phase step = 2Pi/(output period*sample freq)          */
		newphase = context->phase + ((2.0 * M_PI) / ((DSS_SQUAREWAVE2__T_OFF + DSS_SQUAREWAVE2__T_ON) * discrete_current_context->sample_rate));
		/* Keep the new phasor in the 2Pi range.*/
		context->phase = fmod(newphase, 2.0 * M_PI);

		if(context->phase>context->trigger)
			node->output[0]=(DSS_SQUAREWAVE2__AMP/2.0);
		else
			node->output[0]=-(DSS_SQUAREWAVE2__AMP/2.0);

		/* Add DC Bias component */
		node->output[0] = node->output[0] + DSS_SQUAREWAVE2__BIAS;
	}
	else
	{
		node->output[0]=0;
	}
}

static void dss_squarewave2_reset(node_description *node)
{
	struct dss_squarewave_context *context = node->context;
	double start;

	/* Establish starting phase, convert from degrees to radians */
	/* Only valid if we have set the on/off time                 */
	if((DSS_SQUAREWAVE2__T_OFF + DSS_SQUAREWAVE2__T_ON) != 0.0)
		start = (DSS_SQUAREWAVE2__SHIFT / (DSS_SQUAREWAVE2__T_OFF + DSS_SQUAREWAVE2__T_ON)) * (2.0 * M_PI);
	else
		start = 0.0;
	/* Make sure its always mod 2Pi */
	context->phase = fmod(start, 2.0 * M_PI);

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

/************************************************************************
 *
 * DSS_INVERTER_OSC - Usage of node_description values
 *
 * input0    - Enable input value
 * input1    - RC Resistor
 * input2    - RP Resistor
 * input3    - C Capacitor
 * input4    - Desc
 *
 ************************************************************************/
#define DSS_INVERTER_OSC__ENABLE	(*(node->input[0]))
#define DSS_INVERTER_OSC__MOD		(*(node->input[1]))
#define DSS_INVERTER_OSC__RC		(*(node->input[2]))
#define DSS_INVERTER_OSC__RP		(*(node->input[3]))
#define DSS_INVERTER_OSC__C			(*(node->input[4]))
#define DSS_INVERTER_OSC__R2		(*(node->input[5]))

INLINE double dss_inverter_tftab(node_description *node, double x)
{
	const discrete_inverter_osc_desc *info = node->custom;
	struct dss_inverter_osc_context *context = node->context;

	x = x / info->vB;
	if (x>0)
	 	return info->vB * exp(-context->tf_a * pow(x,context->tf_b));
	else
		return info->vB;
}

INLINE double dss_inverter_tf(node_description *node, double x)
{
	const discrete_inverter_osc_desc *info = node->custom;
	struct dss_inverter_osc_context *context = node->context;
	if (x < 0.0)
		return info->vB;
	else if (x <= info->vB)
		return context->tf_tab[(int)((double)(DSS_INV_TAB_SIZE-1) * x / info->vB)];
	else 
		return context->tf_tab[DSS_INV_TAB_SIZE-1];
}

static void dss_inverter_osc_step(node_description *node)
{
	struct dss_inverter_osc_context *context = node->context;
	const discrete_inverter_osc_desc *info = node->custom;
	double diff,vG1,vG2, vG3, vI;
	double vMix, rMix;

	/* Get new state */
	vI = context->vCap + context->vG2_old;
	switch (info->options & DISC_OSC_INVERTER_TYPE_MASK)
	{
		case DISC_OSC_INVERTER_IS_TYPE1:
		case DISC_OSC_INVERTER_IS_TYPE3:
			vG1 = dss_inverter_tf(node,vI);
			vG2 = dss_inverter_tf(node,vG1);
			vG3 = dss_inverter_tf(node,vG2);
			break;
		case DISC_OSC_INVERTER_IS_TYPE2:
			vG1 = 0;
			vG3 = dss_inverter_tf(node,vI);
			vG2 = dss_inverter_tf(node,vG3);
			break;
		case DISC_OSC_INVERTER_IS_TYPE4:
			vI = MIN(DSS_INVERTER_OSC__ENABLE, vI + 0.7);
			vG1 = 0;
			vG3 = dss_inverter_tf(node,vI);
			vG2 = dss_inverter_tf(node,vG3);
			break;
		case DISC_OSC_INVERTER_IS_TYPE5:
			vI = MAX(DSS_INVERTER_OSC__ENABLE, vI - 0.7);
			vG1 = 0;
			vG3 = dss_inverter_tf(node,vI);
			vG2 = dss_inverter_tf(node,vG3);
			break;
		default:
			fatalerror("DISCRETE_INVERTER_OSC - Wrong type on NODE_%02d", node->node - NODE_00);
	}
	switch (info->options & DISC_OSC_INVERTER_TYPE_MASK)
	{
		case DISC_OSC_INVERTER_IS_TYPE1:
		case DISC_OSC_INVERTER_IS_TYPE2:
		case DISC_OSC_INVERTER_IS_TYPE3:
			if ((info->clamp >= 0.0) && ((vI< - info->clamp) || (vI> info->vB+info->clamp)))
			{
				vI = MAX(vI, (- info->clamp));
				vI = MIN(vI, info->vB + info->clamp);
				diff = vG3 * (context->Rp / (context->Rp + context->R1))
				     - (context->vCap + vG2)
				     + vI*(context->R1 / (context->Rp + context->R1));
				diff = diff - diff * context->wc;
			}
			else
			{
				diff = vG3 - (context->vCap + vG2);
				diff = diff - diff * context->w;
			}
			break;
		case DISC_OSC_INVERTER_IS_TYPE4:
			if ((info->clamp >= 0.0) && ((vI< - info->clamp) || (vI> info->vB+info->clamp)))
			{
				vI = MAX(vI, (- info->clamp));
				vI = MIN(vI, info->vB + info->clamp);
			}
			// FIXME handle R2 = 0
			rMix = (context->R1 * context->R2) / (context->R1 + context->R2);
			vMix = rMix* ((vG3-vG2) / context->R1 + (DSS_INVERTER_OSC__MOD-vG2) / context->R2);
			if (vMix < (vI-vG2-0.7))
			{
				rMix = 1.0/rMix + 1.0/context->Rp;
				rMix = 1.0 / rMix;
				vMix = rMix* ( (vG3-vG2) / context->R1 + (DSS_INVERTER_OSC__MOD-vG2) / context->R2 + (vI-0.7-vG2)/context->Rp);
			}
			diff = vMix - context->vCap;
			diff = diff - diff * exp(-discrete_current_context->sample_time/(context->C * rMix));
			break;
		case DISC_OSC_INVERTER_IS_TYPE5:
			if ((info->clamp >= 0.0) && ((vI< - info->clamp) || (vI> info->vB+info->clamp)))
			{
				vI = MAX(vI, (- info->clamp));
				vI = MIN(vI, info->vB + info->clamp);
			}
			// FIXME handle R2 = 0
			rMix = (context->R1 * context->R2) / (context->R1 + context->R2);
			vMix = rMix* ((vG3-vG2) / context->R1 + (DSS_INVERTER_OSC__MOD-vG2) / context->R2);
			if (vMix > (vI-vG2+0.7))
			{
				rMix = 1.0/rMix + 1.0/context->Rp;
				rMix = 1.0 / rMix;
				vMix = rMix* ( (vG3-vG2) / context->R1 + (DSS_INVERTER_OSC__MOD-vG2) / context->R2 + (vI+0.7-vG2)/context->Rp);
			}
			diff = vMix - context->vCap;
			diff = diff - diff * exp(-discrete_current_context->sample_time/(context->C * rMix));
			break;
		default:
			fatalerror("DISCRETE_INVERTER_OSC - Wrong type on NODE_%02d", node->node - NODE_00);
	}
	context->vCap += diff;
	context->vG2_old = vG2;
	if ((info->options & DISC_OSC_INVERTER_TYPE_MASK)==DISC_OSC_INVERTER_IS_TYPE3)
		node->output[0] = vG1;
	else
		node->output[0] = vG3;
	if (info->options & DISC_OSC_INVERTER_OUT_IS_LOGIC)
		node->output[0] = (node->output[0] > info->vInFall);
}

static void dss_inverter_osc_reset(node_description *node)
{
	struct dss_inverter_osc_context *context = node->context;
	const discrete_inverter_osc_desc *info = node->custom;
	int i;

	/* exponent */
	context->w = exp(-discrete_current_context->sample_time / (DSS_INVERTER_OSC__RC * DSS_INVERTER_OSC__C));
	context->wc = exp(-discrete_current_context->sample_time / ((DSS_INVERTER_OSC__RC * DSS_INVERTER_OSC__RP) / (DSS_INVERTER_OSC__RP + DSS_INVERTER_OSC__RC) * DSS_INVERTER_OSC__C));
	node->output[0] = 0;
	context->vCap = 0;
	context->vG2_old = 0;
	context->Rp = DSS_INVERTER_OSC__RP;
	context->R1 = DSS_INVERTER_OSC__RC;
	context->R2 = DSS_INVERTER_OSC__R2;
	context->C = DSS_INVERTER_OSC__C;
	context->tf_b = (log(0.0 - log(info->vOutLow/info->vB)) - log(0.0 - log((info->vOutHigh/info->vB))) ) / log(info->vInRise / info->vInFall);
	context->tf_a = log(0.0 - log(info->vOutLow/info->vB)) - context->tf_b * log(info->vInRise/info->vB);
	context->tf_a = exp(context->tf_a);

	for (i = 0; i < DSS_INV_TAB_SIZE; i++)
	{
		context->tf_tab[i] = dss_inverter_tftab(node, (double) i / (double)(DSS_INV_TAB_SIZE-1) * info->vB);
	}
}

/************************************************************************
 *
 * DSS_TRIANGLEWAVE - Usage of node_description values for step function
 *
 * input0    - Enable input value
 * input1    - Frequency input value
 * input2    - Amplitde input value
 * input3    - DC Bias value
 * input4    - Initial Phase
 *
 ************************************************************************/
#define DSS_TRIANGLEWAVE__ENABLE	(*(node->input[0]))
#define DSS_TRIANGLEWAVE__FREQ		(*(node->input[1]))
#define DSS_TRIANGLEWAVE__AMP		(*(node->input[2]))
#define DSS_TRIANGLEWAVE__BIAS		(*(node->input[3]))
#define DSS_TRIANGLEWAVE__PHASE		(*(node->input[4]))

static void dss_trianglewave_step(node_description *node)
{
	struct dss_trianglewave_context *context = node->context;

	if(DSS_TRIANGLEWAVE__ENABLE)
	{
		node->output[0]=context->phase < M_PI ? (DSS_TRIANGLEWAVE__AMP * (context->phase / (M_PI/2.0) - 1.0))/2.0 :
									(DSS_TRIANGLEWAVE__AMP * (3.0 - context->phase / (M_PI/2.0)))/2.0 ;

		/* Add DC Bias component */
		node->output[0]=node->output[0]+DSS_TRIANGLEWAVE__BIAS;
	}
	else
	{
		node->output[0]=0;
	}

	/* Work out the phase step based on phase/freq & sample rate */
	/* The enable input only curtails output, phase rotation     */
	/* still occurs                                              */
	/*     phase step = 2Pi/(output period/sample period)        */
	/*                    boils out to                           */
	/*     phase step = (2Pi*output freq)/sample freq)           */
	/* Also keep the new phasor in the 2Pi range.                */
	context->phase=fmod((context->phase+((2.0*M_PI*DSS_TRIANGLEWAVE__FREQ)/discrete_current_context->sample_rate)),2.0*M_PI);
}

static void dss_trianglewave_reset(node_description *node)
{
	struct dss_trianglewave_context *context = node->context;
	double start;

	/* Establish starting phase, convert from degrees to radians */
	start=(DSS_TRIANGLEWAVE__PHASE/360.0)*(2.0*M_PI);
	/* Make sure its always mod 2Pi */
	context->phase=fmod(start,2.0*M_PI);

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


/************************************************************************
 *
 * DSS_ADSR - Attack Decay Sustain Release
 *
 * input0    - Enable input value
 * input1    - Trigger value
 * input2    - gain scaling factor
 *
 ************************************************************************/
#define DSS_ADSR__ENABLE	(*(node->input[0]))

static void dss_adsrenv_step(node_description *node)
{
//  struct dss_adsr_context *context = node->context;

	if(DSS_ADSR__ENABLE)
	{
		node->output[0]=0;
	}
	else
	{
		node->output[0]=0;
	}
}


static void dss_adsrenv_reset(node_description *node)
{
	dss_adsrenv_step(node);
}