Started working on SPICE to netlist conversion. (nw)

This commit is contained in:
couriersud 2015-05-17 13:21:20 +02:00
parent ebc96303ee
commit f63baa6dca
3 changed files with 101 additions and 14 deletions

View File

@ -21,7 +21,7 @@ private:
public: public:
typedef plinearlist_t<pstring, 10> pstring_list; typedef plinearlist_t<pstring, 10> pstring_list;
static pstring_list split(const pstring &str, const pstring &onstr) static pstring_list split(const pstring &str, const pstring &onstr, bool ignore_empty = false)
{ {
pstring_list temp; pstring_list temp;
@ -31,12 +31,18 @@ public:
pn = str.find(onstr, p); pn = str.find(onstr, p);
while (pn>=0) while (pn>=0)
{ {
temp.add(str.substr(p, pn - p)); pstring t = str.substr(p, pn - p);
if (!ignore_empty || t.len() != 0)
temp.add(t);
p = pn + onstr.len(); p = pn + onstr.len();
pn = str.find(onstr, p); pn = str.find(onstr, p);
} }
if (p<str.len()) if (p<str.len())
temp.add(str.substr(p)); {
pstring t = str.substr(p);
if (!ignore_empty || t.len() != 0)
temp.add(t);
}
return temp; return temp;
} }
}; };

View File

@ -70,8 +70,6 @@
//#define NETLIST_DEVELOPMENT 1 //#define NETLIST_DEVELOPMENT 1
#include "netlist/nl_dice_compat.h" #include "netlist/nl_dice_compat.h"
#include "netlist/devices/net_lib.h"
#include "netlist/analog/nld_twoterm.h"
static Mono555Desc a3_555_desc(K_OHM(100.0), U_FARAD(0.1)); static Mono555Desc a3_555_desc(K_OHM(100.0), U_FARAD(0.1));

View File

@ -83,7 +83,8 @@ struct options_entry oplist[] =
{ {
{ "time_to_run;t", "1.0", OPTION_FLOAT, "time to run the emulation (seconds)" }, { "time_to_run;t", "1.0", OPTION_FLOAT, "time to run the emulation (seconds)" },
{ "logs;l", "", OPTION_STRING, "colon separated list of terminals to log" }, { "logs;l", "", OPTION_STRING, "colon separated list of terminals to log" },
{ "f", "-", OPTION_STRING, "file to process (default is stdin)" }, { "file;f", "-", OPTION_STRING, "file to process (default is stdin)" },
{ "cmd;c", "run", OPTION_STRING, "run|convert|listdevices" },
{ "listdevices;ld", "", OPTION_BOOLEAN, "list all devices available for use" }, { "listdevices;ld", "", OPTION_BOOLEAN, "list all devices available for use" },
{ "help;h", "0", OPTION_BOOLEAN, "display help" }, { "help;h", "0", OPTION_BOOLEAN, "display help" },
{ NULL, NULL, 0, NULL } { NULL, NULL, 0, NULL }
@ -101,9 +102,9 @@ NETLIST_END()
CORE IMPLEMENTATION CORE IMPLEMENTATION
***************************************************************************/ ***************************************************************************/
const char *filetobuf(pstring fname) pstring filetobuf(pstring fname)
{ {
static pstring pbuf = ""; pstring pbuf = "";
if (fname == "-") if (fname == "-")
{ {
@ -114,7 +115,7 @@ const char *filetobuf(pstring fname)
pbuf += lbuf; pbuf += lbuf;
} }
printf("%d\n",*(pbuf.right(1).cstr()+1)); printf("%d\n",*(pbuf.right(1).cstr()+1));
return pbuf.cstr(); return pbuf;
} }
else else
{ {
@ -128,7 +129,9 @@ const char *filetobuf(pstring fname)
fread(buf, fsize, 1, f); fread(buf, fsize, 1, f);
buf[fsize] = 0; buf[fsize] = 0;
fclose(f); fclose(f);
return buf; pbuf = buf;
free(buf);
return pbuf;
} }
} }
@ -291,6 +294,81 @@ static void listdevices()
} }
} }
/*-------------------------------------------------
convert - convert a spice netlist
-------------------------------------------------*/
struct sp_net_t
{
const pstring &name() { return m_name;}
pstring m_name;
nl_util::pstring_list m_terminals;
};
static pnamedlist_t<sp_net_t *> nets;
static void add_term(pstring netname, pstring termname)
{
sp_net_t * net = nets.find(netname);
if (net == NULL)
{
net = new sp_net_t;
net->m_name = netname;
nets.add(net, false);
}
net->m_terminals.add(termname);
}
static void convert(core_options &opts)
{
pstring spnlf = filetobuf(opts.value("f"));
nl_util::pstring_list spnl = nl_util::split(spnlf, "\n");
for (int i=0; i < spnl.count(); i++)
{
pstring line = spnl[i].trim();
if (line != "" && line.left(1) != "*")
{
nl_util::pstring_list tt = nl_util::split(line, " ", true);
switch (tt[0].cstr()[0])
{
case '.':
// e.g. SUBCKT - ignored for now
break;
case 'Q':
printf("QBJT(%s, \"%s\")\n", tt[0].cstr(), tt[4].cstr());
add_term(tt[1], tt[0] + ".C");
add_term(tt[2], tt[0] + ".B");
add_term(tt[3], tt[0] + ".E");
break;
case 'R':
// FIXME: Rewrite resistor value
printf("RES(%s, %s)\n", tt[0].cstr(), tt[3].cstr());
add_term(tt[1], tt[0] + ".1");
add_term(tt[2], tt[0] + ".2");
break;
default:
printf("%s: %s\n", tt[0].cstr(), line.cstr());
}
}
}
// print nets
for (int i=0; i<nets.count(); i++)
{
sp_net_t * net = nets[i];
//printf("Net %s\n", net->name().cstr());
printf("NET_C(%s", net->m_terminals[0].cstr() );
for (int j=1; j<net->m_terminals.count(); j++)
{
printf(", %s", net->m_terminals[j].cstr() );
}
printf(")\n");
}
}
/*------------------------------------------------- /*-------------------------------------------------
main - primary entry point main - primary entry point
-------------------------------------------------*/ -------------------------------------------------*/
@ -315,13 +393,18 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (opts.bool_value("ld")) pstring cmd = opts.value("c");
{ if (cmd == "listdevices")
listdevices(); listdevices();
} else if (cmd == "run")
run(opts);
else if (cmd == "convert")
convert(opts);
else else
{ {
run(opts); fprintf(stderr, "Unknown command %s\n", cmd.cstr());
usage(opts);
return 1;
} }
return 0; return 0;