File: sim.c

package info (click to toggle)
irsim 9.7.104-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ansic: 24,763; sh: 7,499; makefile: 418; csh: 269; tcl: 88
file content (975 lines) | stat: -rw-r--r-- 23,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/* 
 *     ********************************************************************* 
 *     * Copyright (C) 1988, 1990 Stanford University.                     * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  Stanford University                 * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     ********************************************************************* 
 */

/*
 * The routines in this file handle network input (from sim files).
 * The input routine "rd_network" replaces the work formerly done by presim.
 * This version differs from the former in the following:
 *  1. voltage drops across transistors are ignored (assumes transistors
 *     driven by nodes with voltage drops have the same resistance as those
 *     driven by a full swing).
 *  2. static power calculations not performed (only useful for nmos anyhow).
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "defs.h"
#include "net.h"
#include "globals.h"
#include "net_macros.h"

#define LSIZE		2000		/* size for input line buffer */
#define	MAXARGS		50		/* max. # of arguments per line */

#define DEFAULT_OUTPUT_RESISTANCE 500	/* output resistance used if no info given */

public	nptr   VDD_node;		/* power supply nodes */
public	nptr   GND_node;
public  char   *simprefix = NULL;	/* if non-NULL, prefix nodes with this */

public	lptr   on_trans;		/* always on transistors */

public	int    nnodes;			/* number of actual nodes */
public	int    naliases;		/* number of aliased nodes */
public	int    ntrans[ NTTYPES ];	/* number of txtors indexed by type */

public	lptr   freeLinks = NULL;	/* free list of Tlist structs */
public	tptr   freeTrans = NULL;	/* free list of Transistor structs */
public	nptr   freeNodes = NULL;	/* free list of Node structs */

public	tptr   tcap = NULL;		/* list of capacitor-transistors */

private	int    lineno;			/* current input file line number */
private	char   *simfname;		/* current input filename */
private	int    nerrs = 0;		/* # of erros found in sim file */
private	int    isBinFile;		/* TRUE binary file, FALSE sim file */

public	tptr   rd_tlist;		/* list of transistors just read */

public
#define	MIN_CAP		0.00001		/* minimum node capacitance (in pf) */


#define	WARNING		0
#define	WARN		1
#define	MAX_ERRS	20
#define	FATAL		(MAX_ERRS + 1)


#define	MIT	0
#define LBL	1
#define	SU	2

private short int simFormat = MIT, offsLBL = 0 ;

private	char    bad_argc_msg[] = "Wrong number of args for '%c' (%d)\n";

#define	BAD_ARGC( CMD, ARGC, ARGV )			\
  {							\
    rsimerror( simfname, lineno, bad_argc_msg, CMD, ARGC );	\
    PrArgs( ARGC, ARGV );				\
    CheckErrs( WARN );					\
    return;						\
  }							\


private void PrArgs( argc, argv )
  int   argc;
  char  *argv[];
  {
    while( argc-- != 0 )
      (void) fprintf( stderr, "%s ", *argv++ );
    (void) fputs( "\n", stderr );
  }


private void CheckErrs( n )
  int n;
  {
    nerrs += n;
    if( nerrs > MAX_ERRS )
      {
	if( n != FATAL )
	    (void) fprintf( stderr, "Too many errors in sim file <%s>\n",
	      simfname );
	exit( 1 );
      }
  }



/*
 * Traverse the transistor list and add the node connection-list.  We have
 * to be careful with ALIASed nodes.  Note that transistors with source/drain
 * connected VDD and GND nodes are not linked.
 */
