]> sigrok.org Git - libsigrok.git/blame - output/vcd.c
Add sr_config_free()
[libsigrok.git] / output / vcd.c
CommitLineData
4c9ffa83
UH
1/*
2 * This file is part of the sigrok project.
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;
8ec95d22 40 char *probelist[SR_MAX_NUM_PROBES + 1];
4c9ffa83 41 int *prevbits;
fbf1ff5d 42 GString *header;
08b488b8 43 uint64_t prevsample;
b33e7d70
HE
44 int period;
45 uint64_t samplerate;
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;
5c3c1241 56 uint64_t *samplerate;
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;
757b8c62 68
5c3c1241 69 for (l = o->sdi->probes; l; l = l->next) {
4c9ffa83 70 probe = l->data;
757b8c62
UH
71 if (!probe->enabled)
72 continue;
73 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
4c9ffa83 74 }
fbf1ff5d 75 if (ctx->num_enabled_probes > 94) {
a944a84b 76 sr_err("VCD only supports 94 probes.");
e46b8fb1 77 return SR_ERR;
fbf1ff5d 78 }
4c9ffa83
UH
79
80 ctx->probelist[ctx->num_enabled_probes] = 0;
fbf1ff5d 81 ctx->header = g_string_sized_new(512);
5c3c1241 82 num_probes = g_slist_length(o->sdi->probes);
4c9ffa83 83
fbf1ff5d
BV
84 /* timestamp */
85 t = time(NULL);
133a37bf 86 timestamp = g_strdup(ctime(&t));
fbf1ff5d
BV
87 timestamp[strlen(timestamp)-1] = 0;
88 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
133a37bf 89 g_free(timestamp);
4c9ffa83 90
fbf1ff5d
BV
91 /* generator */
92 g_string_append_printf(ctx->header, "$version %s %s $end\n",
93 PACKAGE, PACKAGE_VERSION);
4c9ffa83 94
4d15e5c9 95 if (o->sdi->driver && sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
123e1313 96 o->sdi->driver->config_get(SR_CONF_SAMPLERATE,
5c3c1241
BV
97 (const void **)&samplerate, o->sdi);
98 ctx->samplerate = *samplerate;
a00ba012 99 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
fbf1ff5d 100 g_string_free(ctx->header, TRUE);
133a37bf 101 g_free(ctx);
e46b8fb1 102 return SR_ERR;
6b5e3cee 103 }
fbf1ff5d
BV
104 g_string_append_printf(ctx->header, vcd_header_comment,
105 ctx->num_enabled_probes, num_probes, samplerate_s);
133a37bf 106 g_free(samplerate_s);
7aae7462 107 }
4c9ffa83 108
fbf1ff5d
BV
109 /* timescale */
110 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
59df0c77
UH
111 if (ctx->samplerate > SR_MHZ(1))
112 ctx->period = SR_GHZ(1);
113 else if (ctx->samplerate > SR_KHZ(1))
114 ctx->period = SR_MHZ(1);
fbf1ff5d 115 else
59df0c77 116 ctx->period = SR_KHZ(1);
a00ba012 117 if (!(frequency_s = sr_period_string(ctx->period))) {
fbf1ff5d 118 g_string_free(ctx->header, TRUE);
133a37bf 119 g_free(ctx);
e46b8fb1 120 return SR_ERR;
fbf1ff5d
BV
121 }
122 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
133a37bf 123 g_free(frequency_s);
fbf1ff5d
BV
124
125 /* scope */
126 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
127
4c9ffa83 128 /* Wires / channels */
bc010c05 129 for (i = 0; i < ctx->num_enabled_probes; i++) {
fbf1ff5d
BV
130 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
131 (char)('!' + i), ctx->probelist[i]);
4c9ffa83
UH
132 }
133
fbf1ff5d
BV
134 g_string_append(ctx->header, "$upscope $end\n"
135 "$enddefinitions $end\n$dumpvars\n");
4c9ffa83 136
133a37bf 137 if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
fbf1ff5d 138 g_string_free(ctx->header, TRUE);
133a37bf 139 g_free(ctx);
a944a84b 140 sr_err("%s: ctx->prevbits malloc failed", __func__);
e46b8fb1 141 return SR_ERR_MALLOC;
6b5e3cee 142 }
5a8fda15 143
e46b8fb1 144 return SR_OK;
4c9ffa83
UH
145}
146
bbe6e336 147static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi,
d5585e32 148 const struct sr_datafeed_packet *packet)
4c9ffa83 149{
d5585e32 150 const struct sr_datafeed_logic *logic;
4c9ffa83 151 struct context *ctx;
d5585e32 152 GString *text;
fbf1ff5d 153 unsigned int i;
afc8e4de 154 int p, curbit, prevbit;
08b488b8 155 uint64_t sample;
086eac7c 156 static uint64_t samplecount = 0;
d5585e32 157 gboolean first_sample;
4c9ffa83 158
d5585e32
BV
159 (void)sdi;
160
161 if (!o || !o->internal)
162 return NULL;
4c9ffa83 163 ctx = o->internal;
d5585e32
BV
164
165 if (packet->type == SR_DF_END) {
166 text = g_string_sized_new(16);
167 g_string_printf(text, "$dumpoff\n$end\n");
168 return text;
169 } else if (packet->type != SR_DF_LOGIC)
170 return NULL;
6b5e3cee 171
4c9ffa83
UH
172 if (ctx->header) {
173 /* The header is still here, this must be the first packet. */
d5585e32 174 text = ctx->header;
4c9ffa83 175 ctx->header = NULL;
d5585e32
BV
176 first_sample = TRUE;
177 } else {
178 text = g_string_sized_new(512);
179 first_sample = FALSE;
4c9ffa83
UH
180 }
181
d5585e32
BV
182 logic = packet->payload;
183 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
086eac7c 184 samplecount++;
08b488b8 185
d5585e32 186 memcpy(&sample, logic->data + i, logic->unitsize);
08b488b8
HE
187
188 if (first_sample) {
189 /* First packet. We neg to make sure sample is stored. */
190 ctx->prevsample = ~sample;
d5585e32 191 first_sample = FALSE;
08b488b8
HE
192 }
193
4c9ffa83 194 for (p = 0; p < ctx->num_enabled_probes; p++) {
fbe2f794 195 curbit = (sample & ((uint64_t) (1 << p))) >> p;
08b488b8 196 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
fbe2f794
UH
197
198 /* VCD only contains deltas/changes of signals. */
607b58de
UH
199 if (prevbit == curbit)
200 continue;
4c9ffa83 201
fbe2f794 202 /* Output which signal changed to which value. */
d5585e32 203 g_string_append_printf(text, "#%" PRIu64 "\n%i%c\n",
32c0551b 204 (uint64_t)(((float)samplecount / ctx->samplerate)
b33e7d70 205 * ctx->period), curbit, (char)('!' + p));
4c9ffa83 206 }
08b488b8
HE
207
208 ctx->prevsample = sample;
4c9ffa83
UH
209 }
210
d5585e32
BV
211 return text;
212}
213
214static int cleanup(struct sr_output *o)
215{
216 struct context *ctx;
217
218 if (!o || !o->internal)
219 return SR_ERR_ARG;
220
221 ctx = o->internal;
222 g_free(ctx);
4c9ffa83 223
e46b8fb1 224 return SR_OK;
4c9ffa83
UH
225}
226
f50f3f40 227struct sr_output_format output_vcd = {
cdb3573c 228 .id = "vcd",
d494a4aa
UH
229 .description = "Value Change Dump (VCD)",
230 .df_type = SR_DF_LOGIC,
231 .init = init,
bbe6e336
UH
232 .recv = receive,
233 .cleanup = cleanup,
4c9ffa83 234};