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;
37 const char *vcd_header = "\
41 $scope module %s $end\n\
44 $enddefinitions $end\n\
47 const char *vcd_header_comment = "\
48 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
50 static int init(struct output *o)
56 int num_probes, period, i;
57 char *samplerate_s, *frequency_s, *timestamp;
60 if (!(ctx = calloc(1, sizeof(struct context))))
61 return SIGROK_ERR_MALLOC;
64 ctx->num_enabled_probes = 0;
66 for (l = o->device->probes; l; l = l->next) {
70 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
72 if (ctx->num_enabled_probes > 94) {
73 g_warning("VCD only supports 94 probes.");
77 ctx->probelist[ctx->num_enabled_probes] = 0;
78 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
79 ctx->header = g_string_sized_new(512);
80 num_probes = g_slist_length(o->device->probes);
84 timestamp = 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 (o->device->plugin) {
94 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
95 o->device->plugin_index, DI_CUR_SAMPLERATE));
96 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
97 g_string_free(ctx->header, TRUE);
101 g_string_append_printf(ctx->header, vcd_header_comment,
102 ctx->num_enabled_probes, num_probes, samplerate_s);
107 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
108 if (samplerate > MHZ(1))
110 else if (samplerate > KHZ(1))
114 if (!(frequency_s = sigrok_period_string(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; i < ctx->num_enabled_probes; i++) {
127 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
128 (char)('!' + i), ctx->probelist[i]);
131 g_string_append(ctx->header, "$upscope $end\n"
132 "$enddefinitions $end\n$dumpvars\n");
134 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
135 g_string_free(ctx->header, TRUE);
137 return SIGROK_ERR_MALLOC;
143 static int event(struct output *o, int event_type, char **data_out,
144 uint64_t *length_out)
150 switch (event_type) {
152 outbuf = strdup("$dumpoff\n$end\n");
154 *length_out = strlen(outbuf);
167 static int data(struct output *o, char *data_in, uint64_t length_in,
168 char **data_out, uint64_t *length_out)
172 int p, curbit, prevbit;
174 static uint64_t samplecount = 0;
176 int first_sample = 0;
179 out = g_string_sized_new(512);
182 /* The header is still here, this must be the first packet. */
183 g_string_append(out, ctx->header->str);
184 g_string_free(ctx->header, TRUE);
189 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
192 memcpy(&sample, data_in + i, ctx->unitsize);
195 /* First packet. We neg to make sure sample is stored. */
196 ctx->prevsample = ~sample;
201 for (p = 0; p < ctx->num_enabled_probes; p++) {
202 curbit = (sample & ((uint64_t) (1 << p))) >> p;
203 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
205 /* VCD only contains deltas/changes of signals. */
206 if (prevbit == curbit)
209 /* Output which signal changed to which value. */
210 g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n", samplecount,
211 curbit, (char)('!' + p));
214 ctx->prevsample = sample;
217 *data_out = out->str;
218 *length_out = out->len;
219 g_string_free(out, FALSE);
224 struct output_format output_vcd = {
226 "Value Change Dump (VCD)",