private nptr connect_txtors()
  {
    register tptr  t, tnext;
    register nptr  gate, src, drn, nd_list;
    register int   type;
    register long  visited;

    visited = VISITED;
    nd_list = NULL;

    for( t = rd_tlist; t != NULL; t = tnext )
      {
	tnext = t->scache.t;
	for( gate = t->gate; gate->nflags & ALIAS; gate = gate->nlink);
	for( src = t->source; src->nflags & ALIAS; src = src->nlink );
	for( drn = t->drain; drn->nflags & ALIAS; drn = drn->nlink );

	t->gate = gate;
	t->source = src;
	t->drain = drn;

	type = t->ttype;
	t->state = ( type & ALWAYSON ) ? WEAK : UNKNOWN;
	t->tflags = 0;

	ntrans[ type ]++;
	if( src == drn or (src->nflags & drn->nflags & POWER_RAIL) )
	  {
	    t->ttype |= TCAP;		/* transistor is just a capacitor */
	    LINK_TCAP( t );
	  }
	else
	  {
		/* do not connect gate if ALWAYSON since they do not matter */
	    if( t->ttype & ALWAYSON )
	      {
		CONNECT( on_trans, t );
	      }
	    else
	      {
		CONNECT( t->gate->ngate, t );
	      }

	    if( not (src->nflags & POWER_RAIL) )
	      {
		CONNECT( src->nterm, t );
		LINK_TO_LIST( src, nd_list, visited );
	      }
	    if( not (drn->nflags & POWER_RAIL) )
	      {
		CONNECT( drn->nterm, t );
		LINK_TO_LIST( drn, nd_list, visited );
	      }
	  }
      }

    rd_tlist = NULL;	/* Reset the list */
    return( nd_list );
  }


/*
 * node area and perimeter info (N sim command).
 */
private void node_info( targc, targv )
  int   targc;
  char  *targv[];
  {
    register nptr  n;

    if( targc != 8 )
	BAD_ARGC( 'N', targc, targv );

    n = RsimGetNode( targv[1] );

    n->ncap += 	atof( targv[4] ) * (CMA * LAMBDA2) +
		atof( targv[5] ) * (CPA * LAMBDA2) +
		atof( targv[6] ) * (CDA * LAMBDA2) +
		atof( targv[7] ) * 2.0 * (CDP * LAMBDA);
  }


/*
 * new format node area and perimeter info (M sim command).
 */
private void nnode_info( targc, targv )
  int   targc;
  char  *targv[];
  {
    register nptr  n;

    if( targc != 14 )
	BAD_ARGC( 'M', targc, targv );

    n = RsimGetNode( targv[1] );

    n->ncap +=	atof( targv[4] ) * (CM2A * LAMBDA2) +
		atof( targv[5] ) * 2.0 * (CM2P * LAMBDA) +
		atof( targv[6] ) * (CMA * LAMBDA2) +
		atof( targv[7] ) * 2.0 * (CMP * LAMBDA) +
		atof( targv[8] ) * (CPA * LAMBDA2) +
		atof( targv[9] ) * 2.0 * (CPP * LAMBDA) +
		atof( targv[10] ) * (CDA * LAMBDA) +
		atof( targv[11] ) * 2.0 * (CDP * LAMBDA) +
		atof( targv[12] ) * (CPDA * LAMBDA2) +
		atof( targv[13] ) * 2.0 * (CPDP * LAMBDA);
  }

private	int    AP_error = FALSE;

/*------------------------------------------------------*/
/* parse g=...,A_NUM,P_NUM attributes			*/
/* return values (area, perimeter) are in integer	*/
/* lambda^2 and lambda, respectively.			*/
/*------------------------------------------------------*/

private int parseAttr(str, a, p)
char *str;
int *a, *p;
{
   int l;
   char *s;

   if ( (l=strlen(str)) <= 2 ) {
   	*a = 0 ; 
   	*p = 0;
   	return FALSE;
   }

   for ( s = str+l*sizeof(char) ; *s != 'A' && s != str ; s--) ;
   if ( sscanf(s,"A_%d,P_%d", a, p ) != 2) 
    if ( sscanf(s,"a_%d,p_%d", a, p ) != 2) {
      rsimerror( simfname, lineno, "Bad area/perimeter attributes\n");
      return FALSE;
    }
   return TRUE;
}

/*--------------------------------------------------------------*/
/* Convert resistance in metric units to internal units (ohms).	*/
/* Default (unspecified) values are treated as ohms.		*/
/*--------------------------------------------------------------*/

