File: network.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 (564 lines) | stat: -rw-r--r-- 11,834 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
/* 
 *     ********************************************************************* 
 *     * 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.    * 
 *     ********************************************************************* 
 */

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

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

/* event driven mosfet simulator. Chris Terman (2/80) */

public	iptr  hinputs = NULL;	/* list of nodes to be driven high */
public	iptr  linputs = NULL;	/* list of nodes to be driven low */
public	iptr  uinputs = NULL;	/* list of nodes to be driven X */
public	iptr  xinputs = NULL;	/* list of nodes to be removed from input */

public	iptr  infree = NULL;	/* unused input list cells */

public	iptr  *listTbl[8];

public
#define	FreeInput( X )	(X)->next = infree, infree = (X)


public void init_listTbl()
  {
    int  i;

    for( i = 0; i < 8; listTbl[ i++ ] = NULL );
    listTbl[ INPUT_NUM( H_INPUT ) ] = &hinputs;
    listTbl[ INPUT_NUM( L_INPUT ) ] = &linputs;
    listTbl[ INPUT_NUM( U_INPUT ) ] = &uinputs;
    listTbl[ INPUT_NUM( X_INPUT ) ] = &xinputs;
  }


#define	pvalue( node_name, node )	\
    lprintf( stdout, "%s=%c ", (node_name), "0XX1"[(node)->npot] )


private void pgvalue( t )
  register tptr  t;
  {
    register nptr  n;
    static char    *states[] = { "OFF", "ON", "UNKNOWN", "WEAK" };

    if( debug )
	lprintf( stdout, "[%s] ", states[t->state] );

    if( t->ttype & GATELIST )
      {
	lprintf( stdout, "( " );
	for( t = (tptr) t->gate; t != NULL; t = t->scache.t )
	  {
	    n = t->gate;
	    pvalue( pnode( n ), n );
	  }

	lprintf( stdout, ") " );
      }
    else
      {
	n = t->gate;
	pvalue( pnode( n ), n );
      }
  }


private void pr_one_res( s, r )
  char     *s;
  double  r;
  {
    if( r < 1e-9 or r > 100e9 )
        (void) sprintf( s, "%2.1e", r );
    else
      {
	int  e = 3;

	if( r >= 1000.0 )
	    do { e++; r *= 0.001; } while( r >= 1000.0 );
	else if( r < 1 and r > 0 )
	    do { e--; r *= 1000; } while( r < 1.0 );
	(void) sprintf( s, "%.1f%c", r, "num\0KMG"[ e ] );
      }
  }


private void pr_t_res( fp, r )
  FILE     *fp;
  Resists  *r;
  {
    char    buf[3][15];

    pr_one_res( buf[0], r->rstatic );
    pr_one_res( buf[1], r->dynhigh );
    pr_one_res( buf[2], r->dynlow );
    lprintf( fp, "[%s, %s, %s]", buf[0], buf[1], buf[2] );
  }


private void ptrans( t )
  register tptr  t;
  {
    lprintf( stdout, "%s ", ttype[ BASETYPE( t->ttype ) ] );
    if( BASETYPE( t->ttype ) != RESIST )
	pgvalue( t );

    pvalue( pnode( t->source ), t->source );
    pvalue( pnode( t->drain ), t->drain );
    pr_t_res( stdout, t->r );
    if( t->tlink != t and (treport & REPORT_TCOORD) )
	lprintf( stdout, " <%d,%d>\n", t->x.pos, t->y.pos );
    else
	lprintf( stdout, "\n" );
  }


public void idelete( n, list )
  register nptr  n;
  register iptr  *list;
  {
    register iptr  p = *list;
    register iptr  q;

    if( p == NULL )
	return;
    else if( p->inode == n )
      {
	*list = p->next;
	FreeInput( p );
      }
    else
      {
	for( q = p->next; q != NULL; p = q, q = p->next )
	    if( q->inode == n )
	      {
		p->next = q->next;
		FreeInput( q );
		return;
	      }
      }
  }


public void iinsert( n, list )
  nptr  n;
  iptr  *list;
  {
    register iptr  p = infree;

    if( p == NULL )
	p = infree = (iptr) MallocList( sizeof( struct Input ), 1 );
    infree = p->next;

    p->next = *list;
    *list = p;
    p->inode = n;
  }


public void iinsert_once( n, list )
  nptr  n;
  iptr  *list;
  {
    register iptr  p;

    for( p = *list; p != NULL; p = p->next )
	if( p->inode == n )
	    return;

    iinsert( n, list );
  }


private	int clear_input( n )
  register nptr  n;
  {
    if( not( n->nflags & POWER_RAIL ) )
	n->nflags &= ~INPUT;
    return( 0 );
  }


