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