private float rconvert( resstring )
  char *resstring;
{
   /* Convert string to floating-point, and stop at the	*/
   /* first non-numeric character.			*/

   double rval;
   char *pfix;

   rval = strtod((const char *)resstring, &pfix);

   while (*pfix != '\0' && isspace(*pfix)) pfix++;

   /* If no metric suffix is given, then interpret the answer	*/
   /* as ohms.  The return value is in ohms.			*/

   switch (*pfix) {
      case 'o': case '\0': break;			/* ohms */
      case 'k': case 'K': rval *= 1.0e3; break;		/* kiloOhms */
      case 'M': rval *= 1.0e6; break;			/* megaOhms */
      case 'G': rval *= 1.0e9; break;			/* gigaOhms */
      default:
	 rsimerror( simfname, lineno,
	    "Unknown resistor value suffix %s, assuming ohms\n", pfix);
	 break;
   }
   
   return (float)rval;
}

/*--------------------------------------------------------------*/
/* Convert length values (length, width, x, y) in metric units	*/
/* to internal units.  Default (unspecified) values are treated	*/
/* as lambda, using the lambda conversion factor.		*/
/* Return values are in centimicrons.  LAMBDA units are		*/
/* converted to centimicrons using the LAMBDACM factor.		*/
/*--------------------------------------------------------------*/

private float lconvert( lstring )
  char *lstring;
{
   /* Convert string to floating-point, and stop at the	*/
   /* first non-numeric character.			*/

   double lval;
   char *pfix;

   lval = strtod((const char *)lstring, &pfix);

   while (*pfix != '\0' && isspace(*pfix)) pfix++;

   /* If no metric suffix is given, then interpret the answer	*/
   /* as lambda.  The return value is in centimicrons.		*/

   switch (*pfix) {
      case 'n': case 'N': lval *= 0.1;   break;		/* nanometers */
      case 'u': case 'U': lval *= 100;   break;		/* microns */
      case 'm': case 'M': lval *= 1.0e5; break;		/* millimeters */
      case '\0': case 'l': lval *= LAMBDACM; break;	/* lambda */
      default:
	 rsimerror( simfname, lineno,
	    "Unknown length measure suffix %s, assuming lambda\n", pfix);
	 break;
   }
   return (float)lval;
}

/*
 * new transistor.  Implant specifies type.
 * AreaPos specifies the argument number that contains the area (if any).
 */
