summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/netlist.h
blob: 3530247e9a7bd4db016cae2546251393fb740dde (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
/***************************************************************************

    netlist.h

    Discrete netlist implementation.

****************************************************************************

    Couriersud reserves the right to license the code under a less restrictive
    license going forward.

    Copyright Nicola Salmoria and the MAME team
    All rights reserved.

    Redistribution and use of this code or any derivative works are permitted
    provided that the following conditions are met:

    * Redistributions may not be sold, nor may they be used in a commercial
    product or activity.

    * Redistributions that are modified from the original source must include the
    complete source code, including the source code for all components used by a
    binary built from the modified sources. However, as a special exception, the
    source code distributed need not include anything that is normally distributed
    (in either source or binary form) with the major components (compiler, kernel,
    and so on) of the operating system on which the executable runs, unless that
    component itself accompanies the executable.

    * Redistributions must reproduce the above copyright notice, this list of
    conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.


****************************************************************************/

#ifndef NETLIST_H
#define NETLIST_H

#include "emu.h"
#include "tagmap.h"

//============================================================
//  SETUP
//============================================================

#define USE_DELEGATES           (1)
#define USE_DELEGATES_A         (0)

#define NETLIST_CLOCK              (U64(1000000000))

#define NLTIME_FROM_NS(_t)  netlist_time::from_ns(_t)
#define NLTIME_FROM_US(_t)  netlist_time::from_us(_t)
#define NLTIME_FROM_MS(_t)  netlist_time::from_ms(_t)
#define NLTIME_IMMEDIATE    netlist_time::from_ns(0)

#define NETLIST_HIGHIMP_V   (1.23456e20)        /* some voltage we should never see */


//============================================================
//  MACROS / inline netlist definitions
//============================================================

#define NET_ALIAS(_alias, _name)                                                    \
	netlist.register_alias(# _alias, # _name);
#define NET_NEW(_type , _name)  net_create_device_by_classname(# _type, &netlist, # _name)

#define NET_REGISTER_DEV(_type, _name)                                              \
		netlist.register_dev(NET_NEW(_type, _name));
#define NET_REMOVE_DEV(_name)                                                       \
		netlist.remove_dev(# _name);
#define NET_REGISTER_SIGNAL(_type, _name)                                           \
		NET_REGISTER_DEV(_type ## _ ## sig, _name)
#define NET_CONNECT(_name, _input, _output)                                         \
		netlist.register_link(# _name "." # _input, # _output);
#define NETDEV_PARAM(_name, _val)                                                   \
		netlist.find_param(# _name).initial(_val);

#define NETLIST_NAME(_name) netlist ## _ ## _name

#define NETLIST_START(_name) \
ATTR_COLD void NETLIST_NAME(_name)(netlist_setup_t &netlist) \
{
#define NETLIST_END  }

#define NETLIST_INCLUDE(_name)                                                      \
		NETLIST_NAME(_name)(netlist);

#define NETLIST_MEMREGION(_name)                                                    \
		netlist.parse((char *)downcast<netlist_t &>(netlist.netlist()).machine().root_device().memregion(_name)->base());
#if defined(__GNUC__) && (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))
#if !defined(__ppc__) && !defined (__PPC__) && !defined(__ppc64__) && !defined(__PPC64__)
#define ATTR_ALIGN __attribute__ ((aligned(128)))
#else
#define ATTR_ALIGN
#endif
#else
#define ATTR_ALIGN
#endif

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

#define NETLIB_UPDATE(_chip) ATTR_HOT ATTR_ALIGN void _chip :: update(void)
#define NETLIB_START(_chip) ATTR_COLD ATTR_ALIGN void _chip :: start(void)
#define NETLIB_UPDATE_PARAM(_chip) ATTR_HOT ATTR_ALIGN void _chip :: update_param(void)
#define NETLIB_FUNC_VOID(_chip, _name) ATTR_HOT ATTR_ALIGN inline void _chip :: _name (void)

#define NETLIB_SIGNAL(_name, _num_input, _check)                                    \
	class _name : public net_signal_t<_num_input, _check>                           \
	{                                                                               \
	public:                                                                         \
		_name () : net_signal_t<_num_input, _check>() { }                           \
	};
#define NETLIB_DEVICE(_name, _priv)                                                 \
	class _name : public net_device_t                                               \
	{                                                                               \
	public:                                                                         \
		_name () : net_device_t() { }                                               \
		ATTR_HOT void update();                                                     \
		ATTR_COLD void start();                                                     \
	protected:                                                                      \
		_priv                                                                       \
	}
#define NETLIB_SUBDEVICE(_name, _priv)                                              \
	class _name : public net_core_device_t                                          \
	{                                                                               \
	public:                                                                         \
		_name () : net_core_device_t() { }                                          \
		ATTR_HOT void update();                                                     \
	/*protected:*/                                                                  \
		_priv                                                                       \
	}
#define NETLIB_DEVICE_WITH_PARAMS(_name, _priv)                                     \
	class _name : public net_device_t                                               \
	{                                                                               \
	public:                                                                         \
		_name () : net_device_t() { }                                               \
		ATTR_HOT void update_param();                                               \
		ATTR_HOT void update();                                                     \
		ATTR_COLD void start();                                                     \
	/* protected: */                                                                \
		_priv                                                                       \
	}
// MAME specific

#define MCFG_NETLIST_ADD(_tag, _setup )                                             \
	MCFG_DEVICE_ADD(_tag, NETLIST, NETLIST_CLOCK)                                   \
	MCFG_NETLIST_SETUP(_setup)
#define MCFG_NETLIST_REPLACE(_tag, _setup)                                          \
	MCFG_DEVICE_REPLACE(_tag, NETLIST, NETLIST_CLOCK)                               \
	MCFG_NETLIST_SETUP(_setup)
#define MCFG_NETLIST_SETUP(_setup)                                                  \
	netlist_mame_device::static_set_constructor(*device, NETLIST_NAME(_setup));

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


#if USE_DELEGATES || USE_DELEGATES_A
typedef delegate<void ()> net_update_delegate;
#endif

typedef UINT8 net_sig_t;

typedef delegate<void (const double)> net_output_delegate;

// ----------------------------------------------------------------------------------------
// Support classes
// ----------------------------------------------------------------------------------------

struct netlist_time
{
public:

	typedef UINT64 INTERNALTYPE;

	static const INTERNALTYPE RESOLUTION = U64(1024000000000);

	inline netlist_time() : m_time(0) {}

	friend netlist_time operator-(const netlist_time &left, const netlist_time &right);
	friend netlist_time operator+(const netlist_time &left, const netlist_time &right);
	friend netlist_time operator*(const netlist_time &left, const UINT64 factor);
	friend bool operator>(const netlist_time &left, const netlist_time &right);
	friend bool operator<(const netlist_time &left, const netlist_time &right);
	friend bool operator>=(const netlist_time &left, const netlist_time &right);
	friend bool operator<=(const netlist_time &left, const netlist_time &right);

	inline netlist_time &operator=(const netlist_time &right) { m_time = right.m_time; return *this; }
	inline netlist_time &operator+=(const netlist_time &right) { m_time += right.m_time; return *this; }

	inline const INTERNALTYPE as_raw() const { return m_time; }

	static inline const netlist_time from_ns(const int ns) { return netlist_time((UINT64) ns * RESOLUTION / U64(1000000000)); }
	static inline const netlist_time from_us(const int us) { return netlist_time((UINT64) us * RESOLUTION / U64(1000000)); }
	static inline const netlist_time from_ms(const int ms) { return netlist_time((UINT64) ms * RESOLUTION / U64(1000)); }
	static inline const netlist_time from_hz(const UINT64 hz) { return netlist_time(RESOLUTION / hz); }
	static inline const netlist_time from_raw(const INTERNALTYPE raw) { return netlist_time(raw); }

	static const netlist_time zero;

protected:

	inline netlist_time(const INTERNALTYPE val) : m_time(val) {}

	INTERNALTYPE m_time;
};

inline netlist_time operator-(const netlist_time &left, const netlist_time &right)
{
	return netlist_time::from_raw(left.m_time - right.m_time);
}

inline netlist_time operator*(const netlist_time &left, const UINT64 factor)
{
	return netlist_time::from_raw(left.m_time * factor);
}

inline netlist_time operator+(const netlist_time &left, const netlist_time &right)
{
	return netlist_time::from_raw(left.m_time + right.m_time);
}

inline bool operator<(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time < right.m_time);
}

inline bool operator>(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time > right.m_time);
}

inline bool operator<=(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time <= right.m_time);
}

inline bool operator>=(const netlist_time &left, const netlist_time &right)
{
	return (left.m_time >= right.m_time);
}

template <class _ListClass, int _NumElements>
struct net_list_t
{
public:
	net_list_t()
	{
		m_list = global_alloc_array(_ListClass, _NumElements);
		m_ptr = m_list;
		m_ptr--;
	}
	~net_list_t()
	{
		global_free(m_list);
	}
	ATTR_HOT inline void add(const _ListClass elem)
	{
		assert(m_ptr-m_list <= _NumElements - 1);

		*(++m_ptr) = elem;
	}
	ATTR_HOT inline _ListClass *first() const { return m_list; }
	ATTR_HOT inline _ListClass *last() const  { return m_ptr; }
	ATTR_HOT inline _ListClass item(int i) const { return m_list[i]; }
	inline int count() const { return m_ptr - m_list + 1; }
	ATTR_HOT inline bool empty() const { return (m_ptr < m_list); }
	ATTR_HOT inline void clear() { m_ptr = m_list - 1; }
private:
	_ListClass * m_ptr;
	//_ListClass m_list[_NumElements];
	_ListClass *m_list;
};

#define SIZE ((1 << _Size) - 1)

template <class _QC, int _Size>
class netlist_timed_queue
{
public:
	struct entry_t
	{
	public:
		inline entry_t() {}
		inline entry_t(netlist_time atime, _QC elem) : m_time(atime), m_object(elem) {}
		ATTR_HOT inline const netlist_time &time() const { return m_time; }
		ATTR_HOT inline _QC object() const { return m_object; }
	private:
		netlist_time m_time;
		_QC m_object;
	};


	netlist_timed_queue()
	{
		clear();
	}

	ATTR_HOT inline bool is_empty() { return ((m_start & SIZE) == (m_end & SIZE)); }
	ATTR_HOT inline bool is_not_empty() { return ((m_start & SIZE) != (m_end & SIZE)); }

	ATTR_HOT void push(const entry_t &e);

	ATTR_HOT inline entry_t &pop()
	{
		m_end--;
		return item(m_end);
	}

	ATTR_COLD void clear()
	{
		m_end = m_start = (1 << _Size) >> 1;
	}
private:
	ATTR_HOT inline entry_t &item(UINT32 x) { return m_list[x & SIZE]; }
	ATTR_HOT inline void set_item(UINT32 x, const entry_t &aitem) { m_list[x & SIZE] = aitem; }

	UINT32 m_start;
	UINT32 m_end;
	entry_t m_list[SIZE + 1];
};

template <class _QC, int _Size>
ATTR_HOT ATTR_ALIGN void netlist_timed_queue<_QC, _Size>::push(const entry_t &e)
{
	if (is_empty() || (e.time() <= item(m_end - 1).time()))
	{
		set_item(m_end,  e);
		m_end++;
	}
	else if (e.time() >= item(m_start).time())
	{
		m_start--;
		set_item(m_start, e);
	}
	else
	{
		register UINT32 i = m_end;
		register UINT32 j = i - 1;
		m_end++;
		while ((e.time() > item(j).time()))
		{
			set_item(i, item(j));
			i = j;
			j--;
		}
		set_item(i, e);
	}
}
// ----------------------------------------------------------------------------------------
// forward definitions
// ----------------------------------------------------------------------------------------

class net_output_t;
class net_core_device_t;
class net_param_t;
class netlist_setup_t;
class netlist_base_t;

// ----------------------------------------------------------------------------------------
// net_input_t
// ----------------------------------------------------------------------------------------

class net_object_t
{
public:
	enum type_t {
		INPUT = 0,
		OUTPUT = 1,
		DEVICE = 2,
		PARAM = 3,
		TYPE_MASK = 0x03,
		SIGNAL_DIGITAL = 0x00,
		SIGNAL_ANALOG =  0x10,
		SIGNAL_MASK =    0x10,
	};

	net_object_t(int atype)
		: m_objtype(atype) {}

	ATTR_HOT inline int object_type() const { return m_objtype; }
	ATTR_HOT inline int object_type(int mask) const { return m_objtype & mask; }

private:
	int m_objtype;
};


class net_input_t : public net_object_t
{
public:

	enum net_input_state {
		INP_STATE_PASSIVE = 0,
		INP_STATE_ACTIVE = 1,
		INP_STATE_HL = 2,
		INP_STATE_LH = 4,
	};

	net_input_t(const int atype) : net_object_t(atype), m_state(INP_STATE_ACTIVE) {}
	ATTR_COLD void init(net_core_device_t *dev,int astate = INP_STATE_ACTIVE);

	ATTR_HOT inline net_output_t * RESTRICT output() const { return m_output; }
	ATTR_HOT inline bool is_state(const net_input_state astate) { return (m_state & astate); }
	ATTR_HOT inline UINT32 state() const { return m_state; }

	ATTR_COLD void set_output(net_output_t *aout)   { m_output = aout; }
	ATTR_HOT inline void inactivate();
	ATTR_HOT inline void activate();
	ATTR_HOT inline void activate_hl();
	ATTR_HOT inline void activate_lh();

	ATTR_HOT inline net_core_device_t * RESTRICT netdev() const { return m_netdev; }

	double m_low_thresh_V;
	double m_high_thresh_V;

#if USE_DELEGATES
	net_update_delegate h;
#endif

private:
	UINT32 m_state;
	net_core_device_t * RESTRICT m_netdev;
	net_output_t * RESTRICT m_output;
};

class logic_input_t : public net_input_t
{
public:
	logic_input_t()
		: net_input_t(INPUT | SIGNAL_DIGITAL)
	{
		// default to TTL
		m_low_thresh_V = 0.8;
		m_high_thresh_V = 2.0;
	}

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

	ATTR_COLD inline void set_thresholds(double low, double high)
	{
		m_low_thresh_V = low;
		m_high_thresh_V = high;
	}
};

class ttl_input_t : public logic_input_t
{
public:
	ttl_input_t()
		: logic_input_t() { set_thresholds(0.8 , 2.0); }
};

class analog_input_t : public net_input_t
{
public:
	analog_input_t()
		: net_input_t(INPUT | SIGNAL_ANALOG) { }

	ATTR_HOT inline bool is_highz() const;
	ATTR_HOT inline double Q_Analog() const;
};

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




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

class net_output_t : public net_object_t
{
public:

	net_output_t(int atype);

	friend net_sig_t logic_input_t::Q() const;

	ATTR_HOT inline const net_sig_t last_Q() const  { return m_last_Q;  }
	ATTR_HOT inline const net_sig_t new_Q() const   { return m_new_Q;   }

	ATTR_HOT inline const double Q_Analog() const
	{
		switch (object_type(SIGNAL_MASK))
		{
		case SIGNAL_DIGITAL:    return m_Q ? m_high_V : m_low_V;
		case SIGNAL_ANALOG:     return m_Q_analog;
		default:                assert(true);
		}

		return 0;
	}

	inline net_sig_t *Q_ptr()       { return &m_Q; }
	inline net_sig_t *new_Q_ptr()   { return &m_new_Q; }

	ATTR_COLD void register_con(net_input_t &inp);

	ATTR_HOT void update_devs();
	ATTR_COLD void update_devs_force();

	ATTR_HOT inline const net_core_device_t *netdev() const { return m_netdev; }

	ATTR_HOT inline void inc_active();
	ATTR_HOT inline void dec_active() { m_active--; }
	ATTR_HOT inline int active_count() { return m_active; }

	ATTR_COLD void set_netdev(const net_core_device_t *dev);

protected:

	/* prohibit use in device functions
	 * current (pending) state can be inquired using new_Q()
	 */
	ATTR_HOT inline const net_sig_t Q() const   { return m_Q;   }

	ATTR_HOT inline void register_in_listPS(const netlist_time &delay_ps);

	ATTR_HOT inline void set_Q_PS(const net_sig_t newQ, const netlist_time &delay_ps)
	{
		if (newQ != m_new_Q)
		{
			m_new_Q = newQ;
			register_in_listPS(delay_ps);
		}
	}
	ATTR_HOT inline void set_Q_NoCheckPS(const net_sig_t val, const netlist_time &delay_ps)
	{
		m_new_Q = val;
		register_in_listPS(delay_ps);
	}

	ATTR_HOT inline void set_Q_PS_Analog(const double newQ, const netlist_time &delay_ps)
	{
		if (newQ != m_new_Q_analog)
		{
			m_new_Q_analog = newQ;
			register_in_listPS(delay_ps);
		}
	}
	ATTR_HOT inline void set_Q_NoCheckPS_Analog(const double val, const netlist_time &delay_ps)
	{
		m_new_Q_analog = val;
		register_in_listPS(delay_ps);
	}

	netlist_time m_time;
	int m_in_queue;

	net_sig_t m_Q;
	net_sig_t m_new_Q;
	net_sig_t m_last_Q;
	double m_Q_analog;
	double m_new_Q_analog;

	netlist_base_t *m_netlist;

	int m_active;
	int m_num_cons;
	net_input_t *m_cons[48];

	const net_core_device_t *m_netdev;
	double m_low_V;
	double m_high_V;

};

class logic_output_t : public net_output_t
{
public:

	logic_output_t()
		: net_output_t(OUTPUT | SIGNAL_DIGITAL)
	{
		// Default to TTL
		m_low_V = 0.3;
		m_high_V = 3.4;
	}

	ATTR_COLD void initial(const net_sig_t val) { m_Q = val; m_new_Q = val; m_last_Q = !val; }
	ATTR_HOT inline void clear()    { set_Q_PS(0, netlist_time::zero); }
	ATTR_HOT inline void set()      { set_Q_PS(1, netlist_time::zero); }
	ATTR_HOT inline void setToPS(const UINT8 val, const netlist_time &delay_ps) { set_Q_PS(val, delay_ps); }
	ATTR_HOT inline void setToNoCheckPS(const UINT8 val, const netlist_time &delay_ps) { set_Q_NoCheckPS(val, delay_ps); }
	ATTR_COLD inline void set_levels(const double low, const double high)
	{
		m_low_V = low;
		m_high_V = high;
	}
};

class ttl_output_t : public logic_output_t
{
public:

	ttl_output_t()
		: logic_output_t()
	{ set_levels(0.3, 3.4); }

};

class analog_output_t : public net_output_t
{
public:

	analog_output_t()
		: net_output_t(OUTPUT | SIGNAL_ANALOG) { }

	ATTR_COLD void initial(double val) { m_Q_analog = val; m_new_Q_analog = val; }
	ATTR_HOT inline void setToPS(const double val, const netlist_time &delay_ps) { set_Q_PS_Analog(val,delay_ps); }
	ATTR_HOT inline void setToNoCheckPS(const double val, const netlist_time &delay_ps) { set_Q_NoCheckPS_Analog(val,delay_ps); }
};

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

class net_core_device_t : public net_object_t
{
public:

#if USE_DELEGATES
	friend class net_input_t; // for access to update
#endif

	net_core_device_t();

	virtual ~net_core_device_t();

	ATTR_COLD void init_core(netlist_base_t *anetlist, const char *name);

	ATTR_COLD const char *name() const { return m_name; }

	ATTR_HOT inline void update_device()
	{
#if USE_DELEGATES_A
		h();
#else
		update();
#endif
	}

	ATTR_HOT virtual void update_param() {}

	ATTR_HOT inline net_sig_t INPVAL_PASSIVE(logic_input_t &inp)
	{
		net_sig_t ret;
		inp.activate();
		ret = inp.Q();
		inp.inactivate();
		return ret;
	}

	ATTR_HOT inline net_sig_t INPVAL(const logic_input_t &inp)
	{
		assert(inp.state() != net_input_t::INP_STATE_PASSIVE);
		return inp.Q();
	}

	ATTR_HOT inline net_sig_t INPVAL_LAST(const logic_input_t &inp) { return inp.last_Q(); }

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

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

	ATTR_COLD inline net_output_t * GETINPPTR(net_output_t &out) const { return &out; }

	net_list_t<net_core_device_t *, 20> m_subdevs;

	/* stats */
	osd_ticks_t total_time;
	volatile INT32 stat_count;

protected:

	ATTR_HOT virtual void update() { }

	ATTR_COLD void register_subdevice(net_core_device_t &subdev);

	netlist_base_t *m_netlist;

private:

	net_update_delegate h;
	const char *m_name;
};


class net_device_t : public net_core_device_t
{
public:

	net_device_t();

	virtual ~net_device_t();

	ATTR_COLD void init(netlist_setup_t *setup, const char *name);

	ATTR_COLD netlist_setup_t *setup() const { return m_setup; }

	ATTR_COLD virtual void start() {}

	ATTR_COLD bool variable_input_count() { return m_variable_input_count; }

	ATTR_COLD void register_output(const char *name, net_output_t &out);
	ATTR_COLD void register_output(const net_core_device_t &dev, const char *name, net_output_t &out);

	ATTR_COLD void register_input(const char *name, net_input_t &in, int state = net_input_t::INP_STATE_ACTIVE);
	ATTR_COLD void register_input(net_core_device_t &dev, const char *name, net_input_t &in, int state = net_input_t::INP_STATE_ACTIVE);

	ATTR_COLD void register_link_internal(net_input_t &in, net_output_t &out);
	ATTR_COLD void register_link_internal(net_core_device_t &dev, net_input_t &in, net_output_t &out);

	net_list_t<const char *, 20> m_inputs;

protected:

	ATTR_COLD void register_param(const char *sname, net_param_t &param, const double initialVal = 0.0);
	ATTR_COLD void register_param(net_core_device_t &dev, const char *sname, net_param_t &param, const double initialVal = 0.0);

	netlist_setup_t *m_setup;
	bool m_variable_input_count;

private:
};

class net_param_t
{
public:
	net_param_t() { }

	inline void setTo(const double param) { m_param = param; m_netdev->update_param(); }
	inline void setTo(const int param) { m_param = param; m_netdev->update_param(); }
	inline void initial(const double val) { m_param = val; }
	inline void initial(const int val) { m_param = val; }

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

	ATTR_HOT inline net_core_device_t &netdev() const { return *m_netdev; }
	void set_netdev(net_core_device_t &dev) { m_netdev = &dev; }

private:

	double m_param;
	net_core_device_t *m_netdev;
};

// ----------------------------------------------------------------------------------------
// net_signal_t
// ----------------------------------------------------------------------------------------

template <int _numdev, int _check>
class net_signal_t : public net_device_t
{
public:
	net_signal_t()
	: net_device_t() { }

	ATTR_COLD void start()
	{
		const char *sIN[8] = { "I1", "I2", "I3", "I4", "I5", "I6", "I7", "I8" };

		register_output("Q", m_Q);
		for (int i=0; i < _numdev; i++)
		{
			register_input(sIN[i], m_i[i], net_input_t::INP_STATE_ACTIVE);
		}
		m_Q.initial(1);
	}

	ATTR_HOT inline void update()
	{
		static netlist_time times[2] = { NLTIME_FROM_NS(22), NLTIME_FROM_NS(15) };
		for (int i=0; i< _numdev; i++)
		{
			m_i[i].activate();
			if (INPVAL(m_i[i]) == _check)
			{
				m_Q.setToPS(!_check, times[_check]);// ? 15000 : 22000);
				for (int j = i + 1; j < _numdev; j++)
					m_i[j].inactivate();
				return;
			}
			m_i[i].inactivate();
		}
		m_Q.setToPS(_check, times[1-_check]);// ? 22000 : 15000);
		for (int i = 0; i < _numdev; i++)
			m_i[i].activate();
	}

protected:
	ttl_input_t m_i[8];
	ttl_output_t m_Q;
};

// ----------------------------------------------------------------------------------------
// netlist_setup_t
// ----------------------------------------------------------------------------------------

class netlist_setup_t
{
public:

	typedef tagmap_t<net_device_t *, 393> tagmap_devices_t;
	typedef tagmap_t<astring *, 393> tagmap_astring_t;
	typedef tagmap_t<net_output_t *, 393> tagmap_output_t;
	typedef tagmap_t<net_input_t *, 393> tagmap_input_t;
	typedef tagmap_t<net_param_t *, 393> tagmap_param_t;

	netlist_setup_t(netlist_base_t &netlist);
	~netlist_setup_t();

	netlist_base_t &netlist() { return m_netlist; }

	net_device_t *register_dev(net_device_t *dev);
	void remove_dev(const char *name);

	void register_output(const char *name, net_output_t *out);
	void register_input(const char *name, net_input_t *inp);
	void register_alias(const char *alias, const char *out);
	void register_param(const char *sname, net_param_t *param);

	void register_link(const char *sin, const char *sout);

	net_output_t &find_output(const char *outname_in);
	net_param_t &find_param(const char *param_in);

	void register_callback(const char *devname, net_output_delegate delegate);

	void parse(char *buf);

	void resolve_inputs(void);

	/* not ideal, but needed for save_state */
	tagmap_output_t  m_outputs;

	void print_stats();

protected:

private:

	netlist_base_t &m_netlist;

	tagmap_devices_t m_devices;
	tagmap_astring_t m_alias;
	tagmap_input_t  m_inputs;
	tagmap_param_t  m_params;
	tagmap_astring_t  m_links;

	net_output_t *find_output_exact(const char *outname_in);
	const char *resolve_alias(const char *name) const;
};

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

class netlist_base_t
{
public:

	struct entry_t
	{
	public:
		entry_t() {}
		entry_t(netlist_time atime, net_output_t *aout) : time(atime), out(aout) {}
		netlist_time time;
		net_output_t *out;
	};
	typedef netlist_timed_queue<net_output_t *, 9> queue_t;

	netlist_base_t();
	virtual ~netlist_base_t();

	void set_clock_freq(UINT64 clockfreq);

	ATTR_HOT inline void register_in_listPS1(net_output_t *out, const netlist_time &attime)
	{
		m_queue.push(queue_t::entry_t(attime, out));
	}

	ATTR_HOT void process_list(INT32 &atime);

	ATTR_HOT inline netlist_time &time() { return m_time_ps; }

protected:
	netlist_time m_time_ps;
	UINT32   m_rem;
	UINT32  m_div;

	queue_t m_queue;

	// performance
	int m_perf_out_processed;
	int m_perf_inp_processed;
	int m_perf_inp_active;
	UINT64 m_perf_list_len;


private:

};


// ----------------------------------------------------------------------------------------
// netdev_callback
// ----------------------------------------------------------------------------------------

class netdev_callback : public net_device_t
{
public:
	netdev_callback()
	: net_device_t() {}

	void register_callback(net_output_delegate callback)
	{
		m_callback = callback;
	}

	ATTR_HOT void update();
	ATTR_COLD void start()
	{
		register_input("IN", m_in);
	}


private:
	analog_input_t m_in;
	net_output_delegate m_callback;
};

// ----------------------------------------------------------------------------------------
// netdev_*_const
// ----------------------------------------------------------------------------------------

#define NETDEV_TTL_CONST(_name, _v)                                                 \
		NET_REGISTER_DEV(netdev_ttl_const, _name)                                   \
		NETDEV_PARAM(_name.CONST, _v)
#define NETDEV_ANALOG_CONST(_name, _v)                                              \
		NET_REGISTER_DEV(netdev_analog_const, _name)                                \
		NETDEV_PARAM(_name.CONST, _v)

NETLIB_DEVICE_WITH_PARAMS(netdev_ttl_const,
	ttl_output_t m_Q;
	net_param_t m_const;
);

NETLIB_DEVICE_WITH_PARAMS(netdev_analog_const,
	analog_output_t m_Q;
	net_param_t m_const;
);

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

ATTR_HOT inline void net_input_t::inactivate()
{
	if (m_state != INP_STATE_PASSIVE)
	{
		m_state = INP_STATE_PASSIVE;
		m_output->dec_active();
	}
}

ATTR_HOT inline void net_input_t::activate()
{
	if (m_state == INP_STATE_PASSIVE)
		m_output->inc_active();
	m_state = INP_STATE_ACTIVE;
}

ATTR_HOT inline void net_input_t::activate_hl()
{
	if (m_state == INP_STATE_PASSIVE)
		m_output->inc_active();
	m_state = INP_STATE_HL;
}

ATTR_HOT inline void net_input_t::activate_lh()
{
	if (m_state == INP_STATE_PASSIVE)
		m_output->inc_active();
	m_state = INP_STATE_LH;
}


ATTR_HOT inline void net_output_t::register_in_listPS(const netlist_time &delay_ps)
{
	m_time = m_netlist->time() + delay_ps;
	m_in_queue = 0;     /* not queued */
	if (m_active > 0)
	{
		m_in_queue = 1;     /* pending */
		m_netlist->register_in_listPS1(this, m_time);
	}
}

ATTR_HOT inline void net_output_t::inc_active()
{
	m_active++;
	if (m_active == 1 && m_in_queue == 0)
	{
		if (m_time >= m_netlist->time())
		{
			m_in_queue = 1;     /* pending */
			m_netlist->register_in_listPS1(this, m_time);
		}
		else
		{
			m_Q = m_last_Q = m_new_Q;
			m_Q_analog = m_new_Q_analog;
			m_in_queue = 2;
		}
	}
}


ATTR_HOT inline net_sig_t logic_input_t::Q() const { return output()->Q(); }
ATTR_HOT inline net_sig_t logic_input_t::last_Q() const { return output()->last_Q(); }
ATTR_HOT inline double analog_input_t::Q_Analog() const { return output()->Q_Analog(); }
ATTR_HOT inline bool analog_input_t::is_highz() const { return output()->Q_Analog() == NETLIST_HIGHIMP_V; }

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

class net_device_t_base_factory
{
public:
	virtual ~net_device_t_base_factory() {}
	virtual net_device_t *Create(netlist_setup_t *setup, const char *name) = 0;

	const char *name() { return m_name; }
	const char *classname() { return m_classname; }
protected:
	const char *m_name;                             /* device name */
	const char *m_classname;                        /* device class name */
};

template <class C>
class net_device_t_factory : public net_device_t_base_factory
{
public:
	net_device_t_factory(const char *name, const char *classname) { m_name = name; m_classname = classname; }
	net_device_t *Create(netlist_setup_t *setup, const char *name)
	{
		net_device_t *r = global_alloc_clear(C());
		r->init(setup, name);
		return r;
	}
};

net_device_t *net_create_device_by_classname(const char *classname, netlist_setup_t *setup, const char *icname);
net_device_t *net_create_device_by_name(const char *name, netlist_setup_t *setup, const char *icname);

// ----------------------------------------------------------------------------------------
// MAME glue classes
// ----------------------------------------------------------------------------------------


class netlist_t : public netlist_base_t
{
public:

	netlist_t(device_t &parent)
	: netlist_base_t(),
		m_parent(parent)
	{}
	virtual ~netlist_t() { };

	inline running_machine &machine()   { return m_parent.machine(); }

	device_t &parent() { return m_parent; }

private:
	device_t &m_parent;
};

// ======================> netlist_mame_device

class netlist_mame_device : public device_t,
						public device_execute_interface
{
public:

	template<bool _Required, class _NETClass>
	class output_finder;
	class optional_output;
	template<class C>
	class required_output;
	class optional_param;
	class required_param;
	class on_device_start;

	// construction/destruction
	netlist_mame_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
	virtual ~netlist_mame_device() {}

	static void static_set_constructor(device_t &device, void (*setup_func)(netlist_setup_t &));

	netlist_setup_t &setup() { return *m_setup; }
	netlist_t &netlist() { return *m_netlist; }

	net_list_t<on_device_start *, 393> m_device_start_list;

protected:
	// device-level overrides
	virtual void device_config_complete();
	virtual void device_start();
	virtual void device_stop();
	virtual void device_reset();
	virtual void device_post_load();
	virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
	virtual UINT64 execute_clocks_to_cycles(UINT64 clocks) const;
	virtual UINT64 execute_cycles_to_clocks(UINT64 cycles) const;

	ATTR_HOT virtual void execute_run();

	netlist_t *m_netlist;

	netlist_setup_t *m_setup;

private:

	void step_one_clock();
	void save_state();

	void (*m_setup_func)(netlist_setup_t &);

	int m_icount;


};

// ======================> netlist_output_finder

class netlist_mame_device::on_device_start
{
public:
	virtual bool OnDeviceStart() = 0;
	virtual ~on_device_start() {}
};

// device finder template
template<bool _Required, class _NETClass>
class netlist_mame_device::output_finder : public object_finder_base<_NETClass>,
		netlist_mame_device::on_device_start
{
public:
	// construction/destruction
	output_finder(device_t &base, const char *tag, const char *output)
		: object_finder_base<_NETClass>(base, tag), m_output(output) { }

	// finder
	virtual bool findit(bool isvalidation = false)
	{
		if (isvalidation) return true;
		device_t *device = this->m_base.subdevice(this->m_tag);
		m_netlist = dynamic_cast<netlist_mame_device *>(device);
		if (device != NULL && m_netlist == NULL)
		{
			void mame_printf_warning(const char *format, ...) ATTR_PRINTF(1,2);
			mame_printf_warning("Device '%s' found but is not netlist\n", this->m_tag);
		}
		m_netlist->m_device_start_list.add(this);
		return this->report_missing(m_netlist != NULL, "device", _Required);
	}

protected:
	netlist_mame_device *m_netlist;
	const char *m_output;
};

// optional device finder
class netlist_mame_device::optional_output : public netlist_mame_device::output_finder<false, net_output_t>
{
public:
	optional_output(device_t &base, const char *tag, const char *output) : output_finder<false, net_output_t>(base, tag, output) { }

	virtual bool OnDeviceStart()
	{
		this->m_target = &m_netlist->setup().find_output(m_output);
		return this->report_missing(this->m_target != NULL, "output", false);
	}

};

// required devices are similar but throw an error if they are not found
template<class C>
class netlist_mame_device::required_output : public netlist_mame_device::output_finder<true, C>
{
public:
	required_output(device_t &base, const char *tag, const char *output) : output_finder<true, C>(base, tag, output) { }

	virtual bool OnDeviceStart()
	{
		this->m_target = (C *) &(this->m_netlist->setup().find_output(this->m_output));
		return this->report_missing(this->m_target != NULL, "output", true);
	}

};

// optional device finder
class netlist_mame_device::optional_param : public netlist_mame_device::output_finder<false, net_param_t>
{
public:
	optional_param(device_t &base, const char *tag, const char *output) : output_finder<false, net_param_t>(base, tag, output) { }

	virtual bool OnDeviceStart()
	{
		this->m_target = &m_netlist->setup().find_param(m_output);
		return this->report_missing(this->m_target != NULL, "parameter", false);
	}

};

// required devices are similar but throw an error if they are not found
class netlist_mame_device::required_param : public netlist_mame_device::output_finder<true, net_param_t>
{
public:
	required_param(device_t &base, const char *tag, const char *output) : output_finder<true, net_param_t>(base, tag, output) { }

	virtual bool OnDeviceStart()
	{
		this->m_target = &m_netlist->setup().find_param(m_output);
		return this->report_missing(this->m_target != NULL, "output", true);
	}
};

// device type definition
extern const device_type NETLIST;


#endif