📄 node.c
字号:
#include <string.h>
#include <stdio.h>
#include "ansidecl.h"
#include "hash.h"
#include "graph.h"
#define error(...)
/////////////////////// predefined power source nodes////////////////////////
#define CCS 1
#define CVS 2
#define GND 3
typedef int srctype_t;
typedef struct source_def {
char *name;
/* CCS,CVS,GND */
srctype_t type;
/* ampere or volts */
float_t value;
} source_t;
source_t source[]= {
{"VCC",CVS,+5},
{"VSS",CVS,+5},
{"GND",GND,0 }
};
source_t *get_source (char *string)
{
int i;
for (i = 0; i < sizeof (source)/sizeof (source[0]); i ++ ){
if (strcmp (source[i].name, string)==0 ) return &source[i];
}
return 0;
}
////////////////////// nodes management/////////////////////////////////
node_t **nodes;
int n_nodes;
static max_nodes;
static max_pins;
static struct hash_control *node_tbl;
int node_start(char *label)
{
node_t *q;
source_t *src;
if (n_nodes >= max_nodes) {
node_t **p;
max_nodes += 10;
p = (node_t **) malloc (max_nodes * sizeof (node_t *));
if (!p) {
error (__FILE__,__LINE__,NO_MEM);
return -1;
}
if (nodes) {
memcpy (p, nodes, n_nodes * sizeof (node_t *));
free (nodes);
}
nodes = p;
}
max_pins = 0;
q = (node_t *) malloc (strlen(label) + sizeof (node_t));
if (!q) {
error (__FILE__,__LINE__,NO_MEM);
return -1;
}
memset (q, 0, sizeof (node_t) );
strcpy (q->name,label);
q->equiv_next = -1;
/* if it is a power source */
if (src = get_source (label) ) {
q -> value = src->value;
q -> flag = src->type == CVS ? NODE_FCVS :
src->type == CCS ? NODE_FCCS :
src->type == GND ? NODE_FGND : 0;
}
nodes[ n_nodes++ ] = q;
hash_insert (node_tbl, q->name, q );
}
int node_add_pin (int pin_gid)
{
int i = n_nodes-1;
int j = nodes[ i ]->n_pins++;
if (j >= max_pins ){
int *p;
max_nodes += 10;
p = (int *) malloc (max_pins * sizeof (int));
if (!p) {
error (__FILE__,__LINE__,NO_MEM);
return -1;
}
if (nodes[i]->pin_gid) {
memcpy (p, nodes[i]->pin_gid, j * sizeof (int));
free (nodes[i]->pin_gid);
}
nodes[i]->pin_gid = p;
}
nodes[ i ]->pin_gid[j] = pin_gid;
pins[ pin_gid ].node_id = i;
assert (COMP (pin_gid));
if (COMPCLASS (pin_gid)->pin_type) {
comp_pintype_t type = COMPCLASS (pin_gid)->pin_type (COMP (pin_gid), pin_gid);
/* check whether flags conflicts, for example, VCC and GND connects to a
* same node. not implemented as yet
*/
/* set the flag */
nodes[ i ]->flag |= (type == COMP_PIN_IOPORT ? NODE_FIOPORT : 0 );
}
}
void node_end()
{
}
int node_init()
{
node_tbl = hash_new();
}
int node_reset()
{
int i;
for (i = 0; i < n_nodes; i ++ )
nodes[i]->flag &= ~NODE_FPROCESSED;
}
int dump_nodes ()
{
int i;
for (i = 0; i < n_nodes; i ++ ) {
printf ("<%d> ",i );
printf ("%s ", nodes[i]->name);
printf ("%x ", nodes[i]->flag);
printf ("%d ", nodes[i]->n_pins);
printf ("%f \n", nodes[i]->value);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -