52 static int checker_count_nodes (
struct definition_t * def) {
64 if (def != NULL && action == def->
action)
return def;
70 static int checker_find_property (
const char * key,
struct pair_t * pp) {
73 if (!strcmp (pp->
key, key))
82 static int checker_is_property (
struct define_t * available,
98 static int checker_count_definition (
struct definition_t * root,
99 const char * type,
char * instance) {
101 for (
struct definition_t * def = root; def != NULL; def = def->next) {
102 if (!strcmp (def->
type, type) && !strcmp (def->instance, instance)) {
118 for (
struct definition_t * def = root; def != NULL; def = def->next) {
119 if (!strcmp (def->
type, type)) {
120 for (pair = def->pairs; pair != NULL; pair = pair->
next) {
121 if (!strcmp (pair->
key, key))
122 if (pair->
value->
ident != NULL && ident != NULL &&
138 for (pair = def->
pairs; pair != NULL; pair = pair->
next) {
139 if (!strcmp (pair->
key, key))
152 for (pair = def->
pairs; pair != NULL; pair = pair->
next) {
153 if (!strcmp (pair->
key, key))
162 static int checker_find_property (
struct definition_t * def,
164 return checker_find_property (key, def->
pairs);
174 if ((val = checker_find_reference (def, key)) == NULL) {
189 if ((val = checker_find_reference (def,
"Subst")) != NULL) {
190 if (ident != NULL && !strcmp (val->
ident, ident))
199 char *
var,
int type,
bool pass) {
217 static int checker_resolve_variable (
struct definition_t * root,
219 struct pair_t * pair,
int type) {
222 if (value->
ident != NULL) {
225 if ((val = checker_find_variable (root,
"SW",
"Param", value->
ident))) {
227 if (!strcmp (def->
type,
"SW") && !strcmp (pair->
key,
"Param")) {
228 checker_add_variable (root->
env, value->
ident, TAG_DOUBLE,
true);
232 val->
var = TAG_DOUBLE;
233 value->
var = TAG_DOUBLE;
237 if ((val = checker_find_variable (root,
"SW",
"Sim", value->
ident))) {
241 if ((val = checker_find_substrate (def, value->
ident))) {
246 if ((val = checker_find_variable (root,
"Sub",
"Type", value->
ident))) {
254 if ((val = checker_find_variable (root,
"SPfile",
"File", value->
ident))) {
261 value->
var = (type ==
PROP_LIST) ? TAG_VECTOR : TAG_DOUBLE;
264 checker_add_variable (root->
env, value->
ident, value->
var,
false);
270 if ((val = checker_find_variable (root,
"Vfile",
"File", value->
ident))) {
273 if ((val = checker_find_variable (root,
"Ifile",
"File", value->
ident))) {
282 char * txt = (
char *)
283 malloc (strlen (def->
instance) + 1 + strlen (value->
ident) + 1);
285 char * ref = (
char *)
286 malloc (strlen (def->
instance) + 5 + strlen (value->
ident) + 1);
291 value->
ident = strdup (ref);
292 value->
var = TAG_DOUBLE;
298 checker_add_variable (root->
env, ref, TAG_DOUBLE,
false);
312 "used in a `%s:%s' property\n", def->
line, value->
ident,
323 static int checker_evaluate_scale (
struct value_t * value) {
324 double val = value->
value, factor = 1.0;
326 if (value->
scale != NULL) {
327 scale = value->
scale;
330 scale++; factor = 1e18;
break;
332 scale++; factor = 1e15;
break;
334 scale++; factor = 1e12;
break;
336 scale++; factor = 1e9;
break;
338 scale++; factor = 1e6;
break;
340 scale++; factor = 1e3;
break;
346 scale++; factor = 2.54e-5;
353 scale++; factor = 1
e-6;
break;
355 scale++; factor = 1
e-9;
break;
357 scale++; factor = 1
e-12;
break;
361 scale++; factor = 0.3048;
367 scale++; factor = 1
e-18;
break;
371 scale++; val =
pow (10.0, val / 10.0);
373 scale++; factor = 1
e-3;
375 else if (*scale ==
'u') {
376 scale++; factor = 1
e-6;
383 scale++; factor = 2.54e-2;
389 scale++; factor = 0.9144;
393 if (*scale !=
'\0') {
394 value->
unit = strdup (scale);
399 value->
value = val * factor;
405 static int checker_count_definitions (
struct definition_t * root,
406 const char * type,
int action) {
409 if (def->
action == action) {
412 else if (!strcmp (def->
type, type))
422 static struct definition_t * checker_find_subcircuit (
char *
n) {
424 for (def = subcircuit_root; def != NULL; def = def->
next)
425 if (n != NULL && !strcmp (def->
instance, n))
return def;
435 if ((val = checker_find_reference (def,
"Type")) != NULL)
436 sub = checker_find_subcircuit (val->
ident);
441 static int checker_sub_cycles = 0;
445 static int checker_count_nonlinearities (
struct definition_t * root) {
451 if (checker_sub_cycles <= 0) {
452 if (!strcmp (def->
type,
"Sub")) {
453 if ((sub = checker_get_subcircuit (def)) != NULL) {
454 count += checker_count_nonlinearities (sub->
sub);
464 static int checker_count_action (
struct definition_t * root,
char * instance) {
477 static int checker_validate_para_cycles (
struct definition_t * root,
478 char * instance,
strlist * deps) {
487 "detected, involves: %s\n", instance, deps->
toString ());
492 if (!strcmp (def->
type,
"SW")) {
493 if ((val = checker_find_reference (def,
"Sim")) != NULL) {
494 return checker_validate_para_cycles (root, val->
ident, deps);
505 static int checker_validate_para (
struct definition_t * root) {
510 if (def->
action == 1 && !strcmp (def->
type,
"SW")) {
512 if ((val = checker_validate_reference (def,
"Sim")) == NULL) {
523 if (checker_count_action (root, val->
ident) != 1) {
525 "found as referred in `%s:%s'\n", def->
line, val->
ident,
531 errors += checker_validate_para_cycles (root, val->
ident, deps);
553 static int checker_validate_ports (
struct definition_t * root) {
557 const char * prop =
"Num";
558 while ((def = checker_find_port (def)) != NULL) {
559 if ((val = checker_find_prop_value (def, prop)) != NULL) {
560 p = (int) val->
value;
562 while ((port = checker_find_port (port)) != NULL) {
564 if ((val = checker_find_prop_value (port, prop)) != NULL) {
565 if (p == (
int) val->
value) {
567 "with duplicate `%s=%d' property found: `%s:%s' and "
585 static int checker_validate_lists (
struct definition_t * root) {
591 if (def->
action == 1 && (!strcmp (def->
type,
"SW") ||
592 !strcmp (def->
type,
"AC") ||
593 !strcmp (def->
type,
"SP"))) {
594 struct value_t * val = checker_find_reference (def,
"Type");
595 char * type = val->
ident;
597 if (type && (!strcmp (type,
"const") || !strcmp (type,
"list"))) {
599 if ((val = checker_find_prop_value (def,
"Values")) == NULL) {
600 if (!strcmp (type,
"const")) {
601 if ((val = checker_validate_reference (def,
"Values")) == NULL) {
607 "`%s' not found in `%s:%s'\n", def->
line,
"Values",
613 if (!strcmp (type,
"const")) {
615 if (val->
next != NULL) {
617 "needs to be a single constant value in `%s:%s', no "
618 "lists possible\n", def->
line,
"Values",
622 val->
var = TAG_UNKNOWN;
624 if (!strcmp (type,
"list")) {
625 val->
var = TAG_VECTOR;
628 for (; val != NULL; val = val->
next) {
629 if (!checker_evaluate_scale (val))
634 if (checker_find_property (def,
"Start") > 0) {
636 "`%s' is invalid in `%s:%s'\n", def->
line,
"Start",
641 if (checker_find_property (def,
"Stop") > 0) {
643 "`%s' is invalid in `%s:%s'\n", def->
line,
"Stop",
648 if (checker_find_property (def,
"Points") > 0) {
650 "`%s' is invalid in `%s:%s'\n", def->
line,
"Points",
656 else if (type && (!strcmp (type,
"lin") || !strcmp (type,
"log"))) {
658 if (checker_find_property (def,
"Start") <= 0) {
660 "`%s' not found in `%s:%s'\n", def->
line,
"Start",
665 if (checker_find_property (def,
"Stop") <= 0) {
667 "`%s' not found in `%s:%s'\n", def->
line,
"Stop",
672 if (checker_find_property (def,
"Points") <= 0) {
674 "`%s' not found in `%s:%s'\n", def->
line,
"Points",
679 if (checker_find_property (def,
"Values") > 0) {
681 "`%s' is invalid in `%s:%s'\n", def->
line,
"Values",
693 static int checker_validate_actions (
struct definition_t * root) {
694 int a,
c,
n, errors = 0;
695 if ((n = checker_count_definitions (root, NULL, 1)) < 1) {
701 if ((a = checker_count_definitions (root,
"SP", 1)) >= 1) {
702 if ((n = checker_count_definitions (root,
"Pac", 0)) < 1) {
704 "least 1 required\n", n);
709 a += checker_count_definitions (root,
"AC", 1);
711 c = checker_count_nonlinearities (root);
712 n = checker_count_definitions (root,
"DC", 1);
715 "single or none required\n", n);
718 if (a >= 1 && c >= 1 && n < 1) {
720 "circuit definition (accounted %d non-linearities)\n", c);
724 errors += checker_validate_para (root);
725 errors += checker_validate_ports (root);
726 errors += checker_validate_lists (root);
733 static int checker_validate_strips (
struct definition_t * root) {
741 if ((val = checker_validate_reference (def,
"Subst")) == NULL) {
745 if (checker_count_definition (root,
"SUBST", val->
ident) != 1) {
747 "`%s' found as specified in `%s:%s'\n", def->
line,
754 if ((val = checker_validate_reference (def,
"Model")) == NULL) {
766 static int checker_count_nodes (
struct definition_t * root,
char * n) {
771 for (node = def->
nodes; node != NULL; node = node->
next)
772 if (!strcmp (node->
node, n)) count++;
780 static int checker_count_nodesets (
struct definition_t * root,
char * n) {
785 if (!strcmp (node, n)) {
796 static int checker_validate_nodesets (
struct definition_t * root) {
799 if (def->
nodeset && checker_count_nodes (def) == 1) {
801 if (checker_count_nodes (root, node) <= 0) {
803 "as referenced by `%s:%s'\n", def->
line, node, def->
type,
807 if (checker_count_nodesets (root, node) > 1) {
809 "uniquely defined by `%s:%s'\n", def->
line, node, def->
type,
822 static int netlist_checker_variables_intern (
struct definition_t * root,
833 if (def->
action == 1 && !strcmp (def->
type,
"SW")) {
834 para = checker_find_reference (def,
"Param");
835 ref = checker_find_reference (def,
"Sim");
836 if (para != NULL && ref != NULL) {
840 "already defined by `%s:%s'\n", para->
ident,
846 if ((pos = vars->
index (para->
ident)) != -1) {
847 if (strcmp (ref->
ident, refs->
get (pos))) {
849 "already defined by `%s:%s'\n", para->
ident, def->
type,
857 if (strcmp (para->
ident, vars->
get (pos))) {
859 "in `%s:%s' and `%s' in `%s:%s' for `%s'\n",
861 vars->
get (pos), def->
type, instances->
get (pos),
883 return netlist_checker_variables_intern (definition_root, env);
889 static int checker_value_in_prop_range (
char * instance,
struct define_t * def,
899 "checker error, value of `%s' needs to be "
900 "a single value in `%s:%s', no lists possible\n",
908 val->
var = TAG_VECTOR;
910 for (; val != NULL; val = val->
next) {
911 if (!checker_evaluate_scale (val))
921 "checker notice, value of `%s' (variable `%s') could be "
922 "out of range `%c%g,%g%c' in `%s:%s'\n",
927 for (; val != NULL; val = val->
next) {
939 "checker error, value of `%s' (%g) is out of "
940 "range `%c%g,%g%c' in `%s:%s'\n",
950 if (modf (pp->
value->
value, &integral) != 0) {
952 "checker error, value of `%s' (%g) needs to be "
953 "an integer in `%s:%s'\n",
964 "checker error, value of `%s' (%g) needs to be "
965 "an identifier in `%s:%s'\n",
973 char range[256]; sprintf (range,
"[");
980 range[strlen (range) - 1] =
']';
982 "checker error, value of `%s' (%s) needs to be "
983 "in %s in `%s:%s'\n",
997 static int checker_value_in_range (
char * instance,
struct define_t * def,
1003 errors += checker_value_in_prop_range (instance, def, pp,
1010 errors += checker_value_in_prop_range (instance, def, pp,
1014 return errors ? 0 : 1;
1022 checker_build_subcircuits (
struct definition_t * root) {
1024 for (prev = NULL, def = root; def != NULL; def =
next) {
1026 if (!strcmp (def->
type,
"Def")) {
1032 def->
sub = checker_build_subcircuits (def->
sub);
1034 subcircuit_root = def;
1065 static void checker_xlat_subcircuit_nodes (
struct definition_t * type,
1068 struct node_t *
n, * ninst, * ntype;
1071 for (i = 1, ntype = type->
nodes, ninst = inst->
nodes; ntype != NULL;
1072 ntype = ntype->
next, ninst = ninst->
next, i++) {
1073 for (n = sub->
nodes; n != NULL; n = n->
next) {
1076 if (!strcmp (n->
node, ntype->
node)) {
1087 static char * checker_subcircuit_node (
char * type,
char * instances,
1088 char * instance,
char * node) {
1089 char * txt = (
char *)
1090 calloc (1, strlen (type) + strlen (instance) + strlen (node) +
1091 (instances ? strlen (instances) : 0) + 4);
1093 sprintf (txt,
"%s.%s.%s.%s", type, instances, instance, node);
1095 sprintf (txt,
"%s.%s.%s", type, instance, node);
1103 for (root = NULL; nodes != NULL; nodes =
next) {
1116 checker_copy_subcircuit_nodes (
struct definition_t * type,
1121 struct node_t *
n, * ncopy, * root = NULL;
1124 for (n = sub->
nodes; n != NULL; n = n->
next) {
1127 ncopy = (
struct node_t *) calloc (
sizeof (
struct node_t), 1);
1130 if (instances == NULL)
1135 else if (!strcmp (n->
node,
"gnd")) {
1138 else if (n->
node[strlen (n->
node) - 1] ==
'!') {
1142 ncopy->
node = checker_subcircuit_node (type->
instance, instances,
1156 static struct node_t * checker_get_circuit_node (
struct node_t * root,
int n) {
1157 for (
int i = 1; i <
n; i++) {
1159 assert (root != NULL);
1165 static void checker_cleanup_xlat_nodes (
struct definition_t * sub) {
1167 if (n->xlate) free (n->xlate);
1181 checker_copy_circuit_nodes (
struct definition_t * type,
1189 for (ncopy = copy->
nodes; ncopy != NULL; ncopy = ncopy->
next) {
1191 if (ncopy->
node == NULL) {
1194 n = checker_get_circuit_node (sub->
nodes, ncopy->
xlatenr);
1197 if (instances == NULL)
1203 else if (!strcmp (n->
node,
"gnd")) {
1206 else if (n->
node[strlen (n->
node) - 1] ==
'!') {
1210 ncopy->
node = checker_subcircuit_node (type->
instance, instances,
1220 checker_find_last_definition (
struct definition_t * root) {
1222 if (def->
next == NULL)
return def;
1228 static char * checker_subcircuit_instance_list (
strlist * instances) {
1229 if (instances && instances->
length () > 0)
1237 static char * checker_subcircuit_instance (
char * type,
char * instances,
1238 char * instance,
char * base) {
1239 char * txt = (
char *)
1240 calloc (1, strlen (type) + strlen (instance) + strlen (base) +
1241 (instances ? strlen (instances) : 0) + 4);
1243 sprintf (txt,
"%s.%s.%s.%s", type, instances, instance, base);
1245 sprintf (txt,
"%s.%s.%s", type, instance, base);
1268 for (
struct pair_t * pair = inst->
pairs; pair != NULL; pair = pair->
next) {
1270 if (strcmp (pair->
key,
"Type")) {
1284 for (def = type->
sub; def != NULL; def = def->
next) {
1287 checker_xlat_subcircuit_nodes (type, inst, def);
1290 if (!strcmp (def->
type,
"Sub")) {
1292 struct definition_t * sub = checker_get_subcircuit (def);
1294 if ((*instances) == NULL) (*instances) =
new strlist ();
1295 instcopy =
new strlist (*(*instances));
1297 (*instances)->append (inst->
instance);
1298 copy = checker_copy_subcircuits (sub, def, instances, child);
1301 list = checker_subcircuit_instance_list (instcopy);
1304 checker_copy_circuit_nodes (type, inst, def, c, list);
1307 struct definition_t * last = checker_find_last_definition (copy);
1313 *instances = instcopy;
1317 copy = checker_copy_subcircuit (def);
1319 list = checker_subcircuit_instance_list (*instances);
1321 checker_subcircuit_instance (type->
instance, list,
1325 checker_copy_subcircuit_nodes (type, inst, def, copy, list);
1334 checker_cleanup_xlat_nodes (def);
1340 icopy->
append (*(instances));
1353 static int checker_validate_sub_cycles (
struct definition_t * root,
1354 char * type,
char * instance,
1356 int errors = 0,
error;
1361 if ((*deps)->contains (type)) {
1363 "detected, involves: %s\n",
1364 type, instance, (*deps)->toString ());
1367 (*deps)->append (type);
1374 if (!strcmp (def->
type,
"Sub")) {
1376 if ((val = checker_find_reference (def,
"Type")) != NULL) {
1383 sub = checker_find_subcircuit (val->
ident);
1416 d->
nodes = checker_count_nodes (def);
1420 for (o = r = 0, p = def->
pairs; p != NULL; p = p->
next) {
1421 if (p->
value == NULL)
1432 for (o = r = 0, p = def->
pairs; p != NULL; p = p->
next) {
1433 if (p->
value == NULL) {
1472 static void netlist_free_define (
struct define_t * d) {
1475 free ((
char *) d->
type);
1477 for (i = 0, p = d->
required; p[i].
key != NULL; i++) {
1478 free ((
char *) p[i].key);
1482 for (i = 0, p = d->
optional; p[i].
key != NULL; i++) {
1483 free ((
char *) p[i].key);
1492 static int checker_validate_properties (
struct definition_t * root,
1496 int i,
n, errors = 0;
1503 "`%s' occurred %dx in `%s:%s'\n", def->
line,
1513 "`%s' occurred %dx in `%s:%s'\n", def->
line,
1520 for (pair = def->
pairs; pair != NULL; pair = pair->
next) {
1522 int type = checker_is_property (available, pair->
key);
1524 if (strcmp (def->
type,
"Def")) {
1526 "line %d: checker error, extraneous property `%s' is "
1527 "invalid in `%s:%s'\n", def->
line,
1533 if (pair->
value != NULL) {
1535 if (!checker_evaluate_scale (pair->
value))
1538 if (!checker_value_in_range (def->
instance, available, pair)) {
1542 if (!checker_resolve_variable (root, def, pair, type))
1552 static int checker_validate_subcircuits (
struct definition_t * root) {
1557 if (!strcmp (def->
type,
"Sub")) {
1560 if ((val = checker_validate_reference (def,
"Type")) == NULL) {
1568 "`%s' found as referred in `%s:%s'\n", def->
line,
1574 int n1 = checker_count_nodes (def);
1575 int n2 = checker_count_nodes (sub);
1578 "`%s' requires %d nodes in `%s:%s', found %d\n",
1584 struct define_t * available = netlist_create_define (sub);
1585 errors += checker_validate_properties (root, def, available);
1586 netlist_free_define (available);
1589 int err = checker_validate_sub_cycles (sub, sub->
instance,
1592 checker_sub_cycles = err;
1602 static void netlist_free_nodes (
struct node_t * node) {
1604 for (; node != NULL; node =
n) {
1612 static void netlist_free_value (
struct value_t * value) {
1614 if (value->
unit) free (value->
unit);
1620 static void netlist_free_pairs (
struct pair_t * pp) {
1622 for (; pp != NULL; pp = np) {
1625 for (value = pp->
value; value != NULL; value = nv) {
1627 netlist_free_value (value);
1635 static void netlist_free_definition (
struct definition_t * def) {
1636 netlist_free_nodes (def->
nodes);
1637 if (!def->
copy) netlist_free_pairs (def->
pairs);
1652 netlist_free_definition (cand);
1656 for (prev = root; prev != NULL && prev->
next != cand; prev = prev->
next) ;
1659 netlist_free_definition (cand);
1674 for (prev = NULL, def = root; def != NULL; def =
next) {
1677 if (!strcmp (def->
type,
"Sub")) {
1679 sub = checker_get_subcircuit (def);
1681 copy = checker_copy_subcircuits (sub, def, &instances, parent);
1682 if (instances) {
delete instances; instances = NULL; }
1689 netlist_free_definition (def);
1692 struct definition_t * last = checker_find_last_definition (copy);
1694 if (!prev) prev = last;
1710 static int netlist_checker_intern (
struct definition_t * root) {
1716 for (def = root; def != NULL; def = def->
next) {
1719 available = checker_find_definition (def->
type, def->
action);
1720 if (available == NULL) {
1727 def->
nodeset = !strcmp (def->
type,
"NodeSet") ? 1 : 0;
1736 n = def->
ncount = checker_count_nodes (def);
1740 "line %d: checker error, at least 1 node required in "
1741 "`%s:%s', found %d\n", def->
line, def->
type,
1746 else if (available->
nodes != n) {
1748 "line %d: checker error, %d node(s) required in `%s:%s', "
1749 "found %d\n", def->
line,
1754 if (strcmp (def->
type,
"Sub")) {
1755 errors += checker_validate_properties (root, def, available);
1759 n = checker_count_definition (root, def->
type, def->
instance);
1767 errors += checker_validate_strips (root);
1769 errors += checker_validate_subcircuits (root);
1771 errors += checker_validate_nodesets (root);
1777 static void netlist_list_value (
struct value_t * value) {
1780 else if (value->
ident)
1782 else if (value->
next) {
1784 for (; value != NULL; value = value->
next)
1797 static void netlist_lister (
struct definition_t * root,
const char * prefix) {
1801 for (def = root; def != NULL; def = def->
next) {
1803 for (node = def->
nodes; node != NULL; node = node->
next) {
1806 for (pair = def->
pairs; pair != NULL; pair = pair->
next) {
1808 netlist_list_value (pair->
value);
1819 netlist_lister (definition_root,
" ");
1820 for (def = subcircuit_root; def != NULL; def = def->
next) {
1822 netlist_lister (def->
sub,
" ");
1837 for (count = 0, cir = definition_root; cir != NULL; cir = cir->
next) {
1838 if (!strcmp (def->
type, cir->
type)) count++;
1855 for (prev = NULL, def = root; def != NULL; def =
next) {
1857 if (!strcmp (def->
type,
"Eqn")) {
1870 netlist_free_definition (def);
1879 static void checker_setup_env (
struct definition_t * root,
1894 if (root) root->
env =
env;
1899 static void checker_subcircuit_args (
struct definition_t * def,
1901 for (
struct pair_t * pair = def->
pairs; pair != NULL; pair = pair->
next) {
1903 if (strcmp (pair->
key,
"Type")) {
1908 variable * v = checker_add_variable (env, pair->
key, TAG_DOUBLE,
true);
1924 definition_root = checker_build_subcircuits (definition_root);
1926 definition_root = checker_build_equations (definition_root, &eqns);
1928 checker_setup_env (definition_root, env_root, eqns);
1930 errors += netlist_checker_intern (subcircuit_root);
1932 errors += netlist_checker_intern (definition_root);
1939 for (def = subcircuit_root; def != NULL; def = def->
next) {
1941 def->
sub = checker_build_equations (def->
sub, &eqns);
1945 checker_setup_env (def, subenv, eqns);
1948 checker_subcircuit_args (def, subenv);
1950 errors += netlist_checker_intern (def->
sub);
1958 errors += checker_validate_actions (definition_root);
1962 env->
copy (*env_root);
1964 definition_root = checker_expand_subcircuits (definition_root, env);
1967 return errors ? -1 : 0;
1971 static void netlist_destroy_intern (
struct definition_t * root) {
1973 for (def = root; def != NULL; def =
next) {
1975 netlist_free_definition (def);
1981 netlist_destroy_intern (definition_root);
1983 netlist_destroy_intern (def->
sub);
1985 netlist_destroy_intern (subcircuit_root);
1986 definition_root = subcircuit_root = NULL;
1992 if (env_root != NULL) {