]> sigrok.org Git - libsigrok.git/blame - output/output_vcd.c
Gnuplot output format support.
[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;
56 char *c;
57 char sbuf[10], wbuf[1000];
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
UH
82
83 /* Samplerate string */
84 if (samplerate >= GHZ(1))
85 snprintf(sbuf, 10, "%"PRIu64" GHz", samplerate / 1000000000);
86 else if (samplerate >= MHZ(1))
87 snprintf(sbuf, 10, "%"PRIu64" MHz", samplerate / 1000000);
88 else if (samplerate >= KHZ(1))
89 snprintf(sbuf, 10, "%"PRIu64" KHz", samplerate / 1000);
90 else
91 snprintf(sbuf, 10, "%"PRIu64" Hz", samplerate);
92
93 /* Wires / channels */
94 wbuf[0] = '\0';
95 for (i = 0; i < num_probes; i++) {
96 c = (char *)&wbuf + strlen((char *)&wbuf);
97 sprintf(c, "$var wire 1 %c channel%i $end\n",
98 (char)('!' + i), i);
99 }
100
101 /* TODO: date: File or signals? Make y/n configurable. */
102 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
103 PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
104 (char *)&sbuf, 1, "ns", PACKAGE, (char *)&wbuf);
e31b636d 105 /* TODO: Handle snprintf errors. */
4c9ffa83
UH
106
107 ctx->prevbits = calloc(sizeof(int), num_probes);
e31b636d
UH
108 if (ctx->prevbits == NULL)
109 return SIGROK_ERR_MALLOC;
5a8fda15
UH
110
111 return 0;
4c9ffa83
UH
112}
113
114static int event(struct output *o, int event_type, char **data_out,
115 uint64_t *length_out)
116{
117 struct context *ctx;
118 char *outbuf;
119 int outlen;
120
121 ctx = o->internal;
122 switch(event_type) {
123 case DF_TRIGGER:
124 break;
125 case DF_END:
126 outlen = strlen("$dumpoff\n$end\n");
127 outbuf = malloc(outlen + 1);
e31b636d
UH
128 if (outbuf == NULL)
129 return SIGROK_ERR_MALLOC;
4c9ffa83
UH
130 snprintf(outbuf, outlen, "$dumpoff\n$end\n");
131 *data_out = outbuf;
132 *length_out = outlen;
133 free(o->internal);
134 o->internal = NULL;
135 break;
136 }
137
138 return SIGROK_OK;
139}
140
141static int data(struct output *o, char *data_in, uint64_t length_in,
142 char **data_out, uint64_t *length_out)
143{
144 struct context *ctx;
145 int offset, outsize, p, curbit, prevbit;
146 uint64_t sample, prevsample;
147 char *outbuf, *c;
148
149 ctx = o->internal;
150 outsize = strlen(ctx->header);
151 outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
e31b636d
UH
152 if (outbuf == NULL)
153 return SIGROK_ERR_MALLOC;
4c9ffa83
UH
154 if (ctx->header) {
155 /* The header is still here, this must be the first packet. */
156 strncpy(outbuf, ctx->header, outsize);
157 free(ctx->header);
158 ctx->header = NULL;
159 } else {
160 outbuf[0] = 0;
161 }
162
163 /* TODO: Are disabled probes handled correctly? */
164
165 for (offset = 0; offset <= length_in - ctx->unitsize;
166 offset += ctx->unitsize) {
167 memcpy(&sample, data_in + offset, ctx->unitsize);
168 for (p = 0; p < ctx->num_enabled_probes; p++) {
169 curbit = (sample & ((uint64_t) (1 << p))) != 0;
170 if (offset == 0) {
171 prevbit = ~curbit;
172 } else {
173 memcpy(&prevsample, data_in + offset - 1, ctx->unitsize);
174 prevbit = (prevsample & ((uint64_t) (1 << p))) != 0;
175 }
176
177 if (prevbit != curbit) {
178 /* FIXME: Only once per sample? */
179 c = outbuf + strlen(outbuf);
180 sprintf(c, "#%i\n", offset * 1 /* TODO */);
181
182 c = outbuf + strlen(outbuf);
183 sprintf(c, "%i%c\n", curbit, (char)('!' + p /* FIXME? */));
184 }
185 }
186
187 /* TODO: Do a realloc() here if strlen(outbuf) is almost "full"... */
188 }
189
190 *data_out = outbuf;
191 *length_out = strlen(outbuf);
192
193 return SIGROK_OK;
194}
195
196struct output_format output_vcd = {
197 "vcd",
198 "Value Change Dump (VCD)",
199 init,
200 data,
201 event,
202};