public void ClearInputs()
  {
    register int   i;
    register iptr  p, next;
    register nptr  n;

    for( i = 0; i < 5; i++ )
      {
	if( listTbl[ i ] == NULL )
	    continue;
	for( p = *listTbl[ i ]; p != NULL; p = next )
	  {
	    next = p->next;
	    n = p->inode;
	    FreeInput( p );
	    if( not( n->nflags & POWER_RAIL ) )
		n->nflags &= ~(INPUT_MASK | INPUT);
	  }
	*(listTbl[ i ]) = NULL;
      }
    walk_net( clear_input, (char *) 0 );
  }


/*
 * set/clear input status of node and add/remove it to/from corresponding list.
 */
public int setin( n, which )
  register nptr  n;
  char           *which;
  {
    char locwhich = *which;

    if (locwhich == '!') 	/* Toggle the bit value */
    {
	if (n->npot == HIGH)
	    locwhich = 'l';
	else if (n->npot == LOW)
	    locwhich = 'h';
    }

    while( n->nflags & ALIAS )
	n = n->nlink;

    if( n->nflags & (POWER_RAIL | MERGED) )	/* Gnd, Vdd, or merged node */
      {
	if( (n->nflags & MERGED) or "lxuh"[ n->npot ] != locwhich )
	    lprintf( stdout, "Can't drive `%s' to `%c'\n",
	     pnode( n ), locwhich );
      }
    else
      {
	iptr  *list = listTbl[ INPUT_NUM( n->nflags ) ];

#	define	WASINP( N, P )	( ((N)->nflags & INPUT) && (N)->npot == (P) )

	switch( locwhich )
	  {
	    case 'h' :
		if( list != NULL and list != &hinputs )
		  {
		    n->nflags = n->nflags & ~INPUT_MASK;
		    idelete( n, list );
		  }
		if( not (list == &hinputs or WASINP( n, HIGH )) )
		  {
		    n->nflags = (n->nflags & ~INPUT_MASK) | H_INPUT;
		    iinsert( n, &hinputs );
		  }
		break;

	    case 'l' :
		if( list != NULL and list != &linputs )
		  {
		    n->nflags = n->nflags & ~INPUT_MASK;
		    idelete( n, list );
		  }
		if( not (list == &linputs or WASINP( n, LOW )) )
		  {
		    n->nflags = (n->nflags & ~INPUT_MASK) | L_INPUT;
		    iinsert( n, &linputs );
		  }
		break;

	    case 'u' :
		if( list != NULL and list != &uinputs )
		  {
		    n->nflags = n->nflags & ~INPUT_MASK;
		    idelete( n, list );
		  }
		if( not (list == &uinputs or WASINP( n, X )) )
		  {
		    n->nflags = (n->nflags & ~INPUT_MASK) | U_INPUT;
		    iinsert( n, &uinputs );
		  }
		break;

	    case 'x' :
		if( list == &xinputs )
		    break;
		if( list )
		  {
		    n->nflags = n->nflags & ~INPUT_MASK;
		    idelete( n, list );
		  }
		if( n->nflags & INPUT )
		  {
		    n->nflags = (n->nflags & ~INPUT_MASK) | X_INPUT;
		    iinsert( n, &xinputs );
		  }
		break;
	    default :
		return( 0 );
	  }
      }
    return( 1 );
  }


private int wr_value( n, fp )
  register nptr  n;
  FILE           *fp;
  {
    if( not (n->nflags & (ALIAS | POWER_RAIL)) )
      {
	int ch = ((n->nflags & INPUT) ? '4' : '0') + n->npot;
	(void) putc( ch, fp );
      }
    return( 0 );
  }


public int wr_state( fname )
  char  *fname;
  {
    FILE  *fp;

    if( (fp = fopen( fname, "w" )) == NULL )
	return( 1 );

    (void) fprintf( fp, "%d\n", nnodes );
    walk_net( wr_value, (char *) fp );
    (void) fclose( fp );
    return( 0 );
  }


typedef struct
  {
    FILE  *file;
    int   errs;
    int   restore;
  } StateFile;


