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";
48 static int init(struct output *o)
50 /* Maximum header length */
51 #define MAX_HEADER_LEN 2048
58 char *c, *samplerate_s;
59 char wbuf[1000], comment[128];
61 if (!(ctx = calloc(1, sizeof(struct context))))
62 return SIGROK_ERR_MALLOC;
64 ctx->num_enabled_probes = 0;
65 for (l = o->device->probes; l; l = l->next) {
68 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
71 ctx->probelist[ctx->num_enabled_probes] = 0;
72 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
74 /* TODO: Allow for configuration via o->param. */
76 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
78 return SIGROK_ERR_MALLOC;
80 num_probes = g_slist_length(o->device->probes);
83 if (o->device->plugin) {
84 /* TODO: Handle num_probes == 0, too many probes, etc. */
85 /* TODO: Error handling. */
86 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
87 o->device->plugin_index, DI_CUR_SAMPLERATE));
88 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
93 /* TODO: Handle sprintf() errors. */
94 snprintf(comment, 127, vcd_header_comment,
95 ctx->num_enabled_probes, num_probes, samplerate_s);
99 /* Wires / channels */
101 for (i = 0; i < ctx->num_enabled_probes; i++) {
102 c = (char *)&wbuf + strlen((char *)&wbuf);
103 /* TODO: Needs fixing for very large number of probes. */
104 /* TODO: Handle sprintf() errors. */
105 sprintf(c, "$var wire 1 %c channel%s $end\n",
106 (char)('!' + i), ctx->probelist[i]);
109 /* TODO: Date: File or signals? Make y/n configurable. */
110 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
111 PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
112 /* TODO: Handle snprintf() errors. */
114 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
117 return SIGROK_ERR_MALLOC;
123 static int event(struct output *o, int event_type, char **data_out,
124 uint64_t *length_out)
131 switch (event_type) {
136 outlen = strlen("$dumpoff\n$end\n");
137 if (!(outbuf = malloc(outlen + 1)))
138 return SIGROK_ERR_MALLOC;
139 /* TODO: Bug? Drop the + 1? */
140 /* TODO: Handle snprintf() errors. */
141 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
143 *length_out = outlen;
152 static int data(struct output *o, char *data_in, uint64_t length_in,
153 char **data_out, uint64_t *length_out)
156 unsigned int offset, outsize;
157 int p, curbit, prevbit;
158 uint64_t sample, prevsample;
164 outsize = strlen(ctx->header);
166 /* FIXME: Use realloc(). */
167 if (!(outbuf = calloc(1, outsize + 1 + 10000)))
168 return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */
172 /* The header is still here, this must be the first packet. */
173 strncpy(outbuf, ctx->header, outsize);
178 /* TODO: Are disabled probes handled correctly? */
180 for (offset = 0; offset <= length_in - ctx->unitsize;
181 offset += ctx->unitsize) {
182 memcpy(&sample, data_in + offset, ctx->unitsize);
183 for (p = 0; p < ctx->num_enabled_probes; p++) {
184 curbit = (sample & ((uint64_t) (1 << p))) != 0;
188 memcpy(&prevsample, data_in + offset - 1,
191 (prevsample & ((uint64_t) (1 << p))) != 0;
194 if (prevbit != curbit) {
195 /* FIXME: Only once per sample? */
196 c = outbuf + strlen(outbuf);
197 sprintf(c, "#%i\n", offset * 1 /* TODO */);
199 c = outbuf + strlen(outbuf);
200 sprintf(c, "%i%c\n", curbit, (char)('!' + p));
204 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
208 *length_out = strlen(outbuf);
213 struct output_format output_vcd = {
215 "Value Change Dump (VCD)",