-
Notifications
You must be signed in to change notification settings - Fork 12
/
vcd.c
233 lines (221 loc) · 7.63 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
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
#include <ctype.h>
#include <inttypes.h> // for scanf(u64) portability
#include <stdint.h> // u64 typedef
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define USAGE "USAGE: vcd < in.vcd > out.ascii :\n"
#define PROLOG "Fatal error. Send the VCD on https://github.com/yne/vcd/issues"
#define REBUILD(D) #D " reached (" VAL(D) "), rebuild with -D" #D "=...\n"
#define die(...) exit(fprintf(stderr, PROLOG "\nReason: " __VA_ARGS__))
#define SFL 127 // scanf Token limit
#ifndef MAX_SCOPE
#define MAX_SCOPE 32 // how many char scopes[] to allocate
#endif
#ifndef MAX_CHANNEL
#define MAX_CHANNEL 400 // how many Channel to allocate 96*96 = 8836
#endif
#define ITV_TIME 10 // sample interval to display timestamp
#define VALUES "0123456789zZxXbU-" // allowed bus values/types
#define COUNT(A) (sizeof(A) / sizeof(*A))
#define MAX(A, B) (A > B ? A : B)
#define VAL(A) #A
#define TXT(A) VAL(A)
typedef char Token[SFL + 1]; // parsing token (channel name, scope name, ...)
typedef struct {
char *low, *raise, *high, *drown, *start, *end;
unsigned skip;
} PrintOpt;
typedef struct {
// could have as much state as it bus size but nobody handle such case
// 'UZX' => show state
// '\0' => show .val
char state;
unsigned val;
} Sample;
typedef struct {
unsigned size;
unsigned scope;
Token name;
Sample* samples;
} Channel;
typedef struct {
Channel ch[MAX_CHANNEL]; // [0] = timestamps
Token scopes[MAX_SCOPE]; // [0] = default
unsigned total, scope_count, chan_str;
float scale; // duration of each sample
Token date, version, unit; // file info
// parsing related values
unsigned scope_cur;
unsigned scope_lim, ch_lim, sz_lim;
} ParseCtx;
/* convert a base-94 or 'c'+num chan id (!...~) to integer */
size_t chanId(char* str_id, unsigned isStr) {
size_t id = 0;
if (isStr) {
id = atoi(str_id + 1);
} else {
for (size_t i = strlen(str_id); i >= 1; i--) {
id = (id * 94) + str_id[i - 1] - '!';
}
}
if (id > MAX_CHANNEL) die(REBUILD(MAX_CHANNEL));
return id;
}
size_t unilen(char* s) {
size_t j = 0;
for (; *s; s++) j += ((*s & 0xc0) != 0x80);
return j;
}
/* read a $instruction and it opt if needed until $end*/
void parseVcdInstruction(ParseCtx* p) {
Token token;
scanf("%" TXT(SFL) "s", token);
if (!strcmp("var", token)) {
Token id;
Channel c = {.scope = p->scope_cur, .samples = malloc(sizeof(Sample))};
scanf(" %*s %u %" TXT(SFL) "[^ ] %" TXT(SFL) "[^$]", &c.size, id, c.name);
p->ch_lim = MAX(p->ch_lim, strlen(c.name));
p->sz_lim = MAX(p->sz_lim, c.size);
p->ch[chanId(id, p->chan_str)] = c;
} else if (!strcmp("scope", token)) {
p->scope_count++;
if (p->scope_count == MAX_SCOPE) die(REBUILD(MAX_SCOPE));
p->scope_cur = p->scope_count;
scanf("%*s %" TXT(SFL) "[^ $]", p->scopes[p->scope_cur]);
p->scope_lim = MAX(p->scope_lim, strlen(p->scopes[p->scope_cur]));
} else if (!strcmp("date", token)) {
scanf("\n%" TXT(SFL) "[^$\n]", p->date);
} else if (!strcmp("version", token)) {
scanf("\n%" TXT(SFL) "[^$\n]", p->version);
// ROHD use 's'+digit channel ID sequencing
p->chan_str = strstr(p->version, "ROHD") != NULL;
} else if (!strcmp("timescale", token)) {
scanf("\n%f%" TXT(SFL) "[^$\n]", &p->scale, p->unit);
} else if (!strcmp("comment", token)) {
scanf("\n%*[^$]");
} else if (!strcmp("upscope", token)) {
scanf("\n%*[^$]");
p->scope_cur = 0; // back to the root
} else if (!strcmp("enddefinitions", token)) {
scanf("\n%*[^$]");
} else if (!strcmp("dumpvars", token)) {
} else if (!strcmp("end", token)) {
} else {
printf("unknown token : %s\n", token);
}
}
/* Parse a time line (ex: '#210000000') and copy all previous samples values */
void parseVcdTimestamp(ParseCtx* p) {
// copy previous sample on every channel
if (p->total > 0) {
for (Channel* ch = p->ch; ch < p->ch + COUNT(p->ch); ch++) {
if (!ch->size) continue; // skip unused channels
ch->samples = realloc(ch->samples, sizeof(Sample) * (p->total + 1));
ch->samples[p->total] = ch->samples[p->total - 1];
}
}
uint64_t _unused;
scanf("%" PRIu64, &_unused); // p->timestamps[p->total]
p->total++;
}
/*
sample line end with the channel ID and start either with a state or data:
1^
Z^
b0100 ^
0! 0" 1# 0$ 1% 0& 1'
*/
void parseVcdSample(ParseCtx* p, int c) {
Sample s = {'\0', 0};
if (c == 'b') {
for (c = getchar(); c != EOF && c != ' '; c = getchar()) {
if (c == '0' || c == '1') {
s.val = s.val * 2 + (c - '0');
} else if (strchr(VALUES, c)) {
s.state = c;
} else {
die("Unknown sample value: %c", c);
}
}
} else {
s.state = isalpha(c) ? c : '\0';
s.val = isdigit(c) ? c - '0' : 0;
}
Token id_str;
scanf("%" TXT(SFL) "[^ \n]", id_str);
if (!p->total) return; // ROHD define value BEFORE timestamp #0
p->ch[chanId(id_str, p->chan_str)].samples[p->total - 1] = s;
}
void parseVcd(ParseCtx* p) {
for (int c = getchar(); c != EOF; c = getchar()) {
if (isspace(c)) continue;
if (c == '$') {
parseVcdInstruction(p);
} else if (c == '#') {
parseVcdTimestamp(p);
} else if (strchr(VALUES, c)) {
parseVcdSample(p, c);
} else {
die("unknow char : %c\n", c);
}
}
}
void printYml(ParseCtx* p, PrintOpt* opt) {
if (unilen(opt->high) != 1) die("high waveform length must be 1");
if (unilen(opt->low) != 1) die("low waveform length must be 1");
if (unilen(opt->drown) > 1) die("drown waveform length must be 1 or empty");
if (unilen(opt->raise) > 1) die("raise waveform length must be 1 or empty");
int zoom = (p->sz_lim + 7) >> 2; // how many char per sample (8bit => 2)
int trans = *opt->drown && *opt->raise;
printf("global:\n");
printf(" zoom: %i\n", zoom);
printf(" date: %s\n", p->date);
printf(" total: %i\n", p->total);
printf(" skip: %i\n", opt->skip);
printf(" time:\n");
printf(" scale: %.2f\n", p->scale);
printf(" unit: %s\n", p->unit ?: "?");
printf(" %-*s: %s", p->ch_lim, "line", opt->start);
for (double smpl = opt->skip; smpl < p->total; smpl += ITV_TIME) {
printf("%-*g ", ITV_TIME * zoom - 1, smpl * p->scale);
}
printf("%s\nchannels:\n", opt->end);
for (Channel* ch = p->ch; ch - p->ch < (signed)COUNT(p->ch); ch++) {
// skip empty ch
if (!ch->size) continue;
// print scope (if changed)
if (ch == p->ch || ch->scope != ((ch - 1)->scope)) {
printf(" %s:\n", ch->scope ? p->scopes[ch->scope] : "default");
}
printf(" %-*s: %s", p->ch_lim, ch->name, opt->start);
for (Sample* s = ch->samples + opt->skip; s < ch->samples + p->total; s++) {
Sample* prev = s > ch->samples ? s - 1 : s;
if (s->state) { // state data: UUUUZZZZ-
printf("%-*c", zoom, s->state);
} else if (ch->size == 1) { // binary wave: ▁▁/▔▔
// have a different data => print a transition
for (int w = 0; w < zoom; w++) {
if (!w && trans && s->state == prev->state && s->val != prev->val)
printf("%s", prev->val ? opt->drown : opt->raise);
else
printf("%s", s->val ? opt->high : opt->low);
}
} else { // bus : show hex value
printf("%-*X", zoom, s->val);
}
}
printf("%s\n", opt->end);
}
}
int main() {
PrintOpt opt = {getenv("LOW") ?: "▁", getenv("RAISE") ?: "╱",
getenv("HIGH") ?: "▔", getenv("DROWN") ?: "╲",
getenv("STX") ?: "\"", getenv("ETX") ?: "\"",
atoi(getenv("SKIP") ?: "0")};
// PrintOpt opt = {"_", "/", "#", "\\"} {"▁", "╱", "▔", "╲"};
ParseCtx ctx = {0};
parseVcd(&ctx);
printYml(&ctx, &opt);
return 0;
}