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_probes;
38 unsigned int unitsize;
41 static const char *vcd_header_comment = "\
42 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
44 static int init(struct sr_output *o)
47 struct sr_probe *probe;
51 char *samplerate_s, *frequency_s, *timestamp;
54 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
55 sr_err("%s: ctx malloc failed", __func__);
60 ctx->num_enabled_probes = 0;
61 ctx->probeindices = g_array_new(FALSE, FALSE, sizeof(int));
63 for (l = o->sdi->probes; l; l = l->next) {
65 if (probe->type != SR_PROBE_LOGIC)
69 ctx->probeindices = g_array_append_val(
70 ctx->probeindices, probe->index);
71 ctx->num_enabled_probes++;
73 if (ctx->num_enabled_probes > 94) {
74 sr_err("VCD only supports 94 probes.");
78 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
79 ctx->header = g_string_sized_new(512);
80 num_probes = g_slist_length(o->sdi->probes);
84 timestamp = g_strdup(ctime(&t));
85 timestamp[strlen(timestamp)-1] = 0;
86 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
90 g_string_append_printf(ctx->header, "$version %s %s $end\n",
91 PACKAGE, PACKAGE_VERSION);
93 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
95 ctx->samplerate = g_variant_get_uint64(gvar);
96 g_variant_unref(gvar);
97 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
98 g_string_free(ctx->header, TRUE);
102 g_string_append_printf(ctx->header, vcd_header_comment,
103 ctx->num_enabled_probes, num_probes, samplerate_s);
104 g_free(samplerate_s);
108 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
109 if (ctx->samplerate > SR_MHZ(1))
110 ctx->period = SR_GHZ(1);
111 else if (ctx->samplerate > SR_KHZ(1))
112 ctx->period = SR_MHZ(1);
114 ctx->period = SR_KHZ(1);
115 if (!(frequency_s = sr_period_string(ctx->period))) {
116 g_string_free(ctx->header, TRUE);
120 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
124 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
126 /* Wires / channels */
127 for (i = 0, l = o->sdi->probes; l; l = l->next, i++) {
129 if (probe->type != SR_PROBE_LOGIC)
133 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
134 (char)('!' + i), probe->name);
137 g_string_append(ctx->header, "$upscope $end\n"
138 "$enddefinitions $end\n$dumpvars\n");
140 if (!(ctx->prevsample = g_try_malloc0(ctx->unitsize))) {
141 g_string_free(ctx->header, TRUE);
143 sr_err("%s: ctx->prevsample malloc failed", __func__);
144 return SR_ERR_MALLOC;
150 static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
151 const struct sr_datafeed_packet *packet, GString **out)
153 const struct sr_datafeed_logic *logic;
156 int p, curbit, prevbit, index;
158 static uint64_t samplecount = 0;
163 if (!o || !o->internal)
167 if (packet->type == SR_DF_END) {
168 *out = g_string_new("$dumpoff\n$end\n");
170 } else if (packet->type != SR_DF_LOGIC)
174 /* The header is still here, this must be the first packet. */
178 *out = g_string_sized_new(512);
181 logic = packet->payload;
182 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
185 sample = logic->data + i;
187 for (p = 0; p < ctx->num_enabled_probes; p++) {
188 index = g_array_index(ctx->probeindices, int, p);
189 curbit = (sample[p / 8] & (((uint8_t) 1) << index)) >> index;
190 prevbit = (ctx->prevsample[p / 8] & (((uint8_t) 1) << index)) >> index;
192 /* VCD only contains deltas/changes of signals. */
193 if (prevbit == curbit)
196 /* Output which signal changed to which value. */
197 g_string_append_printf(*out, "#%" PRIu64 "\n%i%c\n",
198 (uint64_t)(((float)samplecount / ctx->samplerate)
199 * ctx->period), curbit, (char)('!' + p));
202 memcpy(ctx->prevsample, sample, ctx->unitsize);
208 static int cleanup(struct sr_output *o)
212 if (!o || !o->internal)
221 struct sr_output_format output_vcd = {
223 .description = "Value Change Dump (VCD)",
224 .df_type = SR_DF_LOGIC,