]> sigrok.org Git - libsigrok.git/blame - output/output_vcd.c
zeroplus: Fix compiler warnings.
[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 = "\
36$date\n %s\n$end\n\
37$version\n %s\n$end\n\
38$comment\n Acquisition with %d/%d probes at %s\n$end\n\
39$timescale\n %i %s\n$end\n\
40$scope module %s $end\n\
41%s\
42$upscope $end\n\
43$enddefinitions $end\n\
44$dumpvars\n";
45
5a8fda15 46static int init(struct output *o)
4c9ffa83
UH
47{
48/* Maximum header length */
49#define MAX_HEADER_LEN 2048
50
51 struct context *ctx;
52 struct probe *probe;
53 GSList *l;
54 uint64_t samplerate;
55 int i, b, num_probes;
25e7d9b1
UH
56 char *c, *samplerate_s;
57 char wbuf[1000];
4c9ffa83
UH
58
59 ctx = malloc(sizeof(struct context));
e31b636d
UH
60 if (ctx == NULL)
61 return SIGROK_ERR_MALLOC;
4c9ffa83
UH
62 o->internal = ctx;
63 ctx->num_enabled_probes = 0;
64 for (l = o->device->probes; l; l = l->next) {
65 probe = l->data;
66 if (probe->enabled)
67 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
68 }
69
70 ctx->probelist[ctx->num_enabled_probes] = 0;
71 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
72
73 /* TODO: Allow for configuration via o->param. */
74
75 ctx->header = calloc(1, MAX_HEADER_LEN + 1);
e31b636d
UH
76 if (ctx->header == NULL)
77 return SIGROK_ERR_MALLOC;
4c9ffa83 78 num_probes = g_slist_length(o->device->probes);
e31b636d 79 /* TODO: Handle num_probes == 0, too many probes, etc. */
4c9ffa83 80 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
4c100f32 81 o->device->plugin_index, DI_CUR_SAMPLERATE));
4c9ffa83 82
25e7d9b1
UH
83 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
84 return -1; // FIXME
4c9ffa83
UH
85
86 /* Wires / channels */
87 wbuf[0] = '\0';
bc010c05 88 for (i = 0; i < ctx->num_enabled_probes; i++) {
4c9ffa83 89 c = (char *)&wbuf + strlen((char *)&wbuf);
bc010c05
UH
90 sprintf(c, "$var wire 1 %c channel%s $end\n",
91 (char)('!' + i), ctx->probelist[i]);
4c9ffa83
UH
92 }
93
94 /* TODO: date: File or signals? Make y/n configurable. */
95 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
96 PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
25e7d9b1 97 samplerate_s, 1, "ns", PACKAGE, (char *)&wbuf);
e31b636d 98 /* TODO: Handle snprintf errors. */
4c9ffa83 99
25e7d9b1
UH
100 free(samplerate_s);
101
4c9ffa83 102 ctx->prevbits = calloc(sizeof(int), num_probes);
e31b636d
UH
103 if (ctx->prevbits == NULL)
104 return SIGROK_ERR_MALLOC;
5a8fda15
UH
105
106 return 0;
4c9ffa83
UH
107}
108
109static int event(struct output *o, int event_type, char **data_out,
110 uint64_t *length_out)
111{
112 struct context *ctx;
113 char *outbuf;
114 int outlen;
115
116 ctx = o->internal;
117 switch(event_type) {
118 case DF_TRIGGER:
119 break;
120 case DF_END:
121 outlen = strlen("$dumpoff\n$end\n");
122 outbuf = malloc(outlen + 1);
e31b636d
UH
123 if (outbuf == NULL)
124 return SIGROK_ERR_MALLOC;
bc010c05 125 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
4c9ffa83
UH
126 *data_out = outbuf;
127 *length_out = outlen;
128 free(o->internal);
129 o->internal = NULL;
130 break;
131 }
132
133 return SIGROK_OK;
134}
135
136static int data(struct output *o, char *data_in, uint64_t length_in,
137 char **data_out, uint64_t *length_out)
138{
139 struct context *ctx;
140 int offset, outsize, p, curbit, prevbit;
141 uint64_t sample, prevsample;
142 char *outbuf, *c;
143
144 ctx = o->internal;
145 outsize = strlen(ctx->header);
146 outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
e31b636d
UH
147 if (outbuf == NULL)
148 return SIGROK_ERR_MALLOC;
4c9ffa83
UH
149 if (ctx->header) {
150 /* The header is still here, this must be the first packet. */
151 strncpy(outbuf, ctx->header, outsize);
152 free(ctx->header);
153 ctx->header = NULL;
154 } else {
155 outbuf[0] = 0;
156 }
157
158 /* TODO: Are disabled probes handled correctly? */
159
160 for (offset = 0; offset <= length_in - ctx->unitsize;
161 offset += ctx->unitsize) {
162 memcpy(&sample, data_in + offset, ctx->unitsize);
163 for (p = 0; p < ctx->num_enabled_probes; p++) {
164 curbit = (sample & ((uint64_t) (1 << p))) != 0;
165 if (offset == 0) {
166 prevbit = ~curbit;
167 } else {
168 memcpy(&prevsample, data_in + offset - 1, ctx->unitsize);
169 prevbit = (prevsample & ((uint64_t) (1 << p))) != 0;
170 }
171
172 if (prevbit != curbit) {
173 /* FIXME: Only once per sample? */
174 c = outbuf + strlen(outbuf);
175 sprintf(c, "#%i\n", offset * 1 /* TODO */);
176
177 c = outbuf + strlen(outbuf);
178 sprintf(c, "%i%c\n", curbit, (char)('!' + p /* FIXME? */));
179 }
180 }
181
182 /* TODO: Do a realloc() here if strlen(outbuf) is almost "full"... */
183 }
184
185 *data_out = outbuf;
186 *length_out = strlen(outbuf);
187
188 return SIGROK_OK;
189}
190
191struct output_format output_vcd = {
192 "vcd",
193 "Value Change Dump (VCD)",
194 init,
195 data,
196 event,
197};