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