private void newtrans( implant, targc, targv )
  int   implant;
  int   targc;
  char  *targv[];
  {
    nptr           gate, src, drn;
    long           x, y;
    int		   width, length;
    double         cap;
    register tptr  t;
    unsigned int fetHasAP = FALSE ;
    unsigned int asrc, adrn, psrc, pdrn;

    if( implant == RESIST )
      {
	if( targc != 4 )
	    BAD_ARGC( 'r', targc, targv );

	gate = VDD_node;
	src = RsimGetNode( targv[1] );
	drn = RsimGetNode( targv[2] );

	length = rconvert( targv[3] ) * LAMBDACM;
	width = 0;
      }
    else
      {
	if( targc < 4+offsLBL or targc > 11+offsLBL )
	    BAD_ARGC( targv[0][0], targc, targv );

	gate = RsimGetNode( targv[1] );
	src = RsimGetNode( targv[2] );
	drn = RsimGetNode( targv[3] );

	if( targc > 5+offsLBL )
	  {
	    length = (int)lconvert(targv[4 + offsLBL]);
	    width = (int)lconvert(targv[5 + offsLBL]);
	    if (width <= 0 or length <= 0)
	      {
		rsimerror( simfname, lineno,
		  "Bad transistor width=%d or length=%d\n", width, length );
		return;
	      }
	    if (targc > 7+offsLBL)
	      {
		x = (int)lconvert(targv[6+offsLBL]);
		y = (int)lconvert(targv[7+offsLBL]);
	      }
	  }
	else
	    width = length = (int)(2 * LAMBDACM);

	/* Compute capacitance in picoFarads.		*/
	/* CTGA is in pF/centimicron^2			*/
	/* length and width are in centimicrons		*/

	cap = (double)(length * width * CTGA);
      }

    NEW_TRANS( t );			/* create new transistor */

    t->ttype = implant;
    t->gate = gate;
    t->source = src;
    t->drain = drn;

    if( targc > 7+offsLBL )
      {
	t->x.pos = x;
	t->y.pos = y;
	EnterPos( t, TRUE );		/* Enter transistor position */
	if ( simFormat == SU && targc >= 9 ) { /* parse area and perimeter */
	  register int i ;

	  for ( fetHasAP = TRUE, i = 8 ; i < targc ; i++ ) {
	    if ( targv[i][0] == 's' )
	       fetHasAP = fetHasAP && parseAttr(targv[i], &asrc, &psrc);
	    else if ( targv[i][0] == 'd' )
	       fetHasAP = fetHasAP && parseAttr(targv[i], &adrn, &pdrn);
	   }
	}
      }
    else {
	EnterPos( t, FALSE );		/* Enter transistor position */
	if ( simFormat == SU && ! AP_error ) {
		rsimerror(  simfname, lineno, "no area/perim S/D attributes on fet\n");
		AP_error = TRUE;
	 }
	}

    t->scache.t = rd_tlist;		/* link it to the list */
    rd_tlist = t;

    t->r = requiv( implant, width, length );

		/* update node capacitances  */
    gate->ncap += cap;

    if ( simFormat == SU ) {
       double capsrc = 0, capdrn =0 ;

       if ( fetHasAP ) {
          if ( implant == PCHAN ) {
	    capsrc = asrc * LAMBDA2 * CPDA + psrc * LAMBDA *CPDP;
	    capdrn = adrn * LAMBDA2 * CPDA + pdrn * LAMBDA *CPDP;
	   } 
	  else if ( implant == NCHAN || implant == DEP ) {
	    capsrc = asrc * LAMBDA2 * CDA + psrc * LAMBDA *CDP;
	    capdrn = adrn * LAMBDA2 * CDA + pdrn * LAMBDA *CDP;
          } 
	} 
       else if ( ! AP_error  ) {
	 lprintf( stderr, "Warning: Junction capacitances might be incorrect\n");
	 AP_error = TRUE;
       }
#ifdef NOTDEF
printf("LAMBDA=%f CDA=%f CDP=%f asrc=%d psrc=%d adrn=%d pdrn=%d capsrc=%lf capdrn=%lf\n", LAMBDA, CDA, CDP, asrc, psrc, adrn, pdrn, capsrc, capdrn);
#endif
       src->ncap += capsrc;
       drn->ncap += capdrn;
     }
    else if( config_flags & TDIFFCAP ) 
      {
	if ( implant == PCHAN ) {
		cap = CPTDW * width + CPTDE;
	}
	else if ( implant == NCHAN || implant == DEP )
		cap = CTDW * width + CTDE;
	else	cap = 0.0 ;
	src->ncap += cap;
	drn->ncap += cap;
      }
  }

/*
 * accept a bunch of aliases for a node (= sim command).
 */
public void alias( targc, targv )
  int   targc;
  char  *targv[];
  {
    register nptr  n, m;
    register int   i;

    if( targc < 3 )
	BAD_ARGC( '=', targc, targv );

    n = RsimGetNode( targv[1] );

    for( i = 2; i < targc; i++ )
      {
	m = RsimGetNode( targv[i] );
	if( m == n )
	    continue;

	if( m->nflags & POWER_RAIL )
	    SWAP_NODES( m, n );

	if( m->nflags & POWER_RAIL )
	  {
	    rsimerror( simfname, lineno, "Can't alias the power supplies\n" );
	    continue;
	  }

	n->ncap += m->ncap;

	m->nlink = n;
	m->nflags |= ALIAS;
	m->ncap = 0.0;
	nnodes--;
	naliases++;
      }
  }


/*
 * node threshold voltages (t sim command).
 */
private void nthresh( targc, targv )
  int   targc;
  char  *targv[];
  {
    register nptr  n;

    if( targc != 4 )
	BAD_ARGC( 't', targc, targv );

    n = RsimGetNode( targv[1] );
    n->vlow = atof( targv[2] );
    n->vhigh = atof( targv[3] );
  }


/*
 * User delay for a node (D sim command).
 */
