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