summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/nl_base.h
blob: 1664d98b54eed6925fc8084701bf581bbe1ec31d (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
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nlbase.h
 *
 *  A mixed signal circuit simulation
 *
 *  D: Device
 *  O: Rail output (output)
 *  I: Infinite impedance input (input)
 *  T: Terminal (finite impedance)
 *
 *  +---+     +---+     +---+     +---+     +---+
 *  |   |     |   |     |   |     |   |     |   |
 *  | D |     | D |     | D |     | D |     | D |
 *  |   |     |   |     |   |     |   |     |   |
 *  +-O-+     +-I-+     +-I-+     +-T-+     +-T-+
 *    |         |         |         |         |
 *  +-+---------+---------+---------+---------+-+
 *  | rail net                                  |
 *  +-------------------------------------------+
 *
 *  A rail net is a net which is driven by exactly one output with an (idealized) internal resistance
 *  of zero. Ideally, it can deliver infinite current.
 *
 *  A infinite resistance input does not source or sink current.
 *
 *  Terminals source or sink finite (but never zero) current.
 *
 *  The system differentiates between analog and logic input and outputs and analog terminals.
 *  Analog and logic devices can not be connected to the same net. Instead, proxy devices
 *  are inserted automatically:
 *
 *  +---+     +---+
 *  |   |     |   |
 *  | D1|     | D2|
 *  | A |     | L |
 *  +-O-+     +-I-+
 *    |         |
 *  +-+---------+---+
 *  | rail net      |
 *  +---------------+
 *
 *  is converted into
 *              +----------+
 *              |          |
 *  +---+     +-+-+        |   +---+
 *  |   |     | L |  A-L   |   |   |
 *  | D1|     | D | Proxy  |   | D2|
 *  | A |     | A |        |   |   |
 *  +-O-+     +-I-+        |   +-I-+
 *    |         |          |     |
 *  +-+---------+--+     +-+-----+-------+
 *  | rail net (A) |     | rail net (L)  |
 *  +--------------|     +---------------+
 *
 *  This works both analog to logic as well as logic to analog.
 *
 *  The above is an advanced implementation of the existing discrete
 *  subsystem in MAME. Instead of relying on a fixed time-step, analog devices could
 *  either connect to fixed time-step clock or use an internal clock to update them.
 *  This would however introduce macro devices for RC, diodes and transistors again.
 *
 *  ====================================================================================
 *
 *  Instead, the following approach in case of a pure terminal/input network is taken:
 *
 *  +---+     +---+     +---+     +---+     +---+
 *  |   |     |   |     |   |     |   |     |   |
 *  | D |     | D |     | D |     | D |     | D |
 *  |   |     |   |     |   |     |   |     |   |
 *  +-T-+     +-I-+     +-I-+     +-T-+     +-T-+
 *    |         |         |         |         |
 *   '+'        |         |        '-'       '-'
 *  +-+---------+---------+---------+---------+-+
 *  | Calculated net                            |
 *  +-------------------------------------------+
 *
 *  SPICE uses the following basic two terminal device:
 *
 *       (k)
 *  +-----T-----+
 *  |     |     |
 *  |  +--+--+  |
 *  |  |     |  |
 *  |  R     |  |
 *  |  R     |  |
 *  |  R     I  |
 *  |  |     I  |  Device n
 *  |  V+    I  |
 *  |  V     |  |
 *  |  V-    |  |
 *  |  |     |  |
 *  |  +--+--+  |
 *  |     |     |
 *  +-----T-----+
 *       (l)
 *
 *  This is a resistance in series to a voltage source and paralleled by a current source.
 *  This is suitable to model voltage sources, current sources, resistors, capacitors,
 *  inductances and diodes.
 *
 *  I(n,l) = - I(n,k) = ( V(k) - V - V(l) ) * (1/R(n)) + I(n)
 *
 *  Now, the sum of all currents for a given net must be 0:
 *
 *  Sum(n,I(n,l)) = 0 = sum(n, ( V(k) - V(n) - V(l) ) * (1/R(n)) + I(n) )
 *
 *  With G(n) = 1 / R(n) and sum(n, G(n)) = Gtot and k=k(n)
 *
 *  0 = - V(l) * Gtot + sum(n, (V(k(n)) - V(n)) * G(n) + I(n))
 *
 *  and with l=l(n) and fixed k
 *
 *  0 =  -V(k) * Gtot + sum(n, ( V(l(n) + V(n) ) * G(n) - I(n))
 *
 *  These equations represent a linear Matrix equation (with more math).
 *
 *  In the end the solution of the analog subsystem boils down to
 *
 *  (G - D) * V = I
 *
 *  with G being the conductance matrix, D a diagonal matrix with the total conductance
 *  on the diagonal elements, V the net voltage vector and I the current vector.
 *
 *  By using solely two terminal devices, we can simplify the whole calculation
 *  significantly. A BJT now is a four terminal device with two terminals being
 *  connected internally.
 *
 *  The system is solved using an iterative approach:
 *
 *  G * V - D * V = I
 *
 *  assuming V=Vn=Vo
 *
 *  Vn = D-1 * (I - G * Vo)
 *
 *  Each terminal thus has three properties:
 *
 *  a) Resistance
 *  b) Voltage source
 *  c) Current source/sink
 *
 *  Going forward, the approach can be extended e.g. to use a linear equation solver
 *
 *  The formal representation of the circuit will stay the same, thus scales.
 *
 */

