]> sigrok.org Git - libsigrok.git/blame - output/vcd.c
config.h usage cleanups.
[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>
c73d2ea4 5 * Copyright (C) 2010-2012 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
UH
28
29struct context {
30 int num_enabled_probes;
31 int unitsize;
8ec95d22 32 char *probelist[SR_MAX_NUM_PROBES + 1];
4c9ffa83 33 int *prevbits;
fbf1ff5d 34 GString *header;
08b488b8 35 uint64_t prevsample;
b33e7d70
HE
36 int period;
37 uint64_t samplerate;
4c9ffa83
UH
38};
39
d078d2e5 40static const char *vcd_header_comment = "\
7aae7462
BV
41$comment\n Acquisition with %d/%d probes at %s\n$end\n";
42
f50f3f40 43static int init(struct sr_output *o)
4c9ffa83 44{
4c9ffa83 45 struct context *ctx;
1afe8989 46 struct sr_probe *probe;
4c9ffa83 47 GSList *l;
5c3c1241 48 uint64_t *samplerate;
b33e7d70 49 int num_probes, i;
fbf1ff5d 50 char *samplerate_s, *frequency_s, *timestamp;
5cca9adb 51 time_t t;
4c9ffa83 52
133a37bf
UH
53 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
54 sr_err("vcd out: %s: ctx malloc failed", __func__);
e46b8fb1 55 return SR_ERR_MALLOC;
133a37bf 56 }
757b8c62 57
4c9ffa83
UH
58 o->internal = ctx;
59 ctx->num_enabled_probes = 0;
757b8c62 60
5c3c1241 61 for (l = o->sdi->probes; l; l = l->next) {
4c9ffa83 62 probe = l->data;
757b8c62
UH
63 if (!probe->enabled)
64 continue;
65 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
4c9ffa83 66 }
fbf1ff5d 67 if (ctx->num_enabled_probes > 94) {
7b48d6e1 68 sr_err("vcd out: VCD only supports 94 probes.");
e46b8fb1 69 return SR_ERR;
fbf1ff5d 70 }
4c9ffa83
UH
71
72 ctx->probelist[ctx->num_enabled_probes] = 0;
73 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
fbf1ff5d 74 ctx->header = g_string_sized_new(512);
5c3c1241 75 num_probes = g_slist_length(o->sdi->probes);
4c9ffa83 76
fbf1ff5d
BV
77 /* timestamp */
78 t = time(NULL);
133a37bf 79 timestamp = g_strdup(ctime(&t));
fbf1ff5d
BV
80 timestamp[strlen(timestamp)-1] = 0;
81 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
133a37bf 82 g_free(timestamp);
4c9ffa83 83
fbf1ff5d
BV
84 /* generator */
85 g_string_append_printf(ctx->header, "$version %s %s $end\n",
86 PACKAGE, PACKAGE_VERSION);
4c9ffa83 87
5c3c1241
BV
88 if (o->sdi->driver && sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
89 o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
90 (const void **)&samplerate, o->sdi);
91 ctx->samplerate = *samplerate;
a00ba012 92 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
fbf1ff5d 93 g_string_free(ctx->header, TRUE);
133a37bf 94 g_free(ctx);
e46b8fb1 95 return SR_ERR;
6b5e3cee 96 }
fbf1ff5d
BV
97 g_string_append_printf(ctx->header, vcd_header_comment,
98 ctx->num_enabled_probes, num_probes, samplerate_s);
133a37bf 99 g_free(samplerate_s);
7aae7462 100 }
4c9ffa83 101
fbf1ff5d
BV
102 /* timescale */
103 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
59df0c77
UH
104 if (ctx->samplerate > SR_MHZ(1))
105 ctx->period = SR_GHZ(1);
106 else if (ctx->samplerate > SR_KHZ(1))
107 ctx->period = SR_MHZ(1);
fbf1ff5d 108 else
59df0c77 109 ctx->period = SR_KHZ(1);
a00ba012 110 if (!(frequency_s = sr_period_string(ctx->period))) {
fbf1ff5d 111 g_string_free(ctx->header, TRUE);
133a37bf 112 g_free(ctx);
e46b8fb1 113 return SR_ERR;
fbf1ff5d
BV
114 }
115 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
133a37bf 116 g_free(frequency_s);
fbf1ff5d
BV
117
118 /* scope */
119 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
120
4c9ffa83 121 /* Wires / channels */
bc010c05 122 for (i = 0; i < ctx->num_enabled_probes; i++) {
fbf1ff5d
BV
123 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
124 (char)('!' + i), ctx->probelist[i]);
4c9ffa83
UH
125 }
126
fbf1ff5d
BV
127 g_string_append(ctx->header, "$upscope $end\n"
128 "$enddefinitions $end\n$dumpvars\n");
4c9ffa83 129
133a37bf 130 if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
fbf1ff5d 131 g_string_free(ctx->header, TRUE);
133a37bf
UH
132 g_free(ctx);
133 sr_err("vcd out: %s: ctx->prevbits malloc failed", __func__);
e46b8fb1 134 return SR_ERR_MALLOC;
6b5e3cee 135 }
5a8fda15 136
e46b8fb1 137 return SR_OK;
4c9ffa83
UH
138}
139
054e6709 140static int event(struct sr_output *o, int event_type, uint8_t **data_out,
4c9ffa83
UH
141 uint64_t *length_out)
142{
054e6709 143 uint8_t *outbuf;
4c9ffa83 144
99c1fc59 145 switch (event_type) {
5a2326a7 146 case SR_DF_END:
054e6709 147 outbuf = (uint8_t *)g_strdup("$dumpoff\n$end\n");
4c9ffa83 148 *data_out = outbuf;
054e6709 149 *length_out = strlen((const char *)outbuf);
133a37bf 150 g_free(o->internal);
4c9ffa83
UH
151 o->internal = NULL;
152 break;
9ab95e54
BV
153 default:
154 *data_out = NULL;
155 *length_out = 0;
156 break;
4c9ffa83
UH
157 }
158
e46b8fb1 159 return SR_OK;
4c9ffa83
UH
160}
161
054e6709
UH
162static int data(struct sr_output *o, const uint8_t *data_in,
163 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
4c9ffa83
UH
164{
165 struct context *ctx;
fbf1ff5d 166 unsigned int i;
afc8e4de 167 int p, curbit, prevbit;
08b488b8 168 uint64_t sample;
086eac7c 169 static uint64_t samplecount = 0;
fbf1ff5d 170 GString *out;
08b488b8 171 int first_sample = 0;
4c9ffa83
UH
172
173 ctx = o->internal;
fbf1ff5d 174 out = g_string_sized_new(512);
6b5e3cee 175
4c9ffa83
UH
176 if (ctx->header) {
177 /* The header is still here, this must be the first packet. */
fbf1ff5d
BV
178 g_string_append(out, ctx->header->str);
179 g_string_free(ctx->header, TRUE);
4c9ffa83 180 ctx->header = NULL;
08b488b8 181 first_sample = 1;
4c9ffa83
UH
182 }
183
607b58de 184 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
086eac7c 185 samplecount++;
08b488b8 186
607b58de 187 memcpy(&sample, data_in + i, ctx->unitsize);
08b488b8
HE
188
189 if (first_sample) {
190 /* First packet. We neg to make sure sample is stored. */
191 ctx->prevsample = ~sample;
192 first_sample = 0;
193 }
194
4c9ffa83 195 for (p = 0; p < ctx->num_enabled_probes; p++) {
fbe2f794 196 curbit = (sample & ((uint64_t) (1 << p))) >> p;
08b488b8 197 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
fbe2f794
UH
198
199 /* VCD only contains deltas/changes of signals. */
607b58de
UH
200 if (prevbit == curbit)
201 continue;
4c9ffa83 202
fbe2f794 203 /* Output which signal changed to which value. */
b33e7d70 204 g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n",
32c0551b 205 (uint64_t)(((float)samplecount / ctx->samplerate)
b33e7d70 206 * ctx->period), curbit, (char)('!' + p));
4c9ffa83 207 }
08b488b8
HE
208
209 ctx->prevsample = sample;
4c9ffa83
UH
210 }
211
054e6709 212 *data_out = (uint8_t *)out->str;
fbf1ff5d
BV
213 *length_out = out->len;
214 g_string_free(out, FALSE);
4c9ffa83 215
e46b8fb1 216 return SR_OK;
4c9ffa83
UH
217}
218
f50f3f40 219struct sr_output_format output_vcd = {
cdb3573c 220 .id = "vcd",
d494a4aa
UH
221 .description = "Value Change Dump (VCD)",
222 .df_type = SR_DF_LOGIC,
223 .init = init,
224 .data = data,
225 .event = event,
4c9ffa83 226};