This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverisys.cc
591 lines (495 loc) · 13.8 KB
/
verisys.cc
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
/*****************************************************************************
* Copyright 1994-2005, Elliot Mednick and Mark Hummel
* This file is part of Veriwell.
*
* Veriwell is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Veriwell is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/* VERISYS.C - PLI system routine hooks */
#define VERISYS_C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "veriuser.h"
#include "acc_user.h"
#include "glue.h"
#include "vtypes.h"
#include "tree.h"
#include "veriwell.h"
#include "io.h"
#include <stdio.h>
#include <stdlib.h>
#include "verisys.h"
int timeformatcheck(int, int);
int timeformatcall(int, int);
int p(int user, int reason);
int pcheck(int user, int reason);
int mon_check(int user, int reason);
int mon_call(int user, int reason);
int mon_misc(int data, int reason);
int pmisc(int user, int reason);
#include "sdf.h"
#include "random.h"
#include "plihacks.h"
s_tfcell verisystfs[] = {
/*** Template for an entry:
{ usertask|userfunction, data, checktf(), sizetf(), calltf(), misctf(),
"$tfname", forwref?, Vtool?, ErrMsg? },
Example:
{ usertask, 0, my_check, 0, my_func, my_misctf, "$my_task" },
***/
/*** add user entries here ***/
{usertask, 0, timeformatcheck, 0, timeformatcall, 0, "$timeformat"},
{usertask, 0, showallinstances_call, 0, showallinstances_call, 0,
"$showallinstances"},
{usertask, 0, pcheck, 0, p, pmisc, "$p"},
{usertask, 1, mon_check, 0, mon_call, mon_misc, "$my_monitor"},
{usertask, 0, dumpstructure_call, 0, dumpstructure_call,
dumpstructure_call,
"$$dumpstructure"},
{usertask, 0, 0, 0, abort_call, 0, "$$abort"},
{usertask, 0, 0, 0, setvalue_call, 0, "$$testsetvalue"},
{usertask, 0, sdf_check, 0, sdf_call, sdf_misc, "$sdf_annotate"},
{userfunction, 0, dist_uniform, dist_uniform, dist_uniform,
0, "$dist_uniform"},
{userfunction, 0, dist_normal, dist_normal, dist_normal,
0, "$dist_normal"},
{userfunction, 0, dist_exponential, dist_exponential, dist_exponential,
0, "$dist_exponential"},
{userfunction, 0, dist_poisson, dist_poisson, dist_poisson, 0,
"$dist_poisson"},
{userfunction, 0, dist_chi_square, dist_chi_square, dist_chi_square,
0, "$dist_chi_square"},
{userfunction, 0, dist_t, dist_t, dist_t, 0, "$dist_t"},
{userfunction, 0, dist_erlang, dist_erlang, dist_erlang, 0, "$dist_erlang"},
{0} /*** final entry must be 0 ***/
};
#include <stdio.h>
int p(int user, int reason)
{
printf("Get here on execution, user is %d, reason is %d\n", user,
reason);
printf("Number of arguments: %d\n", tf_nump());
tf_rosynchronize();
tf_synchronize();
return (0);
}
int pcheck(int user, int reason)
{
printf("Get here on compile, user is %d, reason is %d\n", user,
reason);
return (0);
}
int pmisc(int user, int reason)
{
return (0);
}
/**********************************************
* vcl example from manual
**********************************************/
int mon_consume(p_vc_record);
typedef struct t_mon_node {
handle param;
char name[256];
short int value;
} s_mon_node, *p_mon_node;
p_mon_node mon_array;
char *mon_instance_p;
int mon_num_params;
int mon_check(int data, int reason)
{
int i;
handle object;
static int net_reg_list[3] = { accNet, accRegister, 0 };
acc_initialize();
acc_configure(accDevelopmentVersion, "1.6a");
for (i = 1; i <= tf_nump(); i++) {
if (!(object = acc_handle_tfarg(i)))
tf_error("parameter #%d to $my_monitor illegal", i);
if (acc_fetch_type(object) != accNet
&& acc_fetch_type(object) != accRegister)
tf_error("parameter #%d to $my_monitor illegal", i);
if (acc_fetch_size(object) != 1)
tf_error("parameter #%d to $my_monitor illegal", i);
}
acc_close();
return (0);
}
int mon_call(int data, int reason)
{
int i;
char *value_str;
acc_initialize();
acc_configure(accDevelopmentVersion, "1.6a");
mon_instance_p = tf_getinstance();
mon_num_params = tf_nump();
mon_array = (p_mon_node) malloc(mon_num_params * (sizeof(s_mon_node)));
for (i = 0; i < mon_num_params; i++) {
mon_array[i].param = acc_handle_tfarg(i + 1);
strcpy(mon_array[i].name, acc_fetch_name(mon_array[i].param));
value_str = acc_fetch_value(mon_array[i].param, "%b");
switch (value_str[0]) {
case '0':
mon_array[i].value = vcl0;
break;
case '1':
mon_array[i].value = vcl1;
break;
case 'x':
mon_array[i].value = vclX;
break;
case 'z':
mon_array[i].value = vclZ;
break;
default:
tf_error("Unknown value...");
break;
}
acc_vcl_add(mon_array[i].param, mon_consume,
(char *) &mon_array[i], vcl_verilog_logic);
}
acc_close();
tf_synchronize();
return (0);
}
int mon_consume(p_vc_record change_data)
{
p_mon_node curr_mon_node;
switch (change_data->vc_reason) {
case logic_value_change:
case sregister_value_change:
curr_mon_node = (p_mon_node) change_data->user_data;
curr_mon_node->value = change_data->out_value.logic_value;
tf_isynchronize(mon_instance_p);
break;
default:
tf_error("Illegal callback from VCL");
}
return (TRUE);
}
int mon_misc(int data, int reason)
{
int i;
char *value;
switch (reason) {
case reason_synch:
io_printf("%s ", tf_strgettime());
for (i = 0; i < mon_num_params; i++) {
switch (mon_array[i].value) {
case vcl0:
value = "0";
break;
case vcl1:
value = "1";
break;
case vclX:
value = "x";
break;
case vclZ:
value = "z";
break;
}
io_printf("%s=%s ", mon_array[i].name, value);
}
io_printf("\n");
break;
}
return (0);
}
/* $timeformat (units, prec, suffix, width) */
/* This is for $timeformat. NOTE: This is imported by print.c! */
struct timeformat {
int units;
int precision;
char *suffix;
int width;
int use_defaults;
} timeformat;
int timeformatcall(int user, int reason)
{
char *tmp_suffix;
acc_initialize();
timeformat.units = tf_getp(1); /* units */
timeformat.precision = tf_getp(2); /* precision */
if (!timeformat.use_defaults) /* deallocate old stuff, if any */
free(timeformat.suffix);
tmp_suffix = tf_getcstringp(3); /* suffix string */
timeformat.suffix = (char *) xmalloc(strlen(tmp_suffix) + 1);
strcpy(timeformat.suffix, tmp_suffix); /* make perm copy */
timeformat.width = tf_getp(4);
timeformat.use_defaults = 0;
return 0;
}
int timeformatcheck(int user, int reason)
{
return 0;
}
/* this is called from print.c when %t is used (file_handle is the VeriWell file
handle) */
void timeformatprint(handle_t file_handle, tree scope, double this_time)
{
char spec_format[100]; /* this holds the print spec */
s_timescale_info timescale;
int printunits;
int scaleunits; /* how much to rescale this_time */
double realmult; /* multiplier */
double printtime; /* The time to print after rescaling */
int numwidth; /* width of number part */
/* get the timescale for the module containing the display */
acc_fetch_timescale_info((handle) scope, ×cale);
if (timeformat.use_defaults) {
printunits = acc_fetch_precision(); /* get smallest precision */
strcpy(spec_format, "%20.0f"); /* default time format */
} else {
printunits = timeformat.units;
numwidth = timeformat.width - strlen(timeformat.suffix);
if (numwidth < 0)
numwidth = 0;
sprintf(spec_format, "%%%d.%df%s", timeformat.width,
timeformat.precision, timeformat.suffix);
}
/* the given time has already been scaled to local units; unscale and
scale to timeformat units */
scaleunits = timescale.unit - printunits;
realmult = pow(10, scaleunits);
printtime = this_time * realmult; /* rescale */
fprintf_V(file_handle, spec_format, printtime); /* finally, print it */
}
/*****************************************************************
*
* showallinstances_call
* implements $showallinstances
*
*****************************************************************
*/
static ShowallEntry_t **showallTable;
int showallinstances_call(int data, int reason)
{
handle object;
acc_initialize();
switch (reason) {
case reason_checktf:
if (tf_nump() != 0) {
tf_error("$showallinstances takes no arguments");
}
break;
case reason_calltf:
if (!showall_init()) {
break;
}
object = null;
while (TRUE) {
object = acc_next_topmod(object);
if (object == null) {
break;
}
if (!showall_search(object)) {
acc_close();
return 0;
}
}
showall_output();
break;
}
acc_close();
return 0;
}
/*---------------------------------------------------
showall_init
- initialize hash table
-----------------------------------------------------*/
int showall_init(void)
{
int i;
showallTable = (ShowallEntry_t **) xmalloc(SHOWALL_HASH_SIZE *
sizeof(ShowallEntry_t
*));
if (showallTable == NULL) {
tf_error("$showallinstances: not enough memory");
return FALSE;
}
for (i = 0; i < SHOWALL_HASH_SIZE; i++) {
showallTable[i] = NULL;
}
return TRUE;
}
/*---------------------------------------------------
showall_register
- log the object
-----------------------------------------------------*/
int showall_register(handle object)
{
char *ptr;
int type;
char *name;
char *filename;
s_location location;
int index;
ShowallEntry_t *entry;
int fulltype;
name = acc_fetch_defname(object);
acc_fetch_location(&location, object);
type = acc_fetch_type(object);
fulltype = acc_fetch_fulltype(object);
if (type == accModule || fulltype == accCombPrim
|| fulltype == accSeqPrim) {
if (type == accModule && fulltype != accTopModule) {
type = accModule;
} else {
type = fulltype;
}
}
index = 0;
for (ptr = name; *ptr; ptr = ptr + 1) {
index = index ^ (unsigned) *ptr;
}
index &= 0xff;
for (entry = showallTable[index]; entry; entry = entry->next) {
if (!strcmp(entry->name, name) && entry->type == type) {
entry->count++;
return TRUE;
}
}
entry = (ShowallEntry_t *) xmalloc(sizeof(ShowallEntry_t));
if (entry == NULL) {
tf_error("$showallinstances: Out of memory");
return FALSE;
}
entry->name = name;
entry->type = type;
entry->count = 1;
entry->next = showallTable[index];
entry->filename = location.filename;
showallTable[index] = entry;
return TRUE;
}
/*---------------------------------------------------
showall_search
- traverse the hierarchy
-----------------------------------------------------*/
int showall_search(handle object)
{
int type;
handle newObject;
type = acc_fetch_type(object);
switch (type) {
case accModule:
if (!showall_register(object)) {
return FALSE;
}
case accTask:
case accFunction:
newObject = null;
while (TRUE) {
newObject = acc_next_primitive(object, newObject);
if (newObject == null) {
break;
}
if (!showall_register(newObject)) {
return FALSE;
}
}
newObject = null;
while (TRUE) {
newObject = acc_next_scope(object, newObject);
if (newObject == null) {
break;
}
if (!showall_search(newObject)) {
return FALSE;
}
}
break;
}
return TRUE;
}
/*---------------------------------------------------
showall_output
- dump object list and free structures
-----------------------------------------------------*/
void showall_output(void)
{
ShowallEntry_t *entry;
ShowallEntry_t *temp;
int i;
int moduleCount;
int primitiveCount;
int gateCount;
gateCount = 0;
primitiveCount = 0;
moduleCount = 0;
for (i = 0; i < SHOWALL_HASH_SIZE; i++) {
entry = showallTable[i];
while (entry) {
switch (entry->type) {
case accModule:
moduleCount += entry->count;
break;
case accPrimitive:
gateCount += entry->count;
break;
case accSeqPrim:
case accCombPrim:
gateCount += entry->count;
primitiveCount += entry->count;
break;
}
entry = entry->next;
}
}
io_printf("Total number of module instances = %d\n", moduleCount);
io_printf("Total number of primitive instances = %d\n",
primitiveCount);
for (i = 0; i < SHOWALL_HASH_SIZE; i++) {
entry = showallTable[i];
while (entry) {
switch (entry->type) {
case accTopModule:
io_printf("\ttop level module %s, from file \"%s\"\n",
entry->name, entry->filename);
break;
case accSeqPrim:
case accCombPrim:
io_printf("\t%d of primitive %s, from file \"%s\"\n",
entry->count, entry->name, entry->filename);
break;
case accModule:
io_printf("\t%d of module %s, from file \"%s\"\n",
entry->count, entry->name, entry->filename);
break;
}
entry = entry->next;
}
}
io_printf("Total number of gates = %d\n", gateCount);
for (i = 0; i < SHOWALL_HASH_SIZE; i++) {
entry = showallTable[i];
while (entry) {
switch (entry->type) {
case accPrimitive:
io_printf("\t%d of %s\n", entry->count, entry->name);
break;
}
temp = entry;
entry = entry->next;
free(temp);
}
}
free(showallTable);
showallTable = NULL;
}
void init_verisys()
{
timeformat.use_defaults = 1;
}