2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 int num_enabled_probes;
35 const char *vcd_header = "\
37 $version\n %s\n$end\n%s\
38 $timescale\n %i %s\n$end\n\
39 $scope module %s $end\n\
42 $enddefinitions $end\n\
45 const char *vcd_header_comment = "\
46 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
49 static int init(struct output *o)
51 /* Maximum header length */
52 #define MAX_HEADER_LEN 2048
59 char *c, *samplerate_s;
60 char wbuf[1000], comment[128];
62 ctx = malloc(sizeof(struct context));
64 return SIGROK_ERR_MALLOC;
66 ctx->num_enabled_probes = 0;
67 for (l = o->device->probes; l; l = l->next) {
70 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
73 ctx->probelist[ctx->num_enabled_probes] = 0;
74 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
76 /* TODO: Allow for configuration via o->param. */
78 ctx->header = calloc(1, MAX_HEADER_LEN + 1);
79 if (ctx->header == NULL)
80 return SIGROK_ERR_MALLOC;
81 num_probes = g_slist_length(o->device->probes);
83 if (o->device->plugin) {
84 /* TODO: Handle num_probes == 0, too many probes, etc. */
85 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
86 o->device->plugin_index, DI_CUR_SAMPLERATE));
87 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
89 snprintf(comment, 127, vcd_header_comment, ctx->num_enabled_probes,
90 num_probes, samplerate_s);
96 /* Wires / channels */
98 for (i = 0; i < ctx->num_enabled_probes; i++) {
99 c = (char *)&wbuf + strlen((char *)&wbuf);
100 sprintf(c, "$var wire 1 %c channel%s $end\n",
101 (char)('!' + i), ctx->probelist[i]);
104 /* TODO: Date: File or signals? Make y/n configurable. */
105 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
106 PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
107 /* TODO: Handle snprintf errors. */
109 ctx->prevbits = calloc(sizeof(int), num_probes);
110 if (ctx->prevbits == NULL)
111 return SIGROK_ERR_MALLOC;
116 static int event(struct output *o, int event_type, char **data_out,
117 uint64_t *length_out)
124 switch (event_type) {
128 outlen = strlen("$dumpoff\n$end\n");
129 outbuf = malloc(outlen + 1);
131 return SIGROK_ERR_MALLOC;
132 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
134 *length_out = outlen;
143 static int data(struct output *o, char *data_in, uint64_t length_in,
144 char **data_out, uint64_t *length_out)
147 unsigned int offset, outsize;
148 int p, curbit, prevbit;
149 uint64_t sample, prevsample;
155 outsize = strlen(ctx->header);
156 outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
158 return SIGROK_ERR_MALLOC;
160 /* The header is still here, this must be the first packet. */
161 strncpy(outbuf, ctx->header, outsize);
168 /* TODO: Are disabled probes handled correctly? */
170 for (offset = 0; offset <= length_in - ctx->unitsize;
171 offset += ctx->unitsize) {
172 memcpy(&sample, data_in + offset, ctx->unitsize);
173 for (p = 0; p < ctx->num_enabled_probes; p++) {
174 curbit = (sample & ((uint64_t) (1 << p))) != 0;
178 memcpy(&prevsample, data_in + offset - 1,
181 (prevsample & ((uint64_t) (1 << p))) != 0;
184 if (prevbit != curbit) {
185 /* FIXME: Only once per sample? */
186 c = outbuf + strlen(outbuf);
187 sprintf(c, "#%i\n", offset * 1 /* TODO */);
189 c = outbuf + strlen(outbuf);
190 sprintf(c, "%i%c\n", curbit,
191 (char)('!' + p /* FIXME? */));
195 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
199 *length_out = strlen(outbuf);
204 struct output_format output_vcd = {
206 "Value Change Dump (VCD)",