private int rd_stvalue( n, st )
  register nptr  n;
  StateFile      *st;
  {
    int   ch, inp;

    if( n->nflags & (ALIAS | POWER_RAIL) )
	return( 0 );

    FreeHistList( n );
    while( n->events != NULL )		/* remove any pending events */
	free_event( n->events );

    if( st->file == NULL )
      {
	ch = X;
	st->errs ++;
      }
    else
      {
	ch = getc( st->file );
	if( ch == EOF )
	  {
	    ch = X;
	    st->errs ++;
	    (void) fclose( st->file );
	    st->file = NULL;
	  }
	else if( ch < '0' or ch > '7' or ch == '2' or ch == '6' )
	  {
	    st->errs ++;
	    ch = X;
	  }
	else if( st->restore and ch >= '4' )
	  {
	    ch = ch - '4';
	    inp = 1;
	  }
	else
	  {
	    ch = (ch - '0') & (LOW | X | HIGH);
	    inp = 0;
	  }
      }

    if( n->nflags & MERGED )
	return( 0 );

    if( inp )
	n->nflags |= INPUT;

    n->head.val = ch;
    n->head.inp = inp;

    if( ch != n->npot )
      {
	register lptr  l;
	register tptr  t;

	n->npot = ch;
	for( l = n->ngate; l != NULL; l = l->next )
	  {
	    t = l->xtor;
	    t->state = compute_trans_state( t );
	  }
      }
    return( 0 );
  }


public char *rd_state( fname, restore )
  char  *fname;
  int   restore;
  {
    char       rline[25];
    StateFile  st;

    if( (st.file = fopen( fname, "r" )) == NULL )
	return( "can not read state file\n" );

    (void) fgetline( rline, 25, st.file );
    if( atoi( rline ) != nnodes )
      {
	(void) fclose( st.file );
	return( "bad node count in state file\n" );
      }

    ClearInputs();

    if( analyzerON )
	StopAnalyzer();

    sim_time0 = cur_delta = 0;

    st.errs = 0;
    st.restore = restore;
    walk_net( rd_stvalue, (char *) &st );

    NoInit();

    if( analyzerON )
	RestartAnalyzer( sim_time0, cur_delta, FALSE );

    if( st.file == NULL )
      {
	(void) sprintf( fname, "premature EOF in state file (%d errors)\n",
	  st.errs );
	return( fname );
      }

    (void) fclose( st.file );

    if( st.errs != 0 )
      {
	(void) sprintf( fname, "%d errors found in state file\n", st.errs );
	return( fname );
      }
    return( NULL );
  }


public int info( n, which )
  register nptr  n;
  char           *which;
  {
    register tptr  t;
    register lptr  l;
    char           *name;

    if( n == NULL )
	return( 0 );

    if( int_received )
	return( 1 );

    name = pnode( n );
    while( n->nflags & ALIAS )
	n = n->nlink;

    if( n->nflags & MERGED )
      {
	lprintf( stdout, "%s => node is inside a transistor stack\n", name );
	return( 1 );
      }

    pvalue( name, n );
    if( n->nflags & INPUT )
	lprintf( stdout, "[NOTE: node is an input] " );
    lprintf( stdout, "(vl=%.2f vh=%.2f) ", n->vlow, n->vhigh );
    if( n->nflags & USERDELAY )
	lprintf( stdout, "(tplh=%d, tphl=%d) ", n->tplh, n->tphl );
    lprintf( stdout, "(%5.4f pf) ", n->ncap );

    if ((*which == '?') || !strcmp(which, "querysource"))
      {
	lprintf( stdout, "is computed from:\n" );
	for( l = n->nterm; l != NULL and int_received == 0; l = l->next )
	  {
	    t = l->xtor;
	    lprintf( stdout, "  " );
	    if( debug == 0 )
	      {
		char  *drive = NULL;
		nptr  rail;

		rail = (t->drain->nflags & POWER_RAIL) ? t->drain : t->source;
		if( BASETYPE( t->ttype ) == NCHAN and rail == GND_node )
		    drive = "pulled down by ";
		else if( BASETYPE( t->ttype ) == PCHAN and rail == VDD_node )
		    drive = "pulled up by ";
		else if( BASETYPE( t->ttype ) == DEP and rail == VDD_node and
		  other_node( t, rail ) == t->gate )
		    drive = "pullup ";
		else
		    ptrans( t );

		if( drive != NULL )
		  {
		    lprintf( stdout, drive );
		    pgvalue( t );
		    pr_t_res( stdout, t->r );
		    if( t->tlink != t and (treport & REPORT_TCOORD) )
			lprintf( stdout, " <%d,%d>\n", t->x.pos, t->y.pos );
		    else
			lprintf( stdout, "\n" );
		  }
	      }
	    else
		ptrans( t );
	  }
      }
    else
      {
	lprintf( stdout, "affects:\n" );
	for( l = n->ngate; l != NULL and int_received == 0; l = l->next )
	    ptrans( l->xtor );
      }

    if( int_received )
	lprintf( stdout, "-- interrupted\n" );

    if( n->events != NULL )
      {
	register evptr  e;

	lprintf( stdout, "Pending events:\n" );
	for( e = n->events; e != NULL; e = e->nlink )
	    lprintf( stdout, "   transition to %c at %2.2fns\n",
	      "0XX1"[e->eval], d2ns( e->ntime ) );
      }

    return( 1 );
  }