]>
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 | |
2ea1fdf1 | 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
4c9ffa83 UH |
19 | */ |
20 | ||
6ec6c43b | 21 | #include <config.h> |
4c9ffa83 UH |
22 | #include <stdlib.h> |
23 | #include <string.h> | |
24 | #include <glib.h> | |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
45c59c8b | 26 | #include "libsigrok-internal.h" |
4c9ffa83 | 27 | |
3544f848 | 28 | #define LOG_PREFIX "output/vcd" |
a944a84b | 29 | |
4c9ffa83 | 30 | struct context { |
ba7dd8bb | 31 | int num_enabled_channels; |
b050fc48 | 32 | uint8_t *prevsample; |
6cb45d96 | 33 | gboolean header_done; |
4ddea314 | 34 | uint64_t period; |
6cb45d96 | 35 | int *channel_index; |
b33e7d70 | 36 | uint64_t samplerate; |
563080a8 | 37 | uint64_t samplecount; |
4c9ffa83 UH |
38 | }; |
39 | ||
a755b0e1 | 40 | static int init(struct sr_output *o, GHashTable *options) |
4c9ffa83 | 41 | { |
4c9ffa83 | 42 | struct context *ctx; |
ba7dd8bb | 43 | struct sr_channel *ch; |
4c9ffa83 | 44 | GSList *l; |
6cb45d96 | 45 | int num_enabled_channels, i; |
757b8c62 | 46 | |
a755b0e1 BV |
47 | (void)options; |
48 | ||
6cb45d96 | 49 | num_enabled_channels = 0; |
ba7dd8bb UH |
50 | for (l = o->sdi->channels; l; l = l->next) { |
51 | ch = l->data; | |
3f239f08 | 52 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 53 | continue; |
ba7dd8bb | 54 | if (!ch->enabled) |
757b8c62 | 55 | continue; |
6cb45d96 | 56 | num_enabled_channels++; |
4c9ffa83 | 57 | } |
6cb45d96 | 58 | if (num_enabled_channels > 94) { |
ba7dd8bb | 59 | sr_err("VCD only supports 94 channels."); |
e46b8fb1 | 60 | return SR_ERR; |
fbf1ff5d | 61 | } |
4c9ffa83 | 62 | |
6cb45d96 | 63 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 64 | o->priv = ctx; |
6cb45d96 BV |
65 | ctx->num_enabled_channels = num_enabled_channels; |
66 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); | |
67 | ||
68 | /* Once more to map the enabled channels. */ | |
69 | for (i = 0, l = o->sdi->channels; l; l = l->next) { | |
70 | ch = l->data; | |
71 | if (ch->type != SR_CHANNEL_LOGIC) | |
72 | continue; | |
73 | if (!ch->enabled) | |
74 | continue; | |
75 | ctx->channel_index[i++] = ch->index; | |
76 | } | |
77 | ||
78 | return SR_OK; | |
79 | } | |
80 | ||
17c30d05 GS |
81 | /* |
82 | * VCD can only handle 1/10/100 factors in the s to fs range. Find a | |
83 | * suitable timescale which satisfies this resolution constraint, yet | |
84 | * won't result in excessive overhead. | |
85 | */ | |
86 | static uint64_t get_timescale_freq(uint64_t samplerate) | |
87 | { | |
88 | uint64_t timescale; | |
89 | int max_up_scale; | |
90 | ||
91 | /* Go to the next full decade. */ | |
92 | timescale = 1; | |
93 | while (timescale < samplerate) { | |
94 | timescale *= 10; | |
95 | } | |
96 | ||
97 | /* | |
98 | * Avoid loss of precision, go up a few more decades when needed. | |
99 | * For example switch to 10GHz timescale when samplerate is 400MHz. | |
100 | * Stop after at most factor 100 to not loop endlessly for odd | |
101 | * samplerates, yet provide good enough accuracy. | |
102 | */ | |
103 | max_up_scale = 2; | |
104 | while (max_up_scale--) { | |
105 | if (timescale / samplerate * samplerate == timescale) | |
106 | break; | |
107 | timescale *= 10; | |
108 | } | |
109 | ||
110 | return timescale; | |
111 | } | |
112 | ||
a755b0e1 | 113 | static GString *gen_header(const struct sr_output *o) |
6cb45d96 BV |
114 | { |
115 | struct context *ctx; | |
116 | struct sr_channel *ch; | |
117 | GVariant *gvar; | |
118 | GString *header; | |
119 | GSList *l; | |
120 | time_t t; | |
121 | int num_channels, i; | |
122 | char *samplerate_s, *frequency_s, *timestamp; | |
123 | ||
d686c5ec | 124 | ctx = o->priv; |
6cb45d96 | 125 | header = g_string_sized_new(512); |
ba7dd8bb | 126 | num_channels = g_slist_length(o->sdi->channels); |
4c9ffa83 | 127 | |
fbf1ff5d BV |
128 | /* timestamp */ |
129 | t = time(NULL); | |
133a37bf | 130 | timestamp = g_strdup(ctime(&t)); |
405b9c10 | 131 | timestamp[strlen(timestamp) - 1] = 0; |
6cb45d96 | 132 | g_string_printf(header, "$date %s $end\n", timestamp); |
133a37bf | 133 | g_free(timestamp); |
4c9ffa83 | 134 | |
fbf1ff5d | 135 | /* generator */ |
6cb45d96 | 136 | g_string_append_printf(header, "$version %s %s $end\n", |
2868bca3 | 137 | PACKAGE_NAME, sr_package_version_string_get()); |
6cb45d96 BV |
138 | g_string_append_printf(header, "$comment\n Acquisition with " |
139 | "%d/%d channels", ctx->num_enabled_channels, num_channels); | |
140 | ||
141 | if (ctx->samplerate == 0) { | |
142 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
143 | &gvar) == SR_OK) { | |
144 | ctx->samplerate = g_variant_get_uint64(gvar); | |
145 | g_variant_unref(gvar); | |
6b5e3cee | 146 | } |
6cb45d96 BV |
147 | } |
148 | if (ctx->samplerate != 0) { | |
149 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
150 | g_string_append_printf(header, " at %s", samplerate_s); | |
133a37bf | 151 | g_free(samplerate_s); |
7aae7462 | 152 | } |
6cb45d96 | 153 | g_string_append_printf(header, "\n$end\n"); |
4c9ffa83 | 154 | |
fbf1ff5d | 155 | /* timescale */ |
17c30d05 | 156 | ctx->period = get_timescale_freq(ctx->samplerate); |
6984cfb2 | 157 | frequency_s = sr_period_string(1, ctx->period); |
6cb45d96 | 158 | g_string_append_printf(header, "$timescale %s $end\n", frequency_s); |
133a37bf | 159 | g_free(frequency_s); |
fbf1ff5d BV |
160 | |
161 | /* scope */ | |
b9eb8e1a | 162 | g_string_append_printf(header, "$scope module %s $end\n", PACKAGE_NAME); |
fbf1ff5d | 163 | |
4c9ffa83 | 164 | /* Wires / channels */ |
a299a954 | 165 | for (i = 0, l = o->sdi->channels; l; l = l->next) { |
ba7dd8bb | 166 | ch = l->data; |
3f239f08 | 167 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 168 | continue; |
ba7dd8bb | 169 | if (!ch->enabled) |
d601c0e9 | 170 | continue; |
6cb45d96 | 171 | g_string_append_printf(header, "$var wire 1 %c %s $end\n", |
ba7dd8bb | 172 | (char)('!' + i), ch->name); |
a299a954 | 173 | i++; |
4c9ffa83 UH |
174 | } |
175 | ||
6cb45d96 | 176 | g_string_append(header, "$upscope $end\n$enddefinitions $end\n"); |
4c9ffa83 | 177 | |
6cb45d96 | 178 | return header; |
4c9ffa83 UH |
179 | } |
180 | ||
a755b0e1 | 181 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 182 | GString **out) |
4c9ffa83 | 183 | { |
6cb45d96 | 184 | const struct sr_datafeed_meta *meta; |
d5585e32 | 185 | const struct sr_datafeed_logic *logic; |
6cb45d96 BV |
186 | const struct sr_config *src; |
187 | GSList *l; | |
4c9ffa83 | 188 | struct context *ctx; |
fbf1ff5d | 189 | unsigned int i; |
ba6568c5 | 190 | int p, curbit, prevbit, index; |
b050fc48 | 191 | uint8_t *sample; |
2b6363b4 | 192 | gboolean timestamp_written; |
4c9ffa83 | 193 | |
17f63de6 | 194 | *out = NULL; |
d686c5ec | 195 | if (!o || !o->priv) |
191c7e5f | 196 | return SR_ERR_BUG; |
d686c5ec | 197 | ctx = o->priv; |
d5585e32 | 198 | |
6cb45d96 BV |
199 | switch (packet->type) { |
200 | case SR_DF_META: | |
201 | meta = packet->payload; | |
202 | for (l = meta->config; l; l = l->next) { | |
203 | src = l->data; | |
204 | if (src->key != SR_CONF_SAMPLERATE) | |
205 | continue; | |
206 | ctx->samplerate = g_variant_get_uint64(src->data); | |
207 | } | |
208 | break; | |
209 | case SR_DF_LOGIC: | |
210 | logic = packet->payload; | |
211 | ||
212 | if (!ctx->header_done) { | |
213 | *out = gen_header(o); | |
214 | ctx->header_done = TRUE; | |
215 | } else { | |
216 | *out = g_string_sized_new(512); | |
217 | } | |
2b78ffea | 218 | |
6cb45d96 BV |
219 | if (!ctx->prevsample) { |
220 | /* Can't allocate this until we know the stream's unitsize. */ | |
221 | ctx->prevsample = g_malloc0(logic->unitsize); | |
2b78ffea | 222 | } |
2b78ffea | 223 | |
6cb45d96 BV |
224 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
225 | sample = logic->data + i; | |
226 | timestamp_written = FALSE; | |
08b488b8 | 227 | |
6cb45d96 | 228 | for (p = 0; p < ctx->num_enabled_channels; p++) { |
aa097948 GS |
229 | /* |
230 | * TODO Check whether the mapping from | |
231 | * data image positions to channel numbers | |
232 | * is required. Experiments suggest that | |
233 | * the data image "is dense", and packs | |
234 | * bits of enabled channels, and leaves no | |
235 | * room for positions of disabled channels. | |
236 | */ | |
237 | /* index = ctx->channel_index[p]; */ | |
238 | index = p; | |
2b6363b4 | 239 | |
6cb45d96 BV |
240 | curbit = ((unsigned)sample[index / 8] |
241 | >> (index % 8)) & 1; | |
242 | prevbit = ((unsigned)ctx->prevsample[index / 8] | |
243 | >> (index % 8)) & 1; | |
fbe2f794 | 244 | |
6cb45d96 BV |
245 | /* VCD only contains deltas/changes of signals. */ |
246 | if (prevbit == curbit && ctx->samplecount > 0) | |
247 | continue; | |
4c9ffa83 | 248 | |
6cb45d96 BV |
249 | /* Output timestamp of subsequent signal changes. */ |
250 | if (!timestamp_written) | |
251 | g_string_append_printf(*out, "#%.0f", | |
252 | (double)ctx->samplecount / | |
253 | ctx->samplerate * ctx->period); | |
2b6363b4 | 254 | |
6cb45d96 BV |
255 | /* Output which signal changed to which value. */ |
256 | g_string_append_c(*out, ' '); | |
257 | g_string_append_c(*out, '0' + curbit); | |
258 | g_string_append_c(*out, '!' + p); | |
2b6363b4 | 259 | |
6cb45d96 BV |
260 | timestamp_written = TRUE; |
261 | } | |
08b488b8 | 262 | |
6cb45d96 BV |
263 | if (timestamp_written) |
264 | g_string_append_c(*out, '\n'); | |
2b6363b4 | 265 | |
6cb45d96 BV |
266 | ctx->samplecount++; |
267 | memcpy(ctx->prevsample, sample, logic->unitsize); | |
268 | } | |
269 | break; | |
270 | case SR_DF_END: | |
271 | /* Write final timestamp as length indicator. */ | |
272 | *out = g_string_sized_new(512); | |
273 | g_string_printf(*out, "#%.0f\n", | |
274 | (double)ctx->samplecount / ctx->samplerate * ctx->period); | |
275 | break; | |
4c9ffa83 UH |
276 | } |
277 | ||
17f63de6 | 278 | return SR_OK; |
d5585e32 BV |
279 | } |
280 | ||
281 | static int cleanup(struct sr_output *o) | |
282 | { | |
283 | struct context *ctx; | |
284 | ||
d686c5ec | 285 | if (!o || !o->priv) |
d5585e32 BV |
286 | return SR_ERR_ARG; |
287 | ||
d686c5ec | 288 | ctx = o->priv; |
2b78ffea | 289 | g_free(ctx->prevsample); |
6cb45d96 | 290 | g_free(ctx->channel_index); |
d5585e32 | 291 | g_free(ctx); |
4c9ffa83 | 292 | |
e46b8fb1 | 293 | return SR_OK; |
4c9ffa83 UH |
294 | } |
295 | ||
a755b0e1 | 296 | struct sr_output_module output_vcd = { |
cdb3573c | 297 | .id = "vcd", |
a755b0e1 | 298 | .name = "VCD", |
b20eb520 | 299 | .desc = "Value Change Dump data", |
8a174d23 | 300 | .exts = (const char*[]){"vcd", NULL}, |
3cd4b381 | 301 | .flags = 0, |
a755b0e1 | 302 | .options = NULL, |
d494a4aa | 303 | .init = init, |
17f63de6 | 304 | .receive = receive, |
bbe6e336 | 305 | .cleanup = cleanup, |
4c9ffa83 | 306 | }; |