private void ndelay( targc, targv )
  int   targc;
  char  *targv[];
  {
    register nptr  n;

    if( targc != 4 )
	BAD_ARGC( 'D', targc, targv );

    n = RsimGetNode( targv[1] );
    n->nflags |= USERDELAY;
    n->tplh = ns2d( atof( targv[2] ) );
    n->tphl = ns2d( atof( targv[3] ) );
  }

private float cconvert( capstring )
  char *capstring;
{
   /* Convert string to floating-point, and stop at the	*/
   /* first non-numeric character.			*/

   double cval;
   char *pfix;

   cval = strtod((const char *)capstring, &pfix);

   while (*pfix != '\0' && isspace(*pfix)) pfix++;

   /* Convert metric suffixes to pF.  If no suffix is given, interprets  */
   /* the value as the default femtoFarads.  Return value is picoFarads. */

   switch (*pfix) {
      case 'm': cval *= 1.0e9;  break;				/* milliFarads */
      case 'u': case 'U': cval *= 1.0e6;  break;		/* microFarads */
      case 'n': case 'N': cval *= 1.0e3; break;			/* nanoFarads  */
      case 'p': case 'P': break;				/* picoFarads  */
      case 'f': case 'F': case '\0': cval *= 1.0e-3; break;	/* femtoFarads */
      case 'a': case 'A': cval *= 1.0e-6; break;		/* attoFarads  */
      default:
	 rsimerror( simfname, lineno,
	    "Unknown capacitance value suffix %s, assuming femtoFarads\n", pfix);
	 break;
   }
   return (float)cval;
}

/*
 * add capacitance to a node (c sim command).
 */
private void ncap( targc, targv )
  int   targc;
  char  *targv[];
  {
    register nptr n, m;
    float         cap;

    if( targc == 3 )
      {
	n = RsimGetNode( targv[1] );
	n->ncap += cconvert( targv[2] );
      }
    else if( targc == 4 )		/* two terminal caps	*/
      {
	cap = cconvert( targv[3] );
	n = RsimGetNode( targv[1] );
	m = RsimGetNode( targv[2] );
	if( n != m )			/* add cap to both nodes */
	  {
	    if( m != GND_node )	m->ncap += cap;
	    if( n != GND_node )	n->ncap += cap;
	  }
	else if( n == GND_node )	/* same node, only GND makes sense */
	    n->ncap += cap;
      }
    else
	BAD_ARGC( 'c', targc, targv );
  }


/*
 * parse input line into tokens, filling up carg and setting targc
 */
private int parse_line( line, carg )
  register char  *line;
  register char  **carg;
  {
    register char  ch;
    register int   targc;

    targc = 0;
    while( ch = *line++ )
      {
	if( ch <= ' ' )
	    continue;
	targc++;
	*carg++ = line - 1;
	while( *line > ' ' )
	    line++;
	if( *line )
	    *line++ = '\0';
      }
    *carg = 0;
    return( targc );
  }


private	int    R_error = FALSE;
private	int    A_error = FALSE;

