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