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, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "config.h" /* Needed for PACKAGE and others. */
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "output/vcd"
32 int num_enabled_channels;
33 GArray *channelindices;
42 static int init(struct sr_output *o, GHashTable *options)
45 struct sr_channel *ch;
47 int num_enabled_channels, i;
51 num_enabled_channels = 0;
52 for (l = o->sdi->channels; l; l = l->next) {
54 if (ch->type != SR_CHANNEL_LOGIC)
58 num_enabled_channels++;
60 if (num_enabled_channels > 94) {
61 sr_err("VCD only supports 94 channels.");
65 ctx = g_malloc0(sizeof(struct context));
67 ctx->num_enabled_channels = num_enabled_channels;
68 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
70 /* Once more to map the enabled channels. */
71 for (i = 0, l = o->sdi->channels; l; l = l->next) {
73 if (ch->type != SR_CHANNEL_LOGIC)
77 ctx->channel_index[i++] = ch->index;
83 static GString *gen_header(const struct sr_output *o)
86 struct sr_channel *ch;
92 char *samplerate_s, *frequency_s, *timestamp;
95 header = g_string_sized_new(512);
96 num_channels = g_slist_length(o->sdi->channels);
100 timestamp = g_strdup(ctime(&t));
101 timestamp[strlen(timestamp)-1] = 0;
102 g_string_printf(header, "$date %s $end\n", timestamp);
106 g_string_append_printf(header, "$version %s %s $end\n",
107 PACKAGE, PACKAGE_VERSION);
108 g_string_append_printf(header, "$comment\n Acquisition with "
109 "%d/%d channels", ctx->num_enabled_channels, num_channels);
111 if (ctx->samplerate == 0) {
112 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
114 ctx->samplerate = g_variant_get_uint64(gvar);
115 g_variant_unref(gvar);
118 if (ctx->samplerate != 0) {
119 samplerate_s = sr_samplerate_string(ctx->samplerate);
120 g_string_append_printf(header, " at %s", samplerate_s);
121 g_free(samplerate_s);
123 g_string_append_printf(header, "\n$end\n");
126 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
127 if (ctx->samplerate > SR_MHZ(1))
128 ctx->period = SR_GHZ(1);
129 else if (ctx->samplerate > SR_KHZ(1))
130 ctx->period = SR_MHZ(1);
132 ctx->period = SR_KHZ(1);
133 frequency_s = sr_period_string(ctx->period);
134 g_string_append_printf(header, "$timescale %s $end\n", frequency_s);
138 g_string_append_printf(header, "$scope module %s $end\n", PACKAGE);
140 /* Wires / channels */
141 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
143 if (ch->type != SR_CHANNEL_LOGIC)
147 g_string_append_printf(header, "$var wire 1 %c %s $end\n",
148 (char)('!' + i), ch->name);
151 g_string_append(header, "$upscope $end\n$enddefinitions $end\n");
156 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
159 const struct sr_datafeed_meta *meta;
160 const struct sr_datafeed_logic *logic;
161 const struct sr_config *src;
165 int p, curbit, prevbit, index;
167 gboolean timestamp_written;
174 switch (packet->type) {
176 meta = packet->payload;
177 for (l = meta->config; l; l = l->next) {
179 if (src->key != SR_CONF_SAMPLERATE)
181 ctx->samplerate = g_variant_get_uint64(src->data);
185 logic = packet->payload;
187 if (!ctx->header_done) {
188 *out = gen_header(o);
189 ctx->header_done = TRUE;
191 *out = g_string_sized_new(512);
194 if (!ctx->prevsample) {
195 /* Can't allocate this until we know the stream's unitsize. */
196 ctx->prevsample = g_malloc0(logic->unitsize);
199 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
200 sample = logic->data + i;
201 timestamp_written = FALSE;
203 for (p = 0; p < ctx->num_enabled_channels; p++) {
204 index = ctx->channel_index[p];
206 curbit = ((unsigned)sample[index / 8]
208 prevbit = ((unsigned)ctx->prevsample[index / 8]
211 /* VCD only contains deltas/changes of signals. */
212 if (prevbit == curbit && ctx->samplecount > 0)
215 /* Output timestamp of subsequent signal changes. */
216 if (!timestamp_written)
217 g_string_append_printf(*out, "#%.0f",
218 (double)ctx->samplecount /
219 ctx->samplerate * ctx->period);
221 /* Output which signal changed to which value. */
222 g_string_append_c(*out, ' ');
223 g_string_append_c(*out, '0' + curbit);
224 g_string_append_c(*out, '!' + p);
226 timestamp_written = TRUE;
229 if (timestamp_written)
230 g_string_append_c(*out, '\n');
233 memcpy(ctx->prevsample, sample, logic->unitsize);
237 /* Write final timestamp as length indicator. */
238 *out = g_string_sized_new(512);
239 g_string_printf(*out, "#%.0f\n",
240 (double)ctx->samplecount / ctx->samplerate * ctx->period);
247 static int cleanup(struct sr_output *o)
255 g_free(ctx->prevsample);
256 g_free(ctx->channel_index);
262 struct sr_output_module output_vcd = {
265 .desc = "Value Change Dump",