]> sigrok.org Git - libsigrok.git/blame - output/vcd.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / 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. */
45c59c8b
BV
26#include "libsigrok.h"
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;
fbf1ff5d 34 GString *header;
b050fc48 35 uint8_t *prevsample;
b33e7d70
HE
36 int period;
37 uint64_t samplerate;
563080a8 38 uint64_t samplecount;
b050fc48 39 unsigned int unitsize;
4c9ffa83
UH
40};
41
191c7e5f 42static const char *const vcd_header_comment =
ba7dd8bb 43 "$comment\n Acquisition with %d/%d channels at %s\n$end\n";
7aae7462 44
f50f3f40 45static int init(struct sr_output *o)
4c9ffa83 46{
4c9ffa83 47 struct context *ctx;
ba7dd8bb 48 struct sr_channel *ch;
4c9ffa83 49 GSList *l;
ec4063b8 50 GVariant *gvar;
ba7dd8bb 51 int num_channels, i;
fbf1ff5d 52 char *samplerate_s, *frequency_s, *timestamp;
5cca9adb 53 time_t t;
4c9ffa83 54
133a37bf 55 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 56 sr_err("%s: ctx malloc failed", __func__);
e46b8fb1 57 return SR_ERR_MALLOC;
133a37bf 58 }
757b8c62 59
4c9ffa83 60 o->internal = ctx;
ba7dd8bb
UH
61 ctx->num_enabled_channels = 0;
62 ctx->channelindices = g_array_new(FALSE, FALSE, sizeof(int));
757b8c62 63
ba7dd8bb
UH
64 for (l = o->sdi->channels; l; l = l->next) {
65 ch = l->data;
66 if (ch->type != SR_PROBE_LOGIC)
3699a8a1 67 continue;
ba7dd8bb 68 if (!ch->enabled)
757b8c62 69 continue;
ba7dd8bb
UH
70 ctx->channelindices = g_array_append_val(
71 ctx->channelindices, ch->index);
72 ctx->num_enabled_channels++;
4c9ffa83 73 }
ba7dd8bb
UH
74 if (ctx->num_enabled_channels > 94) {
75 sr_err("VCD only supports 94 channels.");
e46b8fb1 76 return SR_ERR;
fbf1ff5d 77 }
4c9ffa83 78
ba7dd8bb 79 ctx->unitsize = (ctx->num_enabled_channels + 7) / 8;
fbf1ff5d 80 ctx->header = g_string_sized_new(512);
ba7dd8bb 81 num_channels = g_slist_length(o->sdi->channels);
4c9ffa83 82
fbf1ff5d
BV
83 /* timestamp */
84 t = time(NULL);
133a37bf 85 timestamp = g_strdup(ctime(&t));
fbf1ff5d
BV
86 timestamp[strlen(timestamp)-1] = 0;
87 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
133a37bf 88 g_free(timestamp);
4c9ffa83 89
fbf1ff5d
BV
90 /* generator */
91 g_string_append_printf(ctx->header, "$version %s %s $end\n",
92 PACKAGE, PACKAGE_VERSION);
4c9ffa83 93
d3c74a6f
BV
94 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
95 &gvar) == SR_OK) {
ec4063b8 96 ctx->samplerate = g_variant_get_uint64(gvar);
af51a771 97 g_variant_unref(gvar);
a00ba012 98 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
fbf1ff5d 99 g_string_free(ctx->header, TRUE);
133a37bf 100 g_free(ctx);
e46b8fb1 101 return SR_ERR;
6b5e3cee 102 }
fbf1ff5d 103 g_string_append_printf(ctx->header, vcd_header_comment,
ba7dd8bb 104 ctx->num_enabled_channels, num_channels, samplerate_s);
133a37bf 105 g_free(samplerate_s);
7aae7462 106 }
4c9ffa83 107
fbf1ff5d
BV
108 /* timescale */
109 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
59df0c77
UH
110 if (ctx->samplerate > SR_MHZ(1))
111 ctx->period = SR_GHZ(1);
112 else if (ctx->samplerate > SR_KHZ(1))
113 ctx->period = SR_MHZ(1);
fbf1ff5d 114 else
59df0c77 115 ctx->period = SR_KHZ(1);
a00ba012 116 if (!(frequency_s = sr_period_string(ctx->period))) {
fbf1ff5d 117 g_string_free(ctx->header, TRUE);
133a37bf 118 g_free(ctx);
e46b8fb1 119 return SR_ERR;
fbf1ff5d
BV
120 }
121 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
133a37bf 122 g_free(frequency_s);
fbf1ff5d
BV
123
124 /* scope */
125 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
126
4c9ffa83 127 /* Wires / channels */
ba7dd8bb
UH
128 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
129 ch = l->data;
130 if (ch->type != SR_PROBE_LOGIC)
3699a8a1 131 continue;
ba7dd8bb 132 if (!ch->enabled)
d601c0e9 133 continue;
fbf1ff5d 134 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
ba7dd8bb 135 (char)('!' + i), ch->name);
4c9ffa83
UH
136 }
137
fbf1ff5d 138 g_string_append(ctx->header, "$upscope $end\n"
191c7e5f 139 "$enddefinitions $end\n");
4c9ffa83 140
b050fc48 141 if (!(ctx->prevsample = g_try_malloc0(ctx->unitsize))) {
fbf1ff5d 142 g_string_free(ctx->header, TRUE);
133a37bf 143 g_free(ctx);
b050fc48 144 sr_err("%s: ctx->prevsample malloc failed", __func__);
e46b8fb1 145 return SR_ERR_MALLOC;
6b5e3cee 146 }
5a8fda15 147
e46b8fb1 148 return SR_OK;
4c9ffa83
UH
149}
150
17f63de6
BV
151static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
152 const struct sr_datafeed_packet *packet, GString **out)
4c9ffa83 153{
d5585e32 154 const struct sr_datafeed_logic *logic;
4c9ffa83 155 struct context *ctx;
fbf1ff5d 156 unsigned int i;
ba6568c5 157 int p, curbit, prevbit, index;
b050fc48 158 uint8_t *sample;
2b6363b4 159 gboolean timestamp_written;
4c9ffa83 160
d5585e32
BV
161 (void)sdi;
162
17f63de6 163 *out = NULL;
d5585e32 164 if (!o || !o->internal)
191c7e5f 165 return SR_ERR_BUG;
4c9ffa83 166 ctx = o->internal;
d5585e32 167
4c9ffa83
UH
168 if (ctx->header) {
169 /* The header is still here, this must be the first packet. */
17f63de6 170 *out = ctx->header;
4c9ffa83 171 ctx->header = NULL;
563080a8 172 ctx->samplecount = 0;
d5585e32 173 } else {
17f63de6 174 *out = g_string_sized_new(512);
4c9ffa83
UH
175 }
176
fb9e91ae
DE
177 if (packet->type != SR_DF_LOGIC) {
178 if (packet->type == SR_DF_END)
179 /* Write final timestamp as length indicator. */
180 g_string_append_printf(*out, "#%.0f\n",
181 (double)ctx->samplecount /
182 ctx->samplerate * ctx->period);
183 return SR_OK;
184 }
185
d5585e32
BV
186 logic = packet->payload;
187 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
b050fc48 188 sample = logic->data + i;
2b6363b4 189 timestamp_written = FALSE;
08b488b8 190
ba7dd8bb
UH
191 for (p = 0; p < ctx->num_enabled_channels; p++) {
192 index = g_array_index(ctx->channelindices, int, p);
2b6363b4
DE
193
194 curbit = ((unsigned)sample[index / 8]
195 >> (index % 8)) & 1;
196 prevbit = ((unsigned)ctx->prevsample[index / 8]
197 >> (index % 8)) & 1;
fbe2f794
UH
198
199 /* VCD only contains deltas/changes of signals. */
563080a8 200 if (prevbit == curbit && ctx->samplecount > 0)
607b58de 201 continue;
4c9ffa83 202
2b6363b4
DE
203 /* Output timestamp of subsequent signal changes. */
204 if (!timestamp_written)
205 g_string_append_printf(*out, "#%.0f",
206 (double)ctx->samplecount /
207 ctx->samplerate * ctx->period);
208
fbe2f794 209 /* Output which signal changed to which value. */
2b6363b4
DE
210 g_string_append_c(*out, ' ');
211 g_string_append_c(*out, '0' + curbit);
212 g_string_append_c(*out, '!' + p);
213
214 timestamp_written = TRUE;
4c9ffa83 215 }
08b488b8 216
2b6363b4
DE
217 if (timestamp_written)
218 g_string_append_c(*out, '\n');
219
563080a8 220 ctx->samplecount++;
b050fc48 221 memcpy(ctx->prevsample, sample, ctx->unitsize);
4c9ffa83
UH
222 }
223
17f63de6 224 return SR_OK;
d5585e32
BV
225}
226
227static int cleanup(struct sr_output *o)
228{
229 struct context *ctx;
230
231 if (!o || !o->internal)
232 return SR_ERR_ARG;
233
234 ctx = o->internal;
235 g_free(ctx);
4c9ffa83 236
e46b8fb1 237 return SR_OK;
4c9ffa83
UH
238}
239
f50f3f40 240struct sr_output_format output_vcd = {
cdb3573c 241 .id = "vcd",
d494a4aa
UH
242 .description = "Value Change Dump (VCD)",
243 .df_type = SR_DF_LOGIC,
244 .init = init,
17f63de6 245 .receive = receive,
bbe6e336 246 .cleanup = cleanup,
4c9ffa83 247};