summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/analog/nld_switches.cpp
blob: 6b87279e398db902165fd3a6d9480a58cd2ede97 (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
// license:BSD-3-Clause
// copyright-holders:Couriersud

#include "nlid_twoterm.h"
#include "nl_base.h"
#include "nl_factory.h"
#include "solver/nld_solver.h"

// FIXME : convert to parameters

#define R_OFF   (plib::reciprocal(exec().gmin()))
#define R_ON    nlconst::magic(0.01)

namespace netlist
{
	namespace analog
	{
	// ----------------------------------------------------------------------------------------
	// SWITCH
	// ----------------------------------------------------------------------------------------

	class nld_switch1 : public base_device_t
	{
	public:
		nld_switch1(constructor_param_t data)
		: base_device_t(data)
		, m_R(*this, "R")
		, m_POS(*this, "POS", false)
		{
			register_sub_alias("1", m_R().P());
			register_sub_alias("2", m_R().N());
		}

		NETLIB_RESETI()
		{
			m_R().set_R(R_OFF);
		}
		NETLIB_UPDATE_PARAMI()
		{
			m_R().change_state([this]()
			{
				m_R().set_R(m_POS() ? R_ON : R_OFF);
			});
		}

	private:
		NETLIB_SUB_NS(analog, R_base) m_R;
		param_logic_t                 m_POS;
	};

// ----------------------------------------------------------------------------------------
// SWITCH2
// ----------------------------------------------------------------------------------------

	class nld_switch2 : public base_device_t
	{
	public:
		nld_switch2(constructor_param_t data)
		: base_device_t(data)
		, m_R1(*this, "R1")
		, m_R2(*this, "R2")
		, m_POS(*this, "POS", false)
		{
			connect(m_R1().N(), m_R2().N());

			register_sub_alias("1", m_R1().P());
			register_sub_alias("2", m_R2().P());

			register_sub_alias("Q", m_R1().N());
		}

		NETLIB_RESETI();
		NETLIB_UPDATE_PARAMI();

	private:
		NETLIB_SUB_NS(analog, R_base) m_R1;
		NETLIB_SUB_NS(analog, R_base) m_R2;
		param_logic_t                 m_POS;
	};

	NETLIB_RESET(switch2)
	{
		m_R1().set_R(R_ON);
		m_R2().set_R(R_OFF);
	}

#ifdef FIXMELATER
	NETLIB_UPDATE(switch2)
	{
		if (!m_POS())
		{
			m_R1.set_R(R_ON);
			m_R2.set_R(R_OFF);
		}
		else
		{
			m_R1.set_R(R_OFF);
			m_R2.set_R(R_ON);
		}
	}
#endif
	NETLIB_UPDATE_PARAM(switch2)
	{
		// R1 and R2 are connected. However this net may be a rail net.
		// The code here thus is a bit more complex.

		nl_fptype r1 = m_POS() ? R_OFF : R_ON;
		nl_fptype r2 = m_POS() ? R_ON : R_OFF;

		if (m_R1().solver() == m_R2().solver())
			m_R1().change_state([this, &r1, &r2]() { m_R1().set_R(r1); m_R2().set_R(r2); });
		else
		{
			m_R1().change_state([this, &r1]() { m_R1().set_R(r1); });
			m_R2().change_state([this, &r2]() { m_R2().set_R(r2); });
		}
	}

	} //namespace analog

	namespace devices {
		NETLIB_DEVICE_IMPL_NS(analog, switch1, "SWITCH",  "")
		NETLIB_DEVICE_IMPL_NS(analog, switch2, "SWITCH2", "")
	} // namespace devices
} // namespace netlist