File: hist.c

package info (click to toggle)
irsim 9.7.75-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,596 kB
  • sloc: ansic: 24,733; sh: 6,803; makefile: 411; csh: 269; tcl: 76
file content (446 lines) | stat: -rw-r--r-- 10,120 bytes parent folder | download | duplicates (7)
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
/* 
 *     ********************************************************************* 
 *     * 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 "defs.h"
#include "net.h"
#include "ASSERT.h"
#include "globals.h"

#ifdef FAULT_SIM
extern	int	fault_mode;
#define	DoingFault	(fault_mode != 0)
#else
#define	DoingFault	(0)
#define	hchange	head		/* for the compiler's sake */
#endif


public	hptr   freeHist = NULL;		/* list of free history entries */
public  hptr   last_hist;		/* pointer to dummy hist-entry that
					 * serves as tail for all nodes */
public	int    num_edges = 0;
public	int    num_punted = 0;
public	int    num_cons_punted = 0;

public	hptr   first_model;		/* first model entry */
private	hptr   curr_model;		/* ptr. to current model entry */

private	char   mem_msg[] = "*** OUT OF MEMORY:Will stop collecting history\n";

public void init_hist()
  {
    static HistEnt  dummy;
    static HistEnt  dummy_model;

    last_hist = &dummy;
    dummy.next = last_hist;
    dummy.time = MAX_TIME;
    dummy.val = X;
    dummy.inp = 1;
    dummy.punt = 0;
    dummy.t.r.delay = dummy.t.r.rtime = 0;

    dummy_model.time = 0;
    dummy_model.val = model_num;
    dummy_model.inp = 0;
    dummy_model.punt = 0;
    dummy_model.next = NULL;
    first_model = curr_model = &dummy_model;
  }


#define	NEW_HIST( NEW, ACTnoMEM )					\
  {									\
    if( ((NEW) = freeHist) == NULL )					\
      {									\
	if( ((NEW) = (hptr) MallocList( sizeof( HistEnt ), 0 )) == NULL ) \
	  {								\
	    lprintf( stderr, mem_msg );					\
	    sm_stat |= OUT_OF_MEM;					\
	    ACTnoMEM;							\
	  }								\
      }									\
    freeHist = (NEW)->next;						\
  }									\


public void SetFirstHist( node, value, inp, time )
  nptr  node;
  int   value, inp;
  Ulong  time;
  {
    node->head.time = time;
    node->head.val = value;
    node->head.inp = inp;
    node->head.t.r.rtime = node->head.t.r.delay = 0;
  }


/*
 * Add a new entry to the history list.  Update curr to point to this change.
 */
public void AddHist( node, value, inp, time, delay, rtime )
  register nptr  node;
  int            value;
  int            inp;
  Ulong          time;
  long           delay, rtime;
  {
    register hptr  newh, curr;

    num_edges++;

    curr = node->curr;

    if( sm_stat & OUT_OF_MEM )
        return;

    while( curr->next->punt )		/* skip past any punted events */
	curr = curr->next;

    NEW_HIST( newh, return );

    newh->next = curr->next;
    newh->time = time;
    newh->val = value;
    newh->inp = inp;
    newh->punt = 0;
    newh->t.r.delay = delay;
    newh->t.r.rtime = rtime;
    node->curr = curr->next = newh;
  }


#define	PuntTime( H )		( (H)->time - (H)->t.p.ptime )


/*
 * Add a punted event to the history list for the node.  Consecutive punted
 * events are kept in punted-order, so that h->ptime < h->next->ptime.
 * Adding a punted event does not change the current pointer, which always
 * points to the last "effective" node change.
 */
public void AddPunted( node, ev, tim )
  register nptr  node;
  evptr          ev;
  Ulong          tim;
  {
    register hptr  newp, h;

    h = node->curr;

    num_punted++;
    if( (sm_stat & OUT_OF_MEM) or DoingFault )
        return;

    NEW_HIST( newp, return );		/* allocate the punted event itself */

    newp->time = ev->ntime;
    newp->val = ev->eval;
    newp->inp = 0;
    newp->punt = 1;
    newp->t.p.delay = (Uint)(ev->delay);
    newp->t.p.rtime = (Uint)(ev->rtime);
    newp->t.p.ptime = (Uint)(newp->time - tim);

    if( h->next->punt )		/* there are some punted events already */
      {
	num_cons_punted++;
	do { h = h->next; } while( h->next->punt );
      }

    newp->next = h->next;
    h->next = newp;
  }


/*
 * Free up a node's history list
 */
public void FreeHistList( node )
  register nptr  node;
  {
    register hptr  h, next;

    if( (h = node->head.next) == last_hist )		/* nothing to do */
    	return;

    while( (next = h->next) != last_hist )		/* find last entry */
	h = next;

    h->next = freeHist;				/* link list to free list */
    freeHist = node->head.next;

    node->head.next = last_hist;
    node->curr = &(node->head);

    sm_stat &= ~OUT_OF_MEM;
  }



public void NoMoreIncSim()
  {
    lprintf( stderr, "*** can't continue incremetal simulation\n" );
    exit( 1 );		/* do something here ? */
  }


/*
 * Add a new model entry, recording the time of the change.
 */
public void NewModel( nmodel )
  int  nmodel;
  {
    if( curr_model->time != cur_delta )
      {
	hptr newh;

	NEW_HIST( newh, NoMoreIncSim() );

	newh->next = NULL;
	newh->time = cur_delta;
	newh->val = nmodel;
	curr_model->next = newh;
	curr_model = newh;
      }
    else
	curr_model->val = nmodel;
  }



