]> sigrok.org Git - libsigrok.git/blame - output/output_vcd.c
input: use dummy device when loading from file
[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 83 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
99c1fc59 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 90 sprintf(c, "$var wire 1 %c channel%s $end\n",
99c1fc59 91 (char)('!' + i), ctx->probelist[i]);
4c9ffa83
UH
92 }
93
99c1fc59 94 /* TODO: Date: File or signals? Make y/n configurable. */
4c9ffa83
UH
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;
99c1fc59 117 switch (event_type) {
4c9ffa83
UH
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;
afc8e4de
UH
140 unsigned int offset, outsize;
141 int p, curbit, prevbit;
4c9ffa83
UH
142 uint64_t sample, prevsample;
143 char *outbuf, *c;
144
145 ctx = o->internal;
e273a904
HE
146 outsize = 0;
147 if (ctx->header)
148 outsize = strlen(ctx->header);
99c1fc59 149 outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
e31b636d
UH
150 if (outbuf == NULL)
151 return SIGROK_ERR_MALLOC;
4c9ffa83
UH
152 if (ctx->header) {
153 /* The header is still here, this must be the first packet. */
154 strncpy(outbuf, ctx->header, outsize);
155 free(ctx->header);
156 ctx->header = NULL;
157 } else {
158 outbuf[0] = 0;
159 }
160
161 /* TODO: Are disabled probes handled correctly? */
162
163 for (offset = 0; offset <= length_in - ctx->unitsize;
99c1fc59 164 offset += ctx->unitsize) {
4c9ffa83
UH
165 memcpy(&sample, data_in + offset, ctx->unitsize);
166 for (p = 0; p < ctx->num_enabled_probes; p++) {
167 curbit = (sample & ((uint64_t) (1 << p))) != 0;
168 if (offset == 0) {
169 prevbit = ~curbit;
170 } else {
99c1fc59
UH
171 memcpy(&prevsample, data_in + offset - 1,
172 ctx->unitsize);
173 prevbit =
174 (prevsample & ((uint64_t) (1 << p))) != 0;
4c9ffa83
UH
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);
99c1fc59
UH
183 sprintf(c, "%i%c\n", curbit,
184 (char)('!' + p /* FIXME? */));
4c9ffa83
UH
185 }
186 }
187
99c1fc59 188 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
4c9ffa83
UH
189 }
190
191 *data_out = outbuf;
192 *length_out = strlen(outbuf);
193
194 return SIGROK_OK;
195}
196
197struct output_format output_vcd = {
198 "vcd",
199 "Value Change Dump (VCD)",
200 init,
201 data,
202 event,
203};