2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2010-2012 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 /* Message logging helpers with driver-specific prefix string. */
30 #define DRIVER_LOG_DOMAIN "output/vcd: "
31 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
36 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
39 int num_enabled_probes;
40 char *probelist[SR_MAX_NUM_PROBES + 1];
48 static const char *vcd_header_comment = "\
49 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
51 static int init(struct sr_output *o)
54 struct sr_probe *probe;
58 char *samplerate_s, *frequency_s, *timestamp;
61 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
62 sr_err("%s: ctx malloc failed", __func__);
67 ctx->num_enabled_probes = 0;
69 for (l = o->sdi->probes; l; l = l->next) {
73 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
75 if (ctx->num_enabled_probes > 94) {
76 sr_err("VCD only supports 94 probes.");
80 ctx->probelist[ctx->num_enabled_probes] = 0;
81 ctx->header = g_string_sized_new(512);
82 num_probes = g_slist_length(o->sdi->probes);
86 timestamp = g_strdup(ctime(&t));
87 timestamp[strlen(timestamp)-1] = 0;
88 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
92 g_string_append_printf(ctx->header, "$version %s %s $end\n",
93 PACKAGE, PACKAGE_VERSION);
95 if (o->sdi->driver && sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
96 o->sdi->driver->config_get(SR_CONF_SAMPLERATE,
97 (const void **)&samplerate, o->sdi);
98 ctx->samplerate = *samplerate;
99 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
100 g_string_free(ctx->header, TRUE);
104 g_string_append_printf(ctx->header, vcd_header_comment,
105 ctx->num_enabled_probes, num_probes, samplerate_s);
106 g_free(samplerate_s);
110 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
111 if (ctx->samplerate > SR_MHZ(1))
112 ctx->period = SR_GHZ(1);
113 else if (ctx->samplerate > SR_KHZ(1))
114 ctx->period = SR_MHZ(1);
116 ctx->period = SR_KHZ(1);
117 if (!(frequency_s = sr_period_string(ctx->period))) {
118 g_string_free(ctx->header, TRUE);
122 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
126 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
128 /* Wires / channels */
129 for (i = 0; i < ctx->num_enabled_probes; i++) {
130 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
131 (char)('!' + i), ctx->probelist[i]);
134 g_string_append(ctx->header, "$upscope $end\n"
135 "$enddefinitions $end\n$dumpvars\n");
137 if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
138 g_string_free(ctx->header, TRUE);
140 sr_err("%s: ctx->prevbits malloc failed", __func__);
141 return SR_ERR_MALLOC;
147 static GString *recv(struct sr_output *o, const struct sr_dev_inst *sdi,
148 const struct sr_datafeed_packet *packet)
150 const struct sr_datafeed_logic *logic;
154 int p, curbit, prevbit;
156 static uint64_t samplecount = 0;
157 gboolean first_sample;
161 if (!o || !o->internal)
165 if (packet->type == SR_DF_END) {
166 text = g_string_sized_new(16);
167 g_string_printf(text, "$dumpoff\n$end\n");
169 } else if (packet->type != SR_DF_LOGIC)
173 /* The header is still here, this must be the first packet. */
178 text = g_string_sized_new(512);
179 first_sample = FALSE;
182 logic = packet->payload;
183 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
186 memcpy(&sample, logic->data + i, logic->unitsize);
189 /* First packet. We neg to make sure sample is stored. */
190 ctx->prevsample = ~sample;
191 first_sample = FALSE;
194 for (p = 0; p < ctx->num_enabled_probes; p++) {
195 curbit = (sample & ((uint64_t) (1 << p))) >> p;
196 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
198 /* VCD only contains deltas/changes of signals. */
199 if (prevbit == curbit)
202 /* Output which signal changed to which value. */
203 g_string_append_printf(text, "#%" PRIu64 "\n%i%c\n",
204 (uint64_t)(((float)samplecount / ctx->samplerate)
205 * ctx->period), curbit, (char)('!' + p));
208 ctx->prevsample = sample;
214 static int cleanup(struct sr_output *o)
218 if (!o || !o->internal)
227 struct sr_output_format output_vcd = {
229 .description = "Value Change Dump (VCD)",
230 .df_type = SR_DF_LOGIC,