#define	QTIME( H )		( (H)->time - (H)->t.r.delay )

/*
 * Add a new change to the history list of node 'nd' caused by event 'ev'.
 * Skip past any punted events already in the history and update nd->curr to
 * point to the new change.
 */
public void NewEdge( nd, ev )
  nptr   nd;
  evptr  ev;
  {
    register hptr  p, h, newh;

    for( p = nd->curr, h = p->next; h->punt; p = h, h = h->next );

    if( DoingFault )
	p = newh = &nd->hchange;
    else
	NEW_HIST( newh, NoMoreIncSim() );

    newh->time = ev->ntime;
    newh->val = ev->eval;
    newh->inp = 0;		/* always true in incremental simulation */
    newh->punt = 0;
    newh->t.r.delay = ev->delay;
    newh->t.r.rtime = ev->rtime;

    p->next = newh;
    newh->next = h;

    nd->curr = newh;		/* newh becomes the current value */
  }


/*
 * Delete the next effective change (following nd->curr) from the history.
 * Punted events before the next change (in nd->t.punts) can now be freed.
 * Punted events following the deleted edge are moved to nd->t.punts.
 */
public void DeleteNextEdge( nd )
  nptr  nd;
  {
    register hptr  a, b, c;

    if( DoingFault )
      {
	if( nd->t.punts != NULL ) lprintf( stderr, "non-null punts\n" );
	
	if( nd->curr != &nd->hchange )
	  {
	    nd->hchange = *nd->curr;
	    nd->curr = &nd->hchange;
	  }
	
	a = nd->curr;
	for( b = a->next; b->punt; a = b, b = b->next );

	nd->hchange.next = b->next;	/* b->next => punt before next edge */
	nd->t.punts = NULL;
	return;
      }

    if( (a = nd->t.punts) != NULL )	/* remove previously punted events */
      {
	for( b = a; b->next != NULL; b = b->next );
	b->next = freeHist;
	freeHist = a;
      }

    a = nd->curr;
    for( b = a->next; b->punt; a = b, b = b->next );
    for( c = b->next; c->punt; b = c, c = c->next );
    c = a->next;			/* c => next edge */
    a->next = b->next;
    a = c->next;			/* a => first punted event after c */

    c->next = freeHist;			/* free the next effective change */
    freeHist = c;

    if( a->punt )			/* move punted events from hist */
      {
	nd->t.punts = a;
	b->next = NULL;
      }
    else
	nd->t.punts = NULL;
  }


#define	NEXTH( H, P )	for( (H) = (P)->next; (H)->punt; (H) = (H)->next )

public void FlushHist( ftime )
  register Ulong  ftime;
  {
    register nptr  n;
    register hptr  h, p, head;

    for( n = GetNodeList(); n != NULL; n = n->n.next )
      {
	head = &(n->head);
	if( head->next == last_hist or (n->nflags & ALIAS) )
	    continue;
	p = head;
	NEXTH( h, p );
	while( h->time < ftime )
	  {
	    p = h;
	    NEXTH( h, h );
	  }
	head->val = p->val;
	head->time = p->time;
	head->inp = p->inp;
	while( p->next != h )
	    p = p->next;
	if( head->next != h )
	  {
	    p->next = freeHist;
	    freeHist = head->next;
	    head->next = h;
	  }
	if( n->curr->time < ftime )
	  {
	    n->curr = head;
	  }
      }
  }


public int backToTime( nd )
  register nptr  nd;
  {
    register hptr  h, p;

    if( nd->nflags & (ALIAS | MERGED) )
	return( 0 );

    h = &(nd->head);
    NEXTH( p, h );
    while( p->time < cur_delta )
      {
	h = p;
	NEXTH( p, p );
      }
    nd->curr = h;

	/* queue pending events */
    for( p = h, h = p->next; ; p = h, h = h->next )
      {
	Ulong  qtime;

	if( h->punt )
	  {
	    if( PuntTime( h ) < cur_delta )	/* already punted, skip it */
		continue;

	    qtime = h->time - h->t.p.delay;	/* pending, enqueue it */
	    if( qtime < cur_delta )
	      {
		Ulong tmp = cur_delta;
		cur_delta = qtime;
		enqueue_event( nd, (int) h->val, (long) h->t.p.delay,
		  (long) h->t.p.rtime );
		cur_delta = tmp;
	      }
	    p->next = h->next;
	    h->next = freeHist;
	    freeHist = h;
	    h = p;
	  }
	else
	  {
	    qtime = QTIME( h );
	    if( qtime < cur_delta )		/* pending, enqueue it */
	      {
		Ulong tmp = cur_delta;
		cur_delta = qtime;
		enqueue_event( nd, (int) h->val, (long) h->t.r.delay,
		  (long) h->t.r.rtime );
		cur_delta = tmp;

		p->next = h->next;		/* and free it */
		h->next = freeHist;
		freeHist = h;
		h = p;
	      }
	    else
		break;
	  }
      }

    p->next = last_hist;
    p = h;
	/* p now points to the 1st event in the future (to be deleted) */
    if( p != last_hist )
      {
	while( h->next != last_hist )
	    h = h->next;
	h->next = freeHist;
	freeHist = p;
      }

    h = nd->curr;
    nd->npot = h->val;
    nd->c.time = h->time;
    if( h->inp )
	nd->nflags |= INPUT;

    if( nd->ngate != NULL )		/* recompute transistor states */
      {
	register lptr  l;
	register tptr  t;

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