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;
41 static const char *const vcd_header_comment =
42 "$comment\n Acquisition with %d/%d channels at %s\n$end\n";
44 static int init(struct sr_output *o)
47 struct sr_channel *ch;
51 char *samplerate_s, *frequency_s, *timestamp;
54 if (!(ctx = g_malloc0(sizeof(struct context)))) {
55 sr_err("%s: ctx malloc failed", __func__);
60 ctx->num_enabled_channels = 0;
61 ctx->channelindices = g_array_new(FALSE, FALSE, sizeof(int));
63 for (l = o->sdi->channels; l; l = l->next) {
65 if (ch->type != SR_CHANNEL_LOGIC)
69 ctx->channelindices = g_array_append_val(
70 ctx->channelindices, ch->index);
71 ctx->num_enabled_channels++;
73 if (ctx->num_enabled_channels > 94) {
74 sr_err("VCD only supports 94 channels.");
78 ctx->header = g_string_sized_new(512);
79 num_channels = g_slist_length(o->sdi->channels);
83 timestamp = g_strdup(ctime(&t));
84 timestamp[strlen(timestamp)-1] = 0;
85 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
89 g_string_append_printf(ctx->header, "$version %s %s $end\n",
90 PACKAGE, PACKAGE_VERSION);
92 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
94 ctx->samplerate = g_variant_get_uint64(gvar);
95 g_variant_unref(gvar);
96 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
97 g_string_free(ctx->header, TRUE);
101 g_string_append_printf(ctx->header, vcd_header_comment,
102 ctx->num_enabled_channels, num_channels, samplerate_s);
103 g_free(samplerate_s);
107 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
108 if (ctx->samplerate > SR_MHZ(1))
109 ctx->period = SR_GHZ(1);
110 else if (ctx->samplerate > SR_KHZ(1))
111 ctx->period = SR_MHZ(1);
113 ctx->period = SR_KHZ(1);
114 if (!(frequency_s = sr_period_string(ctx->period))) {
115 g_string_free(ctx->header, TRUE);
119 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
123 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
125 /* Wires / channels */
126 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
128 if (ch->type != SR_CHANNEL_LOGIC)
132 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
133 (char)('!' + i), ch->name);
136 g_string_append(ctx->header, "$upscope $end\n"
137 "$enddefinitions $end\n");
142 static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
143 const struct sr_datafeed_packet *packet, GString **out)
145 const struct sr_datafeed_logic *logic;
148 int p, curbit, prevbit, index;
150 gboolean timestamp_written;
155 if (!o || !o->internal)
160 /* The header is still here, this must be the first packet. */
163 ctx->samplecount = 0;
165 *out = g_string_sized_new(512);
168 if (packet->type != SR_DF_LOGIC) {
169 if (packet->type == SR_DF_END)
170 /* Write final timestamp as length indicator. */
171 g_string_append_printf(*out, "#%.0f\n",
172 (double)ctx->samplecount /
173 ctx->samplerate * ctx->period);
177 logic = packet->payload;
179 if (!ctx->prevsample) {
180 /* Can't allocate this until we know the stream's unitsize. */
181 if (!(ctx->prevsample = g_malloc0(logic->unitsize))) {
183 sr_err("%s: ctx->prevsample malloc failed", __func__);
184 return SR_ERR_MALLOC;
188 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
189 sample = logic->data + i;
190 timestamp_written = FALSE;
192 for (p = 0; p < ctx->num_enabled_channels; p++) {
193 index = g_array_index(ctx->channelindices, int, p);
195 curbit = ((unsigned)sample[index / 8]
197 prevbit = ((unsigned)ctx->prevsample[index / 8]
200 /* VCD only contains deltas/changes of signals. */
201 if (prevbit == curbit && ctx->samplecount > 0)
204 /* Output timestamp of subsequent signal changes. */
205 if (!timestamp_written)
206 g_string_append_printf(*out, "#%.0f",
207 (double)ctx->samplecount /
208 ctx->samplerate * ctx->period);
210 /* Output which signal changed to which value. */
211 g_string_append_c(*out, ' ');
212 g_string_append_c(*out, '0' + curbit);
213 g_string_append_c(*out, '!' + p);
215 timestamp_written = TRUE;
218 if (timestamp_written)
219 g_string_append_c(*out, '\n');
222 memcpy(ctx->prevsample, sample, logic->unitsize);
228 static int cleanup(struct sr_output *o)
232 if (!o || !o->internal)
236 g_free(ctx->prevsample);
237 g_array_free(ctx->channelindices, TRUE);
243 struct sr_output_format output_vcd = {
245 .description = "Value Change Dump (VCD)",
246 .df_type = SR_DF_LOGIC,