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