summaryrefslogtreecommitdiffstats
path: root/src/lib/netlist/tools/nl_convert.h
blob: f61a84fbce418fd2481430fd193e2f7ba21ef585 (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
// license:BSD-3-Clause
// copyright-holders:Couriersud

#ifndef NL_CONVERT_H_
#define NL_CONVERT_H_

///
/// \file nl_convert.h
///

#include "plib/palloc.h"
#include "plib/pstring.h"
#include "plib/ptypes.h"

#include <memory>

#include "../plib/ptokenizer.h"

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

namespace netlist::convert
{

using arena = plib::aligned_arena<>;

class nl_convert_base_t
{
public:
	using str_list = std::vector<pstring>;

	PCOPYASSIGNMOVE(nl_convert_base_t, delete)

	virtual ~nl_convert_base_t();

	pstring result() { return pstring(putf8string(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_ext_alias(const pstring &alias, const pstring &net);

	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_device_extra_s(const pstring &devname, const pstring &extra);

	template<typename... Args>
	void add_device_extra(const pstring &devname, const pstring &fmt, Args&&... args)
	{
		add_device_extra_s(devname, plib::pfmt(fmt)(std::forward<Args>(args)...));
	}

	void add_term(const pstring &netname, const pstring &termname);
	void add_term(const pstring &netname, const pstring &devname, unsigned term);

	void dump_nl();

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

	double get_sp_val(const pstring &sin) const;

	plib::putf8_fmt_writer out;

	struct replace_t
	{
		pstring m_ce; // controlling element - must be a two terminal element
		pstring m_repterm; // replace with terminal
		pstring m_net; // connect to net
	};
	std::vector<replace_t> m_replace;

private:

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

		const pstring &name() const { return m_name;}
		std::vector<pstring> &terminals(){ return m_terminals; }
		void set_no_export() { m_no_export = true; }
		bool is_no_export() const { 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() const { return m_name;}
		const pstring &type() const { return m_type;}
		const pstring &model() const { return m_model;}
		double value() const { return m_val;}
		const str_list &extra() const { return m_extra;}

		bool has_model() const { return !m_model.empty(); }
		bool has_value() const { return m_has_val; }

		void add_extra(const pstring &s) { m_extra.push_back(s); }
	private:
		pstring m_type;
		pstring m_name;
		pstring m_model;
		double m_val;
		bool m_has_val;
		str_list m_extra;
	};

	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() const { return m_name; }
		const pstring &alias() const { return m_alias; }
	private:
		pstring m_name;
		pstring m_alias;
	};

	void add_device(arena::unique_ptr<dev_t> dev);
	dev_t *get_device(const pstring &name)
	{
		for (auto &e : m_devs)
			if (e->name() == name)
				return e.get();
		return nullptr;
	}

	std::stringstream m_buf = { };

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

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

	std::unordered_map<pstring, str_list> dev_map;

};

class nl_convert_spice_t : public nl_convert_base_t
{
public:

	nl_convert_spice_t() : m_is_kicad(false) { }

	void convert(const pstring &contents) override;

protected:

	bool is_kicad() const { return m_is_kicad; }
	void convert_block(const str_list &contents);
	void process_line(const pstring &line);

private:
	pstring m_subckt;
	bool m_is_kicad;
};

class nl_convert_eagle_t : public nl_convert_base_t
{
public:

	nl_convert_eagle_t() = default;

	class tokenizer : public plib::tokenizer_t, public plib::token_reader_t
	{
	public:
		using token_t = tokenizer_t::token_t;
		using token_type = tokenizer_t::token_type;
		using token_id_t = tokenizer_t::token_id_t;
		using token_store = tokenizer_t::token_store_t;

		tokenizer(nl_convert_eagle_t &convert);

		token_id_t m_tok_ADD;       // NOLINT
		token_id_t m_tok_VALUE;     // NOLINT
		token_id_t m_tok_SIGNAL;    // NOLINT
		token_id_t m_tok_SEMICOLON; // NOLINT
	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() = default;

	class tokenizer : public plib::tokenizer_t, public plib::token_reader_t
	{
	public:
		using token_t = tokenizer_t::token_t;
		using token_type = tokenizer_t::token_type;
		using token_id_t = tokenizer_t::token_id_t;
		using token_store = tokenizer_t::token_store_t;
		tokenizer(nl_convert_rinf_t &convert);

		token_id_t m_tok_HEA; // NOLINT
		token_id_t m_tok_APP; // NOLINT
		token_id_t m_tok_TIM; // NOLINT
		token_id_t m_tok_TYP; // NOLINT
		token_id_t m_tok_ADDC; // NOLINT
		token_id_t m_tok_ATTC; // NOLINT
		token_id_t m_tok_NET; // NOLINT
		token_id_t m_tok_TER; // NOLINT
		token_id_t m_tok_END; // NOLINT
	protected:

		void verror(const pstring &msg) override;

	private:
		nl_convert_rinf_t &m_convert;
	};

	void convert(const pstring &contents) override;

protected:


private:

};

} // namespace netlist::convert

#endif // NL_CONVERT_H_