]> sigrok.org Git - libsigrok.git/blob - output/output_vcd.c
output_vcd: Remember samples between packets.
[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  * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <sigrok.h>
26 #include "config.h"
27
28 struct context {
29         int num_enabled_probes;
30         int unitsize;
31         char *probelist[65];
32         int *prevbits;
33         GString *header;
34         uint64_t prevsample;
35 };
36
37 const char *vcd_header = "\
38 $date %s $end\n\
39 $version %s $end\n%s\
40 $timescale %s $end\n\
41 $scope module %s $end\n\
42 %s\
43 $upscope $end\n\
44 $enddefinitions $end\n\
45 $dumpvars\n";
46
47 const char *vcd_header_comment = "\
48 $comment\n  Acquisition with %d/%d probes at %s\n$end\n";
49
50 static int init(struct output *o)
51 {
52         struct context *ctx;
53         struct probe *probe;
54         GSList *l;
55         uint64_t samplerate;
56         int num_probes, period, i;
57         char *samplerate_s, *frequency_s, *timestamp;
58         time_t t;
59
60         if (!(ctx = calloc(1, sizeof(struct context))))
61                 return SIGROK_ERR_MALLOC;
62
63         o->internal = ctx;
64         ctx->num_enabled_probes = 0;
65
66         for (l = o->device->probes; l; l = l->next) {
67                 probe = l->data;
68                 if (!probe->enabled)
69                         continue;
70                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
71         }
72         if (ctx->num_enabled_probes > 94) {
73                 g_warning("VCD only supports 94 probes.");
74                 return SIGROK_ERR;
75         }
76
77         ctx->probelist[ctx->num_enabled_probes] = 0;
78         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
79         ctx->header = g_string_sized_new(512);
80         num_probes = g_slist_length(o->device->probes);
81
82         /* timestamp */
83         t = time(NULL);
84         timestamp = strdup(ctime(&t));
85         timestamp[strlen(timestamp)-1] = 0;
86         g_string_printf(ctx->header, "$date %s $end\n", timestamp);
87         free(timestamp);
88
89         /* generator */
90         g_string_append_printf(ctx->header, "$version %s %s $end\n",
91                         PACKAGE, PACKAGE_VERSION);
92
93         if (o->device->plugin) {
94                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
95                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
96                 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
97                         g_string_free(ctx->header, TRUE);
98                         free(ctx);
99                         return SIGROK_ERR;
100                 }
101                 g_string_append_printf(ctx->header, vcd_header_comment,
102                                  ctx->num_enabled_probes, num_probes, samplerate_s);
103                 free(samplerate_s);
104         }
105
106         /* timescale */
107         /* VCD can only handle 1/10/100 (s - fs), so scale up first */
108         if (samplerate > MHZ(1))
109                 period = GHZ(1);
110         else if (samplerate > KHZ(1))
111                 period = MHZ(1);
112         else
113                 period = KHZ(1);
114         if (!(frequency_s = sigrok_period_string(period))) {
115                 g_string_free(ctx->header, TRUE);
116                 free(ctx);
117                 return SIGROK_ERR;
118         }
119         g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
120         free(frequency_s);
121
122         /* scope */
123         g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
124
125         /* Wires / channels */
126         for (i = 0; i < ctx->num_enabled_probes; i++) {
127                 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
128                                 (char)('!' + i), ctx->probelist[i]);
129         }
130
131         g_string_append(ctx->header, "$upscope $end\n"
132                         "$enddefinitions $end\n$dumpvars\n");
133
134         if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
135                 g_string_free(ctx->header, TRUE);
136                 free(ctx);
137                 return SIGROK_ERR_MALLOC;
138         }
139
140         return SIGROK_OK;
141 }
142
143 static int event(struct output *o, int event_type, char **data_out,
144                  uint64_t *length_out)
145 {
146         struct context *ctx;
147         char *outbuf;
148
149         ctx = o->internal;
150         switch (event_type) {
151         case DF_END:
152                 outbuf = strdup("$dumpoff\n$end\n");
153                 *data_out = outbuf;
154                 *length_out = strlen(outbuf);
155                 free(o->internal);
156                 o->internal = NULL;
157                 break;
158         default:
159                 *data_out = NULL;
160                 *length_out = 0;
161                 break;
162         }
163
164         return SIGROK_OK;
165 }
166
167 static int data(struct output *o, char *data_in, uint64_t length_in,
168                 char **data_out, uint64_t *length_out)
169 {
170         struct context *ctx;
171         unsigned int i;
172         int p, curbit, prevbit;
173         uint64_t sample;
174         static uint64_t samplecount = 0;
175         GString *out;
176         int first_sample = 0;
177
178         ctx = o->internal;
179         out = g_string_sized_new(512);
180
181         if (ctx->header) {
182                 /* The header is still here, this must be the first packet. */
183                 g_string_append(out, ctx->header->str);
184                 g_string_free(ctx->header, TRUE);
185                 ctx->header = NULL;
186                 first_sample = 1;
187         }
188
189         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
190                 samplecount++;
191
192                 memcpy(&sample, data_in + i, ctx->unitsize);
193
194                 if (first_sample) {
195                         /* First packet. We neg to make sure sample is stored. */
196                         ctx->prevsample = ~sample;
197                         first_sample = 0;
198                 }
199
200
201                 for (p = 0; p < ctx->num_enabled_probes; p++) {
202                         curbit = (sample & ((uint64_t) (1 << p))) >> p;
203                         prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
204
205                         /* VCD only contains deltas/changes of signals. */
206                         if (prevbit == curbit)
207                                 continue;
208
209                         /* Output which signal changed to which value. */
210                         g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n", samplecount,
211                                         curbit, (char)('!' + p));
212                 }
213
214                 ctx->prevsample = sample;
215         }
216
217         *data_out = out->str;
218         *length_out = out->len;
219         g_string_free(out, FALSE);
220
221         return SIGROK_OK;
222 }
223
224 struct output_format output_vcd = {
225         "vcd",
226         "Value Change Dump (VCD)",
227         DF_LOGIC,
228         init,
229         data,
230         event,
231 };