private int input_sim (simfile, has_param_file)
  char  *simfile;
  int   has_param_file;
{
    FILE  *fin;
    char  line[LSIZE];
    char  *targv[MAXARGS];	/* tokens on current command line */
    int   targc;		/* number of args on command line */
    long  offset;		/* state of previously opened file */
    int   olineno;

    /* Test for file with and without extension */

    if ((fin = fopen(simfile, "r")) == NULL)
    {
	char *tmpfname = (char *)malloc(5 + strlen(simfile));

	strcpy(tmpfname, simfile);
	strcat(tmpfname, ".sim");
	fin = fopen(tmpfname, "r");
	free(tmpfname);
	if (fin == NULL)
	{
	    lprintf( stderr, "cannot open '%s' for sim input\n", simfile );
	    return FALSE;
	}
    }
    simfname = simfile;
    lineno = 0;

    while (fgetline(line, LSIZE, fin) != NULL)
    {
	lineno++;
	if ((lineno > 1) && (has_param_file < 0))
	{
	    /* Attempt to load a default config file */
	    has_param_file = config("scmos100");
	    if (has_param_file < 0)
	    {
	        lprintf( stderr, "No prm file specified and unable to load default!\n");
		return FALSE;
	    }
	    else 
	        lprintf( stderr, "config file unknown; using default scmos100.prm\n");
	}
	targc = parse_line(line, targv);

	if (targv[0] == NULL)
	    continue;

	switch (targv[0][0])
	{
	    case '@':
		if (targc != 2)
		{
		    rsimerror(simfname, lineno, bad_argc_msg, '@', targc);
		    CheckErrs(WARN);
		    break;
		}
		offset = ftell(fin);
		olineno = lineno;
		(void) fclose(fin);
		(void) input_sim(targv[1]);
		if ((fin = fopen(simfile, "r")) == NULL)
		{
		    rsimerror(simfname, lineno, "can't re-open sim file '%s'\n",
				simfile);
		    CheckErrs(WARN);
		    return FALSE;
		}
		(void) fseek(fin, offset, 0);
		simfname = simfile;
		lineno = olineno;
		break;

	    case '|':
		if (lineno > 1) break;
		if (targc > 2) {
		    double lmbd = atof(targv[2]) / 100.0;

		    if ((targc > 4) && (has_param_file < 0))
		    {
			char *prmname = malloc(strlen(targv[2]) + strlen(targv[4]) + 1);

			sprintf(prmname, "%s%s", targv[4], targv[2]);
			has_param_file = config( prmname );
			free(prmname);
			if (has_param_file < 0)
			{
			    lprintf(stderr, "Could not load parameter file %s%s.prm\n",
					targv[4], targv[2]);
			    return FALSE;
			}
			else
			    lprintf(stdout, "Parameter file %s%s.prm determined from "
					"header line\n", targv[4], targv[2]);
		    }

		    if (lmbd != LAMBDA)
		    {
			/* Only flag a warning if the difference is	*/
			/* more than is accounted for by roundoff error	*/

			if (fabs(lmbd - LAMBDA)/LAMBDA > 0.01)
			{
			    rsimerror(simfname, lineno, "WARNING: sim "
				"file lambda (%g) != config lambda"
				" (%g)\n", lmbd, LAMBDA);
			    rsimerror( simfname, lineno, "WARNING: Using "
				"the config lambda (%g)\n", LAMBDA);
			}
		    }
		}
		if (targc > 6)
		{
		    if (!strcasecmp(targv[6], "MIT")) simFormat = MIT;
		    else if (!strcasecmp(targv[6], "LBL")) simFormat = LBL;
		    else if (!strcasecmp(targv[6], "SU")) simFormat = SU;
		    else {
		        rsimerror(simfname, lineno, "Unknown format '%s' "
				"assuming MIT\n", targv[6]);
		        simFormat = MIT ;
		    }
	            offsLBL = (simFormat == LBL) ? 1 : 0;  /* skip bulk */
		    if (simFormat == SU)
		    {
			if (CDA == 0.0 || CDP == 0.0 || CPDA == 0.0 || CPDP == 0.0)
			    lprintf(stderr, "Warning: SU format and area/perim"
					" cap values are zero\n");
			else
			    lprintf(stderr, "info: SU format --> using S/D attrs"
					" to compute junction parasitics\n");
		    }
		} 
		break;

	    case 'e':
	    case 'n':
		newtrans(NCHAN, targc, targv);
		break;

	    case 'p':
		newtrans(PCHAN, targc, targv);
		break;

	    case 'd':
		newtrans(DEP, targc, targv);
		break;

	    case 'r':
		newtrans(RESIST, targc, targv);
		break;

#ifdef	USER_SUBCKT 
	    case 'x':
		newsubckt(targc, targv);
		break;
#endif
	    case 'N':
		node_info(targc, targv);
		break;

	    case 'M':
		nnode_info(targc, targv);
		break;

	    case 'c':
	    case 'C':
	    	ncap(targc, targv);
		break;

	    case '=':
	    	alias(targc, targv);
		break;

	    case 't':
	    	nthresh(targc, targv);
		break;

	    case 'D': 
	    	ndelay(targc, targv);
		break;

	    case 'R': 
		if (!R_error)	/* only warn about this 1 time */
		{
		    lprintf(stderr,
			"%s: Ignoring lumped-resistance ('R' construct)\n",
			simfname);
		    R_error = TRUE;
		}
		break;
	    case 'A':
		if (!A_error)	/* only warn about this 1 time */
		{
		    lprintf(stderr,
			"%s: Ignoring attribute-line ('A' construct)\n",
			simfname);
		    A_error = TRUE;
		}
		break;

	    case '<': 
		if (lineno == 1 && rd_netfile(fin, line))
		{
		    (void) fclose(fin);
		    return TRUE;
		}
		/* fall through if rd_netfile returns FALSE */

	    default:
		rsimerror(simfname, lineno, "Unrecognized input line (%s)\n",
		  targv[0]);
		CheckErrs(WARN);
	}
    }
    (void) fclose (fin);
    lprintf(stdout, "\nRead %s lambda:%.2lfu format:%s\n", simfile, LAMBDA, 
			simFormat == MIT ? "MIT" : ((simFormat == LBL) ?
			"LBL" : "SU"));
    return FALSE;
}



