diff options
-rw-r--r-- | scripts/src/netlist.lua | 3 | ||||
-rw-r--r-- | src/lib/netlist/nl_base.c | 1 | ||||
-rw-r--r-- | src/lib/netlist/nl_base.h | 1 | ||||
-rw-r--r-- | src/lib/netlist/nl_parser.c | 1 | ||||
-rw-r--r-- | src/lib/netlist/plib/palloc.c | 2 | ||||
-rw-r--r-- | src/lib/netlist/plib/pfmtlog.c | 166 | ||||
-rw-r--r-- | src/lib/netlist/plib/pfmtlog.h | 292 | ||||
-rw-r--r-- | src/lib/netlist/plib/pstream.c | 114 | ||||
-rw-r--r-- | src/lib/netlist/plib/pstream.h | 100 | ||||
-rw-r--r-- | src/lib/netlist/plib/pstring.c | 107 | ||||
-rw-r--r-- | src/lib/netlist/plib/pstring.h | 277 | ||||
-rw-r--r-- | src/lib/netlist/plib/ptypes.h | 97 | ||||
-rw-r--r-- | src/lib/netlist/prg/nltool.c | 127 | ||||
-rw-r--r-- | src/lib/netlist/solver/nld_ms_direct_lu.h | 2 | ||||
-rw-r--r-- | src/lib/netlist/solver/nld_solver.h | 3 |
15 files changed, 781 insertions, 512 deletions
diff --git a/scripts/src/netlist.lua b/scripts/src/netlist.lua index 34c64261095..7cf8ddc9991 100644 --- a/scripts/src/netlist.lua +++ b/scripts/src/netlist.lua @@ -38,6 +38,8 @@ project "netlist" MAME_DIR .. "src/lib/netlist/plib/pconfig.h", MAME_DIR .. "src/lib/netlist/plib/palloc.c", MAME_DIR .. "src/lib/netlist/plib/palloc.h", + MAME_DIR .. "src/lib/netlist/plib/pfmtlog.c", + MAME_DIR .. "src/lib/netlist/plib/pfmtlog.h", MAME_DIR .. "src/lib/netlist/plib/plists.h", MAME_DIR .. "src/lib/netlist/plib/poptions.h", MAME_DIR .. "src/lib/netlist/plib/pparser.c", @@ -50,6 +52,7 @@ project "netlist" MAME_DIR .. "src/lib/netlist/plib/pstring.h", MAME_DIR .. "src/lib/netlist/plib/pstream.c", MAME_DIR .. "src/lib/netlist/plib/pstream.h", + MAME_DIR .. "src/lib/netlist/plib/ptypes.h", MAME_DIR .. "src/lib/netlist/tools/nl_convert.c", MAME_DIR .. "src/lib/netlist/tools/nl_convert.h", MAME_DIR .. "src/lib/netlist/analog/nld_bjt.c", diff --git a/src/lib/netlist/nl_base.c b/src/lib/netlist/nl_base.c index 6c1dab54289..ce818e0d94f 100644 --- a/src/lib/netlist/nl_base.c +++ b/src/lib/netlist/nl_base.c @@ -271,7 +271,6 @@ ATTR_COLD net_t *netlist_t::find_net(const pstring &name) ATTR_COLD void netlist_t::rebuild_lists() { - //printf("Rebuild Lists\n"); for (std::size_t i = 0; i < m_nets.size(); i++) m_nets[i]->rebuild_list(); } diff --git a/src/lib/netlist/nl_base.h b/src/lib/netlist/nl_base.h index e472031baa6..4bc152e253f 100644 --- a/src/lib/netlist/nl_base.h +++ b/src/lib/netlist/nl_base.h @@ -159,6 +159,7 @@ #include "nl_time.h" #include "nl_util.h" #include "plib/pstate.h" +#include "plib/pfmtlog.h" // ---------------------------------------------------------------------------------------- // Type definitions diff --git a/src/lib/netlist/nl_parser.c b/src/lib/netlist/nl_parser.c index 32609079ba6..312410fafc6 100644 --- a/src/lib/netlist/nl_parser.c +++ b/src/lib/netlist/nl_parser.c @@ -425,7 +425,6 @@ nl_double parser_t::eval_param(const token_t tok) nl_double ret; pstring val; - //printf("param {1}\n", tok.m_token); for (i=1; i<6;i++) if (tok.str().equals(macs[i])) f = i; diff --git a/src/lib/netlist/plib/palloc.c b/src/lib/netlist/plib/palloc.c index 9b0ee555cbb..22dbc1d4e93 100644 --- a/src/lib/netlist/plib/palloc.c +++ b/src/lib/netlist/plib/palloc.c @@ -35,13 +35,11 @@ pmemory_pool *ppool = &sppool; void* operator new(std::size_t size, pmemory_pool *pool) throw (std::bad_alloc) { - //printf("here new\n"); return palloc_raw(size);; } void operator delete(void *ptr, pmemory_pool *pool) { - //printf("here delete\n"); if (ptr != NULL) pfree_raw(ptr); } diff --git a/src/lib/netlist/plib/pfmtlog.c b/src/lib/netlist/plib/pfmtlog.c new file mode 100644 index 00000000000..2b08bb2fa98 --- /dev/null +++ b/src/lib/netlist/plib/pfmtlog.c @@ -0,0 +1,166 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * nl_string.c + * + */ + +#if 0 +#include <cstring> +//FIXME:: pstring should be locale free +#include <cctype> +#include <cstdlib> +#include <cstdio> +#endif + +#include <algorithm> + +#include "pfmtlog.h" +#include "palloc.h" + +pfmt::pfmt(const pstring &fmt) +: m_str(m_str_buf), m_allocated(0), m_arg(0) +{ + unsigned l = fmt.blen() + 1; + if (l>sizeof(m_str_buf)) + { + m_allocated = 2 * l; + m_str = palloc_array(char, 2 * l); + } + memcpy(m_str, fmt.cstr(), l); +} + +pfmt::pfmt(const char *fmt) +: m_str(m_str_buf), m_allocated(0), m_arg(0) +{ + unsigned l = strlen(fmt) + 1; + if (l>sizeof(m_str_buf)) + { + m_allocated = 2 * l; + m_str = palloc_array(char, 2 * l); + } + memcpy(m_str, fmt, l); +} + +pfmt::~pfmt() +{ + if (m_allocated > 0) + pfree_array(m_str); +} + +#if 0 +void pformat::format_element(const char *f, const char *l, const char *fmt_spec, ...) +{ + va_list ap; + va_start(ap, fmt_spec); + char fmt[30] = "%"; + char search[10] = ""; + char buf[1024]; + strcat(fmt, f); + strcat(fmt, l); + strcat(fmt, fmt_spec); + int nl = vsprintf(buf, fmt, ap); + m_arg++; + int sl = sprintf(search, "%%%d", m_arg); + char *p = strstr(m_str, search); + if (p != NULL) + { + // Make room + memmove(p+nl, p+sl, strlen(p) + 1 - sl); + memcpy(p, buf, nl); + } + va_end(ap); +} +#else +void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...) +{ + va_list ap; + va_start(ap, fmt_spec); + char fmt[30] = "%"; + char search[10] = ""; + char buf[2048]; + m_arg++; + int sl = sprintf(search, "{%d:", m_arg); + char *p = strstr(m_str, search); + if (p == NULL) + { + sl = sprintf(search, "{%d}", m_arg); + p = strstr(m_str, search); + if (p == NULL) + { + sl = 2; + p = strstr(m_str, "{}"); + } + if (p==NULL) + { + sl=1; + p = strstr(m_str, "{"); + if (p != NULL) + { + char *p1 = strstr(p, "}"); + if (p1 != NULL) + { + sl = p1 - p + 1; + strncat(fmt, p+1, p1 - p - 2); + } + else + strcat(fmt, f); + } + else + strcat(fmt, f); + } + } + else + { + char *p1 = strstr(p, "}"); + if (p1 != NULL) + { + sl = p1 - p + 1; + if (m_arg>=10) + strncat(fmt, p+4, p1 - p - 4); + else + strncat(fmt, p+3, p1 - p - 3); + } + else + strcat(fmt, f); + } + strcat(fmt, l); + char *pend = fmt + strlen(fmt) - 1; + if (strchr("fge", *fmt_spec) != NULL) + { + if (strchr("fge", *pend) == NULL) + strcat(fmt, fmt_spec); + } + else if (strchr("duxo", *fmt_spec) != NULL) + { + if (strchr("duxo", *pend) == NULL) + strcat(fmt, fmt_spec); + } + else + strcat(fmt, fmt_spec); + int nl = vsprintf(buf, fmt, ap); + if (p != NULL) + { + // check room + unsigned new_size = (p - m_str) + nl + strlen(p) + 1 - sl; + if (new_size > m_allocated) + { + unsigned old_alloc = std::max(m_allocated, (unsigned) sizeof(m_str_buf)); + if (m_allocated < old_alloc) + m_allocated = old_alloc; + while (new_size > m_allocated) + m_allocated *= 2; + char *np = palloc_array(char, m_allocated); + memcpy(np, m_str, old_alloc); + p = np + (p - m_str); + if (m_str != m_str_buf) + pfree_array(m_str); + m_str = np; + } + // Make room + memmove(p+nl, p+sl, strlen(p) + 1 - sl); + memcpy(p, buf, nl); + } + va_end(ap); +} +#endif diff --git a/src/lib/netlist/plib/pfmtlog.h b/src/lib/netlist/plib/pfmtlog.h new file mode 100644 index 00000000000..058152a91cc --- /dev/null +++ b/src/lib/netlist/plib/pfmtlog.h @@ -0,0 +1,292 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * pfmtlog.h + */ + +#ifndef _PFMT_H_ +#define _PFMT_H_ + +//#include <cstdarg> +//#include <cstddef> + +#include "pconfig.h" +#include "pstring.h" +#include "ptypes.h" + +template <typename T> +struct ptype_treats +{ +}; + +template<> +struct ptype_treats<char> +{ + static short cast(char x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<short> +{ + static short cast(short x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<int> +{ + static int cast(int x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return ""; } +}; + +template<> +struct ptype_treats<long> +{ + static long cast(long x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "l"; } +}; + +template<> +struct ptype_treats<long long> +{ + static long long cast(long long x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "ll"; } +}; + +template<> +struct ptype_treats<unsigned char> +{ + static unsigned short cast(unsigned char x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<unsigned short> +{ + static unsigned short cast(unsigned short x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<unsigned int> +{ + static unsigned int cast(unsigned int x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return ""; } +}; + +template<> +struct ptype_treats<unsigned long> +{ + static unsigned long cast(unsigned long x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "l"; } +}; + +template<> +struct ptype_treats<unsigned long long> +{ + static unsigned long long cast(unsigned long long x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "ll"; } +}; + +template <typename P> +class pformat_base +{ +public: + + virtual ~pformat_base() { } + + ATTR_COLD P &operator ()(const double x, const char *f = "") { format_element(f, "", "f", x); return static_cast<P &>(*this); } + ATTR_COLD P & e(const double x, const char *f = "") { format_element(f, "", "e", x); return static_cast<P &>(*this); } + ATTR_COLD P & g(const double x, const char *f = "") { format_element(f, "", "g", x); return static_cast<P &>(*this); } + + ATTR_COLD P &operator ()(const char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast<P &>(*this); } + ATTR_COLD P &operator ()(char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast<P &>(*this); } + ATTR_COLD P &operator ()(const void *x, const char *f = "") { format_element(f, "", "p", x); return static_cast<P &>(*this); } + ATTR_COLD P &operator ()(const pstring &x, const char *f = "") { format_element(f, "", "s", x.cstr() ); return static_cast<P &>(*this); } + + template<typename T> + ATTR_COLD P &operator ()(const T x, const char *f = "") + { + if (ptype_treats<T>::is_signed) + format_element(f, ptype_treats<T>::size_specifier(), "d", ptype_treats<T>::cast(x)); + else + format_element(f, ptype_treats<T>::size_specifier(), "u", ptype_treats<T>::cast(x)); + return static_cast<P &>(*this); + } + + template<typename T> + ATTR_COLD P &x(const T x, const char *f = "") + { + format_element(f, ptype_treats<T>::size_specifier(), "x", x); + return static_cast<P &>(*this); + } + + template<typename T> + ATTR_COLD P &o(const T x, const char *f = "") + { + format_element(f, ptype_treats<T>::size_specifier(), "o", x); + return static_cast<P &>(*this); + } + +protected: + + virtual void format_element(const char *f, const char *l, const char *fmt_spec, ...) = 0; + +}; + +class pfmt : public pformat_base<pfmt> +{ +public: + pfmt(const pstring &fmt); + pfmt(const char *fmt); + virtual ~pfmt(); + + operator pstring() const { return m_str; } + + const char *cstr() { return m_str; } + + +protected: + void format_element(const char *f, const char *l, const char *fmt_spec, ...); + +private: + + char *m_str; + char m_str_buf[256]; + unsigned m_allocated; + unsigned m_arg; +}; + +P_ENUM(plog_level, + DEBUG, + INFO, + VERBOSE, + WARNING, + ERROR, + FATAL) + +class plog_dispatch_intf; + +template <bool build_enabled = true> +class pfmt_writer_t +{ +public: + pfmt_writer_t() : m_enabled(true) { } + virtual ~pfmt_writer_t() { } + + ATTR_COLD void operator ()(const char *fmt) const + { + if (build_enabled && m_enabled) vdowrite(fmt); + } + + template<typename T1> + ATTR_COLD void operator ()(const char *fmt, const T1 &v1) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)); + } + + template<typename T1, typename T2> + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)); + } + + template<typename T1, typename T2, typename T3> + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)); + } + + template<typename T1, typename T2, typename T3, typename T4> + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)); + } + + template<typename T1, typename T2, typename T3, typename T4, typename T5> + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)(v5)); + } + + void set_enabled(const bool v) + { + m_enabled = v; + } + + bool is_enabled() const { return m_enabled; } + +protected: + virtual void vdowrite(const pstring &ls) const {} + +private: + bool m_enabled; + +}; + +template <plog_level::e L, bool build_enabled = true> +class plog_channel : public pfmt_writer_t<build_enabled> +{ +public: + plog_channel(plog_dispatch_intf *b) : pfmt_writer_t<build_enabled>(), m_base(b) { } + virtual ~plog_channel() { } + +protected: + virtual void vdowrite(const pstring &ls) const; + +private: + plog_dispatch_intf *m_base; +}; + +class plog_dispatch_intf +{ + template<plog_level::e, bool> friend class plog_channel; + +public: + virtual ~plog_dispatch_intf() { } +protected: + virtual void vlog(const plog_level &l, const pstring &ls) const = 0; +}; + +template<bool debug_enabled> +class plog_base +{ +public: + + plog_base(plog_dispatch_intf *proxy) + : debug(proxy), + info(proxy), + verbose(proxy), + warning(proxy), + error(proxy), + fatal(proxy) + {} + virtual ~plog_base() {}; + + plog_channel<plog_level::DEBUG, debug_enabled> debug; + plog_channel<plog_level::INFO> info; + plog_channel<plog_level::VERBOSE> verbose; + plog_channel<plog_level::WARNING> warning; + plog_channel<plog_level::ERROR> error; + plog_channel<plog_level::FATAL> fatal; +}; + + +template <plog_level::e L, bool build_enabled> +void plog_channel<L, build_enabled>::vdowrite(const pstring &ls) const +{ + m_base->vlog(L, ls); +} + +#endif /* _PSTRING_H_ */ diff --git a/src/lib/netlist/plib/pstream.c b/src/lib/netlist/plib/pstream.c index ae2f5b580a1..ba7c5c7eda2 100644 --- a/src/lib/netlist/plib/pstream.c +++ b/src/lib/netlist/plib/pstream.c @@ -14,12 +14,62 @@ #include "palloc.h" // ----------------------------------------------------------------------------- +// pistream: input stream +// ----------------------------------------------------------------------------- + +bool pistream::readline(pstring &line) +{ + UINT8 c = 0; + pstringbuffer buf; + if (!this->read(c)) + { + line = ""; + return false; + } + while (true) + { + if (c == 10) + break; + else if (c != 13) /* ignore CR */ + buf += c; + if (!this->read(c)) + break; + } + line = buf; + return true; +} + +// ----------------------------------------------------------------------------- +// postream: output stream +// ----------------------------------------------------------------------------- + +void postream::write(pistream &strm) +{ + char buf[1024]; + unsigned r; + while ( !bad() && ((r=strm.read(buf, 1024)) > 0)) + write(buf, r); +} + +// ----------------------------------------------------------------------------- // Input file stream // ----------------------------------------------------------------------------- -pifilestream::pifilestream(const pstring &fname) : pistream(0), m_pos(0) +pifilestream::pifilestream(const pstring &fname) +: pistream(0), m_pos(0), m_actually_close(true) { - m_file = fopen(fname.cstr(), "rb"); + init(fopen(fname.cstr(), "rb")); +} + +pifilestream::pifilestream(void *file, const bool do_close) +: pistream(0), m_pos(0), m_actually_close(do_close) +{ + init(file); +} + +void pifilestream::init(void *file) +{ + m_file = file; if (m_file == NULL) { set_flag(FLAG_ERROR); @@ -44,8 +94,11 @@ pifilestream::~pifilestream() void pifilestream::close() { - fclose((FILE *) m_file); - set_flag(FLAG_CLOSED); + if (m_actually_close) + { + fclose((FILE *) m_file); + set_flag(FLAG_CLOSED); + } } unsigned pifilestream::vread(void *buf, unsigned n) @@ -89,12 +142,34 @@ pifilestream::pos_type pifilestream::vtell() } // ----------------------------------------------------------------------------- +// pstdin: reads from stdin +// ----------------------------------------------------------------------------- + +pstdin::pstdin() +: pifilestream(stdin, false) +{ + /* nothing to do */ +} + +// ----------------------------------------------------------------------------- // Output file stream // ----------------------------------------------------------------------------- -pofilestream::pofilestream(const pstring &fname) : postream(0), m_pos(0) +pofilestream::pofilestream(const pstring &fname) +: postream(0), m_pos(0), m_actually_close(true) { - m_file = fopen(fname.cstr(), "wb"); + init(fopen(fname.cstr(), "wb")); +} + +pofilestream::pofilestream(void *file, const bool do_close) +: postream(0), m_pos(0), m_actually_close(do_close) +{ + init(file); +} + +void pofilestream::init(void *file) +{ + m_file = file; if (m_file == NULL) { set_flag(FLAG_ERROR); @@ -118,8 +193,11 @@ pofilestream::~pofilestream() void pofilestream::close() { - fclose((FILE *) m_file); - set_flag(FLAG_CLOSED); + if (m_actually_close) + { + fclose((FILE *) m_file); + set_flag(FLAG_CLOSED); + } } void pofilestream::vwrite(const void *buf, unsigned n) @@ -146,7 +224,7 @@ void pofilestream::vseek(pos_type n) } } -pifilestream::pos_type pofilestream::vtell() +pstream::pos_type pofilestream::vtell() { long ret = ftell((FILE *) m_file); if (ret < 0) @@ -158,6 +236,24 @@ pifilestream::pos_type pofilestream::vtell() } // ----------------------------------------------------------------------------- +// pstderr: write to stderr +// ----------------------------------------------------------------------------- + +pstderr::pstderr() +: pofilestream(stderr, false) +{ +} + +// ----------------------------------------------------------------------------- +// pstdout: write to stdout +// ----------------------------------------------------------------------------- + +pstdout::pstdout() +: pofilestream(stdout, false) +{ +} + +// ----------------------------------------------------------------------------- // Memory stream // ----------------------------------------------------------------------------- diff --git a/src/lib/netlist/plib/pstream.h b/src/lib/netlist/plib/pstream.h index af9f939cc6c..32e7558fabe 100644 --- a/src/lib/netlist/plib/pstream.h +++ b/src/lib/netlist/plib/pstream.h @@ -14,6 +14,7 @@ #include "pconfig.h" #include "pstring.h" #include "palloc.h" +#include "pfmtlog.h" // ----------------------------------------------------------------------------- // pstream: things common to all streams @@ -28,7 +29,7 @@ public: static const pos_type SEEK_EOF = (pos_type) -1; - pstream(unsigned flags) : m_flags(flags) + pstream(const unsigned flags) : m_flags(flags) { } virtual ~pstream() @@ -38,7 +39,7 @@ public: bool bad() const { return ((m_flags & FLAG_ERROR) != 0); } bool seekable() const { return ((m_flags & FLAG_SEEKABLE) != 0); } - void seek(pos_type n) + void seek(const pos_type n) { check_seekable(); return vseek(n); @@ -50,7 +51,7 @@ public: } protected: - virtual void vseek(pos_type n) = 0; + virtual void vseek(const pos_type n) = 0; virtual pos_type vtell() = 0; static const unsigned FLAG_EOF = 0x01; @@ -60,11 +61,11 @@ protected: bool closed() { return ((m_flags & FLAG_CLOSED) != 0); } - void set_flag(unsigned flag) + void set_flag(const unsigned flag) { m_flags |= flag; } - void clear_flag(unsigned flag) + void clear_flag(const unsigned flag) { m_flags &= ~flag; } @@ -96,48 +97,28 @@ class pistream : public pstream P_PREVENT_COPYING(pistream) public: - pistream(unsigned flags) : pstream(flags) {} + pistream(const unsigned flags) : pstream(flags) {} virtual ~pistream() {} bool eof() const { return ((flags() & FLAG_EOF) != 0) || bad(); } /* this digests linux & dos/windows text files */ - bool readline(pstring &line) - { - UINT8 c = 0; - pstringbuffer buf; - if (!this->read(c)) - { - line = ""; - return false; - } - while (true) - { - if (c == 10) - break; - else if (c != 13) /* ignore CR */ - buf += c; - if (!this->read(c)) - break; - } - line = buf; - return true; - } + bool readline(pstring &line); bool read(UINT8 &c) { return (read(&c, 1) == 1); } - unsigned read(void *buf, unsigned n) + unsigned read(void *buf, const unsigned n) { return vread(buf, n); } protected: /* read up to n bytes from stream */ - virtual unsigned vread(void *buf, unsigned n) = 0; + virtual unsigned vread(void *buf, const unsigned n) = 0; private: }; @@ -172,14 +153,16 @@ public: write(&c, 1); } - void write(const void *buf, unsigned n) + void write(const void *buf, const unsigned n) { vwrite(buf, n); } + void write(pistream &strm); + protected: /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n) = 0; + virtual void vwrite(const void *buf, const unsigned n) = 0; private: }; @@ -201,8 +184,8 @@ public: protected: /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n); - virtual void vseek(pos_type n); + virtual void vwrite(const void *buf, const unsigned n); + virtual void vseek(const pos_type n); virtual pos_type vtell(); private: @@ -225,11 +208,11 @@ public: protected: /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n) + virtual void vwrite(const void *buf, const unsigned n) { m_buf.cat(buf, n); } - virtual void vseek(pos_type n) { } + virtual void vseek(const pos_type n) { } virtual pos_type vtell() { return m_buf.len(); } private: @@ -251,14 +234,40 @@ public: void close(); protected: + pofilestream(void *file, const bool do_close); /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n); - virtual void vseek(pos_type n); + virtual void vwrite(const void *buf, const unsigned n); + virtual void vseek(const pos_type n); virtual pos_type vtell(); private: void *m_file; pos_type m_pos; + bool m_actually_close; + + void init(void *file); +}; + +// ----------------------------------------------------------------------------- +// pstderr: write to stderr +// ----------------------------------------------------------------------------- + +class pstderr : public pofilestream +{ + P_PREVENT_COPYING(pstderr) +public: + pstderr(); +}; + +// ----------------------------------------------------------------------------- +// pstdout: write to stdout +// ----------------------------------------------------------------------------- + +class pstdout : public pofilestream +{ + P_PREVENT_COPYING(pstdout) +public: + pstdout(); }; // ----------------------------------------------------------------------------- @@ -276,6 +285,8 @@ public: void close(); protected: + pifilestream(void *file, const bool do_close); + /* read up to n bytes from stream */ virtual unsigned vread(void *buf, unsigned n); virtual void vseek(pos_type n); @@ -284,6 +295,21 @@ protected: private: void *m_file; pos_type m_pos; + bool m_actually_close; + + void init(void *file); +}; + +// ----------------------------------------------------------------------------- +// pstdin: reads from stdin +// ----------------------------------------------------------------------------- + +class pstdin : public pifilestream +{ + P_PREVENT_COPYING(pstdin) +public: + + pstdin(); }; // ----------------------------------------------------------------------------- diff --git a/src/lib/netlist/plib/pstring.c b/src/lib/netlist/plib/pstring.c index fb952f6fe91..682f819269b 100644 --- a/src/lib/netlist/plib/pstring.c +++ b/src/lib/netlist/plib/pstring.c @@ -376,12 +376,11 @@ pstr_t *pstring_t<F>::salloc(int n) stk = palloc_array(pstack_t<pstr_t *>, 17); pstr_t *p; unsigned sn= ((32 - countleadbits(n)) + 1) / 2; - unsigned size = sizeof(pstr_t) + (1ULL<<(sn * 2)) + 1; + unsigned size = sizeof(pstr_t) + (1<<(sn * 2)) + 1; if (stk[sn].empty()) p = (pstr_t *) palloc_array(char, size); else { - //printf("%u %u\n", sn, (unsigned) stk[sn].count()); p = stk[sn].pop(); } @@ -439,25 +438,6 @@ void pstring_t<F>::resetmem() // pstring ... // ---------------------------------------------------------------------------------------- -const pstring::type_t pstring::vprintf(va_list args) const -{ - // sprintf into the temporary buffer - char tempbuf[4096]; - std::vsprintf(tempbuf, cstr(), args); - - return type_t(tempbuf); -} - - -const pstring::type_t pstring::sprintf(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - type_t ret = pstring(format).vprintf(ap); - va_end(ap); - return ret; -} - template<typename F> int pstring_t<F>::find(const pstring_t &search, unsigned start) const { @@ -616,90 +596,5 @@ void pstringbuffer::pcat(const pstring &s) m_ptr[m_len] = 0; } -pfmt::pfmt(const pstring &fmt) -: m_arg(0) -{ - memcpy(m_str, fmt.cstr(), fmt.blen() + 1); -} - -pfmt::pfmt(const char *fmt) -: m_arg(0) -{ - strncpy(m_str, fmt, sizeof(m_str) - 1); - m_str[sizeof(m_str) - 1] = 0; -} - -#if 0 -void pformat::format_element(const char *f, const char *l, const char *fmt_spec, ...) -{ - va_list ap; - va_start(ap, fmt_spec); - char fmt[30] = "%"; - char search[10] = ""; - char buf[1024]; - strcat(fmt, f); - strcat(fmt, l); - strcat(fmt, fmt_spec); - int nl = vsprintf(buf, fmt, ap); - m_arg++; - int sl = sprintf(search, "%%%d", m_arg); - char *p = strstr(m_str, search); - if (p != NULL) - { - // Make room - memmove(p+nl, p+sl, strlen(p) + 1 - sl); - memcpy(p, buf, nl); - } - va_end(ap); -} -#else -void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...) -{ - va_list ap; - va_start(ap, fmt_spec); - char fmt[30] = "%"; - char search[10] = ""; - char buf[1024]; - m_arg++; - int sl = sprintf(search, "{%d:", m_arg); - char *p = strstr(m_str, search); - if (p == NULL) - { - sl = sprintf(search, "{%d}", m_arg); - p = strstr(m_str, search); - if (p == NULL) - { - sl = 2; - p = strstr(m_str, "{}"); - } - strcat(fmt, f); - } - else - { - char *p1 = strstr(p, "}"); - if (p1 != NULL) - { - sl = p1 - p + 1; - if (m_arg>=10) - strncat(fmt, p+4, p1 - p - 4); - else - strncat(fmt, p+3, p1 - p - 3); - } - else - strcat(fmt, f); - } - strcat(fmt, l); - strcat(fmt, fmt_spec); - int nl = vsprintf(buf, fmt, ap); - if (p != NULL) - { - // Make room - memmove(p+nl, p+sl, strlen(p) + 1 - sl); - memcpy(p, buf, nl); - } - va_end(ap); -} -#endif - template struct pstring_t<pu8_traits>; template struct pstring_t<putf8_traits>; diff --git a/src/lib/netlist/plib/pstring.h b/src/lib/netlist/plib/pstring.h index 973ca02ad0f..b101ffc47a5 100644 --- a/src/lib/netlist/plib/pstring.h +++ b/src/lib/netlist/plib/pstring.h @@ -307,9 +307,6 @@ public: pstring(const mem_t *string) : type_t(string) { } pstring(const type_t &string) : type_t(string) { } - const type_t vprintf(va_list args) const; - static const type_t sprintf(const char *format, ...) ATTR_PRINTF(1,2); - }; // ---------------------------------------------------------------------------------------- @@ -381,278 +378,4 @@ private: }; -template <typename T> -struct ptype_treats -{ -}; - -template<> -struct ptype_treats<char> -{ - static short cast(char x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats<short> -{ - static short cast(short x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats<int> -{ - static int cast(int x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return ""; } -}; - -template<> -struct ptype_treats<long> -{ - static long cast(long x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "l"; } -}; - -template<> -struct ptype_treats<long long> -{ - static long long cast(long long x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "ll"; } -}; - -template<> -struct ptype_treats<unsigned char> -{ - static unsigned short cast(unsigned char x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats<unsigned short> -{ - static unsigned short cast(unsigned short x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats<unsigned int> -{ - static unsigned int cast(unsigned int x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return ""; } -}; - -template<> -struct ptype_treats<unsigned long> -{ - static unsigned long cast(unsigned long x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "l"; } -}; - -template<> -struct ptype_treats<unsigned long long> -{ - static unsigned long long cast(unsigned long long x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "ll"; } -}; - -template <typename P> -class pformat_base -{ -public: - - virtual ~pformat_base() { } - - ATTR_COLD P &operator ()(const double x, const char *f = "") { format_element(f, "", "f", x); return static_cast<P &>(*this); } - ATTR_COLD P & e(const double x, const char *f = "") { format_element(f, "", "e", x); return static_cast<P &>(*this); } - ATTR_COLD P & g(const double x, const char *f = "") { format_element(f, "", "g", x); return static_cast<P &>(*this); } - - ATTR_COLD P &operator ()(const char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast<P &>(*this); } - ATTR_COLD P &operator ()(char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast<P &>(*this); } - ATTR_COLD P &operator ()(const void *x, const char *f = "") { format_element(f, "", "p", x); return static_cast<P &>(*this); } - ATTR_COLD P &operator ()(const pstring &x, const char *f = "") { format_element(f, "", "s", x.cstr() ); return static_cast<P &>(*this); } - - template<typename T> - ATTR_COLD P &operator ()(const T x, const char *f = "") - { - if (ptype_treats<T>::is_signed) - format_element(f, ptype_treats<T>::size_specifier(), "d", ptype_treats<T>::cast(x)); - else - format_element(f, ptype_treats<T>::size_specifier(), "u", ptype_treats<T>::cast(x)); - return static_cast<P &>(*this); - } - - template<typename T> - ATTR_COLD P &x(const T x, const char *f = "") - { - format_element(f, ptype_treats<T>::size_specifier(), "x", x); - return static_cast<P &>(*this); - } - - template<typename T> - ATTR_COLD P &o(const T x, const char *f = "") - { - format_element(f, ptype_treats<T>::size_specifier(), "o", x); - return static_cast<P &>(*this); - } - -protected: - - virtual void format_element(const char *f, const char *l, const char *fmt_spec, ...) = 0; - -}; - -class pfmt : public pformat_base<pfmt> -{ -public: - ATTR_COLD pfmt(const pstring &fmt); - ATTR_COLD pfmt(const char *fmt); - - operator pstring() const { return m_str; } - - const char *cstr() { return m_str; } - - -protected: - void format_element(const char *f, const char *l, const char *fmt_spec, ...); - -private: - - char m_str[2048]; - unsigned m_arg; -}; - -enum plog_level -{ - DEBUG, - INFO, - VERBOSE, - WARNING, - ERROR, - FATAL -}; - -class plog_dispatch_intf; - -template <bool build_enabled = true> -class pfmt_writer_t -{ -public: - pfmt_writer_t() : m_enabled(true) { } - virtual ~pfmt_writer_t() { } - - ATTR_COLD void operator ()(const char *fmt) const - { - if (build_enabled && m_enabled) vdowrite(fmt); - } - - template<typename T1> - ATTR_COLD void operator ()(const char *fmt, const T1 &v1) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)); - } - - template<typename T1, typename T2> - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)); - } - - template<typename T1, typename T2, typename T3> - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)); - } - - template<typename T1, typename T2, typename T3, typename T4> - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)); - } - - template<typename T1, typename T2, typename T3, typename T4, typename T5> - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)(v5)); - } - - void set_enabled(const bool v) - { - m_enabled = v; - } - - bool is_enabled() const { return m_enabled; } - -protected: - virtual void vdowrite(const pstring &ls) const {} - -private: - bool m_enabled; - -}; - -template <plog_level L, bool build_enabled = true> -class plog_channel : public pfmt_writer_t<build_enabled> -{ -public: - plog_channel(plog_dispatch_intf *b) : pfmt_writer_t<build_enabled>(), m_base(b) { } - virtual ~plog_channel() { } - -protected: - virtual void vdowrite(const pstring &ls) const; - -private: - plog_dispatch_intf *m_base; -}; - -class plog_dispatch_intf -{ - template<plog_level, bool> friend class plog_channel; - -public: - virtual ~plog_dispatch_intf() { } -protected: - virtual void vlog(const plog_level &l, const pstring &ls) const = 0; -}; - -template<bool debug_enabled> -class plog_base -{ -public: - - plog_base(plog_dispatch_intf *proxy) - : debug(proxy), - info(proxy), - verbose(proxy), - warning(proxy), - error(proxy), - fatal(proxy) - {} - virtual ~plog_base() {}; - - plog_channel<DEBUG, debug_enabled> debug; - plog_channel<INFO> info; - plog_channel<VERBOSE> verbose; - plog_channel<WARNING> warning; - plog_channel<ERROR> error; - plog_channel<FATAL> fatal; -}; - - -template <plog_level L, bool build_enabled> -void plog_channel<L, build_enabled>::vdowrite(const pstring &ls) const -{ - m_base->vlog(L, ls); -} - #endif /* _PSTRING_H_ */ diff --git a/src/lib/netlist/plib/ptypes.h b/src/lib/netlist/plib/ptypes.h new file mode 100644 index 00000000000..5ccfe171ed4 --- /dev/null +++ b/src/lib/netlist/plib/ptypes.h @@ -0,0 +1,97 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * ptypes.h + * + */ + +#ifndef PTYPES_H_ +#define PTYPES_H_ + +#include "pconfig.h" +#include "pstring.h" + +//============================================================ +// penum - strongly typed enumeration +//============================================================ + +struct penum_base +{ +protected: + static int from_string_int(const char *str, const char *x) + { + int cnt = 0; + const char *cur = str; + int lx = strlen(x); + while (*str) + { + if (*str == ',') + { + int l = str-cur; + if (l == lx) + if (strncmp(cur, x, lx) == 0) + return cnt; + } + else if (*str == ' ') + { + cur = str + 1; + cnt++; + } + str++; + } + int l = str-cur; + if (l == lx) + if (strncmp(cur, x, lx) == 0) + return cnt; + return -1; + } + static pstring nthstr(int n, const char *str) + { + char buf[64]; + char *bufp = buf; + int cur = 0; + while (*str) + { + if (cur == n) + { + if (*str == ',') + { + *bufp = 0; + return pstring(buf); + } + else if (*str != ' ') + *bufp++ = *str; + } + else + { + if (*str == ',') + cur++; + } + str++; + } + *bufp = 0; + return pstring(buf); + } +}; + +#define P_ENUM(_name, ...) \ + struct _name : public penum_base { \ + enum e { __VA_ARGS__ }; \ + _name (e v) : m_v(v) { } \ + bool set_from_string (const pstring &s) { \ + static const char *strings = # __VA_ARGS__; \ + int f = from_string_int(strings, s.cstr()); \ + if (f>=0) { m_v = (e) f; return true; } else { return false; } \ + } \ + operator e() const {return m_v;} \ + int as_int() const {return (int) m_v;} \ + bool operator==(const _name &rhs) const {return m_v == rhs.m_v;} \ + bool operator==(const e &rhs) const {return m_v == (int) rhs;} \ + const pstring name() const { \ + static const char *strings = # __VA_ARGS__; \ + return nthstr((int) m_v, strings); \ + } \ + private: e m_v; }; + + +#endif /* PTYPES_H_ */ diff --git a/src/lib/netlist/prg/nltool.c b/src/lib/netlist/prg/nltool.c index 1951ec7468f..3510f70e7f2 100644 --- a/src/lib/netlist/prg/nltool.c +++ b/src/lib/netlist/prg/nltool.c @@ -19,6 +19,7 @@ #include "plib/poptions.h" #include "plib/pstring.h" #include "plib/plists.h" +#include "plib/ptypes.h" #include "nl_setup.h" #include "nl_factory.h" #include "nl_parser.h" @@ -122,8 +123,11 @@ public: poption_bool opt_help; }; -//Alternative -//static poption *optlist[] = { &opt_ttr, &opt_logs, &opt_file, &opt_cmd, &opt_verb, &opt_help, NULL }; +pstdout pout_strm; +pstderr perr_strm; + +pstream_fmt_writer_t pout(pout_strm); +pstream_fmt_writer_t perr(perr_strm); NETLIST_START(dummy) /* Standard stuff */ @@ -137,39 +141,6 @@ NETLIST_END() CORE IMPLEMENTATION ***************************************************************************/ -pstring filetobuf(pstring fname) -{ - pstring pbuf = ""; - - if (fname == "-") - { - char lbuf[1024]; - while (!feof(stdin)) - { - fgets(lbuf, 1024, stdin); - pbuf += lbuf; - } - printf("%d\n",*(pbuf.right(1).cstr()+1)); - return pbuf; - } - else - { - FILE *f; - f = fopen(fname.cstr(), "rb"); - fseek(f, 0, SEEK_END); - long fsize = ftell(f); - fseek(f, 0, SEEK_SET); - - char *buf = (char *) malloc(fsize + 1); - fread(buf, fsize, 1, f); - buf[fsize] = 0; - fclose(f); - pbuf = buf; - free(buf); - return pbuf; - } -} - class netlist_tool_t : public netlist::netlist_t { public: @@ -225,30 +196,9 @@ protected: void vlog(const plog_level &l, const pstring &ls) const { - switch (l) - { - case DEBUG: - case INFO: - break; - case VERBOSE: - if (m_opts ? m_opts->opt_verb() : false) - { - printf("%s\n", ls.cstr()); - } - break; - case WARNING: - if (!(m_opts ? m_opts->opt_quiet() : false)) - { - printf("%s\n", ls.cstr()); - } - break; - case ERROR: - printf("%s\n", ls.cstr()); - break; - case FATAL: - printf("%s\n", ls.cstr()); - throw; - } + pout("{}: {}\n", l.name().cstr(), ls.cstr()); + if (l == plog_level::FATAL) + throw; } private: @@ -258,14 +208,14 @@ private: void usage(tool_options_t &opts) { - fprintf(stderr, + perr("{}", "Usage:\n" " nltool -help\n" " nltool [options]\n" "\n" "Where:\n" ); - fprintf(stderr, "%s\n", opts.help().cstr()); + perr("{}\n", opts.help().cstr()); } struct input_t @@ -336,14 +286,20 @@ static void run(tool_options_t &opts) nt.m_opts = &opts; nt.init(); + + if (!opts.opt_verb()) + nt.log().verbose.set_enabled(false); + if (opts.opt_quiet()) + nt.log().warning.set_enabled(false); + nt.read_netlist(opts.opt_file(), opts.opt_name()); plist_t<input_t> *inps = read_input(&nt, opts.opt_inp()); double ttr = opts.opt_ttr(); - printf("startup time ==> %5.3f\n", (double) (osd_ticks() - t) / (double) osd_ticks_per_second() ); - printf("runnning ...\n"); + pout("startup time ==> {1:5.3f}\n", (double) (osd_ticks() - t) / (double) osd_ticks_per_second() ); + pout("runnning ...\n"); t = osd_ticks(); unsigned pos = 0; @@ -361,7 +317,7 @@ static void run(tool_options_t &opts) pfree(inps); double emutime = (double) (osd_ticks() - t) / (double) osd_ticks_per_second(); - printf("%f seconds emulation took %f real time ==> %5.2f%%\n", ttr, emutime, ttr/emutime*100.0); + pout("{1:f} seconds emulation took {2:f} real time ==> {3:5.2f}%\n", ttr, emutime, ttr/emutime*100.0); } /*------------------------------------------------- @@ -446,11 +402,11 @@ int main(int argc, char *argv[]) tool_options_t opts; int ret; - fprintf(stderr, "%s", "WARNING: This is Work In Progress! - It may fail anytime\n"); - fprintf(stderr, "Update dispatching using method %s\n", pmf_verbose[NL_PMF_TYPE]); + perr("{}", "WARNING: This is Work In Progress! - It may fail anytime\n"); + perr("Update dispatching using method {}\n", pmf_verbose[NL_PMF_TYPE]); if ((ret = opts.parse(argc, argv)) != argc) { - fprintf(stderr, "Error parsing %s\n", argv[ret]); + perr("Error parsing {}\n", argv[ret]); usage(opts); return 1; } @@ -468,23 +424,44 @@ int main(int argc, char *argv[]) run(opts); else if (cmd == "convert") { - pstring contents = filetobuf(opts.opt_file()); - nl_convert_base_t *converter = NULL; + pstring contents; + postringstream ostrm; + if (opts.opt_file() == "-") + { + pstdin f; + ostrm.write(f); + } + else + { + pifilestream f(opts.opt_file()); + ostrm.write(f); + } + contents = ostrm.str(); + + pstring result; if (opts.opt_type().equals("spice")) - converter = palloc(nl_convert_spice_t); + { + nl_convert_spice_t c; + c.convert(contents); + result = c.result(); + } else - converter = palloc(nl_convert_eagle_t); - converter->convert(contents); + { + nl_convert_eagle_t c; + c.convert(contents); + result = c.result(); + } /* present result */ - printf("%s\n", converter->result().cstr()); - pfree(converter); + pout_strm.write(result.cstr()); } else { - fprintf(stderr, "Unknown command %s\n", cmd.cstr()); + perr("Unknown command {}\n", cmd.cstr()); usage(opts); return 1; } + + pstring::resetmem(); #if (!PSTANDALONE) } dump_unfreed_mem(); diff --git a/src/lib/netlist/solver/nld_ms_direct_lu.h b/src/lib/netlist/solver/nld_ms_direct_lu.h index 5d7587c0ef1..d988942058e 100644 --- a/src/lib/netlist/solver/nld_ms_direct_lu.h +++ b/src/lib/netlist/solver/nld_ms_direct_lu.h @@ -237,7 +237,7 @@ template <unsigned m_N, unsigned _storage_N> ATTR_COLD void matrix_solver_direct_t<m_N, _storage_N>::vsetup(analog_net_t::list_t &nets) { if (m_dim < nets.size()) - netlist().error("Dimension {1} less than {2}", m_dim, SIZET_PRINTF(nets.size())); + netlist().error("Dimension {1} less than {2}", m_dim,nets.size()); for (unsigned k = 0; k < N(); k++) { diff --git a/src/lib/netlist/solver/nld_solver.h b/src/lib/netlist/solver/nld_solver.h index 608b744372a..040866c5736 100644 --- a/src/lib/netlist/solver/nld_solver.h +++ b/src/lib/netlist/solver/nld_solver.h @@ -14,9 +14,6 @@ //#define ATTR_ALIGNED(N) __attribute__((aligned(N))) #define ATTR_ALIGNED(N) ATTR_ALIGN -#define SOLVER_VERBOSE_OUT(x) do {} while (0) -//#define SOLVER_VERBOSE_OUT(x) printf x - // ---------------------------------------------------------------------------------------- // Macros // ---------------------------------------------------------------------------------------- |