2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Petteri Aimonen <jpa@sr.mail.kapsi.fi>
5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
6 * Copyright (C) 2017-2020 Gerhard Sittig <gerhard.sittig@gmx.net>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * The VCD input module has the following options. See the options[]
24 * declaration near the bottom of the input module's source file.
26 * numchannels: Maximum number of sigrok channels to create. VCD signals
27 * are detected in their order of declaration in the VCD file header,
28 * and mapped to sigrok channels.
30 * skip: Allows to skip data at the start of the input file. This can
31 * speed up operation on long captures.
32 * Value < 0: Skip until first timestamp that is listed in the file.
33 * (This is the default behaviour.)
34 * Value = 0: Do not skip, instead generate samples beginning from
36 * Value > 0: Start at the given timestamp.
38 * downsample: Divide the samplerate by the given factor. This can
39 * speed up operation on long captures.
41 * compress: Trim idle periods which are longer than this value to span
42 * only this many timescale ticks. This can speed up operation on long
43 * captures (default 0, don't compress).
45 * Based on Verilog standard IEEE Std 1364-2001 Version C
48 * - $var with 'wire' and 'reg' types of scalar variables
49 * - $timescale definition for samplerate
50 * - multiple character variable identifiers
51 * - same identifer used for multiple signals (identical values)
52 * - vector variables (bit vectors)
53 * - integer variables (analog signals with 0 digits, passed as single
54 * precision float number)
55 * - real variables (analog signals, passed on with single precision,
56 * arbitrary digits value, not user adjustable)
57 * - nested $scope, results in prefixed sigrok channel names
59 * Most important unsupported features:
60 * - $dumpvars initial value declaration (is not an issue if generators
61 * provide sample data for the #0 timestamp, otherwise session data
62 * starts from zero values, and catches up when the signal changes its
63 * state to a supported value)
65 * Implementor's note: This input module specifically does _not_ use
66 * glib routines where they would hurt performance. Lots of memory
67 * allocations increase execution time not by percents but by huge
68 * factors. This motivated this module's custom code for splitting
69 * words on text lines, and pooling previously allocated buffers.
71 * TODO (in arbitrary order)
72 * - Map VCD scopes to sigrok channel groups?
73 * - Does libsigrok support nested channel groups? Or is this feature
74 * exclusive to Pulseview?
75 * - Check VCD input to VCD output behaviour. Verify that export and
76 * re-import results in identical data (well, VCD's constraints on
77 * timescale values is known to result in differences).
78 * - Check the minimum timestamp delta in the input data set, suggest
79 * the downsample=N option to users for reduced resource consumption.
80 * Popular VCD file creation utilities love to specify insanely tiny
81 * timescale values in the pico or even femto seconds range. Which
82 * results in huge sample counts after import, and potentially even
83 * terminates the application due to resource exhaustion. This issue
84 * only will vanish when common libsigrok infrastructure no longer
85 * depends on constant rate streams of samples at discrete points
86 * in time. The current input module implementation has code in place
87 * to gather timestamp statistics, but the most appropriate condition
88 * when to notify users is yet to be found.
89 * - Cleanup the implementation.
90 * - Consistent use of the glib API (where appropriate).
91 * - More appropriate variable/function identifiers.
92 * - More robust handling of multi-word input phrases and chunked
93 * input buffers? This implementation assumes that e.g. b[01]+
94 * patterns are complete when they start, and the signal identifier
95 * is available as well. Which may be true assuming that input data
96 * comes in complete text lines.
97 * - See if other input modules have learned lessons that we could
98 * benefit from here as well? Pointless BOM (done), line oriented
99 * processing with EOL variants and with optional last EOL, module
100 * state reset and file re-read (stable channels list), buffered
101 * session feed, synchronized feed for mixed signal sources, digits
102 * or formats support for analog input, single vs double precision,
104 * - Re-consider logging. Verbosity levels should be acceptable,
105 * but volume is an issue. Drop duplicates, and drop messages from
106 * known good code paths.
112 #include <libsigrok/libsigrok.h>
113 #include "libsigrok-internal.h"
118 #define LOG_PREFIX "input/vcd"
120 #define CHUNK_SIZE (4 * 1024 * 1024)
121 #define SCOPE_SEP '.'
124 struct vcd_user_opt {
125 size_t maxchannels; /* sigrok channels (output) */
128 uint64_t skip_starttime;
129 gboolean skip_specified;
134 uint64_t prev_timestamp;
136 size_t vcdsignals; /* VCD signals (input) */
137 GSList *ignored_signals;
138 gboolean data_after_timestamp;
139 gboolean ignore_end_keyword;
140 gboolean skip_until_end;
145 uint8_t *current_logic;
146 float *current_floats;
153 GString *scope_prefix;
154 struct feed_queue_logic *feed_logic;
156 size_t total_ts_seen;
157 uint64_t last_ts_value;
158 uint64_t last_ts_delta;
164 uint32_t early_check_shift;
165 size_t early_last_emitted;
177 enum sr_channeltype type;
182 size_t range_lower, range_upper;
184 struct feed_queue_analog *feed_analog;
187 static void free_channel(void *data)
189 struct vcd_channel *vcd_ch;
195 g_free(vcd_ch->name);
196 g_free(vcd_ch->identifier);
197 g_free(vcd_ch->base_name);
198 feed_queue_analog_free(vcd_ch->feed_analog);
204 * Another timestamp delta was observed, update statistics: Update the
205 * sorted list of minimum values, and increment the occurance counter.
206 * Returns the position of the item's statistics slot, or returns a huge
207 * invalid index when the current delta is larger than previously found
210 static size_t ts_stats_update_min(struct ts_stats *stats, uint64_t delta)
212 size_t idx, copy_idx;
214 /* Advance over previously recorded values which are smaller. */
216 while (idx < stats->min_count && stats->min_items[idx].delta < delta)
218 if (idx == ARRAY_SIZE(stats->min_items))
221 /* Found the exact value that previously was registered? */
222 if (stats->min_items[idx].delta == delta) {
223 stats->min_items[idx].count++;
227 /* Allocate another slot, bubble up larger values as needed. */
228 if (stats->min_count < ARRAY_SIZE(stats->min_items))
230 for (copy_idx = stats->min_count - 1; copy_idx > idx; copy_idx--)
231 stats->min_items[copy_idx] = stats->min_items[copy_idx - 1];
233 /* Start tracking this value in the found or freed slot. */
234 memset(&stats->min_items[idx], 0, sizeof(stats->min_items[idx]));
235 stats->min_items[idx].delta = delta;
236 stats->min_items[idx].count++;
242 * Intermediate check for extreme oversampling in the input data. Rate
243 * limited emission of warnings to avoid noise, "late" emission of the
244 * first potential message to avoid false positives, yet need to emit
245 * the messages early (*way* before EOF) to raise awareness.
248 * Tune the limits, improve perception and usefulness of these checks.
249 * Need to start emitting messages soon enough to be seen by users. Yet
250 * avoid unnecessary messages for valid input's idle/quiet phases. Slow
251 * input transitions are perfectly legal before bursty phases are seen
252 * in the input data. Needs the check become an option, on by default,
253 * but suppressable by users?
255 static void ts_stats_check_early(struct ts_stats *stats)
257 static const struct {
260 } *cp, check_points[] = {
261 { 100, 1000000, }, /* Still x100 after 1mio transitions. */
262 { 1000, 100000, }, /* Still x1k after 100k transitions. */
263 { 10000, 10000, }, /* Still x10k after 10k transitions. */
264 { 1000000, 2500, }, /* Still x1m after 2.5k transitions. */
268 uint64_t seen_delta, check_delta;
271 /* Get the current minimum's value and count. */
272 if (!stats->min_count)
274 seen_delta = stats->min_items[0].delta;
275 seen_count = stats->min_items[0].count;
277 /* Emit at most one weak message per import. */
278 if (stats->early_last_emitted)
281 /* Check arbitrary marks, emit rate limited warnings. */
283 check_delta = seen_delta >> stats->early_check_shift;
284 for (cp_idx = 0; cp_idx < ARRAY_SIZE(check_points); cp_idx++) {
285 cp = &check_points[cp_idx];
286 /* No other match can happen below. Done iterating. */
287 if (stats->total_ts_seen > cp->count)
289 /* Advance to the next checkpoint description. */
290 if (stats->total_ts_seen != cp->count)
292 /* First occurance of that timestamp count. Check the value. */
293 sr_dbg("TS early chk: total %zu, min delta %" PRIu64 " / %" PRIu64 ".",
294 cp->count, seen_delta, check_delta);
295 if (check_delta < cp->delta)
297 sr_warn("Low change rate? (weak estimate, min TS delta %" PRIu64 " after %zu timestamps)",
298 seen_delta, stats->total_ts_seen);
299 sr_warn("Consider using the downsample=N option, or increasing its value.");
300 stats->early_last_emitted = stats->total_ts_seen;
305 /* Reset the internal state of the timestamp tracker. */
306 static int ts_stats_prep(struct context *inc)
308 struct ts_stats *stats;
309 uint64_t down_sample_value;
310 uint32_t down_sample_shift;
312 stats = &inc->ts_stats;
313 memset(stats, 0, sizeof(*stats));
315 down_sample_value = inc->options.downsample;
316 down_sample_shift = 0;
317 while (down_sample_value >= 2) {
319 down_sample_value /= 2;
321 stats->early_check_shift = down_sample_shift;
326 /* Inspect another timestamp that was received. */
327 static int ts_stats_check(struct ts_stats *stats, uint64_t curr_ts)
329 uint64_t last_ts, delta;
331 last_ts = stats->last_ts_value;
332 stats->last_ts_value = curr_ts;
333 stats->total_ts_seen++;
334 if (stats->total_ts_seen < 2)
337 delta = curr_ts - last_ts;
338 stats->last_ts_delta = delta;
339 (void)ts_stats_update_min(stats, delta);
341 ts_stats_check_early(stats);
346 /* Postprocess internal timestamp tracker state. */
347 static int ts_stats_post(struct context *inc, gboolean ignore_terminal)
349 struct ts_stats *stats;
351 uint64_t delta, over_sample, over_sample_scaled, suggest_factor;
352 enum sr_loglevel log_level;
353 gboolean is_suspicious, has_downsample;
355 stats = &inc->ts_stats;
358 * Lookup the smallest timestamp delta which was found during
359 * data import. Ignore the last delta if its timestamp was never
360 * followed by data, and this was the only occurance. Absence of
361 * result data is non-fatal here -- this code exclusively serves
362 * to raise users' awareness of potential pitfalls, but does not
363 * change behaviour of data processing.
365 * TODO Also filter by occurance count? To not emit warnings when
366 * captured signals only change slowly by design. Only warn when
367 * the sample rate and samples count product exceeds a threshold?
368 * See below for the necessity (and potential) to adjust the log
369 * message's severity and content.
372 if (ignore_terminal) do {
373 if (min_idx >= stats->min_count)
375 delta = stats->last_ts_delta;
376 if (stats->min_items[min_idx].delta != delta)
378 if (stats->min_items[min_idx].count != 1)
382 if (min_idx >= stats->min_count)
386 * TODO Refine the condition whether to notify the user, and
387 * which severity to use after having inspected all input data.
388 * Any detail could get involved which previously was gathered
389 * during data processing: total sample count, channel count
390 * including their data type and bits width, the oversampling
391 * factor (minimum observed "change rate"), or any combination
392 * thereof. The current check is rather simple (unconditional
393 * warning for ratios starting at 100, regardless of sample or
396 over_sample = stats->min_items[min_idx].delta;
397 over_sample_scaled = over_sample / inc->options.downsample;
398 sr_dbg("TS post stats: oversample unscaled %" PRIu64 ", scaled %" PRIu64,
399 over_sample, over_sample_scaled);
400 if (over_sample_scaled < 10) {
401 sr_dbg("TS post stats: Low oversampling ratio, good.");
406 * Avoid constructing the message from several tiny pieces by
407 * design, because this would be hard on translators. Stick with
408 * complete sentences instead, and accept the redundancy in the
411 log_level = (over_sample_scaled > 20) ? SR_LOG_WARN : SR_LOG_INFO;
412 is_suspicious = over_sample_scaled > 20;
414 sr_log(log_level, LOG_PREFIX ": "
415 "Suspiciously low overall change rate (total min TS delta %" PRIu64 ").",
418 sr_log(log_level, LOG_PREFIX ": "
419 "Low overall change rate (total min TS delta %" PRIu64 ").",
422 has_downsample = inc->options.downsample > 1;
423 suggest_factor = inc->options.downsample;
424 while (over_sample_scaled >= 10) {
425 suggest_factor *= 10;
426 over_sample_scaled /= 10;
428 if (has_downsample) {
429 sr_log(log_level, LOG_PREFIX ": "
430 "Suggest higher downsample value, like %" PRIu64 ".",
433 sr_log(log_level, LOG_PREFIX ": "
434 "Suggest to downsample, value like %" PRIu64 ".",
441 static void check_remove_bom(GString *buf)
443 static const char *bom_text = "\xef\xbb\xbf";
445 if (buf->len < strlen(bom_text))
447 if (strncmp(buf->str, bom_text, strlen(bom_text)) != 0)
449 g_string_erase(buf, 0, strlen(bom_text));
453 * Reads a single VCD section from input file and parses it to name/contents.
454 * e.g. $timescale 1ps $end => "timescale" "1ps"
456 * The section (its content and its opening/closing markers) can span
457 * multiple text lines. This routine must not modify the caller's input
458 * buffer. Executes potentially multiple times on the same input data,
459 * and executes outside of the processing of the file's data section.
461 static gboolean parse_section(GString *buf, char **name, char **contents)
463 static const char *end_text = "$end";
467 const char *grab_start, *grab_end;
468 GString *sname, *scontent;
470 /* Preset falsy return values. Gets updated below. */
471 *name = *contents = NULL;
474 /* Skip any initial white-space. */
476 while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
479 /* Section tag should start with $. */
480 if (buf->str[pos++] != '$')
483 /* Read the section tag. */
484 grab_start = &buf->str[pos];
485 while (pos < buf->len && !g_ascii_isspace(buf->str[pos]))
487 grab_end = &buf->str[pos];
488 sname = g_string_new_len(grab_start, grab_end - grab_start);
490 /* Skip whitespace before content. */
491 while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
494 /* Read the content up to the '$end' marker. */
495 scontent = g_string_sized_new(128);
496 grab_start = &buf->str[pos];
497 grab_end = g_strstr_len(grab_start, buf->len - pos, end_text);
499 /* Advance 'pos' to after '$end' and more whitespace. */
500 pos = grab_end - buf->str;
501 pos += strlen(end_text);
502 while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
505 /* Grab the (trimmed) content text. */
506 while (grab_end > grab_start && g_ascii_isspace(grab_end[-1]))
508 len = grab_end - grab_start;
509 g_string_append_len(scontent, grab_start, len);
513 /* Consume the input text which just was taken. */
514 g_string_erase(buf, 0, pos);
517 /* Return section name and content if a section was seen. */
518 *name = g_string_free(sname, !status);
519 *contents = g_string_free(scontent, !status);
524 static gboolean have_header(GString *buf)
526 static const char *enddef_txt = "$enddefinitions";
527 static const char *end_txt = "$end";
531 /* Search for "end of definitions" section keyword. */
532 p = g_strstr_len(buf->str, buf->len, enddef_txt);
535 p += strlen(enddef_txt);
538 * Search for end of section (content expected to be empty).
539 * Uses DIY logic to scan for the literals' presence including
540 * empty space between keywords. MUST NOT modify the caller's
541 * input data, potentially executes several times on the same
542 * receive buffer, and executes outside of the processing the
543 * file's data section.
545 p_stop = &buf->str[buf->len];
546 p_stop -= strlen(end_txt);
547 while (p < p_stop && g_ascii_isspace(*p))
549 if (strncmp(p, end_txt, strlen(end_txt)) != 0)
551 p += strlen(end_txt);
556 static int parse_timescale(struct context *inc, char *contents)
561 * The standard allows for values 1, 10 or 100
562 * and units s, ms, us, ns, ps and fs.
564 if (sr_parse_period(contents, &p, &q) != SR_OK) {
565 sr_err("Parsing $timescale failed.");
569 inc->samplerate = q / p;
570 sr_dbg("Samplerate: %" PRIu64, inc->samplerate);
572 /* Does not happen unless time value is non-standard */
573 sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 " Hz.",
574 q, p, inc->samplerate);
581 * Handle '$scope' and '$upscope' sections in the input file. Assume that
582 * input signals have a "base name", which may be ambiguous within the
583 * file. These names get declared within potentially nested scopes, which
584 * this implementation uses to create longer but hopefully unique and
585 * thus more usable sigrok channel names.
587 * Track the currently effective scopes in a string variable to simplify
588 * the channel name creation. Start from an empty string, then append the
589 * scope name and a separator when a new scope opens, and remove the last
590 * scope name when a scope closes. This allows to simply prefix basenames
591 * with the current scope to get a full name.
593 * It's an implementation detail to keep the trailing NUL here in the
594 * GString member, to simplify the g_strconcat() call in the channel name
598 * - Check whether scope types must get supported, this implementation
599 * does not distinguish between 'module' and 'begin' and what else
600 * may be seen. The first word simply gets ignored.
601 * - Check the allowed alphabet for scope names. This implementation
602 * assumes "programming language identifier" style (alphanumeric with
603 * underscores, plus brackets since we've seen them in example files).
605 static int parse_scope(struct context *inc, char *contents, gboolean is_up)
607 char *sep_pos, *name_pos, *type_pos;
611 * The 'upscope' case, drop one scope level (if available). Accept
612 * excess 'upscope' calls, assume that a previous 'scope' section
613 * was ignored because it referenced our software package's name.
617 * Check for a second right-most separator (and position
618 * right behind that, which is the start of the last
619 * scope component), or fallback to the start of string.
620 * g_string_erase() from that positon to the end to drop
621 * the last component.
623 name_pos = inc->scope_prefix->str;
625 sep_pos = strrchr(name_pos, SCOPE_SEP);
629 sep_pos = strrchr(name_pos, SCOPE_SEP);
632 name_pos = ++sep_pos;
634 length = name_pos - inc->scope_prefix->str;
635 g_string_truncate(inc->scope_prefix, length);
636 g_string_append_c(inc->scope_prefix, '\0');
637 sr_dbg("$upscope, prefix now: \"%s\"", inc->scope_prefix->str);
642 * The 'scope' case, add another scope level. But skip our own
643 * package name, assuming that this is an artificial node which
644 * was emitted by libsigrok's VCD output module.
646 sr_spew("$scope, got: \"%s\"", contents);
647 type_pos = sr_text_next_word(contents, &contents);
649 sr_err("Cannot parse 'scope' directive");
652 name_pos = sr_text_next_word(contents, &contents);
653 if (!name_pos || contents) {
654 sr_err("Cannot parse 'scope' directive");
658 if (strcmp(name_pos, PACKAGE_NAME) == 0) {
659 sr_info("Skipping scope with application's package name: %s",
664 /* Drop NUL, append scope name and separator, and re-add NUL. */
665 g_string_truncate(inc->scope_prefix, inc->scope_prefix->len - 1);
666 g_string_append_printf(inc->scope_prefix,
667 "%s%c%c", name_pos, SCOPE_SEP, '\0');
669 sr_dbg("$scope, prefix now: \"%s\"", inc->scope_prefix->str);
675 * Parse a $var section which describes a VCD signal ("variable").
677 * @param[in] inc Input module context.
678 * @param[in] contents Input text, content of $var section.
680 static int parse_header_var(struct context *inc, char *contents)
682 char *type, *size_txt, *id, *ref, *idx;
683 gboolean is_reg, is_wire, is_real, is_int;
685 enum sr_channeltype ch_type;
686 size_t size, next_size;
687 struct vcd_channel *vcd_ch;
690 * Format of $var or $reg header specs:
691 * $var type size identifier reference [opt-index] $end
693 type = sr_text_next_word(contents, &contents);
694 size_txt = sr_text_next_word(contents, &contents);
695 id = sr_text_next_word(contents, &contents);
696 ref = sr_text_next_word(contents, &contents);
697 idx = sr_text_next_word(contents, &contents);
700 if (!type || !size_txt || !id || !ref || contents) {
701 sr_warn("$var section should have 4 or 5 items");
705 is_reg = g_strcmp0(type, "reg") == 0;
706 is_wire = g_strcmp0(type, "wire") == 0;
707 is_real = g_strcmp0(type, "real") == 0;
708 is_int = g_strcmp0(type, "integer") == 0;
709 is_str = g_strcmp0(type, "string") == 0;
711 if (is_reg || is_wire) {
712 ch_type = SR_CHANNEL_LOGIC;
713 } else if (is_real || is_int) {
714 ch_type = SR_CHANNEL_ANALOG;
716 sr_warn("Skipping id %s, name '%s%s', unsupported type '%s'.",
717 id, ref, idx ? idx : "", type);
718 inc->ignored_signals = g_slist_append(inc->ignored_signals,
722 sr_err("Unsupported signal type: '%s'", type);
726 size = strtol(size_txt, NULL, 10);
727 if (ch_type == SR_CHANNEL_ANALOG) {
728 if (is_real && size != 32 && size != 64) {
730 * The VCD input module does not depend on the
731 * specific width of the floating point value.
732 * This is just for information. Upon value
733 * changes, a mere string gets converted to
734 * float, so we may not care at all.
736 * Strictly speaking we might warn for 64bit
737 * (double precision) declarations, because
738 * sigrok internally uses single precision
741 sr_info("Unexpected real width: '%s'", size_txt);
743 /* Simplify code paths below, by assuming size 1. */
747 sr_warn("Unsupported signal size: '%s'", size_txt);
750 if (inc->conv_bits.max_bits < size)
751 inc->conv_bits.max_bits = size;
752 next_size = inc->logic_count + inc->analog_count + size;
753 if (inc->options.maxchannels && next_size > inc->options.maxchannels) {
754 sr_warn("Skipping '%s%s', exceeds requested channel count %zu.",
755 ref, idx ? idx : "", inc->options.maxchannels);
756 inc->ignored_signals = g_slist_append(inc->ignored_signals,
761 vcd_ch = g_malloc0(sizeof(*vcd_ch));
762 vcd_ch->identifier = g_strdup(id);
763 vcd_ch->name = g_strconcat(inc->scope_prefix->str, ref, idx, NULL);
765 vcd_ch->type = ch_type;
767 case SR_CHANNEL_LOGIC:
768 vcd_ch->array_index = inc->logic_count;
769 vcd_ch->byte_idx = vcd_ch->array_index / 8;
770 vcd_ch->bit_mask = 1 << (vcd_ch->array_index % 8);
771 inc->logic_count += size;
773 case SR_CHANNEL_ANALOG:
774 vcd_ch->array_index = inc->analog_count++;
775 /* TODO: Use proper 'digits' value for this input module. */
776 vcd_ch->submit_digits = is_real ? 2 : 0;
780 sr_spew("VCD signal %zu '%s' ID '%s' (size %zu), sr type %s, idx %zu.",
781 inc->vcdsignals, vcd_ch->name,
782 vcd_ch->identifier, vcd_ch->size,
783 vcd_ch->type == SR_CHANNEL_ANALOG ? "A" : "L",
784 vcd_ch->array_index);
785 inc->channels = g_slist_append(inc->channels, vcd_ch);
791 * Construct the name of the nth sigrok channel for a VCD signal.
793 * Uses the VCD signal name for scalar types and single-bit signals.
794 * Uses "signal.idx" for multi-bit VCD signals without a range spec in
795 * their declaration. Uses "signal[idx]" when a range is known and was
798 * @param[in] vcd_ch The VCD signal's description.
799 * @param[in] idx The sigrok channel's index within the VCD signal's group.
801 * @return An allocated text buffer which callers need to release, #NULL
802 * upon failure to create a sigrok channel name.
804 static char *get_channel_name(struct vcd_channel *vcd_ch, size_t idx)
806 char *open_pos, *close_pos, *check_pos, *endptr;
807 gboolean has_brackets, has_range;
808 size_t upper, lower, tmp;
811 /* Handle simple scalar types, and single-bit logic first. */
812 if (vcd_ch->size <= 1)
813 return g_strdup(vcd_ch->name);
816 * If not done before: Search for a matching pair of brackets in
817 * the right-most position at the very end of the string. Get the
818 * two colon separated numbers between the brackets, which are
819 * the range limits for array indices into the multi-bit signal.
820 * Grab the "base name" of the VCD signal.
822 * Notice that arrays can get nested. Earlier path components can
823 * be indexed as well, that's why we need the right-most range.
824 * This implementation does not handle bit vectors of size 1 here
825 * by explicit logic. The check for a [0:0] range would even fail.
826 * But the case of size 1 is handled above, and "happens to" give
827 * the expected result (just the VCD signal name).
829 * This implementation also deals with range limits in the reverse
830 * order, as well as ranges which are not 0-based (like "[4:7]").
832 if (!vcd_ch->base_name) {
834 open_pos = strrchr(vcd_ch->name, '[');
835 close_pos = strrchr(vcd_ch->name, ']');
836 if (close_pos && close_pos[1])
838 has_brackets = open_pos && close_pos && close_pos > open_pos;
842 check_pos = &open_pos[1];
844 upper = strtoul(check_pos, &endptr, 10);
845 if (!endptr || *endptr != ':')
849 check_pos = &endptr[1];
851 lower = strtoul(check_pos, &endptr, 10);
852 if (!endptr || endptr != close_pos)
855 if (has_range && lower > upper) {
863 if (upper + 1 - lower != vcd_ch->size)
867 /* Temporarily patch the VCD channel's name. */
869 vcd_ch->base_name = g_strdup(vcd_ch->name);
871 vcd_ch->range_lower = lower;
872 vcd_ch->range_upper = upper;
875 has_range = vcd_ch->range_lower + vcd_ch->range_upper;
876 if (has_range && idx >= vcd_ch->size)
879 return g_strdup_printf("%s.%zu", vcd_ch->name, idx);
882 * Create a sigrok channel name with just the bit's index in
883 * brackets. This avoids "name[7:0].3" results, instead results
886 ch_name = g_strdup_printf("%s[%zu]",
887 vcd_ch->base_name, vcd_ch->range_lower + idx);
892 * Create (analog or logic) sigrok channels for the VCD signals. Create
893 * multiple sigrok channels for vector input since sigrok has no concept
894 * of multi-bit signals. Create a channel group for the vector's bits
895 * though to reflect that they form a unit. This is beneficial when UIs
896 * support optional "collapsed" displays of channel groups (like
897 * "parallel bus, hex output").
899 * Defer channel creation until after completion of parsing the input
900 * file header. Make sure to create all logic channels first before the
901 * analog channels get created. This avoids issues with the mapping of
902 * channel indices to bitmap positions in the sample buffer.
904 static void create_channels(const struct sr_input *in,
905 struct sr_dev_inst *sdi, enum sr_channeltype ch_type)
910 struct vcd_channel *vcd_ch;
913 struct sr_channel_group *cg;
914 struct sr_channel *ch;
919 if (ch_type > SR_CHANNEL_LOGIC)
920 ch_idx += inc->logic_count;
921 if (ch_type > SR_CHANNEL_ANALOG)
922 ch_idx += inc->analog_count;
923 for (l = inc->channels; l; l = l->next) {
925 if (vcd_ch->type != ch_type)
928 if (vcd_ch->size != 1)
929 cg = sr_channel_group_new(sdi, vcd_ch->name, NULL);
930 for (size_idx = 0; size_idx < vcd_ch->size; size_idx++) {
931 ch_name = get_channel_name(vcd_ch, size_idx);
932 sr_dbg("sigrok channel idx %zu, name %s, type %s, en %d.",
934 ch_type == SR_CHANNEL_ANALOG ? "A" : "L", TRUE);
935 ch = sr_channel_new(sdi, ch_idx, ch_type, TRUE, ch_name);
939 cg->channels = g_slist_append(cg->channels, ch);
944 static void create_feeds(const struct sr_input *in)
948 struct vcd_channel *vcd_ch;
950 struct sr_channel *ch;
954 /* Create one feed for logic data. */
955 if (inc->logic_count) {
956 inc->unit_size = (inc->logic_count + 7) / 8;
957 inc->feed_logic = feed_queue_logic_alloc(in->sdi,
958 CHUNK_SIZE / inc->unit_size, inc->unit_size);
961 /* Create one feed per analog channel. */
962 for (l = inc->channels; l; l = l->next) {
964 if (vcd_ch->type != SR_CHANNEL_ANALOG)
966 ch_idx = vcd_ch->array_index;
967 ch_idx += inc->logic_count;
968 ch = g_slist_nth_data(in->sdi->channels, ch_idx);
969 vcd_ch->feed_analog = feed_queue_analog_alloc(in->sdi,
970 CHUNK_SIZE / sizeof(float),
971 vcd_ch->submit_digits, ch);
976 * Keep track of a previously created channel list, in preparation of
977 * re-reading the input file. Gets called from reset()/cleanup() paths.
979 static void keep_header_for_reread(const struct sr_input *in)
985 g_slist_free_full(inc->prev.sr_groups, sr_channel_group_free_cb);
986 inc->prev.sr_groups = in->sdi->channel_groups;
987 in->sdi->channel_groups = NULL;
989 g_slist_free_full(inc->prev.sr_channels, sr_channel_free_cb);
990 inc->prev.sr_channels = in->sdi->channels;
991 in->sdi->channels = NULL;
995 * Check whether the input file is being re-read, and refuse operation
996 * when essential parameters of the acquisition have changed in ways
997 * that are unexpected to calling applications. Gets called after the
998 * file header got parsed (again).
1000 * Changing the channel list across re-imports of the same file is not
1001 * supported, by design and for valid reasons, see bug #1215 for details.
1002 * Users are expected to start new sessions when they change these
1003 * essential parameters in the acquisition's setup. When we accept the
1004 * re-read file, then make sure to keep using the previous channel list,
1005 * applications may still reference them.
1007 static gboolean check_header_in_reread(const struct sr_input *in)
1009 struct context *inc;
1016 if (!inc->prev.sr_channels)
1019 if (sr_channel_lists_differ(inc->prev.sr_channels, in->sdi->channels)) {
1020 sr_err("Channel list change not supported for file re-read.");
1024 g_slist_free_full(in->sdi->channel_groups, sr_channel_group_free_cb);
1025 in->sdi->channel_groups = inc->prev.sr_groups;
1026 inc->prev.sr_groups = NULL;
1028 g_slist_free_full(in->sdi->channels, sr_channel_free_cb);
1029 in->sdi->channels = inc->prev.sr_channels;
1030 inc->prev.sr_channels = NULL;
1035 /* Parse VCD file header sections (rate and variables declarations). */
1036 static int parse_header(const struct sr_input *in, GString *buf)
1038 struct context *inc;
1039 gboolean enddef_seen, header_valid;
1040 char *name, *contents;
1046 /* Parse sections until complete header was seen. */
1047 enddef_seen = FALSE;
1048 header_valid = TRUE;
1049 name = contents = NULL;
1050 inc->conv_bits.max_bits = 1;
1051 while (parse_section(buf, &name, &contents)) {
1052 sr_dbg("Section '%s', contents '%s'.", name, contents);
1054 if (g_strcmp0(name, "enddefinitions") == 0) {
1058 if (g_strcmp0(name, "timescale") == 0) {
1059 if (parse_timescale(inc, contents) != SR_OK)
1060 header_valid = FALSE;
1063 if (g_strcmp0(name, "scope") == 0) {
1064 if (parse_scope(inc, contents, FALSE) != SR_OK)
1065 header_valid = FALSE;
1068 if (g_strcmp0(name, "upscope") == 0) {
1069 if (parse_scope(inc, NULL, TRUE) != SR_OK)
1070 header_valid = FALSE;
1073 if (g_strcmp0(name, "var") == 0) {
1074 if (parse_header_var(inc, contents) != SR_OK)
1075 header_valid = FALSE;
1091 inc->got_header = enddef_seen && header_valid;
1092 if (!inc->got_header)
1095 /* Create sigrok channels here, late, logic before analog. */
1096 create_channels(in, in->sdi, SR_CHANNEL_LOGIC);
1097 create_channels(in, in->sdi, SR_CHANNEL_ANALOG);
1098 if (!check_header_in_reread(in))
1103 * Allocate space for text to number conversion, and buffers to
1104 * hold current sample values before submission to the session
1105 * feed. Allocate one buffer for all logic bits, and another for
1106 * all floating point values of all analog channels.
1108 * The buffers get updated when the VCD input stream communicates
1109 * value changes. Upon reception of VCD timestamps, the buffer can
1110 * provide the previously received values, to "fill in the gaps"
1111 * in the generation of a continuous stream of samples for the
1114 size = (inc->conv_bits.max_bits + 7) / 8;
1115 inc->conv_bits.unit_size = size;
1116 inc->conv_bits.value = g_malloc0(size);
1117 if (!inc->conv_bits.value)
1118 return SR_ERR_MALLOC;
1120 size = (inc->logic_count + 7) / 8;
1121 inc->unit_size = size;
1122 inc->current_logic = g_malloc0(size);
1123 if (inc->unit_size && !inc->current_logic)
1124 return SR_ERR_MALLOC;
1125 size = sizeof(inc->current_floats[0]) * inc->analog_count;
1126 inc->current_floats = g_malloc0(size);
1127 if (size && !inc->current_floats)
1128 return SR_ERR_MALLOC;
1129 for (size = 0; size < inc->analog_count; size++)
1130 inc->current_floats[size] = 0.;
1132 ret = ts_stats_prep(inc);
1140 * Add N copies of previously received values to the session, before
1141 * subsequent value changes will update the data buffer. Locally buffer
1142 * sample data to minimize the number of send() calls.
1144 static void add_samples(const struct sr_input *in, size_t count, gboolean flush)
1146 struct context *inc;
1148 struct vcd_channel *vcd_ch;
1149 struct feed_queue_analog *q;
1154 if (inc->logic_count) {
1155 feed_queue_logic_submit_one(inc->feed_logic,
1156 inc->current_logic, count);
1158 feed_queue_logic_flush(inc->feed_logic);
1160 for (ch_list = inc->channels; ch_list; ch_list = ch_list->next) {
1161 vcd_ch = ch_list->data;
1162 if (vcd_ch->type != SR_CHANNEL_ANALOG)
1164 q = vcd_ch->feed_analog;
1167 value = inc->current_floats[vcd_ch->array_index];
1168 feed_queue_analog_submit_one(q, value, count);
1170 feed_queue_analog_flush(q);
1174 static gint vcd_compare_id(gconstpointer a, gconstpointer b)
1176 return strcmp((const char *)a, (const char *)b);
1179 static gboolean is_ignored(struct context *inc, const char *id)
1183 ignored = g_slist_find_custom(inc->ignored_signals, id, vcd_compare_id);
1184 return ignored != NULL;
1188 * Get an analog channel's value from a bit pattern (VCD 'integer' type).
1189 * The implementation assumes a maximum integer width (64bit), the API
1190 * doesn't (beyond the return data type). The use of SR_CHANNEL_ANALOG
1191 * channels may further constraint the number of significant digits
1192 * (current asumption: float -> 23bit).
1194 static float get_int_val(uint8_t *in_bits_data, size_t in_bits_count)
1197 size_t byte_count, byte_idx;
1198 float flt_value; /* typeof(inc->current_floats[0]) */
1200 /* Convert bit pattern to integer number (limited range). */
1202 byte_count = (in_bits_count + 7) / 8;
1203 for (byte_idx = 0; byte_idx < byte_count; byte_idx++) {
1204 if (byte_idx >= sizeof(int_value))
1206 int_value |= *in_bits_data++ << (byte_idx * 8);
1208 flt_value = int_value;
1214 * Set a logic channel's level depending on the VCD signal's identifier
1215 * and parsed value. Multi-bit VCD values will affect several sigrok
1216 * channels. One VCD signal name can translate to several sigrok channels.
1218 static void process_bits(struct context *inc, char *identifier,
1219 uint8_t *in_bits_data, size_t in_bits_count)
1224 struct vcd_channel *vcd_ch;
1227 uint8_t *in_bit_ptr, in_bit_mask;
1228 uint8_t *out_bit_ptr, out_bit_mask;
1234 for (l = inc->channels; l; l = l->next) {
1236 if (g_strcmp0(identifier, vcd_ch->identifier) != 0)
1238 if (vcd_ch->type == SR_CHANNEL_ANALOG) {
1239 /* Special case for 'integer' VCD signal types. */
1240 size = vcd_ch->size; /* Flag for "VCD signal found". */
1242 int_val = get_int_val(in_bits_data, in_bits_count);
1245 inc->current_floats[vcd_ch->array_index] = int_val;
1248 if (vcd_ch->type != SR_CHANNEL_LOGIC)
1250 sr_spew("Processing %s data, id '%s', ch %zu sz %zu",
1251 (size == 1) ? "bit" : "vector",
1252 identifier, vcd_ch->array_index, vcd_ch->size);
1254 /* Found our (logic) channel. Setup in/out bit positions. */
1255 size = vcd_ch->size;
1256 in_bit_ptr = in_bits_data;
1257 in_bit_mask = 1 << 0;
1258 out_bit_ptr = &inc->current_logic[vcd_ch->byte_idx];
1259 out_bit_mask = vcd_ch->bit_mask;
1262 * Pass VCD input bit(s) to sigrok logic bits. Conversion
1263 * must be done repeatedly because one VCD signal name
1264 * can translate to several sigrok channels, and shifting
1265 * a previously computed bit field to another channel's
1266 * position in the buffer would be nearly as expensive,
1267 * and certain would increase complexity of the code.
1269 for (bit_idx = 0; bit_idx < size; bit_idx++) {
1270 /* Get the bit value from input data. */
1272 if (bit_idx < in_bits_count) {
1273 bit_val = *in_bit_ptr & in_bit_mask;
1276 in_bit_mask = 1 << 0;
1280 /* Manipulate the sample buffer data image. */
1282 *out_bit_ptr |= out_bit_mask;
1284 *out_bit_ptr &= ~out_bit_mask;
1285 /* Update output position after bitmap update. */
1287 if (!out_bit_mask) {
1288 out_bit_mask = 1 << 0;
1293 if (!size && !is_ignored(inc, identifier))
1294 sr_warn("VCD signal not found for ID '%s'.", identifier);
1298 * Set an analog channel's value from a floating point number. One
1299 * VCD signal name can translate to several sigrok channels.
1301 static void process_real(struct context *inc, char *identifier, float real_val)
1305 struct vcd_channel *vcd_ch;
1308 for (l = inc->channels; l; l = l->next) {
1310 if (vcd_ch->type != SR_CHANNEL_ANALOG)
1312 if (g_strcmp0(identifier, vcd_ch->identifier) != 0)
1315 /* Found our (analog) channel. */
1317 sr_spew("Processing real data, id '%s', ch %zu, val %.16g",
1318 identifier, vcd_ch->array_index, real_val);
1319 inc->current_floats[vcd_ch->array_index] = real_val;
1321 if (!found && !is_ignored(inc, identifier))
1322 sr_warn("VCD signal not found for ID '%s'.", identifier);
1326 * Converts a bit position's text character to a number value.
1328 * TODO Check for complete coverage of Verilog's standard logic values
1329 * (IEEE-1364). The set is said to be “01XZHUWL-”, which only a part of
1330 * is handled here. What would be the complete mapping?
1331 * - 0/L -> bit value 0
1332 * - 1/H -> bit value 1
1333 * - X "don't care" -> TODO
1334 * - Z "high impedance" -> TODO
1335 * - W "weak(?)" -> TODO
1336 * - U "undefined" -> TODO
1337 * - '-' "TODO" -> TODO
1339 * For simplicity, this input module implementation maps "known low"
1340 * values to 0, and "known high" values to 1. All other values will
1341 * end up assuming "low" (return number 0), while callers might warn.
1342 * It's up to users to provide compatible input data, or accept the
1343 * warnings. Silently accepting unknown input data is not desirable.
1345 static uint8_t vcd_char_to_value(char bit_char, int *warn)
1348 bit_char = g_ascii_tolower(bit_char);
1350 /* Convert the "undisputed" variants. */
1351 if (bit_char == '0' || bit_char == 'l')
1353 if (bit_char == '1' || bit_char == 'h')
1356 /* Convert the "uncertain" variants. */
1359 if (bit_char == 'x' || bit_char == 'z')
1361 if (bit_char == 'u')
1363 if (bit_char == '-')
1366 /* Unhandled input text. */
1371 * Check the validity of a VCD string value. It's essential to reliably
1372 * accept valid data which the community uses in the field, yet robustly
1373 * reject invalid data for users' awareness. Since IEEE 1800-2017 would
1374 * not discuss the representation of this data type, it's assumed to not
1375 * be an official feature of the VCD file format. This implementation is
1376 * an educated guess after inspection of other arbitrary implementations,
1377 * not backed by any specification or public documentation.
1379 * A quick summary of the implemented assumptions: Must be a sequence of
1380 * ASCII printables. Must not contain whitespace. Might contain escape
1381 * sequences: A backslash followed by a single character, like '\n' or
1382 * '\\'. Or a backslash and the letter x followed by two hex digits,
1383 * like '\x20'. Or a backslash followed by three octal digits, like
1384 * '\007'. As an exception also accepts a single digit '\0' but only at
1385 * the text end. The string value may be empty, but must not be NULL.
1387 * This implementation assumes an ASCII based platform for simplicity
1388 * and readability. Should be a given on sigrok supported platforms.
1390 static gboolean vcd_string_valid(const char *s)
1399 /* Reject non-printable ASCII chars including DEL. */
1404 /* Deeper inspection of escape sequences. */
1408 case 'a': /* BEL, bell aka "alarm" */
1409 case 'b': /* BS, back space */
1410 case 't': /* TAB, tabulator */
1411 case 'n': /* NL, newline */
1412 case 'v': /* VT, vertical tabulator */
1413 case 'f': /* FF, form feed */
1414 case 'r': /* CR, carriage return */
1415 case '"': /* double quotes */
1416 case '\'': /* tick, single quote */
1417 case '?': /* question mark */
1418 case '\\': /* backslash */
1420 case 'x': /* \xNN two hex digits */
1422 if (!g_ascii_isxdigit(c))
1425 if (!g_ascii_isxdigit(c))
1428 case '0': /* \NNN three octal digits */
1436 /* Special case '\0' at end of text. */
1437 if (c == '0' && !*s)
1440 * First digit was covered by the outer
1441 * switch(). Two more digits to check.
1444 if (!g_ascii_isdigit(c) || c > '7')
1447 if (!g_ascii_isdigit(c) || c > '7')
1459 /* Parse one text line of the data section. */
1460 static int parse_textline(const struct sr_input *in, char *line)
1462 struct context *inc;
1464 char *curr_word, curr_first;
1465 gboolean is_timestamp, is_section;
1466 gboolean is_real, is_multibit, is_singlebit, is_string;
1468 char *identifier, *endptr;
1474 * Consume space separated words from a caller's text line. Note
1475 * that many words are self contained, but some require another
1476 * word to follow. This implementation assumes that both words
1477 * (when involved) become available in the same invocation, that
1478 * is that both words reside on the same text line of the file.
1479 * The fact that callers always pass complete text lines should
1480 * make this assumption acceptable. No generator is known to
1481 * split two corresponding words across text lines.
1483 * This constraint applies to bit vector data, multi-bit integer
1484 * and real (float) values, text strings, as well as single-bit
1485 * values with whitespace before their identifiers (if that is
1486 * valid in VCD, we'd accept it here; if generators don't create
1487 * such input, then support for it does not harm).
1492 * Lookup one word here which is mandatory. Locations
1493 * below conditionally lookup another word as needed.
1495 curr_word = sr_text_next_word(line, &line);
1500 curr_first = g_ascii_tolower(curr_word[0]);
1503 * Optionally skip some sections that can be interleaved
1504 * with data (and may or may not be supported by this
1505 * input module). If the section is not skipped but the
1506 * $end keyword needs to get tracked, specifically handle
1507 * this case, for improved robustness (still reject files
1508 * which happen to use invalid syntax).
1510 if (inc->skip_until_end) {
1511 if (strcmp(curr_word, "$end") == 0) {
1512 /* Done with unhandled/unknown section. */
1513 sr_dbg("done skipping until $end");
1514 inc->skip_until_end = FALSE;
1516 sr_spew("skipping word: %s", curr_word);
1520 if (inc->ignore_end_keyword) {
1521 if (strcmp(curr_word, "$end") == 0) {
1522 sr_dbg("done ignoring $end keyword");
1523 inc->ignore_end_keyword = FALSE;
1529 * There may be $keyword sections inside the data part of
1530 * the input file. Do inspect some of the sections' content
1531 * but ignore their surrounding keywords. Silently skip
1532 * unsupported section types (which transparently covers
1533 * $comment sections).
1535 is_section = curr_first == '$' && curr_word[1];
1537 gboolean inspect_data;
1539 inspect_data = FALSE;
1540 inspect_data |= g_strcmp0(curr_word, "$dumpvars") == 0;
1541 inspect_data |= g_strcmp0(curr_word, "$dumpon") == 0;
1542 inspect_data |= g_strcmp0(curr_word, "$dumpoff") == 0;
1544 /* Ignore keywords, yet parse contents. */
1545 sr_dbg("%s section, will parse content", curr_word);
1546 inc->ignore_end_keyword = TRUE;
1548 /* Ignore section from here up to $end. */
1549 sr_dbg("%s section, will skip until $end", curr_word);
1550 inc->skip_until_end = TRUE;
1556 * Numbers prefixed by '#' are timestamps, which translate
1557 * to sigrok sample numbers. Apply optional downsampling,
1558 * and apply the 'skip' logic. Check the recent timestamp
1559 * for plausibility. Submit the corresponding number of
1560 * samples of previously accumulated data values to the
1563 is_timestamp = curr_first == '#' && g_ascii_isdigit(curr_word[1]);
1566 timestamp = strtoull(&curr_word[1], &endptr, 10);
1567 if (!endptr || *endptr) {
1568 sr_err("Invalid timestamp: %s.", curr_word);
1572 sr_spew("Got timestamp: %" PRIu64, timestamp);
1573 ret = ts_stats_check(&inc->ts_stats, timestamp);
1576 if (inc->options.downsample > 1) {
1577 timestamp /= inc->options.downsample;
1578 sr_spew("Downsampled timestamp: %" PRIu64, timestamp);
1582 * Skip < 0 => skip until first timestamp.
1583 * Skip = 0 => don't skip
1584 * Skip > 0 => skip until timestamp >= skip.
1586 if (inc->options.skip_specified && !inc->use_skip) {
1587 sr_dbg("Seeding skip from user spec %" PRIu64,
1588 inc->options.skip_starttime);
1589 inc->prev_timestamp = inc->options.skip_starttime;
1590 inc->use_skip = TRUE;
1592 if (!inc->use_skip) {
1593 sr_dbg("Seeding skip from first timestamp");
1594 inc->options.skip_starttime = timestamp;
1595 inc->prev_timestamp = timestamp;
1596 inc->use_skip = TRUE;
1599 if (inc->options.skip_starttime && timestamp < inc->options.skip_starttime) {
1600 sr_spew("Timestamp skipped, before user spec");
1601 inc->prev_timestamp = inc->options.skip_starttime;
1604 if (timestamp == inc->prev_timestamp) {
1606 * Ignore repeated timestamps (e.g. sigrok
1607 * outputs these). Can also happen when
1608 * downsampling makes distinct input values
1609 * end up at the same scaled down value.
1610 * Also transparently covers the initial
1613 sr_spew("Timestamp is identical to previous timestamp");
1616 if (timestamp < inc->prev_timestamp) {
1617 sr_err("Invalid timestamp: %" PRIu64 " (leap backwards).", timestamp);
1621 if (inc->options.compress) {
1622 /* Compress long idle periods */
1623 count = timestamp - inc->prev_timestamp;
1624 if (count > inc->options.compress) {
1625 sr_dbg("Long idle period, compressing");
1626 count = timestamp - inc->options.compress;
1627 inc->prev_timestamp = count;
1631 /* Generate samples from prev_timestamp up to timestamp - 1. */
1632 count = timestamp - inc->prev_timestamp;
1633 sr_spew("Got a new timestamp, feeding %zu samples", count);
1634 add_samples(in, count, FALSE);
1635 inc->prev_timestamp = timestamp;
1636 inc->data_after_timestamp = FALSE;
1639 inc->data_after_timestamp = TRUE;
1642 * Data values come in different formats, are associated
1643 * with channel identifiers, and correspond to the period
1644 * of time from the most recent timestamp to the next
1647 * Supported input data formats are:
1648 * - S<value> <sep> <id> (value not used, VCD type 'string').
1649 * - R<value> <sep> <id> (analog channel, VCD type 'real').
1650 * - B<value> <sep> <id> (analog channel, VCD type 'integer').
1651 * - B<value> <sep> <id> (logic channels, VCD bit vectors).
1652 * - <value> <id> (logic channel, VCD single-bit values).
1654 * Input values can be:
1655 * - Floating point numbers.
1656 * - Bit strings (which covers multi-bit aka integers
1657 * as well as vectors).
1661 * - Individual bits can be 0/1 which is supported by
1662 * libsigrok, or x or z which is treated like 0 here
1663 * (sigrok lacks support for ternary logic, neither is
1664 * there support for the full IEEE set of values).
1665 * - Single-bit values typically won't be separated from
1666 * the signal identifer, multi-bit values and floats
1667 * are separated (will reference the next word). This
1668 * implementation silently accepts separators for
1669 * single-bit values, too.
1671 is_real = curr_first == 'r' && curr_word[1];
1672 is_multibit = curr_first == 'b' && curr_word[1];
1673 is_singlebit = curr_first == '0' || curr_first == '1';
1674 is_singlebit |= curr_first == 'l' || curr_first == 'h';
1675 is_singlebit |= curr_first == 'x' || curr_first == 'z';
1676 is_singlebit |= curr_first == 'u' || curr_first == '-';
1677 is_string = curr_first == 's';
1682 real_text = &curr_word[1];
1683 identifier = sr_text_next_word(line, &line);
1684 if (!*real_text || !identifier || !*identifier) {
1685 sr_err("Unexpected real format.");
1689 sr_spew("Got real data %s for id '%s'.",
1690 real_text, identifier);
1691 if (sr_atof_ascii(real_text, &real_val) != SR_OK) {
1692 sr_err("Cannot convert value: %s.", real_text);
1696 process_real(inc, identifier, real_val);
1700 char *bits_text_start;
1702 char *bits_text, bit_char;
1704 uint8_t *value_ptr, value_mask;
1705 GString *bits_val_text;
1708 * Fold in single-bit code path here? To re-use
1709 * the X/Z support. Current redundancy is few so
1710 * there is little pressure to unify code paths.
1711 * Also multi-bit handling is often different
1712 * from single-bit handling, so the "unified"
1713 * path would often check for special cases. So
1714 * we may never unify code paths at all here.
1716 bits_text = &curr_word[1];
1717 identifier = sr_text_next_word(line, &line);
1719 if (!*bits_text || !identifier || !*identifier) {
1720 sr_err("Unexpected integer/vector format.");
1724 sr_spew("Got integer/vector data %s for id '%s'.",
1725 bits_text, identifier);
1728 * Accept a bit string of arbitrary length (sort
1729 * of, within the limits of the previously setup
1730 * conversion buffer). The input text omits the
1731 * leading zeroes, hence we convert from end to
1732 * the start, to get the significant bits. There
1733 * should only be errors for invalid input, or
1734 * for input that is rather strange (data holds
1735 * more bits than the signal's declaration in
1736 * the header suggested). Silently accept data
1737 * that fits in the conversion buffer, and has
1738 * more significant bits than the signal's type
1739 * (that'd be non-sence yet acceptable input).
1741 bits_text_start = bits_text;
1742 bits_text += strlen(bits_text);
1743 bit_count = bits_text - bits_text_start;
1744 if (bit_count > inc->conv_bits.max_bits) {
1745 sr_err("Value exceeds conversion buffer: %s",
1750 memset(inc->conv_bits.value, 0, inc->conv_bits.unit_size);
1751 value_ptr = &inc->conv_bits.value[0];
1752 value_mask = 1 << 0;
1753 inc->conv_bits.sig_count = 0;
1754 while (bits_text > bits_text_start) {
1755 inc->conv_bits.sig_count++;
1756 bit_char = *(--bits_text);
1757 bit_value = vcd_char_to_value(bit_char, NULL);
1758 if (bit_value == 0) {
1760 } else if (bit_value == 1) {
1761 *value_ptr |= value_mask;
1763 inc->conv_bits.sig_count = 0;
1769 value_mask = 1 << 0;
1772 if (!inc->conv_bits.sig_count) {
1773 sr_err("Unexpected vector format: %s",
1778 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
1779 bits_val_text = sr_hexdump_new(inc->conv_bits.value,
1780 value_ptr - inc->conv_bits.value + 1);
1781 sr_spew("Vector value: %s.", bits_val_text->str);
1782 sr_hexdump_free(bits_val_text);
1785 process_bits(inc, identifier,
1786 inc->conv_bits.value, inc->conv_bits.sig_count);
1790 char *bits_text, bit_char;
1793 /* Get the value text, and signal identifier. */
1794 bits_text = &curr_word[0];
1795 bit_char = *bits_text;
1797 sr_err("Bit value missing.");
1801 identifier = ++bits_text;
1803 identifier = sr_text_next_word(line, &line);
1804 if (!identifier || !*identifier) {
1805 sr_err("Identifier missing.");
1810 /* Convert value text to single-bit number. */
1811 bit_value = vcd_char_to_value(bit_char, NULL);
1812 if (bit_value != 0 && bit_value != 1) {
1813 sr_err("Unsupported bit value '%c'.", bit_char);
1817 inc->conv_bits.value[0] = bit_value;
1818 process_bits(inc, identifier, inc->conv_bits.value, 1);
1822 const char *str_value;
1824 str_value = &curr_word[1];
1825 identifier = sr_text_next_word(line, &line);
1826 if (!vcd_string_valid(str_value)) {
1827 sr_err("Invalid string data: %s", str_value);
1831 if (!identifier || !*identifier) {
1832 sr_err("String value without identifier.");
1836 sr_spew("Got string data, id '%s', value \"%s\".",
1837 identifier, str_value);
1838 if (!is_ignored(inc, identifier)) {
1839 sr_err("String value for identifier '%s'.",
1847 /* Design choice: Consider unsupported input fatal. */
1848 sr_err("Unknown token '%s'.", curr_word);
1856 static int process_buffer(struct sr_input *in, gboolean is_eof)
1858 struct context *inc;
1859 uint64_t samplerate;
1863 size_t taken, rdlen;
1867 if (!inc->got_header)
1870 /* Send feed header and samplerate (once) before sample data. */
1871 if (!inc->started) {
1872 std_session_send_df_header(in->sdi);
1874 samplerate = inc->samplerate / inc->options.downsample;
1876 gvar = g_variant_new_uint64(samplerate);
1877 sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE, gvar);
1880 inc->started = TRUE;
1884 * Workaround broken generators which output incomplete text
1885 * lines. Enforce the trailing line feed. Proper input is not
1886 * harmed by another empty line of input data.
1889 g_string_append_c(in->buf, '\n');
1891 /* Find and process complete text lines in the input data. */
1893 rdptr = in->buf->str;
1896 rdlen = &in->buf->str[in->buf->len] - rdptr;
1897 line = sr_text_next_line(rdptr, rdlen, &rdptr, &taken);
1902 ret = parse_textline(in, line);
1906 g_string_erase(in->buf, 0, taken);
1911 static int format_match(GHashTable *metadata, unsigned int *confidence)
1913 GString *buf, *tmpbuf;
1915 char *name, *contents;
1917 buf = g_hash_table_lookup(metadata,
1918 GINT_TO_POINTER(SR_INPUT_META_HEADER));
1919 tmpbuf = g_string_new_len(buf->str, buf->len);
1922 * If we can parse the first section correctly, then it is
1923 * assumed that the input is in VCD format.
1925 check_remove_bom(tmpbuf);
1926 status = parse_section(tmpbuf, &name, &contents);
1927 g_string_free(tmpbuf, TRUE);
1938 static int init(struct sr_input *in, GHashTable *options)
1940 struct context *inc;
1943 inc = g_malloc0(sizeof(*inc));
1945 data = g_hash_table_lookup(options, "numchannels");
1946 inc->options.maxchannels = g_variant_get_uint32(data);
1948 data = g_hash_table_lookup(options, "downsample");
1949 inc->options.downsample = g_variant_get_uint64(data);
1950 if (inc->options.downsample < 1)
1951 inc->options.downsample = 1;
1953 data = g_hash_table_lookup(options, "compress");
1954 inc->options.compress = g_variant_get_uint64(data);
1955 inc->options.compress /= inc->options.downsample;
1957 data = g_hash_table_lookup(options, "skip");
1959 inc->options.skip_specified = TRUE;
1960 inc->options.skip_starttime = g_variant_get_uint64(data);
1961 if (inc->options.skip_starttime == ~UINT64_C(0)) {
1962 inc->options.skip_specified = FALSE;
1963 inc->options.skip_starttime = 0;
1965 inc->options.skip_starttime /= inc->options.downsample;
1968 in->sdi = g_malloc0(sizeof(*in->sdi));
1971 inc->scope_prefix = g_string_new("\0");
1976 static int receive(struct sr_input *in, GString *buf)
1978 struct context *inc;
1983 /* Collect all input chunks, potential deferred processing. */
1984 g_string_append_len(in->buf, buf->str, buf->len);
1985 if (!inc->got_header && in->buf->len == buf->len)
1986 check_remove_bom(in->buf);
1988 /* Must complete reception of the VCD header first. */
1989 if (!inc->got_header) {
1990 if (!have_header(in->buf))
1992 ret = parse_header(in, in->buf);
1995 /* sdi is ready, notify frontend. */
1996 in->sdi_ready = TRUE;
2000 /* Process sample data. */
2001 ret = process_buffer(in, FALSE);
2006 static int end(struct sr_input *in)
2008 struct context *inc;
2014 /* Must complete processing of previously received chunks. */
2016 ret = process_buffer(in, TRUE);
2020 /* Flush most recently queued sample data when EOF is seen. */
2021 if (inc->got_header && ret == SR_OK) {
2022 count = inc->data_after_timestamp ? 1 : 0;
2023 add_samples(in, count, TRUE);
2026 /* Optionally suggest downsampling after all input data was seen. */
2027 if (inc->got_header)
2028 (void)ts_stats_post(inc, !inc->data_after_timestamp);
2030 /* Must send DF_END when DF_HEADER was sent before. */
2032 std_session_send_df_end(in->sdi);
2037 static void cleanup(struct sr_input *in)
2039 struct context *inc;
2043 keep_header_for_reread(in);
2045 g_slist_free_full(inc->channels, free_channel);
2046 inc->channels = NULL;
2047 feed_queue_logic_free(inc->feed_logic);
2048 inc->feed_logic = NULL;
2049 g_free(inc->conv_bits.value);
2050 inc->conv_bits.value = NULL;
2051 g_free(inc->current_logic);
2052 inc->current_logic = NULL;
2053 g_free(inc->current_floats);
2054 inc->current_floats = NULL;
2055 g_string_free(inc->scope_prefix, TRUE);
2056 inc->scope_prefix = NULL;
2057 g_slist_free_full(inc->ignored_signals, g_free);
2058 inc->ignored_signals = NULL;
2061 static int reset(struct sr_input *in)
2063 struct context *inc;
2064 struct vcd_user_opt save;
2065 struct vcd_prev prev;
2069 /* Relase previously allocated resources. */
2071 g_string_truncate(in->buf, 0);
2073 /* Restore part of the context, init() won't run again. */
2074 save = inc->options;
2076 memset(inc, 0, sizeof(*inc));
2077 inc->options = save;
2079 inc->scope_prefix = g_string_new("\0");
2092 static struct sr_option options[] = {
2094 "numchannels", "Max number of sigrok channels",
2095 "The maximum number of sigrok channels to create for VCD input signals.",
2098 [OPT_DOWN_SAMPLE] = {
2099 "downsample", "Downsampling factor",
2100 "Downsample the input file's samplerate, i.e. divide by the specified factor.",
2103 [OPT_SKIP_COUNT] = {
2104 "skip", "Skip this many initial samples",
2105 "Skip samples until the specified timestamp. "
2106 "By default samples start at the first timestamp in the file. "
2107 "Value 0 creates samples starting at timestamp 0. "
2108 "Values above 0 only start processing at the given timestamp.",
2112 "compress", "Compress idle periods",
2113 "Compress idle periods which are longer than the specified number of timescale ticks.",
2116 [OPT_MAX] = ALL_ZERO,
2119 static const struct sr_option *get_options(void)
2121 if (!options[0].def) {
2122 options[OPT_NUM_CHANS].def = g_variant_ref_sink(g_variant_new_uint32(0));
2123 options[OPT_DOWN_SAMPLE].def = g_variant_ref_sink(g_variant_new_uint64(1));
2124 options[OPT_SKIP_COUNT].def = g_variant_ref_sink(g_variant_new_uint64(~UINT64_C(0)));
2125 options[OPT_COMPRESS].def = g_variant_ref_sink(g_variant_new_uint64(0));
2131 SR_PRIV struct sr_input_module input_vcd = {
2134 .desc = "Value Change Dump data",
2135 .exts = (const char*[]){"vcd", NULL},
2136 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
2137 .options = get_options,
2138 .format_match = format_match,