]> sigrok.org Git - libsigrok.git/blob - output/output_vcd.c
make output modules a bit more crashproof
[libsigrok.git] / output / output_vcd.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <sigrok.h>
25 #include "config.h"
26
27 struct context {
28         int num_enabled_probes;
29         int unitsize;
30         char *probelist[65];
31         int *prevbits;
32         char *header;
33 };
34
35 const char *vcd_header = "\
36 $date\n  %s$end\n\
37 $version\n  %s\n$end\n%s\
38 $timescale\n  %i %s\n$end\n\
39 $scope module %s $end\n\
40 %s\
41 $upscope $end\n\
42 $enddefinitions $end\n\
43 $dumpvars\n";
44
45 const char *vcd_header_comment = "\
46 $comment\n  Acquisition with %d/%d probes at %s\n$end\n";
47
48 static int init(struct output *o)
49 {
50 /* Maximum header length */
51 #define MAX_HEADER_LEN 2048
52
53         struct context *ctx;
54         struct probe *probe;
55         GSList *l;
56         uint64_t samplerate;
57         int i, b, num_probes;
58         char *c, *samplerate_s;
59         char wbuf[1000], comment[128];
60         time_t t;
61
62         if (!(ctx = calloc(1, sizeof(struct context))))
63                 return SIGROK_ERR_MALLOC;
64
65         o->internal = ctx;
66         ctx->num_enabled_probes = 0;
67
68         for (l = o->device->probes; l; l = l->next) {
69                 probe = l->data;
70                 if (!probe->enabled)
71                         continue;
72                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
73         }
74
75         ctx->probelist[ctx->num_enabled_probes] = 0;
76         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
77
78         /* TODO: Allow for configuration via o->param. */
79
80         if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
81                 free(ctx);
82                 return SIGROK_ERR_MALLOC;
83         }
84         num_probes = g_slist_length(o->device->probes);
85
86         comment[0] = '\0';
87         if (o->device->plugin) {
88                 /* TODO: Handle num_probes == 0, too many probes, etc. */
89                 /* TODO: Error handling. */
90                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
91                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
92                 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
93                         free(ctx->header);
94                         free(ctx);
95                         return SIGROK_ERR;
96                 }
97                 /* TODO: Handle sprintf() errors. */
98                 snprintf(comment, 127, vcd_header_comment,
99                          ctx->num_enabled_probes, num_probes, samplerate_s);
100                 free(samplerate_s);
101         }
102
103         /* Wires / channels */
104         wbuf[0] = '\0';
105         for (i = 0; i < ctx->num_enabled_probes; i++) {
106                 c = (char *)&wbuf + strlen((char *)&wbuf);
107                 /* TODO: Needs fixing for very large number of probes. */
108                 /* TODO: Handle sprintf() errors. */
109                 sprintf(c, "$var wire 1 %c channel%s $end\n",
110                         (char)('!' + i), ctx->probelist[i]);
111         }
112
113         /* TODO: Date: File or signals? Make y/n configurable. */
114         t = time(NULL);
115         b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, ctime(&t),
116                      PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
117         /* TODO: Handle snprintf() errors. */
118
119         if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
120                 free(ctx->header);
121                 free(ctx);
122                 return SIGROK_ERR_MALLOC;
123         }
124
125         return SIGROK_OK;
126 }
127
128 static int event(struct output *o, int event_type, char **data_out,
129                  uint64_t *length_out)
130 {
131         struct context *ctx;
132         char *outbuf;
133         int outlen;
134
135         ctx = o->internal;
136         switch (event_type) {
137         case DF_TRIGGER:
138                 /* TODO: can a trigger mark be in a VCD file? */
139                 *data_out = NULL;
140                 *length_out = 0;
141                 break;
142         case DF_END:
143                 outlen = strlen("$dumpoff\n$end\n");
144                 if (!(outbuf = malloc(outlen + 1)))
145                         return SIGROK_ERR_MALLOC;
146                 /* TODO: Bug? Drop the + 1? */
147                 /* TODO: Handle snprintf() errors. */
148                 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
149                 *data_out = outbuf;
150                 *length_out = outlen;
151                 free(o->internal);
152                 o->internal = NULL;
153                 break;
154         default:
155                 *data_out = NULL;
156                 *length_out = 0;
157                 break;
158         }
159
160         return SIGROK_OK;
161 }
162
163 static int data(struct output *o, char *data_in, uint64_t length_in,
164                 char **data_out, uint64_t *length_out)
165 {
166         struct context *ctx;
167         unsigned int i, outsize;
168         int p, curbit, prevbit;
169         uint64_t sample, prevsample;
170         static uint64_t samplecount = 0;
171         char *outbuf, *c;
172
173         ctx = o->internal;
174         outsize = 0;
175         if (ctx->header)
176                 outsize = strlen(ctx->header);
177
178         /* FIXME: Use realloc(). */
179         if (!(outbuf = calloc(1, outsize + 1 + 10000)))
180                 return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */
181
182         outbuf[0] = '\0';
183         if (ctx->header) {
184                 /* The header is still here, this must be the first packet. */
185                 strncpy(outbuf, ctx->header, outsize);
186                 free(ctx->header);
187                 ctx->header = NULL;
188         }
189
190         /* TODO: Are disabled probes handled correctly? */
191
192         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
193                 samplecount++;
194                 memcpy(&sample, data_in + i, ctx->unitsize);
195                 if (i == 0)
196                         prevsample = sample;
197                 else
198                         memcpy(&prevsample, data_in + i - 1, ctx->unitsize);
199
200                 for (p = 0; p < ctx->num_enabled_probes; p++) {
201                         curbit = (sample & ((uint64_t) (1 << p))) >> p;
202                         prevbit = (prevsample & ((uint64_t) (1 << p))) >> p;
203
204                         /* VCD only contains deltas/changes of signals. */
205                         if (prevbit == curbit)
206                                 continue;
207
208                         /* Output which signal changed to which value. */
209                         c = outbuf + strlen(outbuf);
210                         sprintf(c, "#%" PRIu64 "\n%i%c\n", samplecount,
211                                 curbit, (char)('!' + p));
212                 }
213
214                 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
215         }
216
217         *data_out = outbuf;
218         *length_out = strlen(outbuf);
219
220         return SIGROK_OK;
221 }
222
223 struct output_format output_vcd = {
224         "vcd",
225         "Value Change Dump (VCD)",
226         DF_LOGIC,
227         init,
228         data,
229         event,
230 };