2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2013 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 2 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/>.
24 * - Check the mixed signal queue for completeness and correctness.
25 * - Tune the analog "immediate write" code path for throughput.
26 * - Remove excess diagnostics when the implementation is considered
27 * feature complete and reliable.
37 #include <libsigrok/libsigrok.h>
38 #include "libsigrok-internal.h"
40 #define LOG_PREFIX "output/vcd"
42 static const int with_queue_stats = 0;
43 static const int with_pool_stats = 0;
45 struct vcd_channel_desc {
48 enum sr_channeltype type;
53 uint64_t last_rcvd_snum;
56 /** Queued values for a given sample number. */
57 struct vcd_queue_item {
58 uint64_t samplenum; /**!< sample number, _not_ timestamp */
59 GString *values; /**!< text of value changes */
68 struct vcd_channel_desc *channels;
70 GSList *free_list, *used_list;
71 size_t alloced, freed, reused, pooled;
72 GList *vcd_queue_list;
73 GList *vcd_queue_last;
74 gboolean immediate_write;
79 * Construct VCD signal identifiers from a sigrok channel index. The
80 * routine returns a GString which the caller is supposed to release.
82 * There are 94 printable ASCII characters. For larger channel index
83 * numbers multiple letters get concatenated (sticking with letters).
85 * The current implementation covers these ranges:
86 * - 94 single letter identifiers
87 * - 26 ^ 2 = 676, 94 + 676 = 770 for two letter identifiers
88 * - 26 ^ 3 = 17576, 770 + 17576 = 18346 for three letter identifiers
90 * This approach can get extended as needed when support for larger
91 * channel counts is desired. Any such extension remains transparent
94 * TODO This implementation assumes that the software will run on a
95 * machine which uses the ASCII character set. Platforms that use other
96 * representations or non-contiguous character ranges for their alphabet
97 * cannot use a simple addition, instead need to execute table lookups.
100 #define VCD_IDENT_CHAR_MIN '!'
101 #define VCD_IDENT_CHAR_MAX '~'
102 #define VCD_IDENT_COUNT_1CHAR (VCD_IDENT_CHAR_MAX + 1 - VCD_IDENT_CHAR_MIN)
103 #define VCD_IDENT_ALPHA_MIN 'a'
104 #define VCD_IDENT_ALPHA_MAX 'z'
105 #define VCD_IDENT_COUNT_ALPHA (VCD_IDENT_ALPHA_MAX + 1 - VCD_IDENT_ALPHA_MIN)
106 #define VCD_IDENT_COUNT_2CHAR (VCD_IDENT_COUNT_ALPHA * VCD_IDENT_COUNT_ALPHA)
107 #define VCD_IDENT_COUNT_3CHAR (VCD_IDENT_COUNT_2CHAR * VCD_IDENT_COUNT_ALPHA)
108 #define VCD_IDENT_COUNT (VCD_IDENT_COUNT_1CHAR + VCD_IDENT_COUNT_2CHAR + VCD_IDENT_COUNT_3CHAR)
110 static GString *vcd_identifier(size_t idx)
115 symbol = g_string_sized_new(4);
117 /* First 94 channels, one printable character. */
118 if (idx < VCD_IDENT_COUNT_1CHAR) {
119 c1 = VCD_IDENT_CHAR_MIN + idx;
120 g_string_printf(symbol, "%c", c1);
123 idx -= VCD_IDENT_COUNT_1CHAR;
125 /* Next 676 channels, two lower case characters. */
126 if (idx < VCD_IDENT_COUNT_2CHAR) {
127 c2 = VCD_IDENT_ALPHA_MIN + (idx % VCD_IDENT_COUNT_ALPHA);
128 idx /= VCD_IDENT_COUNT_ALPHA;
129 c1 = VCD_IDENT_ALPHA_MIN + (idx % VCD_IDENT_COUNT_ALPHA);
130 idx /= VCD_IDENT_COUNT_ALPHA;
132 sr_dbg("VCD identifier creation BUG (two char).");
133 g_string_printf(symbol, "%c%c", c1, c2);
136 idx -= VCD_IDENT_COUNT_2CHAR;
138 /* Next 17576 channels, three lower case characters. */
139 if (idx < VCD_IDENT_COUNT_3CHAR) {
140 c3 = VCD_IDENT_ALPHA_MIN + (idx % VCD_IDENT_COUNT_ALPHA);
141 idx /= VCD_IDENT_COUNT_ALPHA;
142 c2 = VCD_IDENT_ALPHA_MIN + (idx % VCD_IDENT_COUNT_ALPHA);
143 idx /= VCD_IDENT_COUNT_ALPHA;
144 c1 = VCD_IDENT_ALPHA_MIN + (idx % VCD_IDENT_COUNT_ALPHA);
145 idx /= VCD_IDENT_COUNT_ALPHA;
147 sr_dbg("VCD identifier creation BUG (three char).");
148 g_string_printf(symbol, "%c%c%c", c1, c2, c3);
151 idx -= VCD_IDENT_COUNT_3CHAR;
155 * Add combinations with more positions or larger character sets
156 * when support for more channels is required.
158 sr_dbg("VCD identifier creation ENOTSUPP (need %zu more).", idx);
159 g_string_free(symbol, TRUE);
165 * Notes on the VCD text output formatting routines:
166 * - Always start new text lines when timestamps get emitted.
167 * - Optionally terminate timestamp lines when the caller asked us to.
168 * - Prepend all values with whitespace, assume they follow a timestamp
169 * or a previously printed value. This works fine from the data point
170 * of view for the start of new lines, as well.
171 * - Put the mandatory whitespace between real (or vector) values and
172 * the following identifier. No whitespace for single bit values.
173 * - For real values callers need not specify "precision" nor the number
174 * of significant digits. The Verilog VCD spec specifically picked the
175 * "%.16g" format such that all bits of the internal presentation of
176 * the IEEE754 floating point value get communicated between the
177 * writer and the reader.
180 static void append_vcd_timestamp(GString *s, double ts, gboolean lf)
183 g_string_append_c(s, '\n');
184 g_string_append_c(s, '#');
185 g_string_append_printf(s, "%.0f", ts);
186 g_string_append_c(s, lf ? '\n' : ' ');
189 static void format_vcd_value_bit(GString *s, uint8_t bit_value, GString *id)
192 g_string_append_c(s, bit_value ? '1' : '0');
193 g_string_append(s, id->str);
196 static void format_vcd_value_real(GString *s, double real_value, GString *id)
199 g_string_append_c(s, 'r');
200 g_string_append_printf(s, "%.16g", real_value);
201 g_string_append_c(s, ' ');
202 g_string_append(s, id->str);
205 static int init(struct sr_output *o, GHashTable *options)
209 struct sr_channel *ch;
211 size_t num_enabled, num_logic, num_analog, desc_idx;
212 struct vcd_channel_desc *desc;
216 /* Determine the number of involved channels. */
220 for (l = o->sdi->channels; l; l = l->next) {
224 if (ch->type == SR_CHANNEL_LOGIC) {
226 } else if (ch->type == SR_CHANNEL_ANALOG) {
233 if (num_enabled > VCD_IDENT_COUNT) {
234 sr_err("Only up to %d VCD signals supported.", VCD_IDENT_COUNT);
238 /* Allocate space for channel descriptions. */
239 ctx = g_malloc0(sizeof(*ctx));
241 ctx->enabled_count = num_enabled;
242 ctx->logic_count = num_logic;
243 ctx->analog_count = num_analog;
244 alloc_size = sizeof(ctx->channels[0]) * ctx->enabled_count;
245 ctx->channels = g_malloc0(alloc_size);
248 * Reiterate input descriptions, to fill in output descriptions.
249 * Map channel indices, and assign symbols to VCD channels.
252 for (l = o->sdi->channels; l; l = l->next) {
256 desc = &ctx->channels[desc_idx];
257 desc->index = ch->index;
258 desc->name = vcd_identifier(desc_idx);
259 desc->type = ch->type;
261 * Make sure to _not_ match next time, to have initial
262 * values dumped when the first sample gets received.
264 if (desc->type == SR_CHANNEL_LOGIC && num_logic) {
266 desc->last.logic = ~0;
267 } else if (desc->type == SR_CHANNEL_ANALOG && num_analog) {
269 /* "Construct" NaN, avoid a compile time error. */
270 desc->last.real = 0.0;
271 desc->last.real = 0.0 / desc->last.real;
273 g_string_free(desc->name, TRUE);
274 memset(desc, 0, sizeof(*desc));
281 * Keep channel counts at hand, and a flag which allows to tune
282 * for special cases' speedup in .receive().
284 ctx->immediate_write = FALSE;
285 if (ctx->analog_count == 0)
286 ctx->immediate_write = TRUE;
287 if (ctx->logic_count == 0 && ctx->analog_count == 1)
288 ctx->immediate_write = TRUE;
291 * Keep a copy of the last logic data bitmap around. To avoid
292 * iterating over individual bits when nothing in the set has
293 * changed. The overhead of two byte array compares should
294 * outweight the tenfold bit count compared to byte counts.
296 alloc_size = (ctx->logic_count + 7) / 8;
297 ctx->last_logic = g_malloc0(alloc_size);
298 if (ctx->logic_count && !ctx->last_logic)
299 return SR_ERR_MALLOC;
305 * VCD can only handle 1/10/100 factors in the s to fs range. Find a
306 * suitable timescale which satisfies this resolution constraint, yet
307 * won't result in excessive overhead.
309 static uint64_t get_timescale_freq(uint64_t samplerate)
314 /* Go to the next full decade. */
316 while (timescale < samplerate) {
321 * Avoid loss of precision, go up a few more decades when needed.
322 * For example switch to 10GHz timescale when samplerate is 400MHz.
323 * Stop after at most factor 100 to not loop endlessly for odd
324 * samplerates, yet provide good enough accuracy.
327 while (max_up_scale--) {
328 if (timescale / samplerate * samplerate == timescale)
336 /* Emit a VCD file header. */
337 static GString *gen_header(const struct sr_output *o)
340 struct sr_channel *ch;
345 size_t num_channels, i;
346 char *samplerate_s, *frequency_s, *timestamp;
347 struct vcd_channel_desc *desc;
348 char *type_text, *size_text;
353 /* Get channel count, and samplerate if not done yet. */
354 num_channels = g_slist_length(o->sdi->channels);
355 if (!ctx->samplerate) {
356 ret = sr_config_get(o->sdi->driver, o->sdi, NULL,
357 SR_CONF_SAMPLERATE, &gvar);
359 ctx->samplerate = g_variant_get_uint64(gvar);
360 g_variant_unref(gvar);
363 ctx->period = get_timescale_freq(ctx->samplerate);
365 timestamp = g_strdup(ctime(&t));
366 timestamp[strlen(timestamp) - 1] = '\0';
369 samplerate_s = sr_samplerate_string(ctx->samplerate);
370 frequency_s = sr_period_string(1, ctx->period);
372 /* Construct the VCD output file header. */
373 header = g_string_sized_new(512);
374 g_string_printf(header, "$date %s $end\n", timestamp);
375 g_string_append_printf(header, "$version %s %s $end\n",
376 PACKAGE_NAME, sr_package_version_string_get());
377 g_string_append_printf(header, "$comment\n");
378 g_string_append_printf(header,
379 " Acquisition with %zu/%zu channels%s%s\n",
380 ctx->enabled_count, num_channels,
381 samplerate_s ? " at " : "", samplerate_s ? : "");
382 g_string_append_printf(header, "$end\n");
383 g_string_append_printf(header, "$timescale %s $end\n", frequency_s);
385 /* List generated VCD signals within a scope. */
386 g_string_append_printf(header, "$scope module %s $end\n", PACKAGE_NAME);
388 for (l = o->sdi->channels; l; l = l->next) {
392 desc = &ctx->channels[i++];
393 if (desc->type == SR_CHANNEL_LOGIC) {
396 } else if (desc->type == SR_CHANNEL_ANALOG) {
403 g_string_append_printf(header, "$var %s %s %s %s $end\n",
404 type_text, size_text, desc->name->str, ch->name);
406 g_string_append(header, "$upscope $end\n");
408 g_string_append(header, "$enddefinitions $end\n");
411 g_free(samplerate_s);
418 * Gets called when a session feed packet was received. Either creates
419 * a VCD file header (once in the output module's lifetime), or an empty
420 * GString. Callers will append the text representation of sample data
421 * to that string as needed.
423 static GString *chk_header(const struct sr_output *o)
430 if (!ctx->header_done) {
431 ctx->header_done = TRUE;
434 s = g_string_sized_new(512);
441 * Helpers to "merge sort" sample data that we have received in chunks
442 * at different times in different code paths. Queue the data until we
443 * have seen samples from all involved channels for a given samplenumber.
444 * Data for a given sample number can only get emitted when we are sure
445 * no other channel's data can arrive any more.
448 static struct vcd_queue_item *queue_alloc_item(struct context *ctx, uint64_t snum)
451 struct vcd_queue_item *item;
453 /* Get an item from the free list if available. */
454 node = ctx->free_list;
458 /* Unlink GSList node from the free list. */
459 ctx->free_list = node->next;
464 /* Setup content of the item. */
465 item->samplenum = snum;
467 item->values = g_string_sized_new(32);
469 g_string_truncate(item->values, 0);
471 /* Keep GSList node in the used list (avoid free/alloc). */
472 node->next = ctx->used_list;
473 ctx->used_list = node;
478 /* Dynamic allocation of an item. */
480 item = g_malloc0(sizeof(*item));
483 item->samplenum = snum;
484 item->values = g_string_sized_new(32);
486 /* Create a used list item, to later move to the free list. */
487 ctx->used_list = g_slist_prepend(ctx->used_list, item);
492 static void queue_free_item(struct context *ctx, struct vcd_queue_item *item)
497 * Put item back into the free list. We can assume to find a
498 * used list node, it got allocated when the item was acquired.
500 node = ctx->used_list;
504 ctx->used_list = node->next;
509 g_string_truncate(item->values, 0);
511 node->next = ctx->free_list;
512 ctx->free_list = node;
518 * Release dynamically allocated resources. Could also be used
519 * to release free list items when the use list is empty.
523 g_string_free(item->values, TRUE);
527 static void queue_drain_pool_cb(gpointer data, gpointer cb_data)
530 struct vcd_queue_item *item;
534 queue_free_item(ctx, item);
537 static void queue_drain_pool(struct context *ctx)
542 * Grab the list and "empty" the context member. Then
543 * iterate over the items, have dymamic memory released.
544 * Then free the GSList nodes (but not their data parts).
545 * Do this for the used and the free lists.
547 list = ctx->used_list;
548 ctx->used_list = NULL;
549 g_slist_foreach(list, queue_drain_pool_cb, ctx);
552 list = ctx->free_list;
553 ctx->free_list = NULL;
554 g_slist_foreach(list, queue_drain_pool_cb, ctx);
558 static int cmp_snum(gconstpointer l, gconstpointer d)
560 const struct vcd_queue_item *list_item;
561 const uint64_t *snum_ptr;
565 if (list_item->samplenum > *snum_ptr)
567 if (list_item->samplenum < *snum_ptr)
572 static int cmp_items(gconstpointer a, gconstpointer b)
574 const struct vcd_queue_item *item_a, *item_b;
578 if (item_a->samplenum > item_b->samplenum)
580 if (item_a->samplenum < item_b->samplenum)
586 * Position the current pointer of the VCD value queue to a specific
587 * sample number. Create a new queue item when needed. The logic assumes
588 * a specific use pattern: Reception of striped sample data for channels
589 * and processing in strict order of sample numbers within a channel.
590 * Lower sample numbers near the start of the queue when channels change
591 * between session feed packets, before another linear sequence follows.
593 * Naive use of convenience glib routines would severely lose performance.
594 * That's why custom code is used, which is as complex as it needs to be,
595 * yet shall execute faster than a simpler implementation. For trivial
596 * cases (logic only, one analog channel only) this queue is bypassed.
598 static int queue_samplenum(struct context *ctx, uint64_t snum)
600 struct vcd_queue_item *item, *add_item;
601 GList *walk_list, *after_snum, *before_snum, *add_list;
603 gboolean add_after_last, do_search;
605 /* Already at that position? */
606 item = ctx->vcd_queue_last ? ctx->vcd_queue_last->data : NULL;
607 if (item && item->samplenum == snum)
611 * Search after the current position in the remaining queue. The
612 * custom code uses the queue's being sorted by sample number.
613 * Narrow down a later insert position as much as possible. This
614 * avoids linear search in huge spaces later on.
617 add_after_last = FALSE;
620 walk_list = ctx->vcd_queue_last;
622 item = walk_list->data;
625 if (item->samplenum == snum) {
626 ctx->vcd_queue_last = walk_list;
630 if (item->samplenum < snum)
631 before_snum = walk_list;
632 if (item->samplenum > snum) {
633 after_snum = walk_list;
636 if (!walk_list->next)
637 add_after_last = TRUE;
638 walk_list = walk_list->next;
642 * No exact match at or beyond the current position. Run another
643 * search from the start of the queue, again restrict the space
644 * which is searched, and narrow down the insert position when
647 * If the searched sample number is larger than any we have seen
648 * before, or was in the above covered range but was not found,
649 * then we know that another queue item needs to get added, and
650 * where to put it. In that case we need not iterate the earlier
653 walk_list = ctx->vcd_queue_list;
659 while (do_search && walk_list && walk_list != ctx->vcd_queue_last) {
660 item = walk_list->data;
663 if (item->samplenum == snum) {
664 ctx->vcd_queue_last = walk_list;
667 if (item->samplenum < snum)
668 before_snum = walk_list;
669 if (item->samplenum > snum) {
670 after_snum = walk_list;
673 walk_list = walk_list->next;
677 * The complete existing queue was exhausted, no exact match was
678 * found. A new queue item must get inserted. Identify a good
679 * position where to start searching for the exact position to
680 * link the new item to the list. Assume that the combination of
681 * the glib routine's list traversal and the sample number check
682 * in the callback is expensive, reduce the amount of work done.
684 * If we have seen an item with a larger sample number than the
685 * wanted, check its immediate predecessor. If this has a smaller
686 * sample number, then we found a perfect location to insert the
687 * new item. If we know that the new item must be inserted after
688 * the last traversed queue item, start there.
690 if (!before_snum) do {
695 walk_list = after_snum->prev;
698 item = walk_list->data;
701 if (item->samplenum == snum) {
702 ctx->vcd_queue_last = walk_list;
705 if (item->samplenum < snum)
706 before_snum = walk_list;
708 add_list = add_after_last ? last : before_snum;
710 walk_list = ctx->vcd_queue_list;
712 item = walk_list->data;
715 if (item->samplenum == snum) {
716 ctx->vcd_queue_last = walk_list;
719 if (item->samplenum > snum) {
720 after_snum = walk_list;
723 add_list = walk_list;
724 walk_list = walk_list->next;
727 if (add_list && (item = add_list->data) && item->samplenum == snum) {
728 ctx->vcd_queue_last = add_list;
733 * Create a new queue item for the so far untracked sample
734 * number. Immediately search for the inserted position (is
735 * unfortunately not returned from the insert call), and
736 * cache that position for subsequent lookups.
738 if (with_queue_stats)
739 sr_dbg("%s(), queue nr %" PRIu64, __func__, snum);
740 add_item = queue_alloc_item(ctx, snum);
742 return SR_ERR_MALLOC;
744 add_list = ctx->vcd_queue_list;
745 if (add_list && add_list->prev)
746 add_list = add_list->prev;
747 walk_list = g_list_insert_sorted(add_list, add_item, cmp_items);
748 if (!walk_list->prev)
749 ctx->vcd_queue_list = walk_list;
750 walk_list = g_list_find_custom(walk_list, &snum, cmp_snum);
751 item = walk_list ? walk_list->data : NULL;
752 if (item && item->samplenum == snum) {
753 ctx->vcd_queue_last = walk_list;
759 * Prepare to append another text fragment for a value change to the
760 * queue item which corresponds to the current sample number. Return
761 * the GString which the caller then will append to.
763 static GString *queue_value_text_prep(struct context *ctx)
765 struct vcd_queue_item *item;
768 /* Cope with not-yet-positioned write pointers. */
769 item = ctx->vcd_queue_last ? ctx->vcd_queue_last->data : NULL;
773 /* Create a GString if not done already. */
776 buff = g_string_sized_new(20);
780 /* Separate items with spaces (if previous content is present). */
782 g_string_append_c(buff, ' ');
787 static double snum_to_ts(struct context *ctx, uint64_t snum)
792 ts /= ctx->samplerate;
799 * Unqueue one item of the VCD values queue which corresponds to one
800 * sample number. Append all of the text to the passed in GString.
802 static int unqueue_item(struct context *ctx,
803 struct vcd_queue_item *item, GString *s)
810 * Start the sample number's string with the timestamp. Append
811 * all value changes. Terminate lines for items which have a
812 * timestamp but no value changes, assuming this is the last
813 * entry which corresponds to SR_DF_END.
815 ts = snum_to_ts(ctx, item->samplenum);
817 is_empty = !buff || !buff->len || !buff->str || !*buff->str;
818 append_vcd_timestamp(s, ts, is_empty);
820 g_string_append(s, buff->str);
826 * Get the last sample number which logic data was received for. This
827 * implementation assumes that all logic channels get received within
828 * exactly one packet of corresponding unitsize.
830 static uint64_t get_last_snum_logic(struct context *ctx)
833 struct vcd_channel_desc *desc;
835 for (i = 0; i < ctx->enabled_count; i++) {
836 desc = &ctx->channels[i];
837 if (desc->type != SR_CHANNEL_LOGIC)
839 return desc->last_rcvd_snum;
846 * Update the last sample number which logic data was received for.
848 static void upd_last_snum_logic(struct context *ctx, uint64_t inc)
851 struct vcd_channel_desc *desc;
853 for (i = 0; i < ctx->enabled_count; i++) {
854 desc = &ctx->channels[i];
855 if (desc->type != SR_CHANNEL_LOGIC)
857 desc->last_rcvd_snum += inc;
862 * Get and update the last sample number which analog data was received
863 * for on a specific channel (which the caller already has identified).
866 static uint64_t get_last_snum_analog(struct vcd_channel_desc *desc)
869 return desc->last_rcvd_snum;
872 static void upd_last_snum_analog(struct vcd_channel_desc *desc, uint64_t inc)
877 desc->last_rcvd_snum += inc;
881 * Determine the maximum sample number which data from all involved
882 * channels was received for.
884 static uint64_t get_max_snum_export(struct context *ctx)
888 struct vcd_channel_desc *desc;
891 for (i = 0; i < ctx->enabled_count; i++) {
892 desc = &ctx->channels[i];
893 if (snum > desc->last_rcvd_snum)
894 snum = desc->last_rcvd_snum;
901 * Determine the maximum sample number of any channel we may have
902 * received data for. Then pretend we had seen that number of samples
903 * on all channels. Such that the next export can flush all previously
904 * queued data up to and including the final number, which serves as
905 * some kind of termination of the VCD output data.
907 static uint64_t get_max_snum_flush(struct context *ctx)
911 struct vcd_channel_desc *desc;
913 /* Determine the maximum sample number. */
915 for (i = 0; i < ctx->enabled_count; i++) {
916 desc = &ctx->channels[i];
917 if (snum < desc->last_rcvd_snum)
918 snum = desc->last_rcvd_snum;
921 /* Record that number as "seen" with all channels. */
922 for (i = 0; i < ctx->enabled_count; i++) {
923 desc = &ctx->channels[i];
924 desc->last_rcvd_snum = snum + 1;
931 * Pass all queued value changes when we are certain we have received
932 * data from all channels.
934 static int write_completed_changes(struct context *ctx, GString *out)
937 GList **listref, *node;
938 struct vcd_queue_item *item;
942 /* Determine the number which all data was received for so far. */
943 upto_snum = get_max_snum_export(ctx);
944 if (with_queue_stats)
945 sr_spew("%s(), check up to %" PRIu64, __func__, upto_snum);
948 * Forward and consume those items from the head of the list
949 * which we completely have accumulated and are certain about.
952 listref = &ctx->vcd_queue_list;
954 /* Find items before the targetted sample number. */
959 if (item->samplenum >= upto_snum)
963 * Unlink the item from the list. Void cached positions.
964 * Append its timestamp and values to the caller's text.
967 if (with_queue_stats)
968 sr_dbg("%s(), dump nr %" PRIu64,
969 __func__, item->samplenum);
970 if (ctx->vcd_queue_last == node)
971 ctx->vcd_queue_last = NULL;
972 *listref = g_list_remove_link(*listref, node);
973 rc = unqueue_item(ctx, item, out);
974 queue_free_item(ctx, item);
982 /* Get packets from the session feed, generate output text. */
983 static int receive(const struct sr_output *o,
984 const struct sr_datafeed_packet *packet, GString **out)
987 const struct sr_datafeed_meta *meta;
988 const struct sr_datafeed_logic *logic;
989 const struct sr_datafeed_analog *analog;
990 const struct sr_config *src;
992 struct vcd_channel_desc *desc;
994 size_t count, index, p, unit_size;
997 uint8_t *sample, *last_logic, prevbit, curbit;
999 struct sr_channel *channel;
1001 float *floats, value;
1009 switch (packet->type) {
1011 meta = packet->payload;
1012 for (l = meta->config; l; l = l->next) {
1014 if (src->key != SR_CONF_SAMPLERATE)
1016 ctx->samplerate = g_variant_get_uint64(src->data);
1020 *out = chk_header(o);
1022 logic = packet->payload;
1023 sample = logic->data;
1024 unit_size = logic->unitsize;
1025 count = logic->length / unit_size;
1026 snum_curr = get_last_snum_logic(ctx);
1027 upd_last_snum_logic(ctx, count);
1029 last_logic = ctx->last_logic;
1031 /* Check whether any logic value has changed. */
1032 changed = memcmp(last_logic, sample, unit_size) != 0;
1033 changed |= snum_curr == 0;
1035 memcpy(last_logic, sample, unit_size);
1038 * Start or continue tracking that sample number.
1039 * Avoid string copies for logic-only setups.
1042 if (ctx->immediate_write) {
1043 ts = snum_to_ts(ctx, snum_curr);
1044 append_vcd_timestamp(*out, ts, FALSE);
1046 queue_samplenum(ctx, snum_curr);
1050 /* Iterate over individual logic channels. */
1051 for (p = 0; changed && p < ctx->enabled_count; p++) {
1053 * TODO Check whether the mapping from
1054 * data image positions to channel numbers
1055 * is required. Experiments suggest that
1056 * the data image "is dense", and packs
1057 * bits of enabled channels, and leaves no
1058 * room for positions of disabled channels.
1060 desc = &ctx->channels[p];
1061 if (desc->type != SR_CHANNEL_LOGIC)
1063 index = desc->index;
1064 prevbit = desc->last.logic;
1066 /* Skip over unchanged values. */
1067 curbit = sample[index / 8];
1068 curbit = (curbit & (1 << (index % 8))) ? 1 : 0;
1069 if (snum_curr != 0 && prevbit == curbit)
1071 desc->last.logic = curbit;
1074 * Queue, or immediately emit the text for
1075 * the observed value change.
1077 if (ctx->immediate_write) {
1078 g_string_append_c(*out, ' ');
1081 s_val = queue_value_text_prep(ctx);
1085 format_vcd_value_bit(s_val, curbit, desc->name);
1088 /* Advance to next set of logic samples. */
1090 sample += unit_size;
1092 write_completed_changes(ctx, *out);
1095 *out = chk_header(o);
1098 * This implementation expects one analog packet per
1099 * individual channel, with a number of samples each.
1100 * Lookup the VCD output channel description.
1102 analog = packet->payload;
1103 count = analog->num_samples;
1104 channels = analog->meaning->channels;
1105 if (g_slist_length(channels) != 1) {
1106 sr_err("Analog packets must be single-channel.");
1109 channel = g_slist_nth_data(channels, 0);
1111 for (index = 0; index < ctx->enabled_count; index++) {
1112 desc = &ctx->channels[index];
1113 if ((int)desc->index == channel->index)
1118 if (desc->type != SR_CHANNEL_ANALOG)
1120 snum_curr = get_last_snum_analog(desc);
1121 upd_last_snum_analog(desc, count);
1124 * Convert incoming data to an array of single precision
1125 * floating point values.
1127 floats = g_try_malloc(sizeof(*floats) * analog->num_samples);
1129 return SR_ERR_MALLOC;
1130 rc = sr_analog_to_float(analog, floats);
1137 * Check for changes in the channel's values. Have the
1138 * sample number's timestamp and new value printed when
1139 * the value has changed.
1141 for (index = 0; index < count; index++) {
1142 /* Check for changes in the channel's values. */
1143 value = floats[index];
1144 changed = value != desc->last.real;
1145 changed |= snum_curr + index == 0;
1148 desc->last.real = value;
1150 /* Queue, or emit the timestamp and the new value. */
1151 if (ctx->immediate_write) {
1152 ts = snum_to_ts(ctx, snum_curr + index);
1153 append_vcd_timestamp(*out, ts, FALSE);
1156 queue_samplenum(ctx, snum_curr + index);
1157 s_val = queue_value_text_prep(ctx);
1159 format_vcd_value_real(s_val, value, desc->name);
1163 write_completed_changes(ctx, *out);
1166 *out = chk_header(o);
1167 /* Push the final timestamp as length indicator. */
1168 snum_curr = get_max_snum_flush(ctx);
1169 queue_samplenum(ctx, snum_curr);
1170 /* Flush previously queued value changes. */
1171 write_completed_changes(ctx, *out);
1178 static int cleanup(struct sr_output *o)
1180 struct context *ctx;
1181 struct vcd_channel_desc *desc;
1188 if (with_pool_stats)
1189 sr_info("STATS: alloc/reuse %zu/%zu, pool/free %zu/%zu",
1190 ctx->alloced, ctx->reused, ctx->pooled, ctx->freed);
1191 queue_drain_pool(ctx);
1192 if (with_pool_stats)
1193 sr_info("STATS: alloc/reuse %zu/%zu, pool/free %zu/%zu",
1194 ctx->alloced, ctx->reused, ctx->pooled, ctx->freed);
1196 while (ctx->enabled_count--) {
1197 desc = &ctx->channels[ctx->enabled_count];
1198 g_string_free(desc->name, TRUE);
1200 g_free(ctx->channels);
1206 struct sr_output_module output_vcd = {
1209 .desc = "Value Change Dump data",
1210 .exts = (const char*[]){"vcd", NULL},