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"
30 int num_enabled_probes;
32 char *probelist[SR_MAX_NUM_PROBES + 1];
40 static const char *vcd_header_comment = "\
41 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
43 static int init(struct sr_output *o)
46 struct sr_probe *probe;
50 char *samplerate_s, *frequency_s, *timestamp;
53 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
54 sr_err("vcd out: %s: ctx malloc failed", __func__);
59 ctx->num_enabled_probes = 0;
61 for (l = o->sdi->probes; l; l = l->next) {
65 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
67 if (ctx->num_enabled_probes > 94) {
68 sr_err("vcd out: VCD only supports 94 probes.");
72 ctx->probelist[ctx->num_enabled_probes] = 0;
73 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
74 ctx->header = g_string_sized_new(512);
75 num_probes = g_slist_length(o->sdi->probes);
79 timestamp = g_strdup(ctime(&t));
80 timestamp[strlen(timestamp)-1] = 0;
81 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
85 g_string_append_printf(ctx->header, "$version %s %s $end\n",
86 PACKAGE, PACKAGE_VERSION);
88 if (o->sdi->driver && sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
89 o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
90 (const void **)&samplerate, o->sdi);
91 ctx->samplerate = *samplerate;
92 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
93 g_string_free(ctx->header, TRUE);
97 g_string_append_printf(ctx->header, vcd_header_comment,
98 ctx->num_enabled_probes, num_probes, samplerate_s);
103 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
104 if (ctx->samplerate > SR_MHZ(1))
105 ctx->period = SR_GHZ(1);
106 else if (ctx->samplerate > SR_KHZ(1))
107 ctx->period = SR_MHZ(1);
109 ctx->period = SR_KHZ(1);
110 if (!(frequency_s = sr_period_string(ctx->period))) {
111 g_string_free(ctx->header, TRUE);
115 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
119 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
121 /* Wires / channels */
122 for (i = 0; i < ctx->num_enabled_probes; i++) {
123 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
124 (char)('!' + i), ctx->probelist[i]);
127 g_string_append(ctx->header, "$upscope $end\n"
128 "$enddefinitions $end\n$dumpvars\n");
130 if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
131 g_string_free(ctx->header, TRUE);
133 sr_err("vcd out: %s: ctx->prevbits malloc failed", __func__);
134 return SR_ERR_MALLOC;
140 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
141 uint64_t *length_out)
145 switch (event_type) {
147 outbuf = (uint8_t *)g_strdup("$dumpoff\n$end\n");
149 *length_out = strlen((const char *)outbuf);
162 static int data(struct sr_output *o, const uint8_t *data_in,
163 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
167 int p, curbit, prevbit;
169 static uint64_t samplecount = 0;
171 int first_sample = 0;
174 out = g_string_sized_new(512);
177 /* The header is still here, this must be the first packet. */
178 g_string_append(out, ctx->header->str);
179 g_string_free(ctx->header, TRUE);
184 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
187 memcpy(&sample, data_in + i, ctx->unitsize);
190 /* First packet. We neg to make sure sample is stored. */
191 ctx->prevsample = ~sample;
195 for (p = 0; p < ctx->num_enabled_probes; p++) {
196 curbit = (sample & ((uint64_t) (1 << p))) >> p;
197 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
199 /* VCD only contains deltas/changes of signals. */
200 if (prevbit == curbit)
203 /* Output which signal changed to which value. */
204 g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n",
205 (uint64_t)(((float)samplecount / ctx->samplerate)
206 * ctx->period), curbit, (char)('!' + p));
209 ctx->prevsample = sample;
212 *data_out = (uint8_t *)out->str;
213 *length_out = out->len;
214 g_string_free(out, FALSE);
219 struct sr_output_format output_vcd = {
221 .description = "Value Change Dump (VCD)",
222 .df_type = SR_DF_LOGIC,