-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcd.c
127 lines (107 loc) · 2.69 KB
/
vcd.c
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "vcd.h"
struct vcd
{
const char *name;
const char *type;
int width;
char id[5];
int value;
double real;
};
#define VARIABLES 100
struct vcd variable[VARIABLES];
static long long last_time = -1;
static FILE *output = NULL;
static int index = 0;
static void vcd_binary (int value, int width)
{
int mask;
fputc ('b', output);
for (mask = (1 << (width - 1)); mask != 0; mask >>= 1)
fputc (value & mask ? '1' : '0', output);
}
void vcd_value (long long current_time, int index, int value)
{
if (variable[index].value == value)
return;
if (current_time > last_time) {
last_time = current_time;
fprintf (output, "#%lld\n", current_time);
}
variable[index].value = value;
if (variable[index].width == 1)
fprintf (output, "%d%s\n", !!value, variable[index].id);
else {
vcd_binary (value, variable[index].width);
fprintf (output, " %s\n", variable[index].id);
}
}
void vcd_real (long long current_time, int index, double value)
{
if (variable[index].real == value)
return;
if (current_time > last_time) {
last_time = current_time;
fprintf (output, "#%lld\n", current_time);
}
variable[index].real = value;
fprintf (output, "r%.16g %s\n", value, variable[index].id);
}
static void vcd_close (void)
{
fclose (output);
}
void vcd_start (const char *file, const char *name, const char *timescale)
{
int i;
output = fopen (file, "w");
if (output == NULL) {
fprintf (stderr, "Error opening %s\n", file);
exit (1);
}
atexit (vcd_close);
fprintf (output, "$version %s $end\n", name);
fprintf (output, "$timescale %s $end\n", timescale);
for (i = 0; i < index; i++) {
fprintf (output, "$var %s %d %s %s",
variable[i].type,
variable[i].width,
variable[i].id,
variable[i].name);
if (variable[i].width > 1)
fprintf (output, " [%d:0]", variable[i].width - 1);
fprintf (output, " $end\n");
}
fprintf (output, "$enddefinitions $end\n");
fprintf (output, "$dumpvars\n");
fflush (output);
}
static void vcd_id (char *id, int index)
{
do {
*id++ = '!' + (index % 93);
index /= 93;
} while (index > 0);
*id = 0;
}
int vcd_variable (const char *name, const char *type, int width)
{
if (output != NULL) {
fprintf (stderr, "Must declare all variables before output.\n");
exit (1);
}
if (index == VARIABLES) {
fprintf (stderr, "Too many variables.\n");
exit (1);
}
variable[index].name = name;
variable[index].type = type;
variable[index].width = width;
variable[index].value = -1;
variable[index].real = NAN;
vcd_id (variable[index].id, index);
return index++;
}