Update at Fri Feb 9 12:38:17 EST 2018 by tim
[xcircuit.git] / spiceparser / README.c
blob05e057023937595ff2caa4a1e6eaf23a341d3a4a
1 /* subckt_translator.c : quick hack to enable subckt files to work in standard "magic" characterization flow
2 Conrad Ziesler
3 */
6 #include <stdio.h>
7 #include <ctype.h>
9 #include "makeutils.lib/debug.h"
10 #include "parsers.lib/scanner.h"
11 #include "netlist.lib/netlist_spice.h"
16 cell: c2 INVX1
17 area: 10.368
18 input: i_a
19 out: o_y
20 vss: i_vss
21 vdd: i_vdd
25 int is_power(char *name)
27 if(strcasecmp(name,"vdd")==0) return 1;
28 return 0;
31 int is_ground(char *name)
33 if(strcasecmp(name,"gnd")==0) return 1;
34 if(strcasecmp(name,"vss")==0) return 1;
35 return 0;
38 int is_input(char *name)
40 if(!is_ground(name))
41 if(!is_power(name))
42 if(tolower(name[0])<='f') return 1;
43 return 0;
46 int is_output(char *name)
48 char x;
49 x=tolower(name[0]);
50 if(!is_ground(name))
51 if(!is_power(name))
52 if(x>='q') return 1;
53 return 0;
56 char *node(node_t n)
58 char *p;
59 p=n->str;
60 return p;
63 void dumprest(FILE *fp, card_t *cp)
65 for(;cp!=NULL;cp=cp->next)
66 { /* just dump the rest verbatim */
67 fprintf(fp," %s",cp->str);
68 if(cp->val!=NULL) fprintf(fp,"=%s",cp->val);
72 void generate_stuff(spice_t *spice, FILE *fp_pins, FILE *fp_flat)
74 subckt_t *ckt,**cktpp;
75 list_t subckts;
76 int i,j;
77 int uniq=1;
80 subckts=spice_list_subckt(spice->ckt);
81 list_iterate(&subckts,i,cktpp)
83 ckt=*cktpp;
84 assert(ckt!=NULL);
85 fprintf(fp_pins,"\n\n");
86 fprintf(fp_pins,"cell: c%i %s\n",i,ckt->name);
87 fprintf(fp_pins,"area: %i\n",ckt->nm); /* estimate area by transitor count */
88 for(j=0;j<ckt->ndefn;j++)
90 char *nn;
91 nn=ckt->defn[j]->str;
92 if(is_input(nn))
94 fprintf(fp_pins,"input: %s\n",nn);
96 else if(is_output(nn))
98 fprintf(fp_pins,"out: %s\n",nn);
100 else if(is_power(nn))
102 fprintf(fp_pins,"vdd: %s\n", nn);
104 else if(is_ground(nn))
106 fprintf(fp_pins,"vss: %s\n", nn);
108 else fprintf(fp_pins,"#error pin %s\n",nn);
111 if(fp_flat!=NULL)
113 fprintf(fp_flat,"\n\n*\n*\n* subckt %s \n\n",ckt->name);
114 for(j=0;j<ckt->nm;j++)
116 fprintf(fp_flat,"M%i c%i_%s c%i_%s c%i_%s c%i_%s ",
117 uniq++,i,node(ckt->m[j].nodes[0]),i,node(ckt->m[j].nodes[1]),
118 i,node(ckt->m[j].nodes[2]),i,node(ckt->m[j].nodes[3]));
120 dumprest(fp_flat,ckt->m[j].rest);
121 fprintf(fp_flat,"\n");
122 /* fprintf(fp_flat," l=%g w=%lf pd=%lf ps=%lf ad=%lf as=%lf \n", */
124 for(j=0;j<ckt->nc;j++)
126 fprintf(fp_flat,"C%i c%i_%s c%i_%s ",
127 uniq++,i,node(ckt->c[j].nodes[0]),i,node(ckt->c[j].nodes[1]));
128 dumprest(fp_flat,ckt->c[j].rest);
129 fprintf(fp_flat,"\n");
132 for(j=0;j<ckt->nr;j++)
134 fprintf(fp_flat,"R%i c%i_%s c%i_%s ",
135 uniq++,i,node(ckt->r[j].nodes[0]),i,node(ckt->r[j].nodes[1]));
136 dumprest(fp_flat,ckt->r[j].rest);
137 fprintf(fp_flat,"\n");
147 int main(int argc, char **argv)
149 char *subckt_filename=NULL;
150 char *pins_output=NULL;
151 char *flat_spice=NULL;
152 int i;
153 scanner_t scan;
154 FILE *fp,*fp_pins,*fp_flat;
155 spice_t *spice;
157 for(i=1;i<argc;i++)
159 char *cmd=argv[i];
160 char *arg=(i<argc)?(argv[i+1]):NULL;
161 if(cmd[0]=='-')
162 switch(cmd[1])
164 case 'i':
165 if(arg!=NULL)
166 { subckt_filename=arg; i++; continue; }
167 break;
168 case 'p':
169 if(arg!=NULL)
170 { pins_output=arg; i++; continue; }
171 break;
172 case 'o':
173 if(arg!=NULL)
174 { flat_spice=arg; i++; continue; }
175 break;
177 fprintf(stderr,"Invalid command line option %s %s, skipping\n",cmd,arg);
179 if((subckt_filename==NULL))
181 fprintf(stderr,"Usage: translator -i input_subckt_file"
182 " -o output_flat_file -p output_pins_file\n");
183 exit(1);
187 scanner_init(&scan);
188 fp=fopen(subckt_filename,"rt");
189 scanner_input_newfp(&scan,fp);
190 spice=spice_new(&scan);
192 if(pins_output!=NULL)
193 fp_pins=fopen(pins_output,"w");
194 else fp_pins=stdout;
196 if(flat_spice!=NULL)
197 fp_flat=fopen(flat_spice,"w");
198 else fp_flat=NULL;
201 generate_stuff(spice,fp_pins,fp_flat);
202 if(fp_pins!=NULL)fclose(fp_pins);
203 if(fp_flat!=NULL)fclose(fp_flat);
205 return 0;