private void init_counts()
{
    register int  i;

    for( i = 0; i < NTTYPES; i++ )
	ntrans[i] = 0;
    nnodes = naliases = 0;
}


public int rd_network( simfile, prefix, has_param_file )
  char  *simfile;
  char  *prefix;
  int   has_param_file;
  {
    static int      firstcall = 1;

    if( firstcall )
      {
	rd_tlist = NULL;
	init_counts();
	init_listTbl();

	VDD_node = RsimGetNode( "Vdd" );
	VDD_node->npot = HIGH;
	VDD_node->nflags |= (INPUT | POWER_RAIL);
	VDD_node->head.inp = 1;
	VDD_node->head.val = HIGH;
	VDD_node->head.punt = 0;
	VDD_node->head.time = 0;
	VDD_node->head.t.r.rtime = VDD_node->head.t.r.delay = 0;
	VDD_node->head.next = last_hist;
	VDD_node->curr = &(VDD_node->head);

	GND_node = RsimGetNode( "Gnd" );
	GND_node->npot = LOW;
	GND_node->nflags |= (INPUT | POWER_RAIL);
	GND_node->head.inp = 1;
	GND_node->head.val = LOW;
	GND_node->head.punt = 0;
	GND_node->head.time = 0;
	GND_node->head.t.r.rtime = GND_node->head.t.r.delay = 0;
	GND_node->head.next = last_hist;
	GND_node->curr = &(GND_node->head);

	NEW_TRANS( tcap );
	tcap->scache.t = tcap->dcache.t = tcap;
	tcap->x.pos = 0;

	firstcall = 0;
      }
    nerrs = 0;

    simprefix = prefix;
    isBinFile = input_sim( simfile, has_param_file );
    simprefix = (char *)NULL;

    if (nerrs > 0)
#ifndef TCL_IRSIM
	exit( 1 );
#else
    {
	lprintf(stderr, "Errors occurred on reading input file %s\n", simfile);
	return 1;
    }
    else
	return 0;
#endif
  }


public void pTotalNodes()
  {
    lprintf( stdout, "%d nodes", nnodes );
    if( naliases != 0 )
	lprintf( stdout, ", %d aliases", naliases );
    lprintf( stdout, "; " );
  }


public void pTotalTxtors()
  {
    int  i;

    lprintf( stdout, "transistors:" );
    for( i = 0; i < NTTYPES; i++ )
	if( ntrans[i] != 0 )
	    lprintf( stdout, " %s=%d", ttype[i], ntrans[i] );
    if( tcap->x.pos != 0 )
	lprintf( stdout, " shorted=%d", tcap->x.pos );
    lprintf( stdout, "\n" );
  }


public void ConnectNetwork()
  {
    nptr  ndlist;

    pTotalNodes();

    ndlist = ( isBinFile ) ? bin_connect_txtors() : connect_txtors();

    make_parallel( ndlist );
    make_stacks( ndlist );

    pTotalTxtors();
    pParallelTxtors();
    pStackedTxtors();
  }