]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Petteri Aimonen <jpa@sr.mail.kapsi.fi> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | /* The VCD input module has the following options: | |
21 | * | |
22 | * numchannels: Maximum number of channels to use. The channels are | |
23 | * detected in the same order as they are listed | |
24 | * in the $var sections of the VCD file. | |
25 | * | |
26 | * skip: Allows skipping until given timestamp in the file. | |
27 | * This can speed up analyzing of long captures. | |
28 | * | |
29 | * Value < 0: Skip until first timestamp listed in | |
30 | * the file. (default) | |
31 | * | |
32 | * Value = 0: Do not skip, instead generate samples | |
33 | * beginning from timestamp 0. | |
34 | * | |
35 | * Value > 0: Start at the given timestamp. | |
36 | * | |
37 | * downsample: Divide the samplerate by the given factor. | |
38 | * This can speed up analyzing of long captures. | |
39 | * | |
40 | * compress: Compress idle periods longer than this value. | |
41 | * This can speed up analyzing of long captures. | |
42 | * Default 0 = don't compress. | |
43 | * | |
44 | * Based on Verilog standard IEEE Std 1364-2001 Version C | |
45 | * | |
46 | * Supported features: | |
47 | * - $var with 'wire' and 'reg' types of scalar variables | |
48 | * - $timescale definition for samplerate | |
49 | * - multiple character variable identifiers | |
50 | * | |
51 | * Most important unsupported features: | |
52 | * - vector variables (bit vectors etc.) | |
53 | * - analog, integer and real number variables | |
54 | * - $dumpvars initial value declaration | |
55 | * - $scope namespaces | |
56 | * - more than 64 channels | |
57 | */ | |
58 | ||
59 | #include <stdlib.h> | |
60 | #include <glib.h> | |
61 | #include <stdio.h> | |
62 | #include <string.h> | |
63 | #include "libsigrok.h" | |
64 | #include "libsigrok-internal.h" | |
65 | ||
66 | #define LOG_PREFIX "input/vcd" | |
67 | ||
68 | #define DEFAULT_NUM_CHANNELS 8 | |
69 | #define CHUNKSIZE 1024 | |
70 | ||
71 | struct context { | |
72 | uint64_t samplerate; | |
73 | int maxchannels; | |
74 | int channelcount; | |
75 | int downsample; | |
76 | unsigned compress; | |
77 | int64_t skip; | |
78 | GSList *channels; | |
79 | }; | |
80 | ||
81 | struct vcd_channel { | |
82 | gchar *name; | |
83 | gchar *identifier; | |
84 | }; | |
85 | ||
86 | ||
87 | /* Read until specific type of character occurs in file. | |
88 | * Skip input if dest is NULL. | |
89 | * Modes: | |
90 | * 'W' read until whitespace | |
91 | * 'N' read until non-whitespace, and ungetc() the character | |
92 | * '$' read until $end | |
93 | */ | |
94 | static gboolean read_until(FILE *file, GString *dest, char mode) | |
95 | { | |
96 | int c; | |
97 | char prev[4] = ""; | |
98 | ||
99 | for(;;) { | |
100 | c = fgetc(file); | |
101 | ||
102 | if (c == EOF) { | |
103 | if (mode == '$') | |
104 | sr_err("Unexpected EOF."); | |
105 | return FALSE; | |
106 | } | |
107 | ||
108 | if (mode == 'W' && g_ascii_isspace(c)) | |
109 | return TRUE; | |
110 | ||
111 | if (mode == 'N' && !g_ascii_isspace(c)) { | |
112 | ungetc(c, file); | |
113 | return TRUE; | |
114 | } | |
115 | ||
116 | if (mode == '$') { | |
117 | prev[0] = prev[1]; prev[1] = prev[2]; prev[2] = prev[3]; prev[3] = c; | |
118 | if (prev[0] == '$' && prev[1] == 'e' && prev[2] == 'n' && prev[3] == 'd') { | |
119 | if (dest != NULL) | |
120 | g_string_truncate(dest, dest->len - 3); | |
121 | ||
122 | return TRUE; | |
123 | } | |
124 | } | |
125 | ||
126 | if (dest != NULL) | |
127 | g_string_append_c(dest, c); | |
128 | } | |
129 | } | |
130 | ||
131 | /* | |
132 | * Reads a single VCD section from input file and parses it to structure. | |
133 | * e.g. $timescale 1ps $end => "timescale" "1ps" | |
134 | */ | |
135 | static gboolean parse_section(FILE *file, gchar **name, gchar **contents) | |
136 | { | |
137 | gboolean status; | |
138 | GString *sname, *scontents; | |
139 | ||
140 | /* Skip any initial white-space */ | |
141 | if (!read_until(file, NULL, 'N')) return FALSE; | |
142 | ||
143 | /* Section tag should start with $. */ | |
144 | if (fgetc(file) != '$') { | |
145 | sr_err("Expected $ at beginning of section."); | |
146 | return FALSE; | |
147 | } | |
148 | ||
149 | /* Read the section tag */ | |
150 | sname = g_string_sized_new(32); | |
151 | status = read_until(file, sname, 'W'); | |
152 | ||
153 | /* Skip whitespace before content */ | |
154 | status = status && read_until(file, NULL, 'N'); | |
155 | ||
156 | /* Read the content */ | |
157 | scontents = g_string_sized_new(128); | |
158 | status = status && read_until(file, scontents, '$'); | |
159 | g_strchomp(scontents->str); | |
160 | ||
161 | /* Release strings if status is FALSE, return them if status is TRUE */ | |
162 | *name = g_string_free(sname, !status); | |
163 | *contents = g_string_free(scontents, !status); | |
164 | return status; | |
165 | } | |
166 | ||
167 | static void free_channel(void *data) | |
168 | { | |
169 | struct vcd_channel *vcd_ch = data; | |
170 | g_free(vcd_ch->name); | |
171 | g_free(vcd_ch->identifier); | |
172 | g_free(vcd_ch); | |
173 | } | |
174 | ||
175 | static void release_context(struct context *ctx) | |
176 | { | |
177 | g_slist_free_full(ctx->channels, free_channel); | |
178 | g_free(ctx); | |
179 | } | |
180 | ||
181 | /* Remove empty parts from an array returned by g_strsplit. */ | |
182 | static void remove_empty_parts(gchar **parts) | |
183 | { | |
184 | gchar **src = parts; | |
185 | gchar **dest = parts; | |
186 | while (*src != NULL) { | |
187 | if (**src != '\0') | |
188 | *dest++ = *src; | |
189 | src++; | |
190 | } | |
191 | ||
192 | *dest = NULL; | |
193 | } | |
194 | ||
195 | /* | |
196 | * Parse VCD header to get values for context structure. | |
197 | * The context structure should be zeroed before calling this. | |
198 | */ | |
199 | static gboolean parse_header(FILE *file, struct context *ctx) | |
200 | { | |
201 | uint64_t p, q; | |
202 | gchar *name = NULL, *contents = NULL; | |
203 | gboolean status = FALSE; | |
204 | struct vcd_channel *vcd_ch; | |
205 | ||
206 | while (parse_section(file, &name, &contents)) { | |
207 | sr_dbg("Section '%s', contents '%s'.", name, contents); | |
208 | ||
209 | if (g_strcmp0(name, "enddefinitions") == 0) { | |
210 | status = TRUE; | |
211 | break; | |
212 | } else if (g_strcmp0(name, "timescale") == 0) { | |
213 | /* | |
214 | * The standard allows for values 1, 10 or 100 | |
215 | * and units s, ms, us, ns, ps and fs. | |
216 | * */ | |
217 | if (sr_parse_period(contents, &p, &q) == SR_OK) { | |
218 | ctx->samplerate = q / p; | |
219 | if (q % p != 0) { | |
220 | /* Does not happen unless time value is non-standard */ | |
221 | sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 " Hz.", | |
222 | q, p, ctx->samplerate); | |
223 | } | |
224 | ||
225 | sr_dbg("Samplerate: %" PRIu64, ctx->samplerate); | |
226 | } else { | |
227 | sr_err("Parsing timescale failed."); | |
228 | } | |
229 | } else if (g_strcmp0(name, "var") == 0) { | |
230 | /* Format: $var type size identifier reference $end */ | |
231 | gchar **parts = g_strsplit_set(contents, " \r\n\t", 0); | |
232 | remove_empty_parts(parts); | |
233 | ||
234 | if (g_strv_length(parts) != 4) | |
235 | sr_warn("$var section should have 4 items"); | |
236 | else if (g_strcmp0(parts[0], "reg") != 0 && g_strcmp0(parts[0], "wire") != 0) | |
237 | sr_info("Unsupported signal type: '%s'", parts[0]); | |
238 | else if (strtol(parts[1], NULL, 10) != 1) | |
239 | sr_info("Unsupported signal size: '%s'", parts[1]); | |
240 | else if (ctx->channelcount >= ctx->maxchannels) | |
241 | sr_warn("Skipping '%s' because only %d channels requested.", parts[3], ctx->maxchannels); | |
242 | else { | |
243 | sr_info("Channel %d is '%s' identified by '%s'.", ctx->channelcount, parts[3], parts[2]); | |
244 | vcd_ch = g_malloc(sizeof(struct vcd_channel)); | |
245 | vcd_ch->identifier = g_strdup(parts[2]); | |
246 | vcd_ch->name = g_strdup(parts[3]); | |
247 | ctx->channels = g_slist_append(ctx->channels, vcd_ch); | |
248 | ctx->channelcount++; | |
249 | } | |
250 | ||
251 | g_strfreev(parts); | |
252 | } | |
253 | ||
254 | g_free(name); name = NULL; | |
255 | g_free(contents); contents = NULL; | |
256 | } | |
257 | ||
258 | g_free(name); | |
259 | g_free(contents); | |
260 | ||
261 | return status; | |
262 | } | |
263 | ||
264 | static int format_match(const char *filename) | |
265 | { | |
266 | FILE *file; | |
267 | gchar *name = NULL, *contents = NULL; | |
268 | gboolean status; | |
269 | ||
270 | file = fopen(filename, "r"); | |
271 | if (file == NULL) | |
272 | return FALSE; | |
273 | ||
274 | /* | |
275 | * If we can parse the first section correctly, | |
276 | * then it is assumed to be a VCD file. | |
277 | */ | |
278 | status = parse_section(file, &name, &contents); | |
279 | status = status && (*name != '\0'); | |
280 | ||
281 | g_free(name); | |
282 | g_free(contents); | |
283 | fclose(file); | |
284 | ||
285 | return status; | |
286 | } | |
287 | ||
288 | static int init(struct sr_input *in, const char *filename) | |
289 | { | |
290 | struct sr_channel *ch; | |
291 | int num_channels, i; | |
292 | char name[SR_MAX_CHANNELNAME_LEN + 1]; | |
293 | char *param; | |
294 | struct context *ctx; | |
295 | ||
296 | (void)filename; | |
297 | ||
298 | if (!(ctx = g_try_malloc0(sizeof(*ctx)))) { | |
299 | sr_err("Input format context malloc failed."); | |
300 | return SR_ERR_MALLOC; | |
301 | } | |
302 | ||
303 | num_channels = DEFAULT_NUM_CHANNELS; | |
304 | ctx->samplerate = 0; | |
305 | ctx->downsample = 1; | |
306 | ctx->skip = -1; | |
307 | ||
308 | if (in->param) { | |
309 | param = g_hash_table_lookup(in->param, "numchannels"); | |
310 | if (param) { | |
311 | num_channels = strtoul(param, NULL, 10); | |
312 | if (num_channels < 1) { | |
313 | release_context(ctx); | |
314 | return SR_ERR; | |
315 | } else if (num_channels > 64) { | |
316 | sr_err("No more than 64 channels supported."); | |
317 | return SR_ERR; | |
318 | } | |
319 | } | |
320 | ||
321 | param = g_hash_table_lookup(in->param, "downsample"); | |
322 | if (param) { | |
323 | ctx->downsample = strtoul(param, NULL, 10); | |
324 | if (ctx->downsample < 1) | |
325 | ctx->downsample = 1; | |
326 | } | |
327 | ||
328 | param = g_hash_table_lookup(in->param, "compress"); | |
329 | if (param) | |
330 | ctx->compress = strtoul(param, NULL, 10); | |
331 | ||
332 | param = g_hash_table_lookup(in->param, "skip"); | |
333 | if (param) | |
334 | ctx->skip = strtoul(param, NULL, 10) / ctx->downsample; | |
335 | } | |
336 | ||
337 | /* Maximum number of channels to parse from the VCD */ | |
338 | ctx->maxchannels = num_channels; | |
339 | ||
340 | /* Create a virtual device. */ | |
341 | in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); | |
342 | in->internal = ctx; | |
343 | ||
344 | for (i = 0; i < num_channels; i++) { | |
345 | snprintf(name, SR_MAX_CHANNELNAME_LEN, "%d", i); | |
346 | ||
347 | if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, name))) { | |
348 | release_context(ctx); | |
349 | return SR_ERR; | |
350 | } | |
351 | ||
352 | in->sdi->channels = g_slist_append(in->sdi->channels, ch); | |
353 | } | |
354 | ||
355 | return SR_OK; | |
356 | } | |
357 | ||
358 | /* Send N samples of the given value. */ | |
359 | static void send_samples(const struct sr_dev_inst *sdi, uint64_t sample, uint64_t count) | |
360 | { | |
361 | struct sr_datafeed_packet packet; | |
362 | struct sr_datafeed_logic logic; | |
363 | uint64_t buffer[CHUNKSIZE]; | |
364 | uint64_t i; | |
365 | unsigned chunksize = CHUNKSIZE; | |
366 | ||
367 | if (count < chunksize) | |
368 | chunksize = count; | |
369 | ||
370 | for (i = 0; i < chunksize; i++) | |
371 | buffer[i] = sample; | |
372 | ||
373 | packet.type = SR_DF_LOGIC; | |
374 | packet.payload = &logic; | |
375 | logic.unitsize = sizeof(uint64_t); | |
376 | logic.data = buffer; | |
377 | ||
378 | while (count) { | |
379 | if (count < chunksize) | |
380 | chunksize = count; | |
381 | ||
382 | logic.length = sizeof(uint64_t) * chunksize; | |
383 | ||
384 | sr_session_send(sdi, &packet); | |
385 | count -= chunksize; | |
386 | } | |
387 | } | |
388 | ||
389 | /* Parse the data section of VCD */ | |
390 | static void parse_contents(FILE *file, const struct sr_dev_inst *sdi, struct context *ctx) | |
391 | { | |
392 | GString *token = g_string_sized_new(32); | |
393 | ||
394 | uint64_t prev_timestamp = 0; | |
395 | uint64_t prev_values = 0; | |
396 | ||
397 | /* Read one space-delimited token at a time. */ | |
398 | while (read_until(file, NULL, 'N') && read_until(file, token, 'W')) { | |
399 | if (token->str[0] == '#' && g_ascii_isdigit(token->str[1])) { | |
400 | /* Numeric value beginning with # is a new timestamp value */ | |
401 | uint64_t timestamp; | |
402 | timestamp = strtoull(token->str + 1, NULL, 10); | |
403 | ||
404 | if (ctx->downsample > 1) | |
405 | timestamp /= ctx->downsample; | |
406 | ||
407 | /* | |
408 | * Skip < 0 => skip until first timestamp. | |
409 | * Skip = 0 => don't skip | |
410 | * Skip > 0 => skip until timestamp >= skip. | |
411 | */ | |
412 | if (ctx->skip < 0) { | |
413 | ctx->skip = timestamp; | |
414 | prev_timestamp = timestamp; | |
415 | } else if (ctx->skip > 0 && timestamp < (uint64_t)ctx->skip) { | |
416 | prev_timestamp = ctx->skip; | |
417 | } | |
418 | else if (timestamp == prev_timestamp) { | |
419 | /* Ignore repeated timestamps (e.g. sigrok outputs these) */ | |
420 | } | |
421 | else { | |
422 | if (ctx->compress != 0 && timestamp - prev_timestamp > ctx->compress) | |
423 | { | |
424 | /* Compress long idle periods */ | |
425 | prev_timestamp = timestamp - ctx->compress; | |
426 | } | |
427 | ||
428 | sr_dbg("New timestamp: %" PRIu64, timestamp); | |
429 | ||
430 | /* Generate samples from prev_timestamp up to timestamp - 1. */ | |
431 | send_samples(sdi, prev_values, timestamp - prev_timestamp); | |
432 | prev_timestamp = timestamp; | |
433 | } | |
434 | } else if (token->str[0] == '$' && token->len > 1) { | |
435 | /* This is probably a $dumpvars, $comment or similar. | |
436 | * $dump* contain useful data, but other tags will be skipped until $end. */ | |
437 | if (g_strcmp0(token->str, "$dumpvars") == 0 | |
438 | || g_strcmp0(token->str, "$dumpon") == 0 | |
439 | || g_strcmp0(token->str, "$dumpoff") == 0 | |
440 | || g_strcmp0(token->str, "$end") == 0) { | |
441 | /* Ignore, parse contents as normally. */ | |
442 | } else { | |
443 | /* Skip until $end */ | |
444 | read_until(file, NULL, '$'); | |
445 | } | |
446 | } | |
447 | else if (strchr("bBrR", token->str[0]) != NULL) { | |
448 | /* A vector value. Skip it and also the following identifier. */ | |
449 | read_until(file, NULL, 'N'); | |
450 | read_until(file, NULL, 'W'); | |
451 | } else if (strchr("01xXzZ", token->str[0]) != NULL) { | |
452 | /* A new 1-bit sample value */ | |
453 | int i, bit; | |
454 | GSList *l; | |
455 | struct vcd_channel *vcd_ch; | |
456 | ||
457 | bit = (token->str[0] == '1'); | |
458 | ||
459 | g_string_erase(token, 0, 1); | |
460 | if (token->len == 0) { | |
461 | /* There was a space between value and identifier. | |
462 | * Read in the rest. | |
463 | */ | |
464 | read_until(file, NULL, 'N'); | |
465 | read_until(file, token, 'W'); | |
466 | } | |
467 | ||
468 | for (i = 0, l = ctx->channels; i < ctx->channelcount && l; i++, l = l->next) { | |
469 | vcd_ch = l->data; | |
470 | ||
471 | if (g_strcmp0(token->str, vcd_ch->identifier) == 0) { | |
472 | /* Found our channel */ | |
473 | if (bit) | |
474 | prev_values |= (uint64_t)1 << i; | |
475 | else | |
476 | prev_values &= ~((uint64_t)1 << i); | |
477 | ||
478 | break; | |
479 | } | |
480 | } | |
481 | ||
482 | if (i == ctx->channelcount) | |
483 | sr_dbg("Did not find channel for identifier '%s'.", token->str); | |
484 | } else { | |
485 | sr_warn("Skipping unknown token '%s'.", token->str); | |
486 | } | |
487 | ||
488 | g_string_truncate(token, 0); | |
489 | } | |
490 | ||
491 | g_string_free(token, TRUE); | |
492 | } | |
493 | ||
494 | static int loadfile(struct sr_input *in, const char *filename) | |
495 | { | |
496 | struct sr_datafeed_packet packet; | |
497 | struct sr_datafeed_meta meta; | |
498 | struct sr_config *src; | |
499 | FILE *file; | |
500 | struct context *ctx; | |
501 | uint64_t samplerate; | |
502 | ||
503 | ctx = in->internal; | |
504 | ||
505 | if ((file = fopen(filename, "r")) == NULL) | |
506 | return SR_ERR; | |
507 | ||
508 | if (!parse_header(file, ctx)) { | |
509 | sr_err("VCD parsing failed"); | |
510 | fclose(file); | |
511 | return SR_ERR; | |
512 | } | |
513 | ||
514 | /* Send header packet to the session bus. */ | |
515 | std_session_send_df_header(in->sdi, LOG_PREFIX); | |
516 | ||
517 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
518 | packet.type = SR_DF_META; | |
519 | packet.payload = &meta; | |
520 | samplerate = ctx->samplerate / ctx->downsample; | |
521 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate)); | |
522 | meta.config = g_slist_append(NULL, src); | |
523 | sr_session_send(in->sdi, &packet); | |
524 | sr_config_free(src); | |
525 | ||
526 | /* Parse the contents of the VCD file */ | |
527 | parse_contents(file, in->sdi, ctx); | |
528 | ||
529 | /* Send end packet to the session bus. */ | |
530 | packet.type = SR_DF_END; | |
531 | sr_session_send(in->sdi, &packet); | |
532 | ||
533 | fclose(file); | |
534 | release_context(ctx); | |
535 | in->internal = NULL; | |
536 | ||
537 | return SR_OK; | |
538 | } | |
539 | ||
540 | SR_PRIV struct sr_input_format input_vcd = { | |
541 | .id = "vcd", | |
542 | .description = "Value Change Dump", | |
543 | .format_match = format_match, | |
544 | .init = init, | |
545 | .loadfile = loadfile, | |
546 | }; |