summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/devices/nld_signal.h
blob: 0e659557e02b2786fdf8767e420632d845e8712c (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
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nld_signal.h
 *
 */

#ifndef NLD_SIGNAL_H_
#define NLD_SIGNAL_H_

#include "nl_base.h"

// ----------------------------------------------------------------------------------------
// MACROS
// ----------------------------------------------------------------------------------------

#define NETLIB_SIGNAL(_name, _num_input, _check, _invert)                           \
	class NETLIB_NAME(_name) : public net_signal_t<_num_input, _check, _invert>     \
	{                                                                               \
	public:                                                                         \
		NETLIB_NAME(_name) ()                                             \
		: net_signal_t<_num_input, _check, _invert>() { }                           \
	}

NETLIB_NAMESPACE_DEVICES_START()

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

template <int _numdev, int _check, int _invert>
class net_signal_t : public device_t
{
public:
	net_signal_t()
	: device_t(), m_active(1)
	{
	}

	void start() override
	{
		const char *sIN[8] = { "A", "B", "C", "D", "E", "F", "G", "H" };

		register_output("Q", m_Q[0]);
		for (int i=0; i < _numdev; i++)
		{
			register_input(sIN[i], m_I[i]);
		}
		save(NLNAME(m_active));
	}

	void reset() override
	{
		m_Q[0].initial(0);
		m_active = 1;
	}

	ATTR_HOT inline netlist_sig_t process()
	{
		for (int i = 0; i< _numdev; i++)
		{
			this->m_I[i].activate();
			if (INPLOGIC(this->m_I[i]) == _check)
			{
				for (int j = 0; j < i; j++)
					this->m_I[j].inactivate();
				for (int j = i + 1; j < _numdev; j++)
					this->m_I[j].inactivate();
				return _check ^ (1 ^ _invert);
			}
		}
		return _check ^ _invert;
	}

	ATTR_HOT virtual void inc_active() override
	{
		const netlist_time times[2] = { NLTIME_FROM_NS(15), NLTIME_FROM_NS(22)};
		nl_assert(netlist().use_deactivate());
		if (++m_active == 1)
		{
			// FIXME: need to activate before time is accessed !
			netlist_time mt = netlist_time::zero;
			for (int i = 0; i< _numdev; i++)
			{
				if (this->m_I[i].net().time() > mt)
					mt = this->m_I[i].net().time();
			}
			netlist_sig_t r = process();
			m_Q[0].net().set_Q_time(r, mt + times[r]);
		}
	}

	ATTR_HOT virtual void dec_active() override
	{
		nl_assert(netlist().use_deactivate());
		if (--m_active == 0)
		{
			for (int i = 0; i< _numdev; i++)
				m_I[i].inactivate();
		}
	}

	virtual void update() override
	{
		const netlist_time times[2] = { NLTIME_FROM_NS(15), NLTIME_FROM_NS(22)};

		netlist_sig_t r = process();
		OUTLOGIC(this->m_Q[0], r, times[r]);
	}

public:
	logic_input_t m_I[_numdev];
	logic_output_t m_Q[1];
	INT32 m_active;
};

NETLIB_NAMESPACE_DEVICES_END()

#endif /* NLD_SIGNAL_H_ */