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