]> sigrok.org Git - libsigrok.git/blob - src/input/vcd.c
input/vcd: abort VCD import when timestamp counts backwards
[libsigrok.git] / src / input / vcd.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Petteri Aimonen <jpa@sr.mail.kapsi.fi>
5  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /* The VCD input module has the following options:
22  *
23  * numchannels: Maximum number of channels to use. The channels are
24  *              detected in the same order as they are listed
25  *              in the $var sections of the VCD file.
26  *
27  * skip:        Allows skipping until given timestamp in the file.
28  *              This can speed up analyzing of long captures.
29  *
30  *              Value < 0: Skip until first timestamp listed in
31  *              the file. (default)
32  *
33  *              Value = 0: Do not skip, instead generate samples
34  *              beginning from timestamp 0.
35  *
36  *              Value > 0: Start at the given timestamp.
37  *
38  * downsample:  Divide the samplerate by the given factor.
39  *              This can speed up analyzing of long captures.
40  *
41  * compress:    Compress idle periods longer than this value.
42  *              This can speed up analyzing of long captures.
43  *              Default 0 = don't compress.
44  *
45  * Based on Verilog standard IEEE Std 1364-2001 Version C
46  *
47  * Supported features:
48  * - $var with 'wire' and 'reg' types of scalar variables
49  * - $timescale definition for samplerate
50  * - multiple character variable identifiers
51  *
52  * Most important unsupported features:
53  * - vector variables (bit vectors etc.)
54  * - analog, integer and real number variables
55  * - $dumpvars initial value declaration
56  * - $scope namespaces
57  * - more than 64 channels
58  */
59
60 #include <config.h>
61 #include <stdlib.h>
62 #include <glib.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <libsigrok/libsigrok.h>
66 #include "libsigrok-internal.h"
67
68 #define LOG_PREFIX "input/vcd"
69
70 #define CHUNK_SIZE (4 * 1024 * 1024)
71
72 struct context {
73         gboolean started;
74         gboolean got_header;
75         uint64_t prev_timestamp;
76         uint64_t samplerate;
77         unsigned int maxchannels;
78         unsigned int channelcount;
79         int downsample;
80         unsigned compress;
81         int64_t skip;
82         gboolean skip_until_end;
83         GSList *channels;
84         size_t bytes_per_sample;
85         size_t samples_in_buffer;
86         uint8_t *buffer;
87         uint8_t *current_levels;
88 };
89
90 struct vcd_channel {
91         gchar *name;
92         gchar *identifier;
93 };
94
95 /*
96  * Reads a single VCD section from input file and parses it to name/contents.
97  * e.g. $timescale 1ps $end => "timescale" "1ps"
98  */
99 static gboolean parse_section(GString *buf, gchar **name, gchar **contents)
100 {
101         GString *sname, *scontent;
102         gboolean status;
103         unsigned int pos;
104
105         *name = *contents = NULL;
106         status = FALSE;
107         pos = 0;
108
109         /* Skip UTF8 BOM */
110         if (buf->len >= 3 && !strncmp(buf->str, "\xef\xbb\xbf", 3))
111                 pos = 3;
112
113         /* Skip any initial white-space. */
114         while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
115                 pos++;
116
117         /* Section tag should start with $. */
118         if (buf->str[pos++] != '$')
119                 return FALSE;
120
121         sname = g_string_sized_new(32);
122         scontent = g_string_sized_new(128);
123
124         /* Read the section tag. */
125         while (pos < buf->len && !g_ascii_isspace(buf->str[pos]))
126                 g_string_append_c(sname, buf->str[pos++]);
127
128         /* Skip whitespace before content. */
129         while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
130                 pos++;
131
132         /* Read the content. */
133         while (pos < buf->len - 4 && strncmp(buf->str + pos, "$end", 4))
134                 g_string_append_c(scontent, buf->str[pos++]);
135
136         if (sname->len && pos < buf->len - 4 && !strncmp(buf->str + pos, "$end", 4)) {
137                 status = TRUE;
138                 pos += 4;
139                 while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
140                         pos++;
141                 g_string_erase(buf, 0, pos);
142         }
143
144         *name = g_string_free(sname, !status);
145         *contents = g_string_free(scontent, !status);
146         if (*contents)
147                 g_strchomp(*contents);
148
149         return status;
150 }
151
152 static void free_channel(void *data)
153 {
154         struct vcd_channel *vcd_ch = data;
155         g_free(vcd_ch->name);
156         g_free(vcd_ch->identifier);
157         g_free(vcd_ch);
158 }
159
160 /* Remove empty parts from an array returned by g_strsplit. */
161 static void remove_empty_parts(gchar **parts)
162 {
163         gchar **src = parts;
164         gchar **dest = parts;
165         while (*src != NULL) {
166                 if (**src != '\0')
167                         *dest++ = *src;
168                 src++;
169         }
170
171         *dest = NULL;
172 }
173
174 /*
175  * Parse VCD header to get values for context structure.
176  * The context structure should be zeroed before calling this.
177  */
178 static gboolean parse_header(const struct sr_input *in, GString *buf)
179 {
180         struct vcd_channel *vcd_ch;
181         uint64_t p, q;
182         struct context *inc;
183         gboolean status;
184         gchar *name, *contents, **parts;
185
186         inc = in->priv;
187         name = contents = NULL;
188         status = FALSE;
189         while (parse_section(buf, &name, &contents)) {
190                 sr_dbg("Section '%s', contents '%s'.", name, contents);
191
192                 if (g_strcmp0(name, "enddefinitions") == 0) {
193                         status = TRUE;
194                         break;
195                 } else if (g_strcmp0(name, "timescale") == 0) {
196                         /*
197                          * The standard allows for values 1, 10 or 100
198                          * and units s, ms, us, ns, ps and fs.
199                          */
200                         if (sr_parse_period(contents, &p, &q) == SR_OK) {
201                                 inc->samplerate = q / p;
202                                 if (q % p != 0) {
203                                         /* Does not happen unless time value is non-standard */
204                                         sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 " Hz.",
205                                                 q, p, inc->samplerate);
206                                 }
207
208                                 sr_dbg("Samplerate: %" PRIu64, inc->samplerate);
209                         } else {
210                                 sr_err("Parsing timescale failed.");
211                         }
212                 } else if (g_strcmp0(name, "var") == 0) {
213                         /* Format: $var type size identifier reference [opt. index] $end */
214                         unsigned int length;
215
216                         parts = g_strsplit_set(contents, " \r\n\t", 0);
217                         remove_empty_parts(parts);
218                         length = g_strv_length(parts);
219
220                         if (length != 4 && length != 5)
221                                 sr_warn("$var section should have 4 or 5 items");
222                         else if (g_strcmp0(parts[0], "reg") != 0 && g_strcmp0(parts[0], "wire") != 0)
223                                 sr_info("Unsupported signal type: '%s'", parts[0]);
224                         else if (strtol(parts[1], NULL, 10) != 1)
225                                 sr_info("Unsupported signal size: '%s'", parts[1]);
226                         else if (inc->maxchannels && inc->channelcount >= inc->maxchannels)
227                                 sr_warn("Skipping '%s%s' because only %d channels requested.",
228                                         parts[3], parts[4] ? : "", inc->maxchannels);
229                         else {
230                                 vcd_ch = g_malloc(sizeof(struct vcd_channel));
231                                 vcd_ch->identifier = g_strdup(parts[2]);
232                                 if (length == 4)
233                                         vcd_ch->name = g_strdup(parts[3]);
234                                 else
235                                         vcd_ch->name = g_strconcat(parts[3], parts[4], NULL);
236
237                                 sr_info("Channel %d is '%s' identified by '%s'.",
238                                                 inc->channelcount, vcd_ch->name, vcd_ch->identifier);
239
240                                 sr_channel_new(in->sdi, inc->channelcount++, SR_CHANNEL_LOGIC, TRUE, vcd_ch->name);
241                                 inc->channels = g_slist_append(inc->channels, vcd_ch);
242                         }
243
244                         g_strfreev(parts);
245                 }
246
247                 g_free(name);
248                 name = NULL;
249                 g_free(contents);
250                 contents = NULL;
251         }
252         g_free(name);
253         g_free(contents);
254
255         /*
256          * Compute how many bytes each sample will have and initialize the
257          * current levels. The current levels will be updated whenever VCD
258          * has changes.
259          */
260         inc->bytes_per_sample = (inc->channelcount + 7) / 8;
261         inc->current_levels = g_malloc0(inc->bytes_per_sample);
262
263         inc->got_header = status;
264
265         return status;
266 }
267
268 static int format_match(GHashTable *metadata, unsigned int *confidence)
269 {
270         GString *buf, *tmpbuf;
271         gboolean status;
272         gchar *name, *contents;
273
274         buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
275         tmpbuf = g_string_new_len(buf->str, buf->len);
276
277         /*
278          * If we can parse the first section correctly,
279          * then it is assumed to be a VCD file.
280          */
281         status = parse_section(tmpbuf, &name, &contents);
282         g_string_free(tmpbuf, TRUE);
283         g_free(name);
284         g_free(contents);
285
286         if (!status)
287                 return SR_ERR;
288         *confidence = 1;
289
290         return SR_OK;
291 }
292
293 /* Send all accumulated bytes from inc->buffer. */
294 static void send_buffer(const struct sr_input *in)
295 {
296         struct context *inc;
297         struct sr_datafeed_packet packet;
298         struct sr_datafeed_logic logic;
299
300         inc = in->priv;
301
302         if (inc->samples_in_buffer == 0)
303                 return;
304
305         packet.type = SR_DF_LOGIC;
306         packet.payload = &logic;
307         logic.unitsize = inc->bytes_per_sample;
308         logic.data = inc->buffer;
309         logic.length = inc->bytes_per_sample * inc->samples_in_buffer;
310         sr_session_send(in->sdi, &packet);
311         inc->samples_in_buffer = 0;
312 }
313
314 /*
315  * Add N copies of the current sample to buffer.
316  * When the buffer fills up, automatically send it.
317  */
318 static void add_samples(const struct sr_input *in, size_t count)
319 {
320         struct context *inc;
321         size_t samples_per_chunk;
322         size_t space_left, i;
323         uint8_t *p;
324
325         inc = in->priv;
326         samples_per_chunk = CHUNK_SIZE / inc->bytes_per_sample;
327
328         while (count) {
329                 space_left = samples_per_chunk - inc->samples_in_buffer;
330
331                 if (space_left > count)
332                         space_left = count;
333
334                 p = inc->buffer + inc->samples_in_buffer * inc->bytes_per_sample;
335                 for (i = 0; i < space_left; i++) {
336                         memcpy(p, inc->current_levels, inc->bytes_per_sample);
337                         p += inc->bytes_per_sample;
338                         inc->samples_in_buffer++;
339                         count--;
340                 }
341
342                 if (inc->samples_in_buffer == samples_per_chunk)
343                         send_buffer(in);
344         }
345 }
346
347 /* Set the channel level depending on the identifier and parsed value. */
348 static void process_bit(struct context *inc, char *identifier, unsigned int bit)
349 {
350         GSList *l;
351         struct vcd_channel *vcd_ch;
352         unsigned int j;
353
354         for (j = 0, l = inc->channels; j < inc->channelcount && l; j++, l = l->next) {
355                 vcd_ch = l->data;
356                 if (g_strcmp0(identifier, vcd_ch->identifier) == 0) {
357                         /* Found our channel. */
358                         size_t byte_idx = (j / 8);
359                         size_t bit_idx = j - 8 * byte_idx;
360                         if (bit)
361                                 inc->current_levels[byte_idx] |= (uint8_t)1 << bit_idx;
362                         else
363                                 inc->current_levels[byte_idx] &= ~((uint8_t)1 << bit_idx);
364                         break;
365                 }
366         }
367         if (j == inc->channelcount)
368                 sr_dbg("Did not find channel for identifier '%s'.", identifier);
369 }
370
371 /* Parse a set of lines from the data section. */
372 static void parse_contents(const struct sr_input *in, char *data)
373 {
374         struct context *inc;
375         uint64_t timestamp;
376         unsigned int bit, i;
377         char **tokens;
378
379         inc = in->priv;
380
381         /* Read one space-delimited token at a time. */
382         tokens = g_strsplit_set(data, " \t\r\n", 0);
383         remove_empty_parts(tokens);
384         for (i = 0; tokens[i]; i++) {
385                 if (inc->skip_until_end) {
386                         if (!strcmp(tokens[i], "$end")) {
387                                 /* Done with unhandled/unknown section. */
388                                 inc->skip_until_end = FALSE;
389                                 break;
390                         }
391                 }
392                 if (tokens[i][0] == '#' && g_ascii_isdigit(tokens[i][1])) {
393                         /* Numeric value beginning with # is a new timestamp value */
394                         timestamp = strtoull(tokens[i] + 1, NULL, 10);
395
396                         if (inc->downsample > 1)
397                                 timestamp /= inc->downsample;
398
399                         /*
400                          * Skip < 0 => skip until first timestamp.
401                          * Skip = 0 => don't skip
402                          * Skip > 0 => skip until timestamp >= skip.
403                          */
404                         if (inc->skip < 0) {
405                                 inc->skip = timestamp;
406                                 inc->prev_timestamp = timestamp;
407                         } else if (inc->skip > 0 && timestamp < (uint64_t)inc->skip) {
408                                 inc->prev_timestamp = inc->skip;
409                         } else if (timestamp == inc->prev_timestamp) {
410                                 /* Ignore repeated timestamps (e.g. sigrok outputs these) */
411                         } else if (timestamp < inc->prev_timestamp) {
412                                 sr_err("Invalid timestamp: %" PRIu64 " (smaller than previous timestamp).", timestamp);
413                                 inc->skip_until_end = TRUE;
414                                 break;
415                         } else {
416                                 if (inc->compress != 0 && timestamp - inc->prev_timestamp > inc->compress) {
417                                         /* Compress long idle periods */
418                                         inc->prev_timestamp = timestamp - inc->compress;
419                                 }
420
421                                 sr_dbg("New timestamp: %" PRIu64, timestamp);
422
423                                 /* Generate samples from prev_timestamp up to timestamp - 1. */
424                                 add_samples(in, timestamp - inc->prev_timestamp);
425                                 inc->prev_timestamp = timestamp;
426                         }
427                 } else if (tokens[i][0] == '$' && tokens[i][1] != '\0') {
428                         /*
429                          * This is probably a $dumpvars, $comment or similar.
430                          * $dump* contain useful data.
431                          */
432                         if (g_strcmp0(tokens[i], "$dumpvars") == 0
433                                         || g_strcmp0(tokens[i], "$dumpon") == 0
434                                         || g_strcmp0(tokens[i], "$dumpoff") == 0
435                                         || g_strcmp0(tokens[i], "$end") == 0) {
436                                 /* Ignore, parse contents as normally. */
437                         } else {
438                                 /* Ignore this and future lines until $end. */
439                                 inc->skip_until_end = TRUE;
440                                 break;
441                         }
442                 } else if (strchr("rR", tokens[i][0]) != NULL) {
443                         sr_dbg("Real type vector values not supported yet!");
444                         if (!tokens[++i])
445                                 /* No tokens left, bail out */
446                                 break;
447                         else
448                                 /* Process next token */
449                                 continue;
450                 } else if (strchr("bB", tokens[i][0]) != NULL) {
451                         bit = (tokens[i][1] == '1');
452
453                         /*
454                          * Bail out if a) char after 'b' is NUL, or b) there is
455                          * a second character after 'b', or c) there is no
456                          * identifier.
457                          */
458                         if (!tokens[i][1] || tokens[i][2] || !tokens[++i]) {
459                                 sr_dbg("Unexpected vector format!");
460                                 break;
461                         }
462
463                         process_bit(inc, tokens[i], bit);
464                 } else if (strchr("01xXzZ", tokens[i][0]) != NULL) {
465                         char *identifier;
466
467                         /* A new 1-bit sample value */
468                         bit = (tokens[i][0] == '1');
469
470                         /*
471                          * The identifier is either the next character, or, if
472                          * there was whitespace after the bit, the next token.
473                          */
474                         if (tokens[i][1] == '\0') {
475                                 if (!tokens[++i]) {
476                                         sr_dbg("Identifier missing!");
477                                         break;
478                                 }
479                                 identifier = tokens[i];
480                         } else {
481                                 identifier = tokens[i] + 1;
482                         }
483                         process_bit(inc, identifier, bit);
484                 } else {
485                         sr_warn("Skipping unknown token '%s'.", tokens[i]);
486                 }
487         }
488         g_strfreev(tokens);
489 }
490
491 static int init(struct sr_input *in, GHashTable *options)
492 {
493         struct context *inc;
494
495         inc = in->priv = g_malloc0(sizeof(struct context));
496
497         inc->maxchannels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
498         inc->downsample = g_variant_get_int32(g_hash_table_lookup(options, "downsample"));
499         if (inc->downsample < 1)
500                 inc->downsample = 1;
501
502         inc->compress = g_variant_get_int32(g_hash_table_lookup(options, "compress"));
503         inc->skip = g_variant_get_int32(g_hash_table_lookup(options, "skip"));
504         inc->skip /= inc->downsample;
505
506         in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
507         in->priv = inc;
508
509         inc->buffer = g_malloc(CHUNK_SIZE);
510
511         return SR_OK;
512 }
513
514 static gboolean have_header(GString *buf)
515 {
516         unsigned int pos;
517         char *p;
518
519         if (!(p = g_strstr_len(buf->str, buf->len, "$enddefinitions")))
520                 return FALSE;
521         pos = p - buf->str + 15;
522         while (pos < buf->len - 4 && g_ascii_isspace(buf->str[pos]))
523                 pos++;
524         if (!strncmp(buf->str + pos, "$end", 4))
525                 return TRUE;
526
527         return FALSE;
528 }
529
530 static int process_buffer(struct sr_input *in)
531 {
532         struct sr_datafeed_packet packet;
533         struct sr_datafeed_meta meta;
534         struct sr_config *src;
535         struct context *inc;
536         uint64_t samplerate;
537         char *p;
538
539         inc = in->priv;
540         if (!inc->started) {
541                 std_session_send_df_header(in->sdi);
542
543                 packet.type = SR_DF_META;
544                 packet.payload = &meta;
545                 samplerate = inc->samplerate / inc->downsample;
546                 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate));
547                 meta.config = g_slist_append(NULL, src);
548                 sr_session_send(in->sdi, &packet);
549                 g_slist_free(meta.config);
550                 sr_config_free(src);
551
552                 inc->started = TRUE;
553         }
554
555         while ((p = g_strrstr_len(in->buf->str, in->buf->len, "\n"))) {
556                 *p = '\0';
557                 g_strstrip(in->buf->str);
558                 if (in->buf->str[0] != '\0')
559                         parse_contents(in, in->buf->str);
560                 g_string_erase(in->buf, 0, p - in->buf->str + 1);
561         }
562
563         return SR_OK;
564 }
565
566 static int receive(struct sr_input *in, GString *buf)
567 {
568         struct context *inc;
569         int ret;
570
571         g_string_append_len(in->buf, buf->str, buf->len);
572
573         inc = in->priv;
574         if (!inc->got_header) {
575                 if (!have_header(in->buf))
576                         return SR_OK;
577                 if (!parse_header(in, in->buf))
578                         /* There was a header in there, but it was malformed. */
579                         return SR_ERR;
580
581                 in->sdi_ready = TRUE;
582                 /* sdi is ready, notify frontend. */
583                 return SR_OK;
584         }
585
586         ret = process_buffer(in);
587
588         return ret;
589 }
590
591 static int end(struct sr_input *in)
592 {
593         struct context *inc;
594         int ret;
595
596         inc = in->priv;
597
598         if (in->sdi_ready)
599                 ret = process_buffer(in);
600         else
601                 ret = SR_OK;
602
603         /* Send any samples that haven't been sent yet. */
604         send_buffer(in);
605
606         if (inc->started)
607                 std_session_send_df_end(in->sdi);
608
609         return ret;
610 }
611
612 static void cleanup(struct sr_input *in)
613 {
614         struct context *inc;
615
616         inc = in->priv;
617         g_slist_free_full(inc->channels, free_channel);
618         g_free(inc->buffer);
619         inc->buffer = NULL;
620         g_free(inc->current_levels);
621         inc->current_levels = NULL;
622 }
623
624 static int reset(struct sr_input *in)
625 {
626         struct context *inc = in->priv;
627
628         cleanup(in);
629         inc->started = FALSE;
630         g_string_truncate(in->buf, 0);
631
632         return SR_OK;
633 }
634
635 static struct sr_option options[] = {
636         { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
637         { "skip", "Skip samples until timestamp", "Skip samples until the specified timestamp; "
638                 "< 0: Skip until first timestamp listed; 0: Don't skip", NULL, NULL },
639         { "downsample", "Downsampling factor", "Downsample, i.e. divide the samplerate by the specified factor", NULL, NULL },
640         { "compress", "Compress idle periods", "Compress idle periods longer than the specified value", NULL, NULL },
641         ALL_ZERO
642 };
643
644 static const struct sr_option *get_options(void)
645 {
646         if (!options[0].def) {
647                 options[0].def = g_variant_ref_sink(g_variant_new_int32(0));
648                 options[1].def = g_variant_ref_sink(g_variant_new_int32(-1));
649                 options[2].def = g_variant_ref_sink(g_variant_new_int32(1));
650                 options[3].def = g_variant_ref_sink(g_variant_new_int32(0));
651         }
652
653         return options;
654 }
655
656 SR_PRIV struct sr_input_module input_vcd = {
657         .id = "vcd",
658         .name = "VCD",
659         .desc = "Value Change Dump data",
660         .exts = (const char*[]){"vcd", NULL},
661         .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
662         .options = get_options,
663         .format_match = format_match,
664         .init = init,
665         .receive = receive,
666         .end = end,
667         .cleanup = cleanup,
668         .reset = reset,
669 };