#ifndef NLBASE_H_
#define NLBASE_H_

#include "nl_config.h"
#include "nl_lists.h"
#include "nl_time.h"
#include "nl_util.h"
#include "pstring.h"
#include "pstate.h"

// ----------------------------------------------------------------------------------------
// Type definitions
// ----------------------------------------------------------------------------------------

class netlist_core_device_t;

#if USE_PMFDELEGATES
typedef void (*net_update_delegate)(netlist_core_device_t *);
#endif

//============================================================
//  MACROS / netlist devices
//============================================================

#define NETLIB_NAME(_chip) nld_ ## _chip

#define NETLIB_NAME_STR_S(_s) # _s
#define NETLIB_NAME_STR(_chip) NETLIB_NAME_STR_S(nld_ ## _chip)

#define NETLIB_UPDATE(_chip) ATTR_HOT ATTR_ALIGN void NETLIB_NAME(_chip) :: update(void)
#define NETLIB_START(_chip) ATTR_COLD void NETLIB_NAME(_chip) :: start(void)
//#define NETLIB_CONSTRUCTOR(_chip) ATTR_COLD _chip :: _chip (netlist_setup_t &setup, const char *name)
//          : net_device_t(setup, name)

#define NETLIB_RESET(_chip) ATTR_COLD void NETLIB_NAME(_chip) :: reset(void)

#define NETLIB_UPDATE_PARAM(_chip) ATTR_HOT ATTR_ALIGN void NETLIB_NAME(_chip) :: update_param(void)
#define NETLIB_FUNC_VOID(_chip, _name, _params) ATTR_HOT ATTR_ALIGN void NETLIB_NAME(_chip) :: _name _params

#define NETLIB_UPDATE_TERMINALS() ATTR_HOT ATTR_ALIGN inline void update_terminals(void)
#define NETLIB_UPDATEI() ATTR_HOT ATTR_ALIGN inline void update(void)

#define NETLIB_DEVICE_BASE(_name, _pclass, _extra, _priv)                           \
	class _name : public _pclass                                                    \
	{                                                                               \
	public:                                                                         \
		_name()                                                                     \
		: _pclass()    { }                                                          \
	protected:                                                                      \
		_extra                                                                      \
		ATTR_HOT void update();                                                     \
		ATTR_HOT void start();                                                      \
		ATTR_HOT void reset();                                                      \
		_priv                                                                       \
	}

#define NETLIB_DEVICE_DERIVED(_name, _pclass, _priv)                                \
		NETLIB_DEVICE_BASE(NETLIB_NAME(_name), NETLIB_NAME(_pclass), , _priv)

#define NETLIB_DEVICE(_name, _priv)                                                 \
		NETLIB_DEVICE_BASE(NETLIB_NAME(_name), netlist_device_t, , _priv)

#define NETLIB_SUBDEVICE(_name, _priv)                                             \
	class NETLIB_NAME(_name) : public netlist_device_t                              \
	{                                                                               \
	public:                                                                         \
		NETLIB_NAME(_name) ()                                                       \
		: netlist_device_t()                                                        \
			{ }                                                                     \
	/*protected:*/                                                                  \
		ATTR_HOT void update();                                                     \
		ATTR_HOT void start();                                                      \
		ATTR_HOT void reset();                                                      \
	public:                                                                         \
		_priv                                                                       \
	}

#define NETLIB_DEVICE_WITH_PARAMS(_name, _priv)                                     \
		NETLIB_DEVICE_BASE(NETLIB_NAME(_name), netlist_device_t,                    \
			ATTR_HOT void update_param();                                           \
		, _priv)

#define NETLIB_DEVICE_WITH_PARAMS_DERIVED(_name, _pclass, _priv)                    \
		NETLIB_DEVICE_BASE(NETLIB_NAME(_name), NETLIB_NAME(_pclass),                \
			ATTR_HOT void update_param();                                           \
		, _priv)

// ----------------------------------------------------------------------------------------
// forward definitions
// ----------------------------------------------------------------------------------------

class netlist_net_t;
class netlist_output_t;
class netlist_param_t;
class netlist_setup_t;
class netlist_base_t;
class netlist_matrix_solver_t;
class NETLIB_NAME(gnd);
class NETLIB_NAME(solver);
class NETLIB_NAME(mainclock);
class NETLIB_NAME(base_d_to_a_proxy);

// ----------------------------------------------------------------------------------------
// netlist_output_family_t
// ----------------------------------------------------------------------------------------

struct netlist_logic_family_desc_t
{
	double m_low_thresh_V;
	double m_high_thresh_V;
	double m_low_V;
	double m_high_V;
	double m_R_low;
	double m_R_high;
};

