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\
38 $comment\n Acquisition with %d/%d probes at %s\n$end\n\
39 $timescale\n %i %s\n$end\n\
40 $scope module %s $end\n\
43 $enddefinitions $end\n\
46 static int init(struct output *o)
48 /* Maximum header length */
49 #define MAX_HEADER_LEN 2048
57 char sbuf[10], wbuf[1000];
59 ctx = malloc(sizeof(struct context));
61 return SIGROK_ERR_MALLOC;
63 ctx->num_enabled_probes = 0;
64 for (l = o->device->probes; l; l = l->next) {
67 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
70 ctx->probelist[ctx->num_enabled_probes] = 0;
71 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
73 /* TODO: Allow for configuration via o->param. */
75 ctx->header = calloc(1, MAX_HEADER_LEN + 1);
76 if (ctx->header == NULL)
77 return SIGROK_ERR_MALLOC;
78 num_probes = g_slist_length(o->device->probes);
79 /* TODO: Handle num_probes == 0, too many probes, etc. */
80 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
81 o->device->plugin_index, DI_CUR_SAMPLERATE));
83 /* Samplerate string */
84 if (samplerate >= GHZ(1))
85 snprintf(sbuf, 10, "%"PRIu64" GHz", samplerate / 1000000000);
86 else if (samplerate >= MHZ(1))
87 snprintf(sbuf, 10, "%"PRIu64" MHz", samplerate / 1000000);
88 else if (samplerate >= KHZ(1))
89 snprintf(sbuf, 10, "%"PRIu64" KHz", samplerate / 1000);
91 snprintf(sbuf, 10, "%"PRIu64" Hz", samplerate);
93 /* Wires / channels */
95 for (i = 0; i < num_probes; i++) {
96 c = (char *)&wbuf + strlen((char *)&wbuf);
97 sprintf(c, "$var wire 1 %c channel%i $end\n",
101 /* TODO: date: File or signals? Make y/n configurable. */
102 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
103 PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
104 (char *)&sbuf, 1, "ns", PACKAGE, (char *)&wbuf);
105 /* TODO: Handle snprintf errors. */
107 ctx->prevbits = calloc(sizeof(int), num_probes);
108 if (ctx->prevbits == NULL)
109 return SIGROK_ERR_MALLOC;
114 static int event(struct output *o, int event_type, char **data_out,
115 uint64_t *length_out)
126 outlen = strlen("$dumpoff\n$end\n");
127 outbuf = malloc(outlen + 1);
129 return SIGROK_ERR_MALLOC;
130 snprintf(outbuf, outlen, "$dumpoff\n$end\n");
132 *length_out = outlen;
141 static int data(struct output *o, char *data_in, uint64_t length_in,
142 char **data_out, uint64_t *length_out)
145 int offset, outsize, p, curbit, prevbit;
146 uint64_t sample, prevsample;
150 outsize = strlen(ctx->header);
151 outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
153 return SIGROK_ERR_MALLOC;
155 /* The header is still here, this must be the first packet. */
156 strncpy(outbuf, ctx->header, outsize);
163 /* TODO: Are disabled probes handled correctly? */
165 for (offset = 0; offset <= length_in - ctx->unitsize;
166 offset += ctx->unitsize) {
167 memcpy(&sample, data_in + offset, ctx->unitsize);
168 for (p = 0; p < ctx->num_enabled_probes; p++) {
169 curbit = (sample & ((uint64_t) (1 << p))) != 0;
173 memcpy(&prevsample, data_in + offset - 1, ctx->unitsize);
174 prevbit = (prevsample & ((uint64_t) (1 << p))) != 0;
177 if (prevbit != curbit) {
178 /* FIXME: Only once per sample? */
179 c = outbuf + strlen(outbuf);
180 sprintf(c, "#%i\n", offset * 1 /* TODO */);
182 c = outbuf + strlen(outbuf);
183 sprintf(c, "%i%c\n", curbit, (char)('!' + p /* FIXME? */));
187 /* TODO: Do a realloc() here if strlen(outbuf) is almost "full"... */
191 *length_out = strlen(outbuf);
196 struct output_format output_vcd = {
198 "Value Change Dump (VCD)",