diff options
Diffstat (limited to 'src/lib/netlist/tools/nl_convert.h')
-rw-r--r-- | src/lib/netlist/tools/nl_convert.h | 168 |
1 files changed, 107 insertions, 61 deletions
diff --git a/src/lib/netlist/tools/nl_convert.h b/src/lib/netlist/tools/nl_convert.h index e070514d5ed..0e36e85a253 100644 --- a/src/lib/netlist/tools/nl_convert.h +++ b/src/lib/netlist/tools/nl_convert.h @@ -1,35 +1,40 @@ -// license:GPL-2.0+ +// license:BSD-3-Clause // copyright-holders:Couriersud -/* - * nl_convert.h - * - */ - -#pragma once #ifndef NL_CONVERT_H_ #define NL_CONVERT_H_ -#include "plib/plists.h" -#include "plib/pparser.h" +/// +/// \file nl_convert.h +/// + +#include "plib/palloc.h" #include "plib/pstring.h" #include "plib/ptypes.h" #include <memory> -/*------------------------------------------------- - convert - convert a spice netlist --------------------------------------------------*/ +#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>; - COPYASSIGNMOVE(nl_convert_base_t, delete) + PCOPYASSIGNMOVE(nl_convert_base_t, delete) virtual ~nl_convert_base_t(); - pstring result() { return pstring(m_buf.str()); } + pstring result() { return pstring(putf8string(m_buf.str())); } virtual void convert(const pstring &contents) = 0; @@ -39,21 +44,40 @@ protected: 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(); - const pstring get_nl_val(const double val); - double get_sp_unit(const pstring &unit); + pstring get_nl_val(double val) const; + double get_sp_unit(const pstring &unit) const; - double get_sp_val(const pstring &sin); + 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 @@ -62,10 +86,10 @@ private: 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; } + 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() { return m_no_export; } + bool is_no_export() const { return m_no_export; } private: pstring m_name; @@ -100,20 +124,23 @@ private: , m_has_val(false) {} - const pstring &name() { return m_name;} - const pstring &type() { return m_type;} - const pstring &model() { return m_model;} - const double &value() { return m_val;} + 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() { return m_model != ""; } - bool has_value() { return m_has_val; } + 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 { @@ -128,26 +155,33 @@ private: 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; } + const pstring &name() const { return m_name; } + const pstring &alias() const { return m_alias; } private: pstring m_name; pstring m_alias; }; -private: - - void add_device(plib::unique_ptr<dev_t> dev); + 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<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<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_numberchars; + pstring m_number_chars; + + std::unordered_map<pstring, str_list> dev_map; }; @@ -155,37 +189,44 @@ class nl_convert_spice_t : public nl_convert_base_t { public: - nl_convert_spice_t() : nl_convert_base_t() {} + 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() : nl_convert_base_t() {} + nl_convert_eagle_t() = default; - class tokenizer : public plib::ptokenizer + class tokenizer : public plib::tokenizer_t, public plib::token_reader_t { public: - tokenizer(nl_convert_eagle_t &convert, plib::putf8_reader &&strm); + 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; - token_id_t m_tok_ADD; - token_id_t m_tok_VALUE; - token_id_t m_tok_SIGNAL; - token_id_t m_tok_SEMICOLON; + 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, int line_num, const pstring &line) override; + void verror(const pstring &msg) override; private: nl_convert_eagle_t &m_convert; @@ -204,26 +245,29 @@ class nl_convert_rinf_t : public nl_convert_base_t { public: - nl_convert_rinf_t() : nl_convert_base_t() {} + nl_convert_rinf_t() = default; - class tokenizer : public plib::ptokenizer + class tokenizer : public plib::tokenizer_t, public plib::token_reader_t { 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; - + 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, int line_num, const pstring &line) override; + void verror(const pstring &msg) override; private: nl_convert_rinf_t &m_convert; @@ -238,4 +282,6 @@ private: }; -#endif /* NL_CONVERT_H_ */ +} // namespace netlist::convert + +#endif // NL_CONVERT_H_ |