summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/tools/nl_convert.h
blob: 6c8765e7d160d0eba4a8fa201bfdc27a87492e59 (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
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nl_convert.h
 *
 */

#ifndef NL_CONVERT_H_
#define NL_CONVERT_H_

#include "plib/plists.h"
#include "plib/pstring.h"
#include "plib/ptokenizer.h"
#include "plib/ptypes.h"

#include <memory>

/*-------------------------------------------------
    convert - convert a spice netlist
-------------------------------------------------*/

class nl_convert_base_t
{
public:

	COPYASSIGNMOVE(nl_convert_base_t, delete)

	virtual ~nl_convert_base_t();

	pstring result() { return pstring(m_buf.str()); }

	virtual void convert(const pstring &contents) = 0;

protected:
	nl_convert_base_t();

	void add_pin_alias(const pstring &devname, const pstring &name, const pstring &alias);

	void add_ext_alias(const pstring &alias);

	void add_device(const pstring &atype, const pstring &aname, const pstring &amodel);
	void add_device(const pstring &atype, const pstring &aname, double aval);
	void add_device(const pstring &atype, const pstring &aname);

	void add_term(const pstring &netname, const pstring &termname);

	void dump_nl();

	const pstring get_nl_val(const double val);
	double get_sp_unit(const pstring &unit);

	double get_sp_val(const pstring &sin);

	plib::putf8_fmt_writer out;
private:

	struct net_t
	{
	public:
		explicit net_t(pstring aname)
		: m_name(std::move(aname)), m_no_export(false) {}

		const pstring &name() { return m_name;}
		std::vector<pstring> &terminals() { return m_terminals; }
		void set_no_export() { m_no_export = true; }
		bool is_no_export() { return m_no_export; }

	private:
		pstring m_name;
		bool m_no_export;
		std::vector<pstring> m_terminals;
	};

	struct dev_t
	{
	public:
		dev_t(pstring atype, pstring aname, pstring amodel)
		: m_type(std::move(atype))
		, m_name(std::move(aname))
		, m_model(std::move(amodel))
		, m_val(0)
		, m_has_val(false)
		{}

		dev_t(pstring atype, pstring aname, double aval)
		: m_type(std::move(atype))
		, m_name(std::move(aname))
		, m_model("")
		, m_val(aval)
		, m_has_val(true)
		{}

		dev_t(pstring atype, pstring aname)
		: m_type(std::move(atype))
		, m_name(std::move(aname))
		, m_model("")
		, m_val(0.0)
		, m_has_val(false)
		{}

		const pstring &name() { return m_name;}
		const pstring &type() { return m_type;}
		const pstring &model() { return m_model;}
		double value() { return m_val;}

		bool has_model() { return m_model != ""; }
		bool has_value() { return m_has_val; }

	private:
		pstring m_type;
		pstring m_name;
		pstring m_model;
		double m_val;
		bool m_has_val;
	};

	struct unit_t {
		pstring m_unit;
		pstring m_func;
		double m_mult;
	};

	struct pin_alias_t
	{
	public:
		pin_alias_t(pstring name, pstring alias)
		: m_name(std::move(name)), m_alias(std::move(alias))
		{}
		const pstring &name() { return m_name; }
		const pstring &alias() { return m_alias; }
	private:
		pstring m_name;
		pstring m_alias;
	};

private:

	void add_device(plib::unique_ptr<dev_t> dev);

	std::stringstream m_buf;

	std::vector<plib::unique_ptr<dev_t>> m_devs;
	std::unordered_map<pstring, plib::unique_ptr<net_t> > m_nets;
	std::vector<pstring> m_ext_alias;
	std::unordered_map<pstring, plib::unique_ptr<pin_alias_t>> m_pins;

	std::vector<unit_t> m_units;
	pstring m_numberchars;

};

class nl_convert_spice_t : public nl_convert_base_t
{
public:

	nl_convert_spice_t() : nl_convert_base_t() {}

	void convert(const pstring &contents) override;

protected:

	void process_line(const pstring &line);

private:
	pstring m_subckt;
};

class nl_convert_eagle_t : public nl_convert_base_t
{
public:

	nl_convert_eagle_t() : nl_convert_base_t() {}

	class tokenizer : public plib::ptokenizer
	{
	public:
		tokenizer(nl_convert_eagle_t &convert, plib::putf8_reader &&strm);

		token_id_t m_tok_ADD;
		token_id_t m_tok_VALUE;
		token_id_t m_tok_SIGNAL;
		token_id_t m_tok_SEMICOLON;

	protected:

		void verror(const pstring &msg) override;

	private:
		nl_convert_eagle_t &m_convert;
	};

	void convert(const pstring &contents) override;

protected:


private:

};

class nl_convert_rinf_t : public nl_convert_base_t
{
public:

	nl_convert_rinf_t() : nl_convert_base_t() {}

	class tokenizer : public plib::ptokenizer
	{
	public:
		tokenizer(nl_convert_rinf_t &convert, plib::putf8_reader &&strm);

		token_id_t m_tok_HEA;
		token_id_t m_tok_APP;
		token_id_t m_tok_TIM;
		token_id_t m_tok_TYP;
		token_id_t m_tok_ADDC;
		token_id_t m_tok_ATTC;
		token_id_t m_tok_NET;
		token_id_t m_tok_TER;
		token_id_t m_tok_END;

	protected:

		void verror(const pstring &msg) override;

	private:
		nl_convert_rinf_t &m_convert;
	};

	void convert(const pstring &contents) override;

protected:


private:

};

#endif /* NL_CONVERT_H_ */