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