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;
32 GArray *channelindices;
41 static int init(struct sr_output *o, GHashTable *options)
44 struct sr_channel *ch;
46 int num_enabled_channels, i;
50 num_enabled_channels = 0;
51 for (l = o->sdi->channels; l; l = l->next) {
53 if (ch->type != SR_CHANNEL_LOGIC)
57 num_enabled_channels++;
59 if (num_enabled_channels > 94) {
60 sr_err("VCD only supports 94 channels.");
64 ctx = g_malloc0(sizeof(struct context));
66 ctx->num_enabled_channels = num_enabled_channels;
67 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
69 /* Once more to map the enabled channels. */
70 for (i = 0, l = o->sdi->channels; l; l = l->next) {
72 if (ch->type != SR_CHANNEL_LOGIC)
76 ctx->channel_index[i++] = ch->index;
82 static GString *gen_header(const struct sr_output *o)
85 struct sr_channel *ch;
91 char *samplerate_s, *frequency_s, *timestamp;
94 header = g_string_sized_new(512);
95 num_channels = g_slist_length(o->sdi->channels);
99 timestamp = g_strdup(ctime(&t));
100 timestamp[strlen(timestamp)-1] = 0;
101 g_string_printf(header, "$date %s $end\n", timestamp);
105 g_string_append_printf(header, "$version %s %s $end\n",
106 PACKAGE_NAME, SR_PACKAGE_VERSION_STRING);
107 g_string_append_printf(header, "$comment\n Acquisition with "
108 "%d/%d channels", ctx->num_enabled_channels, num_channels);
110 if (ctx->samplerate == 0) {
111 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
113 ctx->samplerate = g_variant_get_uint64(gvar);
114 g_variant_unref(gvar);
117 if (ctx->samplerate != 0) {
118 samplerate_s = sr_samplerate_string(ctx->samplerate);
119 g_string_append_printf(header, " at %s", samplerate_s);
120 g_free(samplerate_s);
122 g_string_append_printf(header, "\n$end\n");
125 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
126 if (ctx->samplerate > SR_MHZ(1))
127 ctx->period = SR_GHZ(1);
128 else if (ctx->samplerate > SR_KHZ(1))
129 ctx->period = SR_MHZ(1);
131 ctx->period = SR_KHZ(1);
132 frequency_s = sr_period_string(1, ctx->period);
133 g_string_append_printf(header, "$timescale %s $end\n", frequency_s);
137 g_string_append_printf(header, "$scope module %s $end\n", PACKAGE_NAME);
139 /* Wires / channels */
140 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
142 if (ch->type != SR_CHANNEL_LOGIC)
146 g_string_append_printf(header, "$var wire 1 %c %s $end\n",
147 (char)('!' + i), ch->name);
150 g_string_append(header, "$upscope $end\n$enddefinitions $end\n");
155 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
158 const struct sr_datafeed_meta *meta;
159 const struct sr_datafeed_logic *logic;
160 const struct sr_config *src;
164 int p, curbit, prevbit, index;
166 gboolean timestamp_written;
173 switch (packet->type) {
175 meta = packet->payload;
176 for (l = meta->config; l; l = l->next) {
178 if (src->key != SR_CONF_SAMPLERATE)
180 ctx->samplerate = g_variant_get_uint64(src->data);
184 logic = packet->payload;
186 if (!ctx->header_done) {
187 *out = gen_header(o);
188 ctx->header_done = TRUE;
190 *out = g_string_sized_new(512);
193 if (!ctx->prevsample) {
194 /* Can't allocate this until we know the stream's unitsize. */
195 ctx->prevsample = g_malloc0(logic->unitsize);
198 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
199 sample = logic->data + i;
200 timestamp_written = FALSE;
202 for (p = 0; p < ctx->num_enabled_channels; p++) {
203 index = ctx->channel_index[p];
205 curbit = ((unsigned)sample[index / 8]
207 prevbit = ((unsigned)ctx->prevsample[index / 8]
210 /* VCD only contains deltas/changes of signals. */
211 if (prevbit == curbit && ctx->samplecount > 0)
214 /* Output timestamp of subsequent signal changes. */
215 if (!timestamp_written)
216 g_string_append_printf(*out, "#%.0f",
217 (double)ctx->samplecount /
218 ctx->samplerate * ctx->period);
220 /* Output which signal changed to which value. */
221 g_string_append_c(*out, ' ');
222 g_string_append_c(*out, '0' + curbit);
223 g_string_append_c(*out, '!' + p);
225 timestamp_written = TRUE;
228 if (timestamp_written)
229 g_string_append_c(*out, '\n');
232 memcpy(ctx->prevsample, sample, logic->unitsize);
236 /* Write final timestamp as length indicator. */
237 *out = g_string_sized_new(512);
238 g_string_printf(*out, "#%.0f\n",
239 (double)ctx->samplecount / ctx->samplerate * ctx->period);
246 static int cleanup(struct sr_output *o)
254 g_free(ctx->prevsample);
255 g_free(ctx->channel_index);
261 struct sr_output_module output_vcd = {
264 .desc = "Value Change Dump",
265 .exts = (const char*[]){"vcd", NULL},