/* Terminals inherit the family description from the netlist_device
 * The default is the ttl family, but any device can override the family.
 * For individual terminals, these can be overwritten as well.
 *
 * Only devices of type GENERIC should have a family description entry
 */

extern netlist_logic_family_desc_t netlist_family_ttl;

// ----------------------------------------------------------------------------------------
// netlist_object_t
// ----------------------------------------------------------------------------------------

class netlist_object_t
{
	NETLIST_PREVENT_COPYING(netlist_object_t)
public:
	enum type_t {
		TERMINAL = 0,
		INPUT    = 1,
		OUTPUT   = 2,
		PARAM    = 3,
		NET      = 4,
		DEVICE   = 5,
		NETLIST   = 6,
	};
	enum family_t {
		// Terminal families
		LOGIC     = 1,
		ANALOG    = 2,
		// Device families
		GENERIC   = 3,   // <== devices usually fall into this category
		RESISTOR  = 4,   // Resistor
		CAPACITOR = 5,   // Capacitor
		DIODE     = 6,   // Diode
		BJT_SWITCH = 7,  // BJT(Switch)
		VCVS       = 8,  // Voltage controlled voltage source
		VCCS       = 9,  // Voltage controlled current source
		BJT_EB     = 10, // BJT(Ebers-Moll)
		GND        = 11, // GND device
	};

	ATTR_COLD netlist_object_t(const type_t atype, const family_t afamily);

	virtual ~netlist_object_t();

	ATTR_COLD void init_object(netlist_base_t &nl, const pstring &aname);
	ATTR_COLD bool isInitialized() { return (m_netlist != NULL); }

	ATTR_COLD const pstring name() const;

	PSTATE_INTERFACE_DECL()

	ATTR_HOT inline const type_t type() const { return m_objtype; }
	ATTR_HOT inline const family_t family() const { return m_family; }

	ATTR_HOT inline const bool isType(const type_t atype) const { return (m_objtype == atype); }
	ATTR_HOT inline const bool isFamily(const family_t afamily) const { return (m_family == afamily); }

	ATTR_HOT inline netlist_base_t & RESTRICT netlist() { return *m_netlist; }
	ATTR_HOT inline const netlist_base_t & RESTRICT netlist() const { return *m_netlist; }

	ATTR_COLD void inline do_reset()
	{
		reset();
	}

protected:

	ATTR_COLD virtual void reset() = 0;
	// must call parent save_register !
	ATTR_COLD virtual void save_register() { };

private:
	pstring m_name;
	const type_t m_objtype;
	const family_t m_family;
	netlist_base_t * RESTRICT m_netlist;
};

// ----------------------------------------------------------------------------------------
// netlist_owned_object_t
// ----------------------------------------------------------------------------------------

class netlist_owned_object_t : public netlist_object_t
{
	NETLIST_PREVENT_COPYING(netlist_owned_object_t)
public:
	ATTR_COLD netlist_owned_object_t(const type_t atype, const family_t afamily);

	ATTR_COLD void init_object(netlist_core_device_t &dev, const pstring &aname);

	ATTR_HOT inline netlist_core_device_t & RESTRICT netdev() const { return *m_netdev; }
private:
	netlist_core_device_t * RESTRICT m_netdev;
};

// ----------------------------------------------------------------------------------------
// netlist_core_terminal_t
// ----------------------------------------------------------------------------------------

class netlist_core_terminal_t : public netlist_owned_object_t, public plinked_list_element<netlist_core_terminal_t>
{
	NETLIST_PREVENT_COPYING(netlist_core_terminal_t)
public:

	typedef netlist_list_t<netlist_core_terminal_t *> list_t;

	/* needed here ... */

	enum state_e {
		STATE_INP_PASSIVE = 0,
		STATE_INP_ACTIVE = 1,
		STATE_INP_HL = 2,
		STATE_INP_LH = 4,
		STATE_OUT = 128,
		STATE_NONEX = 256
	};


	ATTR_COLD netlist_core_terminal_t(const type_t atype, const family_t afamily);

	//ATTR_COLD void init_object(netlist_core_device_t &dev, const pstring &aname);

	ATTR_COLD void set_net(netlist_net_t &anet);
	ATTR_COLD inline void clear_net() { m_net = NULL; }
	ATTR_HOT inline bool has_net() const { return (m_net != NULL); }
	ATTR_HOT inline const netlist_net_t & RESTRICT net() const { return *m_net;}
	ATTR_HOT inline netlist_net_t & RESTRICT net() { return *m_net;}

	ATTR_HOT inline const bool is_state(const state_e astate) const { return (m_state == astate); }
	ATTR_HOT inline const state_e state() const { return m_state; }
	ATTR_HOT inline void set_state(const state_e astate)
	{
		assert(astate != STATE_NONEX);
		m_state = astate;
	}

	const netlist_logic_family_desc_t *m_family_desc;

protected:
	ATTR_COLD virtual void save_register()
	{
		save(NAME(m_state));
		netlist_owned_object_t::save_register();
	}

private:
	netlist_net_t * RESTRICT m_net;
	state_e m_state;
};

NETLIST_SAVE_TYPE(netlist_core_terminal_t::state_e, DT_INT);


