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