]> sigrok.org Git - libsigrok.git/blame - output/output_vcd.c
Sigma: Support for decoding partial chunks.
[libsigrok.git] / output / 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>
fbf1ff5d 5 * Copyright (C) 2011 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>
25#include <sigrok.h>
26#include "config.h"
27
28struct context {
29 int num_enabled_probes;
30 int unitsize;
31 char *probelist[65];
32 int *prevbits;
fbf1ff5d 33 GString *header;
4c9ffa83
UH
34};
35
36const char *vcd_header = "\
fbf1ff5d
BV
37$date %s $end\n\
38$version %s $end\n%s\
39$timescale %s $end\n\
4c9ffa83
UH
40$scope module %s $end\n\
41%s\
42$upscope $end\n\
43$enddefinitions $end\n\
44$dumpvars\n";
45
7aae7462
BV
46const char *vcd_header_comment = "\
47$comment\n Acquisition with %d/%d probes at %s\n$end\n";
48
5a8fda15 49static int init(struct output *o)
4c9ffa83 50{
4c9ffa83
UH
51 struct context *ctx;
52 struct probe *probe;
53 GSList *l;
54 uint64_t samplerate;
fbf1ff5d
BV
55 int num_probes, period, i;
56 char *samplerate_s, *frequency_s, *timestamp;
5cca9adb 57 time_t t;
4c9ffa83 58
6b5e3cee 59 if (!(ctx = calloc(1, sizeof(struct context))))
e31b636d 60 return SIGROK_ERR_MALLOC;
757b8c62 61
4c9ffa83
UH
62 o->internal = ctx;
63 ctx->num_enabled_probes = 0;
757b8c62 64
4c9ffa83
UH
65 for (l = o->device->probes; l; l = l->next) {
66 probe = l->data;
757b8c62
UH
67 if (!probe->enabled)
68 continue;
69 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
4c9ffa83 70 }
fbf1ff5d
BV
71 if (ctx->num_enabled_probes > 94) {
72 g_warning("VCD only supports 94 probes.");
73 return SIGROK_ERR;
74 }
4c9ffa83
UH
75
76 ctx->probelist[ctx->num_enabled_probes] = 0;
77 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
fbf1ff5d
BV
78 ctx->header = g_string_sized_new(512);
79 num_probes = g_slist_length(o->device->probes);
4c9ffa83 80
fbf1ff5d
BV
81 /* timestamp */
82 t = time(NULL);
83 timestamp = strdup(ctime(&t));
84 timestamp[strlen(timestamp)-1] = 0;
85 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
86 free(timestamp);
4c9ffa83 87
fbf1ff5d
BV
88 /* generator */
89 g_string_append_printf(ctx->header, "$version %s %s $end\n",
90 PACKAGE, PACKAGE_VERSION);
4c9ffa83 91
7aae7462 92 if (o->device->plugin) {
7aae7462
BV
93 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
94 o->device->plugin_index, DI_CUR_SAMPLERATE));
6b5e3cee 95 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
fbf1ff5d 96 g_string_free(ctx->header, TRUE);
6b5e3cee 97 free(ctx);
7aae7462 98 return SIGROK_ERR;
6b5e3cee 99 }
fbf1ff5d
BV
100 g_string_append_printf(ctx->header, vcd_header_comment,
101 ctx->num_enabled_probes, num_probes, samplerate_s);
7aae7462
BV
102 free(samplerate_s);
103 }
4c9ffa83 104
fbf1ff5d
BV
105 /* timescale */
106 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
107 if (samplerate > MHZ(1))
108 period = GHZ(1);
109 else if (samplerate > KHZ(1))
110 period = MHZ(1);
111 else
112 period = KHZ(1);
113 if (!(frequency_s = sigrok_period_string(period))) {
114 g_string_free(ctx->header, TRUE);
115 free(ctx);
116 return SIGROK_ERR;
117 }
118 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
119 free(frequency_s);
120
121 /* scope */
122 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
123
4c9ffa83 124 /* Wires / channels */
bc010c05 125 for (i = 0; i < ctx->num_enabled_probes; i++) {
fbf1ff5d
BV
126 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
127 (char)('!' + i), ctx->probelist[i]);
4c9ffa83
UH
128 }
129
fbf1ff5d
BV
130 g_string_append(ctx->header, "$upscope $end\n"
131 "$enddefinitions $end\n$dumpvars\n");
4c9ffa83 132
6b5e3cee 133 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
fbf1ff5d 134 g_string_free(ctx->header, TRUE);
6b5e3cee 135 free(ctx);
e31b636d 136 return SIGROK_ERR_MALLOC;
6b5e3cee 137 }
5a8fda15 138
6b5e3cee 139 return SIGROK_OK;
4c9ffa83
UH
140}
141
142static int event(struct output *o, int event_type, char **data_out,
143 uint64_t *length_out)
144{
145 struct context *ctx;
146 char *outbuf;
4c9ffa83
UH
147
148 ctx = o->internal;
99c1fc59 149 switch (event_type) {
4c9ffa83 150 case DF_END:
fbf1ff5d 151 outbuf = strdup("$dumpoff\n$end\n");
4c9ffa83 152 *data_out = outbuf;
fbf1ff5d 153 *length_out = strlen(outbuf);
4c9ffa83
UH
154 free(o->internal);
155 o->internal = NULL;
156 break;
9ab95e54
BV
157 default:
158 *data_out = NULL;
159 *length_out = 0;
160 break;
4c9ffa83
UH
161 }
162
163 return SIGROK_OK;
164}
165
166static int data(struct output *o, char *data_in, uint64_t length_in,
167 char **data_out, uint64_t *length_out)
168{
169 struct context *ctx;
fbf1ff5d 170 unsigned int i;
afc8e4de 171 int p, curbit, prevbit;
4c9ffa83 172 uint64_t sample, prevsample;
086eac7c 173 static uint64_t samplecount = 0;
fbf1ff5d 174 GString *out;
4c9ffa83
UH
175
176 ctx = o->internal;
fbf1ff5d 177 out = g_string_sized_new(512);
6b5e3cee 178
4c9ffa83
UH
179 if (ctx->header) {
180 /* The header is still here, this must be the first packet. */
fbf1ff5d
BV
181 g_string_append(out, ctx->header->str);
182 g_string_free(ctx->header, TRUE);
4c9ffa83 183 ctx->header = NULL;
4c9ffa83
UH
184 }
185
607b58de 186 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
086eac7c 187 samplecount++;
607b58de 188 memcpy(&sample, data_in + i, ctx->unitsize);
fbe2f794
UH
189 if (i == 0)
190 prevsample = sample;
191 else
192 memcpy(&prevsample, data_in + i - 1, ctx->unitsize);
193
4c9ffa83 194 for (p = 0; p < ctx->num_enabled_probes; p++) {
fbe2f794
UH
195 curbit = (sample & ((uint64_t) (1 << p))) >> p;
196 prevbit = (prevsample & ((uint64_t) (1 << p))) >> p;
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. */
fbf1ff5d
BV
203 g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n", samplecount,
204 curbit, (char)('!' + p));
4c9ffa83 205 }
4c9ffa83
UH
206 }
207
fbf1ff5d
BV
208 *data_out = out->str;
209 *length_out = out->len;
210 g_string_free(out, FALSE);
4c9ffa83
UH
211
212 return SIGROK_OK;
213}
214
215struct output_format output_vcd = {
216 "vcd",
217 "Value Change Dump (VCD)",
f0411b1d 218 DF_LOGIC,
4c9ffa83
UH
219 init,
220 data,
221 event,
222};