class netlist_terminal_t : public netlist_core_terminal_t
{
	NETLIST_PREVENT_COPYING(netlist_terminal_t)
public:
	ATTR_COLD netlist_terminal_t();

	double m_Idr; // drive current
	double m_go;  // conductance for Voltage from other term
	double m_gt;  // conductance for total conductance

	ATTR_HOT inline void set(const double G)
	{
		m_Idr = 0;
		m_go = m_gt = G;
	}

	ATTR_HOT inline void set(const double GO, const double GT)
	{
		m_Idr = 0;
		m_go = GO;
		m_gt = GT;
	}

	ATTR_HOT inline void set(const double GO, const double GT, const double I)
	{
		m_Idr = I;
		m_go = GO;
		m_gt = GT;
	}


	netlist_terminal_t *m_otherterm;

protected:
	ATTR_COLD virtual void save_register();

	ATTR_COLD virtual void reset();
};


// ----------------------------------------------------------------------------------------
// netlist_input_t
// ----------------------------------------------------------------------------------------

class netlist_input_t : public netlist_core_terminal_t
{
public:


	ATTR_COLD netlist_input_t(const type_t atype, const family_t afamily)
		: netlist_core_terminal_t(atype, afamily)
	{
		set_state(STATE_INP_ACTIVE);
	}

	ATTR_HOT inline void inactivate();
	ATTR_HOT inline void activate();
	ATTR_HOT inline void activate_hl();
	ATTR_HOT inline void activate_lh();

protected:
	ATTR_COLD virtual void reset()
	{
		//netlist_core_terminal_t::reset();
		set_state(STATE_INP_ACTIVE);
	}

private:
};

// ----------------------------------------------------------------------------------------
// netlist_logic_input_t
// ----------------------------------------------------------------------------------------

class netlist_logic_input_t : public netlist_input_t
{
public:
	ATTR_COLD netlist_logic_input_t()
		: netlist_input_t(INPUT, LOGIC)
	{
	}

	ATTR_HOT inline const netlist_sig_t Q() const;
	ATTR_HOT inline const netlist_sig_t last_Q() const;

};

// ----------------------------------------------------------------------------------------
// netlist_ttl_input_t
// ----------------------------------------------------------------------------------------

class netlist_ttl_input_t : public netlist_logic_input_t
{
public:
	ATTR_COLD netlist_ttl_input_t()
		: netlist_logic_input_t() { }
};

// ----------------------------------------------------------------------------------------
// netlist_analog_input_t
// ----------------------------------------------------------------------------------------

class netlist_analog_input_t : public netlist_input_t
{
public:
	ATTR_COLD netlist_analog_input_t()
		: netlist_input_t(INPUT, ANALOG) { }

	ATTR_HOT inline const double Q_Analog() const;
};

//#define INPVAL(_x) (_x).Q()

// ----------------------------------------------------------------------------------------
// net_net_t
// ----------------------------------------------------------------------------------------

class netlist_net_t : public netlist_object_t
{
	NETLIST_PREVENT_COPYING(netlist_net_t)
public:

	typedef netlist_list_t<netlist_net_t *> list_t;

	friend class NETLIB_NAME(mainclock);
	friend class netlist_matrix_solver_t;
	friend class netlist_logic_output_t;
	friend class netlist_analog_output_t;
	friend class netlist_setup_t;

	ATTR_COLD netlist_net_t(const type_t atype, const family_t afamily);
	ATTR_COLD virtual ~netlist_net_t();

	ATTR_COLD void init_object(netlist_base_t &nl, const pstring &aname);

	ATTR_COLD void register_con(netlist_core_terminal_t &terminal);
	ATTR_COLD void merge_net(netlist_net_t *othernet);
	ATTR_COLD void register_railterminal(netlist_output_t &mr);

	ATTR_HOT inline void update_devs();

	ATTR_HOT inline const netlist_time time() const { return m_time; }
	ATTR_HOT inline void set_time(const netlist_time ntime) { m_time = ntime; }

	ATTR_HOT inline bool isRailNet() const { return !(m_railterminal == NULL); }
	ATTR_HOT inline const netlist_core_terminal_t & RESTRICT  railterminal() const { return *m_railterminal; }

	/* Everything below is used by the logic subsystem */
	ATTR_HOT void inc_active(netlist_core_terminal_t &term);
	ATTR_HOT void dec_active(netlist_core_terminal_t &term);

	ATTR_HOT inline const netlist_sig_t Q() const
	{
		assert(family() == LOGIC);
		return m_cur_Q;
	}

	ATTR_HOT inline const netlist_sig_t last_Q() const
	{
		assert(family() == LOGIC);
		return m_last_Q;
	}

	ATTR_HOT inline const netlist_sig_t new_Q() const
	{
		assert(family() == LOGIC);
		return m_new_Q;
	}

	ATTR_HOT inline const double Q_Analog() const
	{
		//assert(object_type(SIGNAL_MASK) == SIGNAL_ANALOG);
		assert(family() == ANALOG);
		return m_cur_Analog;
	}

