]> sigrok.org Git - libsigrok.git/blame - src/output/vcd.c
license: remove FSF postal address from boiler plate license text
[libsigrok.git] / src / output / vcd.c
CommitLineData
4c9ffa83 1/*
50985c20 2 * This file is part of the libsigrok project.
4c9ffa83
UH
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
13d8e03c 5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
4c9ffa83
UH
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
2ea1fdf1 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
4c9ffa83
UH
19 */
20
6ec6c43b 21#include <config.h>
4c9ffa83
UH
22#include <stdlib.h>
23#include <string.h>
24#include <glib.h>
c1aae900 25#include <libsigrok/libsigrok.h>
45c59c8b 26#include "libsigrok-internal.h"
4c9ffa83 27
3544f848 28#define LOG_PREFIX "output/vcd"
a944a84b 29
4c9ffa83 30struct context {
ba7dd8bb
UH
31 int num_enabled_channels;
32 GArray *channelindices;
b050fc48 33 uint8_t *prevsample;
6cb45d96 34 gboolean header_done;
b33e7d70 35 int period;
6cb45d96 36 int *channel_index;
b33e7d70 37 uint64_t samplerate;
563080a8 38 uint64_t samplecount;
4c9ffa83
UH
39};
40
a755b0e1 41static int init(struct sr_output *o, GHashTable *options)
4c9ffa83 42{
4c9ffa83 43 struct context *ctx;
ba7dd8bb 44 struct sr_channel *ch;
4c9ffa83 45 GSList *l;
6cb45d96 46 int num_enabled_channels, i;
757b8c62 47
a755b0e1
BV
48 (void)options;
49
6cb45d96 50 num_enabled_channels = 0;
ba7dd8bb
UH
51 for (l = o->sdi->channels; l; l = l->next) {
52 ch = l->data;
3f239f08 53 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 54 continue;
ba7dd8bb 55 if (!ch->enabled)
757b8c62 56 continue;
6cb45d96 57 num_enabled_channels++;
4c9ffa83 58 }
6cb45d96 59 if (num_enabled_channels > 94) {
ba7dd8bb 60 sr_err("VCD only supports 94 channels.");
e46b8fb1 61 return SR_ERR;
fbf1ff5d 62 }
4c9ffa83 63
6cb45d96 64 ctx = g_malloc0(sizeof(struct context));
d686c5ec 65 o->priv = ctx;
6cb45d96
BV
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
a755b0e1 82static GString *gen_header(const struct sr_output *o)
6cb45d96
BV
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
d686c5ec 93 ctx = o->priv;
6cb45d96 94 header = g_string_sized_new(512);
ba7dd8bb 95 num_channels = g_slist_length(o->sdi->channels);
4c9ffa83 96
fbf1ff5d
BV
97 /* timestamp */
98 t = time(NULL);
133a37bf 99 timestamp = g_strdup(ctime(&t));
fbf1ff5d 100 timestamp[strlen(timestamp)-1] = 0;
6cb45d96 101 g_string_printf(header, "$date %s $end\n", timestamp);
133a37bf 102 g_free(timestamp);
4c9ffa83 103
fbf1ff5d 104 /* generator */
6cb45d96 105 g_string_append_printf(header, "$version %s %s $end\n",
b9eb8e1a 106 PACKAGE_NAME, SR_PACKAGE_VERSION_STRING);
6cb45d96
BV
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);
6b5e3cee 115 }
6cb45d96
BV
116 }
117 if (ctx->samplerate != 0) {
118 samplerate_s = sr_samplerate_string(ctx->samplerate);
119 g_string_append_printf(header, " at %s", samplerate_s);
133a37bf 120 g_free(samplerate_s);
7aae7462 121 }
6cb45d96 122 g_string_append_printf(header, "\n$end\n");
4c9ffa83 123
fbf1ff5d
BV
124 /* timescale */
125 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
59df0c77
UH
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);
fbf1ff5d 130 else
59df0c77 131 ctx->period = SR_KHZ(1);
6cb45d96
BV
132 frequency_s = sr_period_string(ctx->period);
133 g_string_append_printf(header, "$timescale %s $end\n", frequency_s);
133a37bf 134 g_free(frequency_s);
fbf1ff5d
BV
135
136 /* scope */
b9eb8e1a 137 g_string_append_printf(header, "$scope module %s $end\n", PACKAGE_NAME);
fbf1ff5d 138
4c9ffa83 139 /* Wires / channels */
ba7dd8bb
UH
140 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
141 ch = l->data;
3f239f08 142 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 143 continue;
ba7dd8bb 144 if (!ch->enabled)
d601c0e9 145 continue;
6cb45d96 146 g_string_append_printf(header, "$var wire 1 %c %s $end\n",
ba7dd8bb 147 (char)('!' + i), ch->name);
4c9ffa83
UH
148 }
149
6cb45d96 150 g_string_append(header, "$upscope $end\n$enddefinitions $end\n");
4c9ffa83 151
6cb45d96 152 return header;
4c9ffa83
UH
153}
154
a755b0e1 155static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 156 GString **out)
4c9ffa83 157{
6cb45d96 158 const struct sr_datafeed_meta *meta;
d5585e32 159 const struct sr_datafeed_logic *logic;
6cb45d96
BV
160 const struct sr_config *src;
161 GSList *l;
4c9ffa83 162 struct context *ctx;
fbf1ff5d 163 unsigned int i;
ba6568c5 164 int p, curbit, prevbit, index;
b050fc48 165 uint8_t *sample;
2b6363b4 166 gboolean timestamp_written;
4c9ffa83 167
17f63de6 168 *out = NULL;
d686c5ec 169 if (!o || !o->priv)
191c7e5f 170 return SR_ERR_BUG;
d686c5ec 171 ctx = o->priv;
d5585e32 172
6cb45d96
BV
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 }
2b78ffea 192
6cb45d96
BV
193 if (!ctx->prevsample) {
194 /* Can't allocate this until we know the stream's unitsize. */
195 ctx->prevsample = g_malloc0(logic->unitsize);
2b78ffea 196 }
2b78ffea 197
6cb45d96
BV
198 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
199 sample = logic->data + i;
200 timestamp_written = FALSE;
08b488b8 201
6cb45d96
BV
202 for (p = 0; p < ctx->num_enabled_channels; p++) {
203 index = ctx->channel_index[p];
2b6363b4 204
6cb45d96
BV
205 curbit = ((unsigned)sample[index / 8]
206 >> (index % 8)) & 1;
207 prevbit = ((unsigned)ctx->prevsample[index / 8]
208 >> (index % 8)) & 1;
fbe2f794 209
6cb45d96
BV
210 /* VCD only contains deltas/changes of signals. */
211 if (prevbit == curbit && ctx->samplecount > 0)
212 continue;
4c9ffa83 213
6cb45d96
BV
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);
2b6363b4 219
6cb45d96
BV
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);
2b6363b4 224
6cb45d96
BV
225 timestamp_written = TRUE;
226 }
08b488b8 227
6cb45d96
BV
228 if (timestamp_written)
229 g_string_append_c(*out, '\n');
2b6363b4 230
6cb45d96
BV
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;
4c9ffa83
UH
241 }
242
17f63de6 243 return SR_OK;
d5585e32
BV
244}
245
246static int cleanup(struct sr_output *o)
247{
248 struct context *ctx;
249
d686c5ec 250 if (!o || !o->priv)
d5585e32
BV
251 return SR_ERR_ARG;
252
d686c5ec 253 ctx = o->priv;
2b78ffea 254 g_free(ctx->prevsample);
6cb45d96 255 g_free(ctx->channel_index);
d5585e32 256 g_free(ctx);
4c9ffa83 257
e46b8fb1 258 return SR_OK;
4c9ffa83
UH
259}
260
a755b0e1 261struct sr_output_module output_vcd = {
cdb3573c 262 .id = "vcd",
a755b0e1
BV
263 .name = "VCD",
264 .desc = "Value Change Dump",
8a174d23 265 .exts = (const char*[]){"vcd", NULL},
3cd4b381 266 .flags = 0,
a755b0e1 267 .options = NULL,
d494a4aa 268 .init = init,
17f63de6 269 .receive = receive,
bbe6e336 270 .cleanup = cleanup,
4c9ffa83 271};