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>
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 2 of the License, or
10 * (at your option) any later version.
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.
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/vcd"
31 int num_enabled_channels;
40 static int init(struct sr_output *o, GHashTable *options)
43 struct sr_channel *ch;
45 int num_enabled_channels, i;
49 num_enabled_channels = 0;
50 for (l = o->sdi->channels; l; l = l->next) {
52 if (ch->type != SR_CHANNEL_LOGIC)
56 num_enabled_channels++;
58 if (num_enabled_channels > 94) {
59 sr_err("VCD only supports 94 channels.");
63 ctx = g_malloc0(sizeof(struct context));
65 ctx->num_enabled_channels = num_enabled_channels;
66 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
68 /* Once more to map the enabled channels. */
69 for (i = 0, l = o->sdi->channels; l; l = l->next) {
71 if (ch->type != SR_CHANNEL_LOGIC)
75 ctx->channel_index[i++] = ch->index;
82 * VCD can only handle 1/10/100 factors in the s to fs range. Find a
83 * suitable timescale which satisfies this resolution constraint, yet
84 * won't result in excessive overhead.
86 static uint64_t get_timescale_freq(uint64_t samplerate)
91 /* Go to the next full decade. */
93 while (timescale < samplerate) {
98 * Avoid loss of precision, go up a few more decades when needed.
99 * For example switch to 10GHz timescale when samplerate is 400MHz.
100 * Stop after at most factor 100 to not loop endlessly for odd
101 * samplerates, yet provide good enough accuracy.
104 while (max_up_scale--) {
105 if (timescale / samplerate * samplerate == timescale)
113 static GString *gen_header(const struct sr_output *o)
116 struct sr_channel *ch;
122 char *samplerate_s, *frequency_s, *timestamp;
125 header = g_string_sized_new(512);
126 num_channels = g_slist_length(o->sdi->channels);
130 timestamp = g_strdup(ctime(&t));
131 timestamp[strlen(timestamp) - 1] = 0;
132 g_string_printf(header, "$date %s $end\n", timestamp);
136 g_string_append_printf(header, "$version %s %s $end\n",
137 PACKAGE_NAME, sr_package_version_string_get());
138 g_string_append_printf(header, "$comment\n Acquisition with "
139 "%d/%d channels", ctx->num_enabled_channels, num_channels);
141 if (ctx->samplerate == 0) {
142 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
144 ctx->samplerate = g_variant_get_uint64(gvar);
145 g_variant_unref(gvar);
148 if (ctx->samplerate != 0) {
149 samplerate_s = sr_samplerate_string(ctx->samplerate);
150 g_string_append_printf(header, " at %s", samplerate_s);
151 g_free(samplerate_s);
153 g_string_append_printf(header, "\n$end\n");
156 ctx->period = get_timescale_freq(ctx->samplerate);
157 frequency_s = sr_period_string(1, ctx->period);
158 g_string_append_printf(header, "$timescale %s $end\n", frequency_s);
162 g_string_append_printf(header, "$scope module %s $end\n", PACKAGE_NAME);
164 /* Wires / channels */
165 for (i = 0, l = o->sdi->channels; l; l = l->next) {
167 if (ch->type != SR_CHANNEL_LOGIC)
171 g_string_append_printf(header, "$var wire 1 %c %s $end\n",
172 (char)('!' + i), ch->name);
176 g_string_append(header, "$upscope $end\n$enddefinitions $end\n");
181 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
184 const struct sr_datafeed_meta *meta;
185 const struct sr_datafeed_logic *logic;
186 const struct sr_config *src;
190 int p, curbit, prevbit, index;
192 gboolean timestamp_written;
199 switch (packet->type) {
201 meta = packet->payload;
202 for (l = meta->config; l; l = l->next) {
204 if (src->key != SR_CONF_SAMPLERATE)
206 ctx->samplerate = g_variant_get_uint64(src->data);
210 logic = packet->payload;
212 if (!ctx->header_done) {
213 *out = gen_header(o);
214 ctx->header_done = TRUE;
216 *out = g_string_sized_new(512);
219 if (!ctx->prevsample) {
220 /* Can't allocate this until we know the stream's unitsize. */
221 ctx->prevsample = g_malloc0(logic->unitsize);
224 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
225 sample = logic->data + i;
226 timestamp_written = FALSE;
228 for (p = 0; p < ctx->num_enabled_channels; p++) {
230 * TODO Check whether the mapping from
231 * data image positions to channel numbers
232 * is required. Experiments suggest that
233 * the data image "is dense", and packs
234 * bits of enabled channels, and leaves no
235 * room for positions of disabled channels.
237 /* index = ctx->channel_index[p]; */
240 curbit = ((unsigned)sample[index / 8]
242 prevbit = ((unsigned)ctx->prevsample[index / 8]
245 /* VCD only contains deltas/changes of signals. */
246 if (prevbit == curbit && ctx->samplecount > 0)
249 /* Output timestamp of subsequent signal changes. */
250 if (!timestamp_written)
251 g_string_append_printf(*out, "#%.0f",
252 (double)ctx->samplecount /
253 ctx->samplerate * ctx->period);
255 /* Output which signal changed to which value. */
256 g_string_append_c(*out, ' ');
257 g_string_append_c(*out, '0' + curbit);
258 g_string_append_c(*out, '!' + p);
260 timestamp_written = TRUE;
263 if (timestamp_written)
264 g_string_append_c(*out, '\n');
267 memcpy(ctx->prevsample, sample, logic->unitsize);
271 /* Write final timestamp as length indicator. */
272 *out = g_string_sized_new(512);
273 g_string_printf(*out, "#%.0f\n",
274 (double)ctx->samplecount / ctx->samplerate * ctx->period);
281 static int cleanup(struct sr_output *o)
289 g_free(ctx->prevsample);
290 g_free(ctx->channel_index);
296 struct sr_output_module output_vcd = {
299 .desc = "Value Change Dump data",
300 .exts = (const char*[]){"vcd", NULL},