	ATTR_HOT inline void push_to_queue(const netlist_time delay);
	ATTR_HOT bool inline is_queued() const { return m_in_queue == 1; }

	/* internal state support
	 * FIXME: get rid of this and implement export/import in MAME
	 */
	ATTR_COLD inline netlist_sig_t &Q_state_ptr()
	{
		assert(family() == LOGIC);
		return m_cur_Q;
	}

	ATTR_COLD inline double &Q_Analog_state_ptr()
	{
		//assert(object_type(SIGNAL_MASK) == SIGNAL_ANALOG);
		assert(family() == ANALOG);
		return m_cur_Analog;
	}

	ATTR_HOT inline int num_cons() const { return m_num_cons; }

	// m_terms is only used by analog subsystem
	typedef netlist_list_t<netlist_terminal_t *> terminal_list_t;

	terminal_list_t m_terms;
	terminal_list_t m_rails;
	netlist_matrix_solver_t *m_solver;
	netlist_core_terminal_t * RESTRICT m_railterminal;

	ATTR_HOT void schedule_solve();

	netlist_list_t<netlist_core_terminal_t *> m_core_terms; // save post-start m_list ...
	plinked_list<netlist_core_terminal_t> m_list_active;

	ATTR_COLD void rebuild_list();     /* rebuild m_list after a load */

protected:  //FIXME: needed by current solver code

	UINT16 m_num_cons;

	ATTR_COLD virtual void save_register();
	ATTR_COLD virtual void reset();


private:
	netlist_sig_t m_new_Q;
	netlist_sig_t m_cur_Q;
	netlist_sig_t m_last_Q;

	netlist_time m_time;
	INT32        m_active;
	UINT8        m_in_queue;    /* 0: not in queue, 1: in queue, 2: last was taken */

public:
	double m_last_Analog;
	double m_cur_Analog;
	double m_new_Analog;

};


// ----------------------------------------------------------------------------------------
// net_output_t
// ----------------------------------------------------------------------------------------

class netlist_output_t : public netlist_core_terminal_t
{
	NETLIST_PREVENT_COPYING(netlist_output_t)
public:

	ATTR_COLD netlist_output_t(const type_t atype, const family_t afamily);
	ATTR_COLD virtual ~netlist_output_t() {}

	ATTR_COLD void init_object(netlist_core_device_t &dev, const pstring &aname);
	ATTR_COLD virtual void reset()
	{
		//netlist_core_terminal_t::reset();
		set_state(STATE_OUT);
	}

private:
	netlist_net_t m_my_net;
};


class netlist_logic_output_t : public netlist_output_t
{
	NETLIST_PREVENT_COPYING(netlist_logic_output_t)
public:

	ATTR_COLD netlist_logic_output_t();

	ATTR_COLD void initial(const netlist_sig_t val);

	ATTR_COLD bool has_proxy() const { return (m_proxy != NULL); }
	ATTR_COLD nld_base_d_to_a_proxy *get_proxy() const  { return m_proxy; }
	ATTR_COLD void set_proxy(nld_base_d_to_a_proxy *proxy) { m_proxy = proxy; }

	ATTR_HOT inline void set_Q(const netlist_sig_t newQ, const netlist_time delay)
	{
		if (EXPECTED(newQ !=  net().m_new_Q))
		{
			net().m_new_Q = newQ;
			net().push_to_queue(delay);
		}
	}
private:
	nld_base_d_to_a_proxy *m_proxy;
};

class netlist_ttl_output_t : public netlist_logic_output_t
{
public:

	ATTR_COLD netlist_ttl_output_t();

};

class netlist_analog_output_t : public netlist_output_t
{
	NETLIST_PREVENT_COPYING(netlist_analog_output_t)
public:

	ATTR_COLD netlist_analog_output_t();

	ATTR_COLD void initial(const double val);

	ATTR_HOT void set_Q(const double newQ);

};

// ----------------------------------------------------------------------------------------
// net_param_t
// ----------------------------------------------------------------------------------------

class netlist_param_t : public netlist_owned_object_t
{
	NETLIST_PREVENT_COPYING(netlist_param_t)
public:

	enum param_type_t {
		MODEL,
		STRING,
		DOUBLE,
		INTEGER,
		LOGIC
	};

	ATTR_COLD netlist_param_t(const param_type_t atype);

	ATTR_HOT inline const param_type_t param_type() const { return m_param_type; }

protected:

	ATTR_COLD virtual void reset() { }

private:
	const param_type_t m_param_type;
};

class netlist_param_double_t : public netlist_param_t
{
	NETLIST_PREVENT_COPYING(netlist_param_double_t)
public:
	ATTR_COLD netlist_param_double_t();

	ATTR_HOT inline void setTo(const double param);
	ATTR_COLD inline void initial(const double val) { m_param = val; }
	ATTR_HOT inline const double Value() const        { return m_param;   }

protected:
	ATTR_COLD virtual void save_register()
	{
		save(NAME(m_param));
		netlist_param_t::save_register();
	}

private:
	double m_param;
};

