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;
39 const char *vcd_header = "\
43 $scope module %s $end\n\
46 $enddefinitions $end\n\
49 const char *vcd_header_comment = "\
50 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
52 static int init(struct output *o)
58 char *samplerate_s, *frequency_s, *timestamp;
61 if (!(ctx = calloc(1, sizeof(struct context))))
62 return SIGROK_ERR_MALLOC;
65 ctx->num_enabled_probes = 0;
67 for (l = o->device->probes; l; l = l->next) {
71 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
73 if (ctx->num_enabled_probes > 94) {
74 g_warning("VCD only supports 94 probes.");
78 ctx->probelist[ctx->num_enabled_probes] = 0;
79 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
80 ctx->header = g_string_sized_new(512);
81 num_probes = g_slist_length(o->device->probes);
85 timestamp = strdup(ctime(&t));
86 timestamp[strlen(timestamp)-1] = 0;
87 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
91 g_string_append_printf(ctx->header, "$version %s %s $end\n",
92 PACKAGE, PACKAGE_VERSION);
94 if (o->device->plugin) {
95 ctx->samplerate = *((uint64_t *) o->device->plugin->get_device_info(
96 o->device->plugin_index, DI_CUR_SAMPLERATE));
97 if (!((samplerate_s = sigrok_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);
108 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
109 if (ctx->samplerate > MHZ(1))
110 ctx->period = GHZ(1);
111 else if (ctx->samplerate > KHZ(1))
112 ctx->period = MHZ(1);
114 ctx->period = KHZ(1);
115 if (!(frequency_s = sigrok_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; i < ctx->num_enabled_probes; i++) {
128 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
129 (char)('!' + i), ctx->probelist[i]);
132 g_string_append(ctx->header, "$upscope $end\n"
133 "$enddefinitions $end\n$dumpvars\n");
135 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
136 g_string_free(ctx->header, TRUE);
138 return SIGROK_ERR_MALLOC;
144 static int event(struct output *o, int event_type, char **data_out,
145 uint64_t *length_out)
151 switch (event_type) {
153 outbuf = strdup("$dumpoff\n$end\n");
155 *length_out = strlen(outbuf);
168 static int data(struct output *o, char *data_in, uint64_t length_in,
169 char **data_out, uint64_t *length_out)
173 int p, curbit, prevbit;
175 static uint64_t samplecount = 0;
177 int first_sample = 0;
180 out = g_string_sized_new(512);
183 /* The header is still here, this must be the first packet. */
184 g_string_append(out, ctx->header->str);
185 g_string_free(ctx->header, TRUE);
190 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
193 memcpy(&sample, data_in + i, ctx->unitsize);
196 /* First packet. We neg to make sure sample is stored. */
197 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",
211 (long)(((float)samplecount / ctx->samplerate)
212 * ctx->period), curbit, (char)('!' + p));
215 ctx->prevsample = sample;
218 *data_out = out->str;
219 *length_out = out->len;
220 g_string_free(out, FALSE);
225 struct output_format output_vcd = {
227 "Value Change Dump (VCD)",