]> sigrok.org Git - libsigrok.git/blob - src/output/vcd.c
output: Rename instance private storage pointer to priv.
[libsigrok.git] / src / output / vcd.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2013 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 "config.h" /* Needed for PACKAGE and others. */
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28
29 #define LOG_PREFIX "output/vcd"
30
31 struct context {
32         int num_enabled_channels;
33         GArray *channelindices;
34         uint8_t *prevsample;
35         gboolean header_done;
36         int period;
37         int *channel_index;
38         uint64_t samplerate;
39         uint64_t samplecount;
40 };
41
42 static const char *const vcd_header_comment =
43         "$comment\n  Acquisition with %d/%d channels at %s\n$end\n";
44
45 static int init(struct sr_output *o, GHashTable *options)
46 {
47         struct context *ctx;
48         struct sr_channel *ch;
49         GSList *l;
50         int num_enabled_channels, i;
51
52         (void)options;
53
54         num_enabled_channels = 0;
55         for (l = o->sdi->channels; l; l = l->next) {
56                 ch = l->data;
57                 if (ch->type != SR_CHANNEL_LOGIC)
58                         continue;
59                 if (!ch->enabled)
60                         continue;
61                 num_enabled_channels++;
62         }
63         if (num_enabled_channels > 94) {
64                 sr_err("VCD only supports 94 channels.");
65                 return SR_ERR;
66         }
67
68         ctx = g_malloc0(sizeof(struct context));
69         o->priv = ctx;
70         ctx->num_enabled_channels = num_enabled_channels;
71         ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
72
73         /* Once more to map the enabled channels. */
74         for (i = 0, l = o->sdi->channels; l; l = l->next) {
75                 ch = l->data;
76                 if (ch->type != SR_CHANNEL_LOGIC)
77                         continue;
78                 if (!ch->enabled)
79                         continue;
80                 ctx->channel_index[i++] = ch->index;
81         }
82
83         return SR_OK;
84 }
85
86 static GString *gen_header(const struct sr_output *o)
87 {
88         struct context *ctx;
89         struct sr_channel *ch;
90         GVariant *gvar;
91         GString *header;
92         GSList *l;
93         time_t t;
94         int num_channels, i;
95         char *samplerate_s, *frequency_s, *timestamp;
96
97         ctx = o->priv;
98         header = g_string_sized_new(512);
99         num_channels = g_slist_length(o->sdi->channels);
100
101         /* timestamp */
102         t = time(NULL);
103         timestamp = g_strdup(ctime(&t));
104         timestamp[strlen(timestamp)-1] = 0;
105         g_string_printf(header, "$date %s $end\n", timestamp);
106         g_free(timestamp);
107
108         /* generator */
109         g_string_append_printf(header, "$version %s %s $end\n",
110                         PACKAGE, PACKAGE_VERSION);
111         g_string_append_printf(header, "$comment\n  Acquisition with "
112                         "%d/%d channels", ctx->num_enabled_channels, num_channels);
113
114         if (ctx->samplerate == 0) {
115                 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
116                                 &gvar) == SR_OK) {
117                         ctx->samplerate = g_variant_get_uint64(gvar);
118                         g_variant_unref(gvar);
119                 }
120         }
121         if (ctx->samplerate != 0) {
122                 samplerate_s = sr_samplerate_string(ctx->samplerate);
123                 g_string_append_printf(header, " at %s", samplerate_s);
124                 g_free(samplerate_s);
125         }
126         g_string_append_printf(header, "\n$end\n");
127
128         /* timescale */
129         /* VCD can only handle 1/10/100 (s - fs), so scale up first */
130         if (ctx->samplerate > SR_MHZ(1))
131                 ctx->period = SR_GHZ(1);
132         else if (ctx->samplerate > SR_KHZ(1))
133                 ctx->period = SR_MHZ(1);
134         else
135                 ctx->period = SR_KHZ(1);
136         frequency_s = sr_period_string(ctx->period);
137         g_string_append_printf(header, "$timescale %s $end\n", frequency_s);
138         g_free(frequency_s);
139
140         /* scope */
141         g_string_append_printf(header, "$scope module %s $end\n", PACKAGE);
142
143         /* Wires / channels */
144         for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
145                 ch = l->data;
146                 if (ch->type != SR_CHANNEL_LOGIC)
147                         continue;
148                 if (!ch->enabled)
149                         continue;
150                 g_string_append_printf(header, "$var wire 1 %c %s $end\n",
151                                 (char)('!' + i), ch->name);
152         }
153
154         g_string_append(header, "$upscope $end\n$enddefinitions $end\n");
155
156         return header;
157 }
158
159 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
160                 GString **out)
161 {
162         const struct sr_datafeed_meta *meta;
163         const struct sr_datafeed_logic *logic;
164         const struct sr_config *src;
165         GSList *l;
166         struct context *ctx;
167         unsigned int i;
168         int p, curbit, prevbit, index;
169         uint8_t *sample;
170         gboolean timestamp_written;
171
172         *out = NULL;
173         if (!o || !o->priv)
174                 return SR_ERR_BUG;
175         ctx = o->priv;
176
177         switch (packet->type) {
178         case SR_DF_META:
179                 meta = packet->payload;
180                 for (l = meta->config; l; l = l->next) {
181                         src = l->data;
182                         if (src->key != SR_CONF_SAMPLERATE)
183                                 continue;
184                         ctx->samplerate = g_variant_get_uint64(src->data);
185                 }
186                 break;
187         case SR_DF_LOGIC:
188                 logic = packet->payload;
189
190                 if (!ctx->header_done) {
191                         *out = gen_header(o);
192                         ctx->header_done = TRUE;
193                 } else {
194                         *out = g_string_sized_new(512);
195                 }
196
197                 if (!ctx->prevsample) {
198                         /* Can't allocate this until we know the stream's unitsize. */
199                         ctx->prevsample = g_malloc0(logic->unitsize);
200                 }
201
202                 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
203                         sample = logic->data + i;
204                         timestamp_written = FALSE;
205
206                         for (p = 0; p < ctx->num_enabled_channels; p++) {
207                                 index = ctx->channel_index[p];
208
209                                 curbit = ((unsigned)sample[index / 8]
210                                                 >> (index % 8)) & 1;
211                                 prevbit = ((unsigned)ctx->prevsample[index / 8]
212                                                 >> (index % 8)) & 1;
213
214                                 /* VCD only contains deltas/changes of signals. */
215                                 if (prevbit == curbit && ctx->samplecount > 0)
216                                         continue;
217
218                                 /* Output timestamp of subsequent signal changes. */
219                                 if (!timestamp_written)
220                                         g_string_append_printf(*out, "#%.0f",
221                                                 (double)ctx->samplecount /
222                                                         ctx->samplerate * ctx->period);
223
224                                 /* Output which signal changed to which value. */
225                                 g_string_append_c(*out, ' ');
226                                 g_string_append_c(*out, '0' + curbit);
227                                 g_string_append_c(*out, '!' + p);
228
229                                 timestamp_written = TRUE;
230                         }
231
232                         if (timestamp_written)
233                                 g_string_append_c(*out, '\n');
234
235                         ctx->samplecount++;
236                         memcpy(ctx->prevsample, sample, logic->unitsize);
237                 }
238                 break;
239         case SR_DF_END:
240                 /* Write final timestamp as length indicator. */
241                 *out = g_string_sized_new(512);
242                 g_string_printf(*out, "#%.0f\n",
243                                 (double)ctx->samplecount / ctx->samplerate * ctx->period);
244                 break;
245         }
246
247         return SR_OK;
248 }
249
250 static int cleanup(struct sr_output *o)
251 {
252         struct context *ctx;
253
254         if (!o || !o->priv)
255                 return SR_ERR_ARG;
256
257         ctx = o->priv;
258         g_free(ctx->prevsample);
259         g_free(ctx->channel_index);
260         g_free(ctx);
261
262         return SR_OK;
263 }
264
265 struct sr_output_module output_vcd = {
266         .id = "vcd",
267         .name = "VCD",
268         .desc = "Value Change Dump",
269         .options = NULL,
270         .init = init,
271         .receive = receive,
272         .cleanup = cleanup,
273 };