]>
Commit | Line | Data |
---|---|---|
4c9ffa83 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
4c9ffa83 UH |
3 | * |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
13d8e03c | 5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
4c9ffa83 UH |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <glib.h> | |
545f9786 | 25 | #include "config.h" /* Needed for PACKAGE and others. */ |
45c59c8b BV |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
4c9ffa83 | 28 | |
3544f848 | 29 | #define LOG_PREFIX "output/vcd" |
a944a84b | 30 | |
4c9ffa83 | 31 | struct context { |
ba7dd8bb UH |
32 | int num_enabled_channels; |
33 | GArray *channelindices; | |
b050fc48 | 34 | uint8_t *prevsample; |
6cb45d96 | 35 | gboolean header_done; |
b33e7d70 | 36 | int period; |
6cb45d96 | 37 | int *channel_index; |
b33e7d70 | 38 | uint64_t samplerate; |
563080a8 | 39 | uint64_t samplecount; |
4c9ffa83 UH |
40 | }; |
41 | ||
191c7e5f | 42 | static const char *const vcd_header_comment = |
ba7dd8bb | 43 | "$comment\n Acquisition with %d/%d channels at %s\n$end\n"; |
7aae7462 | 44 | |
f50f3f40 | 45 | static int init(struct sr_output *o) |
4c9ffa83 | 46 | { |
4c9ffa83 | 47 | struct context *ctx; |
ba7dd8bb | 48 | struct sr_channel *ch; |
4c9ffa83 | 49 | GSList *l; |
6cb45d96 | 50 | int num_enabled_channels, i; |
757b8c62 | 51 | |
6cb45d96 | 52 | num_enabled_channels = 0; |
ba7dd8bb UH |
53 | for (l = o->sdi->channels; l; l = l->next) { |
54 | ch = l->data; | |
3f239f08 | 55 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 56 | continue; |
ba7dd8bb | 57 | if (!ch->enabled) |
757b8c62 | 58 | continue; |
6cb45d96 | 59 | num_enabled_channels++; |
4c9ffa83 | 60 | } |
6cb45d96 | 61 | if (num_enabled_channels > 94) { |
ba7dd8bb | 62 | sr_err("VCD only supports 94 channels."); |
e46b8fb1 | 63 | return SR_ERR; |
fbf1ff5d | 64 | } |
4c9ffa83 | 65 | |
6cb45d96 BV |
66 | ctx = g_malloc0(sizeof(struct context)); |
67 | o->internal = ctx; | |
68 | ctx->num_enabled_channels = num_enabled_channels; | |
69 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); | |
70 | ||
71 | /* Once more to map the enabled channels. */ | |
72 | for (i = 0, l = o->sdi->channels; l; l = l->next) { | |
73 | ch = l->data; | |
74 | if (ch->type != SR_CHANNEL_LOGIC) | |
75 | continue; | |
76 | if (!ch->enabled) | |
77 | continue; | |
78 | ctx->channel_index[i++] = ch->index; | |
79 | } | |
80 | ||
81 | return SR_OK; | |
82 | } | |
83 | ||
84 | static GString *gen_header(struct sr_output *o) | |
85 | { | |
86 | struct context *ctx; | |
87 | struct sr_channel *ch; | |
88 | GVariant *gvar; | |
89 | GString *header; | |
90 | GSList *l; | |
91 | time_t t; | |
92 | int num_channels, i; | |
93 | char *samplerate_s, *frequency_s, *timestamp; | |
94 | ||
95 | ctx = o->internal; | |
96 | header = g_string_sized_new(512); | |
ba7dd8bb | 97 | num_channels = g_slist_length(o->sdi->channels); |
4c9ffa83 | 98 | |
fbf1ff5d BV |
99 | /* timestamp */ |
100 | t = time(NULL); | |
133a37bf | 101 | timestamp = g_strdup(ctime(&t)); |
fbf1ff5d | 102 | timestamp[strlen(timestamp)-1] = 0; |
6cb45d96 | 103 | g_string_printf(header, "$date %s $end\n", timestamp); |
133a37bf | 104 | g_free(timestamp); |
4c9ffa83 | 105 | |
fbf1ff5d | 106 | /* generator */ |
6cb45d96 | 107 | g_string_append_printf(header, "$version %s %s $end\n", |
fbf1ff5d | 108 | PACKAGE, PACKAGE_VERSION); |
6cb45d96 BV |
109 | g_string_append_printf(header, "$comment\n Acquisition with " |
110 | "%d/%d channels", ctx->num_enabled_channels, num_channels); | |
111 | ||
112 | if (ctx->samplerate == 0) { | |
113 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
114 | &gvar) == SR_OK) { | |
115 | ctx->samplerate = g_variant_get_uint64(gvar); | |
116 | g_variant_unref(gvar); | |
6b5e3cee | 117 | } |
6cb45d96 BV |
118 | } |
119 | if (ctx->samplerate != 0) { | |
120 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
121 | g_string_append_printf(header, " at %s", samplerate_s); | |
133a37bf | 122 | g_free(samplerate_s); |
7aae7462 | 123 | } |
6cb45d96 | 124 | g_string_append_printf(header, "\n$end\n"); |
4c9ffa83 | 125 | |
fbf1ff5d BV |
126 | /* timescale */ |
127 | /* VCD can only handle 1/10/100 (s - fs), so scale up first */ | |
59df0c77 UH |
128 | if (ctx->samplerate > SR_MHZ(1)) |
129 | ctx->period = SR_GHZ(1); | |
130 | else if (ctx->samplerate > SR_KHZ(1)) | |
131 | ctx->period = SR_MHZ(1); | |
fbf1ff5d | 132 | else |
59df0c77 | 133 | ctx->period = SR_KHZ(1); |
6cb45d96 BV |
134 | frequency_s = sr_period_string(ctx->period); |
135 | g_string_append_printf(header, "$timescale %s $end\n", frequency_s); | |
133a37bf | 136 | g_free(frequency_s); |
fbf1ff5d BV |
137 | |
138 | /* scope */ | |
6cb45d96 | 139 | g_string_append_printf(header, "$scope module %s $end\n", PACKAGE); |
fbf1ff5d | 140 | |
4c9ffa83 | 141 | /* Wires / channels */ |
ba7dd8bb UH |
142 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { |
143 | ch = l->data; | |
3f239f08 | 144 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 145 | continue; |
ba7dd8bb | 146 | if (!ch->enabled) |
d601c0e9 | 147 | continue; |
6cb45d96 | 148 | g_string_append_printf(header, "$var wire 1 %c %s $end\n", |
ba7dd8bb | 149 | (char)('!' + i), ch->name); |
4c9ffa83 UH |
150 | } |
151 | ||
6cb45d96 | 152 | g_string_append(header, "$upscope $end\n$enddefinitions $end\n"); |
4c9ffa83 | 153 | |
6cb45d96 | 154 | return header; |
4c9ffa83 UH |
155 | } |
156 | ||
dba3e682 BV |
157 | static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet, |
158 | GString **out) | |
4c9ffa83 | 159 | { |
6cb45d96 | 160 | const struct sr_datafeed_meta *meta; |
d5585e32 | 161 | const struct sr_datafeed_logic *logic; |
6cb45d96 BV |
162 | const struct sr_config *src; |
163 | GSList *l; | |
4c9ffa83 | 164 | struct context *ctx; |
fbf1ff5d | 165 | unsigned int i; |
ba6568c5 | 166 | int p, curbit, prevbit, index; |
b050fc48 | 167 | uint8_t *sample; |
2b6363b4 | 168 | gboolean timestamp_written; |
4c9ffa83 | 169 | |
17f63de6 | 170 | *out = NULL; |
d5585e32 | 171 | if (!o || !o->internal) |
191c7e5f | 172 | return SR_ERR_BUG; |
4c9ffa83 | 173 | ctx = o->internal; |
d5585e32 | 174 | |
6cb45d96 BV |
175 | switch (packet->type) { |
176 | case SR_DF_META: | |
177 | meta = packet->payload; | |
178 | for (l = meta->config; l; l = l->next) { | |
179 | src = l->data; | |
180 | if (src->key != SR_CONF_SAMPLERATE) | |
181 | continue; | |
182 | ctx->samplerate = g_variant_get_uint64(src->data); | |
183 | } | |
184 | break; | |
185 | case SR_DF_LOGIC: | |
186 | logic = packet->payload; | |
187 | ||
188 | if (!ctx->header_done) { | |
189 | *out = gen_header(o); | |
190 | ctx->header_done = TRUE; | |
191 | } else { | |
192 | *out = g_string_sized_new(512); | |
193 | } | |
2b78ffea | 194 | |
6cb45d96 BV |
195 | if (!ctx->prevsample) { |
196 | /* Can't allocate this until we know the stream's unitsize. */ | |
197 | ctx->prevsample = g_malloc0(logic->unitsize); | |
2b78ffea | 198 | } |
2b78ffea | 199 | |
6cb45d96 BV |
200 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
201 | sample = logic->data + i; | |
202 | timestamp_written = FALSE; | |
08b488b8 | 203 | |
6cb45d96 BV |
204 | for (p = 0; p < ctx->num_enabled_channels; p++) { |
205 | index = ctx->channel_index[p]; | |
2b6363b4 | 206 | |
6cb45d96 BV |
207 | curbit = ((unsigned)sample[index / 8] |
208 | >> (index % 8)) & 1; | |
209 | prevbit = ((unsigned)ctx->prevsample[index / 8] | |
210 | >> (index % 8)) & 1; | |
fbe2f794 | 211 | |
6cb45d96 BV |
212 | /* VCD only contains deltas/changes of signals. */ |
213 | if (prevbit == curbit && ctx->samplecount > 0) | |
214 | continue; | |
4c9ffa83 | 215 | |
6cb45d96 BV |
216 | /* Output timestamp of subsequent signal changes. */ |
217 | if (!timestamp_written) | |
218 | g_string_append_printf(*out, "#%.0f", | |
219 | (double)ctx->samplecount / | |
220 | ctx->samplerate * ctx->period); | |
2b6363b4 | 221 | |
6cb45d96 BV |
222 | /* Output which signal changed to which value. */ |
223 | g_string_append_c(*out, ' '); | |
224 | g_string_append_c(*out, '0' + curbit); | |
225 | g_string_append_c(*out, '!' + p); | |
2b6363b4 | 226 | |
6cb45d96 BV |
227 | timestamp_written = TRUE; |
228 | } | |
08b488b8 | 229 | |
6cb45d96 BV |
230 | if (timestamp_written) |
231 | g_string_append_c(*out, '\n'); | |
2b6363b4 | 232 | |
6cb45d96 BV |
233 | ctx->samplecount++; |
234 | memcpy(ctx->prevsample, sample, logic->unitsize); | |
235 | } | |
236 | break; | |
237 | case SR_DF_END: | |
238 | /* Write final timestamp as length indicator. */ | |
239 | *out = g_string_sized_new(512); | |
240 | g_string_printf(*out, "#%.0f\n", | |
241 | (double)ctx->samplecount / ctx->samplerate * ctx->period); | |
242 | break; | |
4c9ffa83 UH |
243 | } |
244 | ||
17f63de6 | 245 | return SR_OK; |
d5585e32 BV |
246 | } |
247 | ||
248 | static int cleanup(struct sr_output *o) | |
249 | { | |
250 | struct context *ctx; | |
251 | ||
252 | if (!o || !o->internal) | |
253 | return SR_ERR_ARG; | |
254 | ||
255 | ctx = o->internal; | |
2b78ffea | 256 | g_free(ctx->prevsample); |
6cb45d96 | 257 | g_free(ctx->channel_index); |
d5585e32 | 258 | g_free(ctx); |
4c9ffa83 | 259 | |
e46b8fb1 | 260 | return SR_OK; |
4c9ffa83 UH |
261 | } |
262 | ||
f50f3f40 | 263 | struct sr_output_format output_vcd = { |
cdb3573c | 264 | .id = "vcd", |
d494a4aa | 265 | .description = "Value Change Dump (VCD)", |
d494a4aa | 266 | .init = init, |
17f63de6 | 267 | .receive = receive, |
bbe6e336 | 268 | .cleanup = cleanup, |
4c9ffa83 | 269 | }; |