class netlist_param_int_t : public netlist_param_t
{
	NETLIST_PREVENT_COPYING(netlist_param_int_t)
public:
	ATTR_COLD netlist_param_int_t();

	ATTR_HOT inline void setTo(const int param);
	ATTR_COLD inline void initial(const int val) { m_param = val; }

	ATTR_HOT inline const int Value() const     { return m_param;     }

protected:
	ATTR_COLD virtual void save_register()
	{
		save(NAME(m_param));
		netlist_param_t::save_register();
	}

private:
	int m_param;
};

class netlist_param_logic_t : public netlist_param_int_t
{
	NETLIST_PREVENT_COPYING(netlist_param_logic_t)
public:
	ATTR_COLD netlist_param_logic_t();
};

class netlist_param_str_t : public netlist_param_t
{
	NETLIST_PREVENT_COPYING(netlist_param_str_t)
public:
	ATTR_COLD netlist_param_str_t();

	ATTR_HOT inline void setTo(const pstring &param);
	ATTR_COLD inline void initial(const pstring &val) { m_param = val; }

	ATTR_HOT inline const pstring &Value() const     { return m_param;     }

private:
	pstring m_param;
};

class netlist_param_model_t : public netlist_param_t
{
	NETLIST_PREVENT_COPYING(netlist_param_model_t)
public:
	ATTR_COLD netlist_param_model_t();

	ATTR_COLD inline void initial(const pstring &val) { m_param = val; }

	ATTR_HOT inline const pstring &Value() const     { return m_param;     }

	/* these should be cached! */
	ATTR_COLD double model_value(const pstring &entity, const double defval = 0.0) const;
	ATTR_COLD const pstring model_type() const;

private:
	pstring m_param;
};

// ----------------------------------------------------------------------------------------
// net_device_t
// ----------------------------------------------------------------------------------------

class netlist_core_device_t : public netlist_object_t
{
	NETLIST_PREVENT_COPYING(netlist_core_device_t)
public:

	typedef netlist_list_t<netlist_core_device_t *> list_t;

	ATTR_COLD netlist_core_device_t(const family_t afamily, const netlist_logic_family_desc_t *family_desc);

	ATTR_COLD virtual ~netlist_core_device_t();

	ATTR_COLD virtual void init(netlist_base_t &anetlist, const pstring &name);
	ATTR_HOT virtual void update_param() {}

	ATTR_HOT inline void update_dev()
	{
#if USE_PMFDELEGATES
		static_update(this);
#else
		update();
#endif
	}
	ATTR_HOT inline void start_dev()
	{
		start();
	}

	ATTR_HOT const netlist_sig_t INPLOGIC_PASSIVE(netlist_logic_input_t &inp);

	ATTR_HOT inline const netlist_sig_t INPLOGIC(const netlist_logic_input_t &inp) const
	{
		assert(inp.state() != netlist_input_t::STATE_INP_PASSIVE);
		return inp.Q();
	}

	ATTR_HOT inline void OUTLOGIC(netlist_logic_output_t &out, const netlist_sig_t val, const netlist_time delay)
	{
		out.set_Q(val, delay);
	}

	ATTR_HOT inline bool INP_HL(const netlist_logic_input_t &inp) const
	{
		return ((inp.last_Q() & !inp.Q()) == 1);
	}

	ATTR_HOT inline bool INP_LH(const netlist_logic_input_t &inp) const
	{
		return ((!inp.last_Q() & inp.Q()) == 1);
	}

	ATTR_HOT inline const double INPANALOG(const netlist_analog_input_t &inp) const { return inp.Q_Analog(); }

	ATTR_HOT inline const double TERMANALOG(const netlist_terminal_t &term) const { return term.net().Q_Analog(); }

	ATTR_HOT inline void OUTANALOG(netlist_analog_output_t &out, const double val)
	{
		out.set_Q(val);
	}

	ATTR_HOT virtual void inc_active() {  }

	ATTR_HOT virtual void dec_active() {  }

	ATTR_HOT virtual void step_time(const double st) { }
	ATTR_HOT virtual void update_terminals() { }



#if (NL_KEEP_STATISTICS)
	/* stats */
	osd_ticks_t total_time;
	INT32 stat_count;
#endif

#if USE_PMFDELEGATES
	net_update_delegate static_update;
#endif

	const netlist_logic_family_desc_t *m_family_desc;

protected:

	ATTR_HOT virtual void update() { }
	ATTR_COLD virtual void start() { }

private:
};


class netlist_device_t : public netlist_core_device_t
{
	NETLIST_PREVENT_COPYING(netlist_device_t)
public:

	ATTR_COLD netlist_device_t();
	ATTR_COLD netlist_device_t(const family_t afamily);

	ATTR_COLD virtual ~netlist_device_t();

	ATTR_COLD virtual void init(netlist_base_t &anetlist, const pstring &name);

	ATTR_COLD netlist_setup_t &setup();

	ATTR_COLD void register_sub(netlist_device_t &dev, const pstring &name);
	ATTR_COLD void register_subalias(const pstring &name, netlist_core_terminal_t &term);
	ATTR_COLD void register_terminal(const pstring &name, netlist_terminal_t &port);
	ATTR_COLD void register_output(const pstring &name, netlist_output_t &out);
	ATTR_COLD void register_input(const pstring &name, netlist_input_t &in);

