Modified the UGetCursor() routine to return a valid response if the
[xcircuit.git] / spiceparser / netlist_dev.c
blob2438e224fe2f213b2f2429b6f4c0d42baf3cf2cf
1 /********************
2 This file is part of the software library CADLIB written by Conrad Ziesler
3 Copyright 2003, Conrad Ziesler, all rights reserved.
5 *************************
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ******************/
21 /* netlist_dev.c, netlist functions for netlists of circuital devices (ie fets)
22 Conrad Ziesler
26 /* our goal here is to provide all the netlist convenience functions
27 which are dependent on the particular definitions for the devices (ie hard coded device types)
28 by device netlists we refer to netlists to be fed to spice-like simulators.
30 this was split off the core device-independent netlist routines
33 #include <stdio.h>
34 #include "debug.h"
36 #include "netlist_dev.h"
39 /* make a new device netlist, using client sizes as given
40 ni is ptr returned by netlist_parse_input
44 netlist_t *netlist_devnew_inplace(netlist_input_t ni, int extrasize, int extrasizes[DEVT_MAX])
46 int i,r;
47 int terms[]=DEVT_TERMS;
48 int vals[]=DEVT_VALS;
49 char *syms[]=DEVT_SYMS;
50 int others[]=DEVT_OTHERS;
51 netlist_t *nl;
53 nl=malloc(sizeof(netlist_t)+extrasize);
54 assert(nl!=NULL);
55 netlist_init(nl);
57 for(i=0;i<DEVT_MAX;i++)
59 r=netlist_register_entity(nl,i,terms[i],vals[i],others[i],syms[i],extrasizes[i]);
60 assert(r==i);
63 nl->input=ni;
64 if(ni.p!=NULL)
66 switch(ni.generic->magic)
68 case SPICE_MAGIC:
69 spice_count(nl);
71 for(i=0;i<DEVT_MAX;i++)
72 if(nl->e[i].qcount>0)
73 list_hint(&(nl->e[i].l),nl->e[i].qcount);
75 spice_build(nl);
76 break;
77 case EXTRACT_MAGIC:
79 extract_count(nl);
80 for(i=0;i<DEVT_MAX;i++)
81 if(nl->e[i].qcount>0)
82 list_hint(&(nl->e[i].l),nl->e[i].qcount);
83 extract_build(nl);
85 break;
88 return nl;
91 netlist_t *netlist_devnew(netlist_input_t ni)
93 int sz[DEVT_MAX];
94 memset(sz,0,sizeof(sz));
95 return netlist_devnew_inplace(ni, 0, sz);
99 /**** output spice_like file ****/
101 void netlist_o_spice(void *of_fp, netlist_t *nl, int n_start)
103 FILE *of=of_fp;
104 int i,j,k;
105 int m_i=1, c_i=1, l_i=1, i_i=1, v_i=1;
107 devall_p p;
108 entity_t *e;
109 int nodes[32];
110 float vals[32];
111 eqn_t *v;
113 if(nl==NULL) return;
114 eqnl_evaldep(&(nl->eqnl));
116 fprintf(of,"*** netlist output \n");
117 for(i=0;i<DEVT_MAX;i++)
119 e=nl->e+i;
120 assert(e->qterms<(sizeof(nodes)/sizeof(int)));
121 assert(e->qvals<(sizeof(vals)/sizeof(float)));
123 list_iterate(&(e->l),j,p.p)
125 for(k=0;k<e->qterms;k++)
126 nodes[k]=n_start+netlist_node_termptr(nl,p.genp->n[k]);
128 v=NETLIST_VALS(nl,i,j);
129 for(k=0;k<e->qvals;k++)
130 vals[k]=eqnl_eval(&(nl->eqnl),v[k]);
132 switch(e->id)
134 case DEVT_NODE: /* output a mini node-name directory */
135 fprintf(of,"* %i %s\n",j+n_start,names_lookup(nl->names,j));
136 break;
138 case DEVT_FET:
139 fprintf(of,"m%i %i %i %i %i %s L=%g W=%g AS=%g AD=%g PS=%g PD=%g\n",m_i++,
140 nodes[DEVFET_S],nodes[DEVFET_G],nodes[DEVFET_D], nodes[DEVFET_B],
141 (p.fetp->type==DEVFET_nmos)?"n":"p",
142 vals[DEVFET_l],vals[DEVFET_w], vals[DEVFET_as],
143 vals[DEVFET_ad], vals[DEVFET_ps], vals[DEVFET_pd]
145 break;
146 case DEVT_CAP:
147 fprintf(of,"c%i %i %i %g\n",c_i++,
148 nodes[DEV2TERM_P], nodes[DEV2TERM_N], vals[DEVCAP_c]
150 break;
151 case DEVT_RES:
152 fprintf(of,"c%i %i %i %g\n",c_i++,
153 nodes[DEV2TERM_P], nodes[DEV2TERM_N], vals[DEVRES_r]
155 break;
156 case DEVT_IND:
157 fprintf(of,"l%i %i %i %g\n",l_i++,
158 nodes[DEV2TERM_P], nodes[DEV2TERM_N], vals[0]
160 break;
161 case DEVT_VSRC:
162 fprintf(of,"v%i %i %i %g\n",v_i++,
163 nodes[DEV2TERM_P], nodes[DEV2TERM_N], vals[0]
165 break;
166 case DEVT_ISRC:
167 fprintf(of,"i%i %i %i %g\n",i_i++,
168 nodes[DEV2TERM_P], nodes[DEV2TERM_N], vals[0]
170 break;
172 default:
173 break;
177 fprintf(of,"*** end netlist output \n");
182 netlist_t *netlist_copyish(netlist_t *in, int size, int sizes[])
184 int i;
185 int extras[TERMPTR_MAX_DEVT];
186 int defsize[DEVT_MAX]=NETLIST_DEFSIZES;
187 for(i=0;i<TERMPTR_MAX_DEVT;i++)
189 if(i<DEVT_MAX)
190 extras[i]=sizes[i]-defsize[i];
191 else extras[i]=0;
193 if(extras[i]<0)extras[i]=0;
194 assert(extras[i]<2048);
197 return netlist_copyisher(in,size-sizeof(netlist_t),extras);