2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 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
29 int num_enabled_probes;
36 const char *vcd_header = "\
40 $scope module %s $end\n\
43 $enddefinitions $end\n\
46 const char *vcd_header_comment = "\
47 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
49 static int init(struct output *o)
55 int num_probes, period, i;
56 char *samplerate_s, *frequency_s, *timestamp;
59 if (!(ctx = calloc(1, sizeof(struct context))))
60 return SIGROK_ERR_MALLOC;
63 ctx->num_enabled_probes = 0;
65 for (l = o->device->probes; l; l = l->next) {
69 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
71 if (ctx->num_enabled_probes > 94) {
72 g_warning("VCD only supports 94 probes.");
76 ctx->probelist[ctx->num_enabled_probes] = 0;
77 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
78 ctx->header = g_string_sized_new(512);
79 num_probes = g_slist_length(o->device->probes);
83 timestamp = 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 (o->device->plugin) {
93 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
94 o->device->plugin_index, DI_CUR_SAMPLERATE));
95 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
96 g_string_free(ctx->header, TRUE);
100 g_string_append_printf(ctx->header, vcd_header_comment,
101 ctx->num_enabled_probes, num_probes, samplerate_s);
106 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
107 if (samplerate > MHZ(1))
109 else if (samplerate > KHZ(1))
113 if (!(frequency_s = sigrok_period_string(period))) {
114 g_string_free(ctx->header, TRUE);
118 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
122 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
124 /* Wires / channels */
125 for (i = 0; i < ctx->num_enabled_probes; i++) {
126 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
127 (char)('!' + i), ctx->probelist[i]);
130 g_string_append(ctx->header, "$upscope $end\n"
131 "$enddefinitions $end\n$dumpvars\n");
133 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
134 g_string_free(ctx->header, TRUE);
136 return SIGROK_ERR_MALLOC;
142 static int event(struct output *o, int event_type, char **data_out,
143 uint64_t *length_out)
149 switch (event_type) {
151 outbuf = strdup("$dumpoff\n$end\n");
153 *length_out = strlen(outbuf);
166 static int data(struct output *o, char *data_in, uint64_t length_in,
167 char **data_out, uint64_t *length_out)
171 int p, curbit, prevbit;
172 uint64_t sample, prevsample;
173 static uint64_t samplecount = 0;
177 out = g_string_sized_new(512);
180 /* The header is still here, this must be the first packet. */
181 g_string_append(out, ctx->header->str);
182 g_string_free(ctx->header, TRUE);
186 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
188 memcpy(&sample, data_in + i, ctx->unitsize);
192 memcpy(&prevsample, data_in + i - 1, ctx->unitsize);
194 for (p = 0; p < ctx->num_enabled_probes; p++) {
195 curbit = (sample & ((uint64_t) (1 << p))) >> p;
196 prevbit = (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(out, "#%" PRIu64 "\n%i%c\n", samplecount,
204 curbit, (char)('!' + p));
208 *data_out = out->str;
209 *length_out = out->len;
210 g_string_free(out, FALSE);
215 struct output_format output_vcd = {
217 "Value Change Dump (VCD)",