	ATTR_COLD void connect(netlist_core_terminal_t &t1, netlist_core_terminal_t &t2);

	netlist_list_t<pstring, 20> m_terminals;

protected:

	ATTR_HOT virtual void update() { }
	ATTR_HOT virtual void start() { }
	ATTR_HOT virtual void update_terminals() { }

	template <class C, class T>
	ATTR_COLD void register_param(const pstring &sname, C &param, const T initialVal);

private:
};


// ----------------------------------------------------------------------------------------
// netlist_queue_t
// ----------------------------------------------------------------------------------------

class netlist_queue_t : public netlist_timed_queue<netlist_net_t *, netlist_time, 512>,
						public pstate_callback_t
{
public:

	netlist_queue_t(netlist_base_t &nl);

	void register_state(pstate_manager_t &manager, const pstring &module);
	void on_pre_save();
	void on_post_load();

	pstate_callback_t &callback() { return *this; }

private:
	netlist_base_t &m_netlist;
	int m_qsize;
	netlist_time::INTERNALTYPE m_times[512];
	char m_name[512][64];
};

// ----------------------------------------------------------------------------------------
// netlist_base_t
// ----------------------------------------------------------------------------------------


typedef tagmap_t<netlist_device_t *, 393> tagmap_devices_t;

class netlist_base_t : public netlist_object_t, public pstate_manager_t
{
	NETLIST_PREVENT_COPYING(netlist_base_t)
public:

	netlist_base_t();
	virtual ~netlist_base_t();

	ATTR_COLD void start();

	ATTR_HOT inline const netlist_queue_t &queue() const { return m_queue; }
	ATTR_HOT inline netlist_queue_t &queue() { return m_queue; }
	ATTR_HOT inline const netlist_time time() const { return m_time; }
	ATTR_HOT inline NETLIB_NAME(solver) *solver() const { return m_solver; }
	ATTR_HOT inline NETLIB_NAME(gnd) *gnd() const { return m_gnd; }
	ATTR_HOT const double gmin() const;

	ATTR_HOT inline void push_to_queue(netlist_net_t *out, const netlist_time attime)
	{
		m_queue.push(netlist_queue_t::entry_t(attime, out));
	}

	ATTR_HOT void process_queue(const netlist_time delta);
	ATTR_HOT inline void abort_current_queue_slice() { m_stop = netlist_time::zero; }

	ATTR_COLD void rebuild_lists(); /* must be called after post_load ! */

	ATTR_COLD void set_setup(netlist_setup_t *asetup) { m_setup = asetup;  }
	ATTR_COLD netlist_setup_t &setup() { return *m_setup; }

	ATTR_COLD netlist_net_t *find_net(const pstring &name);

	ATTR_COLD void error(const char *format, ...) const ATTR_PRINTF(2,3);
	ATTR_COLD void warning(const char *format, ...) const ATTR_PRINTF(2,3);
	ATTR_COLD void log(const char *format, ...) const ATTR_PRINTF(2,3);

	template<class _C>
	netlist_list_t<_C *> get_device_list()
	{
		netlist_list_t<_C *> tmp;
		for (tagmap_devices_t::entry_t *entry = m_devices.first(); entry != NULL; entry = m_devices.next(entry))
		{
			_C *dev = dynamic_cast<_C *>(entry->object());
			if (dev != NULL)
				tmp.add(dev);
		}
		return tmp;
	}

	template<class _C>
	_C *get_first_device()
	{
		for (tagmap_devices_t::entry_t *entry = m_devices.first(); entry != NULL; entry = m_devices.next(entry))
		{
			_C *dev = dynamic_cast<_C *>(entry->object());
			if (dev != NULL)
				return dev;
		}
		return NULL;
	}

	template<class _C>
	_C *get_single_device(const char *classname)
	{
		_C *ret = NULL;
		for (tagmap_devices_t::entry_t *entry = m_devices.first(); entry != NULL; entry = m_devices.next(entry))
		{
			_C *dev = dynamic_cast<_C *>(entry->object());
			if (dev != NULL)
			{
				if (ret != NULL)
					this->error("more than one %s device found", classname);
				else
					ret = dev;
			}
		}
		return ret;
	}

	tagmap_devices_t m_devices;
	netlist_net_t::list_t m_nets;

protected:

	enum loglevel_e
	{
		NL_ERROR,
		NL_WARNING,
		NL_LOG,
	};

	// any derived netlist must override this ...
	ATTR_COLD virtual void verror(const loglevel_e level,
			const char *format, va_list ap) const = 0;

	/* from netlist_object */
	ATTR_COLD virtual void reset();
	ATTR_COLD virtual void save_register();

#if (NL_KEEP_STATISTICS)
	// performance
	int m_perf_out_processed;
	int m_perf_inp_processed;
	int m_perf_inp_active;
#endif

private:
	netlist_time                m_stop;     // target time for current queue processing

	netlist_time                m_time;
	netlist_queue_t             m_queue;

