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