]> sigrok.org Git - libsigrok.git/blame - output/output_vcd.c
VCD/Gnuplot: Fix incorrect sample counter.
[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>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
24#include <sigrok.h>
25#include "config.h"
26
27struct context {
28 int num_enabled_probes;
29 int unitsize;
30 char *probelist[65];
31 int *prevbits;
32 char *header;
4c9ffa83
UH
33};
34
35const char *vcd_header = "\
5cca9adb 36$date\n %s$end\n\
7aae7462 37$version\n %s\n$end\n%s\
4c9ffa83
UH
38$timescale\n %i %s\n$end\n\
39$scope module %s $end\n\
40%s\
41$upscope $end\n\
42$enddefinitions $end\n\
43$dumpvars\n";
44
7aae7462
BV
45const char *vcd_header_comment = "\
46$comment\n Acquisition with %d/%d probes at %s\n$end\n";
47
5a8fda15 48static int init(struct output *o)
4c9ffa83
UH
49{
50/* Maximum header length */
51#define MAX_HEADER_LEN 2048
52
53 struct context *ctx;
54 struct probe *probe;
55 GSList *l;
56 uint64_t samplerate;
57 int i, b, num_probes;
25e7d9b1 58 char *c, *samplerate_s;
7aae7462 59 char wbuf[1000], comment[128];
5cca9adb 60 time_t t;
4c9ffa83 61
6b5e3cee 62 if (!(ctx = calloc(1, sizeof(struct context))))
e31b636d 63 return SIGROK_ERR_MALLOC;
4c9ffa83
UH
64 o->internal = ctx;
65 ctx->num_enabled_probes = 0;
66 for (l = o->device->probes; l; l = l->next) {
67 probe = l->data;
68 if (probe->enabled)
69 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
70 }
71
72 ctx->probelist[ctx->num_enabled_probes] = 0;
73 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
74
75 /* TODO: Allow for configuration via o->param. */
76
6b5e3cee
UH
77 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
78 free(ctx);
e31b636d 79 return SIGROK_ERR_MALLOC;
6b5e3cee 80 }
4c9ffa83 81 num_probes = g_slist_length(o->device->probes);
4c9ffa83 82
6b5e3cee 83 comment[0] = '\0';
7aae7462
BV
84 if (o->device->plugin) {
85 /* TODO: Handle num_probes == 0, too many probes, etc. */
6b5e3cee 86 /* TODO: Error handling. */
7aae7462
BV
87 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
88 o->device->plugin_index, DI_CUR_SAMPLERATE));
6b5e3cee
UH
89 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
90 free(ctx->header);
91 free(ctx);
7aae7462 92 return SIGROK_ERR;
6b5e3cee
UH
93 }
94 /* TODO: Handle sprintf() errors. */
95 snprintf(comment, 127, vcd_header_comment,
96 ctx->num_enabled_probes, num_probes, samplerate_s);
7aae7462
BV
97 free(samplerate_s);
98 }
4c9ffa83
UH
99
100 /* Wires / channels */
101 wbuf[0] = '\0';
bc010c05 102 for (i = 0; i < ctx->num_enabled_probes; i++) {
4c9ffa83 103 c = (char *)&wbuf + strlen((char *)&wbuf);
6b5e3cee
UH
104 /* TODO: Needs fixing for very large number of probes. */
105 /* TODO: Handle sprintf() errors. */
bc010c05 106 sprintf(c, "$var wire 1 %c channel%s $end\n",
99c1fc59 107 (char)('!' + i), ctx->probelist[i]);
4c9ffa83
UH
108 }
109
99c1fc59 110 /* TODO: Date: File or signals? Make y/n configurable. */
5cca9adb
UH
111 t = time(NULL);
112 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, ctime(&t),
6b5e3cee
UH
113 PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
114 /* TODO: Handle snprintf() errors. */
4c9ffa83 115
6b5e3cee
UH
116 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
117 free(ctx->header);
118 free(ctx);
e31b636d 119 return SIGROK_ERR_MALLOC;
6b5e3cee 120 }
5a8fda15 121
6b5e3cee 122 return SIGROK_OK;
4c9ffa83
UH
123}
124
125static int event(struct output *o, int event_type, char **data_out,
126 uint64_t *length_out)
127{
128 struct context *ctx;
129 char *outbuf;
130 int outlen;
131
132 ctx = o->internal;
99c1fc59 133 switch (event_type) {
4c9ffa83 134 case DF_TRIGGER:
6b5e3cee 135 /* TODO */
4c9ffa83
UH
136 break;
137 case DF_END:
138 outlen = strlen("$dumpoff\n$end\n");
6b5e3cee 139 if (!(outbuf = malloc(outlen + 1)))
e31b636d 140 return SIGROK_ERR_MALLOC;
6b5e3cee
UH
141 /* TODO: Bug? Drop the + 1? */
142 /* TODO: Handle snprintf() errors. */
bc010c05 143 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
4c9ffa83
UH
144 *data_out = outbuf;
145 *length_out = outlen;
146 free(o->internal);
147 o->internal = NULL;
148 break;
149 }
150
151 return SIGROK_OK;
152}
153
154static int data(struct output *o, char *data_in, uint64_t length_in,
155 char **data_out, uint64_t *length_out)
156{
157 struct context *ctx;
607b58de 158 unsigned int i, outsize;
afc8e4de 159 int p, curbit, prevbit;
4c9ffa83 160 uint64_t sample, prevsample;
086eac7c 161 static uint64_t samplecount = 0;
4c9ffa83
UH
162 char *outbuf, *c;
163
164 ctx = o->internal;
e273a904
HE
165 outsize = 0;
166 if (ctx->header)
167 outsize = strlen(ctx->header);
6b5e3cee
UH
168
169 /* FIXME: Use realloc(). */
170 if (!(outbuf = calloc(1, outsize + 1 + 10000)))
171 return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */
172
173 outbuf[0] = '\0';
4c9ffa83
UH
174 if (ctx->header) {
175 /* The header is still here, this must be the first packet. */
176 strncpy(outbuf, ctx->header, outsize);
177 free(ctx->header);
178 ctx->header = NULL;
4c9ffa83
UH
179 }
180
181 /* TODO: Are disabled probes handled correctly? */
182
607b58de 183 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
086eac7c 184 samplecount++;
607b58de 185 memcpy(&sample, data_in + i, ctx->unitsize);
4c9ffa83
UH
186 for (p = 0; p < ctx->num_enabled_probes; p++) {
187 curbit = (sample & ((uint64_t) (1 << p))) != 0;
607b58de 188 if (i == 0) {
4c9ffa83
UH
189 prevbit = ~curbit;
190 } else {
607b58de 191 memcpy(&prevsample, data_in + i - 1,
99c1fc59
UH
192 ctx->unitsize);
193 prevbit =
194 (prevsample & ((uint64_t) (1 << p))) != 0;
4c9ffa83
UH
195 }
196
607b58de
UH
197 /* VCD only contains deltas/changes. */
198 if (prevbit == curbit)
199 continue;
4c9ffa83 200
607b58de 201 /* FIXME: Only once per sample? */
607b58de 202 c = outbuf + strlen(outbuf);
086eac7c
UH
203 sprintf(c, "#%" PRIu64 "\n%i%c\n", samplecount,
204 curbit, (char)('!' + p));
4c9ffa83
UH
205 }
206
99c1fc59 207 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
4c9ffa83
UH
208 }
209
210 *data_out = outbuf;
211 *length_out = strlen(outbuf);
212
213 return SIGROK_OK;
214}
215
216struct output_format output_vcd = {
217 "vcd",
218 "Value Change Dump (VCD)",
219 init,
220 data,
221 event,
222};