	NETLIB_NAME(mainclock) *    m_mainclock;
	NETLIB_NAME(solver) *       m_solver;
	NETLIB_NAME(gnd) *          m_gnd;

	netlist_setup_t *m_setup;
};

// ----------------------------------------------------------------------------------------
// Inline implementations
// ----------------------------------------------------------------------------------------

PSTATE_INTERFACE(netlist_object_t, m_netlist, name())

ATTR_HOT inline void netlist_param_str_t::setTo(const pstring &param)
{
	m_param = param;
	netdev().update_param();
}

ATTR_HOT inline void netlist_param_int_t::setTo(const int param)
{
	if (m_param != param)
	{
		m_param = param;
		netdev().update_param();
	}
}

ATTR_HOT inline void netlist_param_double_t::setTo(const double param)
{
	if (m_param != param)
	{
		m_param = param;
		netdev().update_param();
	}
}


ATTR_HOT inline void netlist_input_t::inactivate()
{
	if (EXPECTED(!is_state(STATE_INP_PASSIVE)))
	{
		set_state(STATE_INP_PASSIVE);
		net().dec_active(*this);
	}
}

ATTR_HOT inline void netlist_input_t::activate()
{
	if (is_state(STATE_INP_PASSIVE))
	{
		net().inc_active(*this);
		set_state(STATE_INP_ACTIVE);
	}
}

ATTR_HOT inline void netlist_input_t::activate_hl()
{
	if (is_state(STATE_INP_PASSIVE))
	{
		net().inc_active(*this);
		set_state(STATE_INP_HL);
	}
}

ATTR_HOT inline void netlist_input_t::activate_lh()
{
	if (is_state(STATE_INP_PASSIVE))
	{
		net().inc_active(*this);
		set_state(STATE_INP_LH);
	}
}


ATTR_HOT inline void netlist_net_t::push_to_queue(const netlist_time delay)
{
	//if (UNEXPECTED(m_num_cons == 0 || is_queued()))
	if (!is_queued())
	{
		m_time = netlist().time() + delay;
		m_in_queue = (m_active > 0);     /* queued ? */
		if (EXPECTED(m_in_queue))
		{
			netlist().push_to_queue(this, m_time);
		}
	}
}


ATTR_HOT inline const netlist_sig_t netlist_logic_input_t::Q() const
{
	return net().Q();
}

ATTR_HOT inline const netlist_sig_t netlist_logic_input_t::last_Q() const
{
	return net().last_Q();
}

ATTR_HOT inline const double netlist_analog_input_t::Q_Analog() const
{
	return net().Q_Analog();
}


// ----------------------------------------------------------------------------------------
// net_dev class factory
// ----------------------------------------------------------------------------------------

class net_device_t_base_factory
{
	NETLIST_PREVENT_COPYING(net_device_t_base_factory)
public:
	ATTR_COLD net_device_t_base_factory(const pstring &name, const pstring &classname,
			const pstring &def_param)
	: m_name(name), m_classname(classname), m_def_param(def_param)
	{}

	ATTR_COLD virtual ~net_device_t_base_factory() {}

	ATTR_COLD virtual netlist_device_t *Create() const = 0;

	ATTR_COLD const pstring &name() const { return m_name; }
	ATTR_COLD const pstring &classname() const { return m_classname; }
	ATTR_COLD const pstring &param_desc() const { return m_def_param; }
	ATTR_COLD const nl_util::pstring_list term_param_list();
	ATTR_COLD const nl_util::pstring_list def_params();

protected:
	pstring m_name;                             /* device name */
	pstring m_classname;                        /* device class name */
	pstring m_def_param;                        /* default parameter */
};

template <class C>
class net_device_t_factory : public net_device_t_base_factory
{
	NETLIST_PREVENT_COPYING(net_device_t_factory)
public:
	ATTR_COLD net_device_t_factory(const pstring &name, const pstring &classname,
			const pstring &def_param)
	: net_device_t_base_factory(name, classname, def_param) { }

	ATTR_COLD netlist_device_t *Create() const
	{
		netlist_device_t *r = new C();
		//r->init(setup, name);
		return r;
	}
};

class netlist_factory_t
{
public:
	typedef netlist_list_t<net_device_t_base_factory *> list_t;

	ATTR_COLD netlist_factory_t();
	ATTR_COLD ~netlist_factory_t();

	ATTR_COLD void initialize();

	template<class _C>
	ATTR_COLD void register_device(const pstring &name, const pstring &classname,
			const pstring &def_param)
	{
		m_list.add(new net_device_t_factory< _C >(name, classname, def_param) );
	}

	ATTR_COLD netlist_device_t *new_device_by_classname(const pstring &classname, netlist_setup_t &setup) const;
	ATTR_COLD netlist_device_t *new_device_by_name(const pstring &name, netlist_setup_t &setup) const;
	ATTR_COLD net_device_t_base_factory * factory_by_name(const pstring &name, netlist_setup_t &setup) const;

	const list_t &list() { return m_list; }

private:
	list_t m_list;

};


#endif /* NLBASE_H_ */