]> sigrok.org Git - libsigrok.git/blob - output/output_vcd.c
Fix all warnings and re-enable -Wextra.
[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\n$end\n\
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\
41 %s\
42 $upscope $end\n\
43 $enddefinitions $end\n\
44 $dumpvars\n";
45
46 static int init(struct output *o)
47 {
48 /* Maximum header length */
49 #define MAX_HEADER_LEN 2048
50
51         struct context *ctx;
52         struct probe *probe;
53         GSList *l;
54         uint64_t samplerate;
55         int i, b, num_probes;
56         char *c, *samplerate_s;
57         char wbuf[1000];
58
59         ctx = malloc(sizeof(struct context));
60         if (ctx == NULL)
61                 return SIGROK_ERR_MALLOC;
62         o->internal = ctx;
63         ctx->num_enabled_probes = 0;
64         for (l = o->device->probes; l; l = l->next) {
65                 probe = l->data;
66                 if (probe->enabled)
67                         ctx->probelist[ctx->num_enabled_probes++] = probe->name;
68         }
69
70         ctx->probelist[ctx->num_enabled_probes] = 0;
71         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
72
73         /* TODO: Allow for configuration via o->param. */
74
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));
82
83         if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
84                 return -1; // FIXME
85
86         /* Wires / channels */
87         wbuf[0] = '\0';
88         for (i = 0; i < ctx->num_enabled_probes; i++) {
89                 c = (char *)&wbuf + strlen((char *)&wbuf);
90                 sprintf(c, "$var wire 1 %c channel%s $end\n",
91                          (char)('!' + i), ctx->probelist[i]);
92         }
93
94         /* TODO: date: File or signals? Make y/n configurable. */
95         b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
96                      PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
97                      samplerate_s, 1, "ns", PACKAGE, (char *)&wbuf);
98         /* TODO: Handle snprintf errors. */
99
100         free(samplerate_s);
101
102         ctx->prevbits = calloc(sizeof(int), num_probes);
103         if (ctx->prevbits == NULL)
104                 return SIGROK_ERR_MALLOC;
105
106         return 0;
107 }
108
109 static int event(struct output *o, int event_type, char **data_out,
110                  uint64_t *length_out)
111 {
112         struct context *ctx;
113         char *outbuf;
114         int outlen;
115
116         ctx = o->internal;
117         switch(event_type) {
118         case DF_TRIGGER:
119                 break;
120         case DF_END:
121                 outlen = strlen("$dumpoff\n$end\n");
122                 outbuf = malloc(outlen + 1);
123                 if (outbuf == NULL)
124                         return SIGROK_ERR_MALLOC;
125                 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
126                 *data_out = outbuf;
127                 *length_out = outlen;
128                 free(o->internal);
129                 o->internal = NULL;
130                 break;
131         }
132
133         return SIGROK_OK;
134 }
135
136 static int data(struct output *o, char *data_in, uint64_t length_in,
137                 char **data_out, uint64_t *length_out)
138 {
139         struct context *ctx;
140         unsigned int offset, outsize;
141         int p, curbit, prevbit;
142         uint64_t sample, prevsample;
143         char *outbuf, *c;
144
145         ctx = o->internal;
146         outsize = strlen(ctx->header);
147         outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
148         if (outbuf == NULL)
149                 return SIGROK_ERR_MALLOC;
150         if (ctx->header) {
151                 /* The header is still here, this must be the first packet. */
152                 strncpy(outbuf, ctx->header, outsize);
153                 free(ctx->header);
154                 ctx->header = NULL;
155         } else {
156                 outbuf[0] = 0;
157         }
158
159         /* TODO: Are disabled probes handled correctly? */
160
161         for (offset = 0; offset <= length_in - ctx->unitsize;
162                                                 offset += ctx->unitsize) {
163                 memcpy(&sample, data_in + offset, ctx->unitsize);
164                 for (p = 0; p < ctx->num_enabled_probes; p++) {
165                         curbit = (sample & ((uint64_t) (1 << p))) != 0;
166                         if (offset == 0) {
167                                 prevbit = ~curbit;
168                         } else {
169                                 memcpy(&prevsample, data_in + offset - 1, ctx->unitsize);
170                                 prevbit = (prevsample & ((uint64_t) (1 << p))) != 0;
171                         }
172
173                         if (prevbit != curbit) {
174                                 /* FIXME: Only once per sample? */
175                                 c = outbuf + strlen(outbuf);
176                                 sprintf(c, "#%i\n", offset * 1 /* TODO */);
177
178                                 c = outbuf + strlen(outbuf);
179                                 sprintf(c, "%i%c\n", curbit, (char)('!' + p /* FIXME? */));
180                         }
181                 }
182
183                 /* TODO: Do a realloc() here if strlen(outbuf) is almost "full"... */
184         }
185
186         *data_out = outbuf;
187         *length_out = strlen(outbuf);
188
189         return SIGROK_OK;
190 }
191
192 struct output_format output_vcd = {
193         "vcd",
194         "Value Change Dump (VCD)",
195         init,
196         data,
197         event,
198 };