]>
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 | |
a755b0e1 | 45 | static int init(struct sr_output *o, GHashTable *options) |
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 | |
a755b0e1 BV |
52 | (void)options; |
53 | ||
6cb45d96 | 54 | num_enabled_channels = 0; |
ba7dd8bb UH |
55 | for (l = o->sdi->channels; l; l = l->next) { |
56 | ch = l->data; | |
3f239f08 | 57 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 58 | continue; |
ba7dd8bb | 59 | if (!ch->enabled) |
757b8c62 | 60 | continue; |
6cb45d96 | 61 | num_enabled_channels++; |
4c9ffa83 | 62 | } |
6cb45d96 | 63 | if (num_enabled_channels > 94) { |
ba7dd8bb | 64 | sr_err("VCD only supports 94 channels."); |
e46b8fb1 | 65 | return SR_ERR; |
fbf1ff5d | 66 | } |
4c9ffa83 | 67 | |
6cb45d96 BV |
68 | ctx = g_malloc0(sizeof(struct context)); |
69 | o->internal = ctx; | |
70 | ctx->num_enabled_channels = num_enabled_channels; | |
71 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); | |
72 | ||
73 | /* Once more to map the enabled channels. */ | |
74 | for (i = 0, l = o->sdi->channels; l; l = l->next) { | |
75 | ch = l->data; | |
76 | if (ch->type != SR_CHANNEL_LOGIC) | |
77 | continue; | |
78 | if (!ch->enabled) | |
79 | continue; | |
80 | ctx->channel_index[i++] = ch->index; | |
81 | } | |
82 | ||
83 | return SR_OK; | |
84 | } | |
85 | ||
a755b0e1 | 86 | static GString *gen_header(const struct sr_output *o) |
6cb45d96 BV |
87 | { |
88 | struct context *ctx; | |
89 | struct sr_channel *ch; | |
90 | GVariant *gvar; | |
91 | GString *header; | |
92 | GSList *l; | |
93 | time_t t; | |
94 | int num_channels, i; | |
95 | char *samplerate_s, *frequency_s, *timestamp; | |
96 | ||
97 | ctx = o->internal; | |
98 | header = g_string_sized_new(512); | |
ba7dd8bb | 99 | num_channels = g_slist_length(o->sdi->channels); |
4c9ffa83 | 100 | |
fbf1ff5d BV |
101 | /* timestamp */ |
102 | t = time(NULL); | |
133a37bf | 103 | timestamp = g_strdup(ctime(&t)); |
fbf1ff5d | 104 | timestamp[strlen(timestamp)-1] = 0; |
6cb45d96 | 105 | g_string_printf(header, "$date %s $end\n", timestamp); |
133a37bf | 106 | g_free(timestamp); |
4c9ffa83 | 107 | |
fbf1ff5d | 108 | /* generator */ |
6cb45d96 | 109 | g_string_append_printf(header, "$version %s %s $end\n", |
fbf1ff5d | 110 | PACKAGE, PACKAGE_VERSION); |
6cb45d96 BV |
111 | g_string_append_printf(header, "$comment\n Acquisition with " |
112 | "%d/%d channels", ctx->num_enabled_channels, num_channels); | |
113 | ||
114 | if (ctx->samplerate == 0) { | |
115 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
116 | &gvar) == SR_OK) { | |
117 | ctx->samplerate = g_variant_get_uint64(gvar); | |
118 | g_variant_unref(gvar); | |
6b5e3cee | 119 | } |
6cb45d96 BV |
120 | } |
121 | if (ctx->samplerate != 0) { | |
122 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
123 | g_string_append_printf(header, " at %s", samplerate_s); | |
133a37bf | 124 | g_free(samplerate_s); |
7aae7462 | 125 | } |
6cb45d96 | 126 | g_string_append_printf(header, "\n$end\n"); |
4c9ffa83 | 127 | |
fbf1ff5d BV |
128 | /* timescale */ |
129 | /* VCD can only handle 1/10/100 (s - fs), so scale up first */ | |
59df0c77 UH |
130 | if (ctx->samplerate > SR_MHZ(1)) |
131 | ctx->period = SR_GHZ(1); | |
132 | else if (ctx->samplerate > SR_KHZ(1)) | |
133 | ctx->period = SR_MHZ(1); | |
fbf1ff5d | 134 | else |
59df0c77 | 135 | ctx->period = SR_KHZ(1); |
6cb45d96 BV |
136 | frequency_s = sr_period_string(ctx->period); |
137 | g_string_append_printf(header, "$timescale %s $end\n", frequency_s); | |
133a37bf | 138 | g_free(frequency_s); |
fbf1ff5d BV |
139 | |
140 | /* scope */ | |
6cb45d96 | 141 | g_string_append_printf(header, "$scope module %s $end\n", PACKAGE); |
fbf1ff5d | 142 | |
4c9ffa83 | 143 | /* Wires / channels */ |
ba7dd8bb UH |
144 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { |
145 | ch = l->data; | |
3f239f08 | 146 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 147 | continue; |
ba7dd8bb | 148 | if (!ch->enabled) |
d601c0e9 | 149 | continue; |
6cb45d96 | 150 | g_string_append_printf(header, "$var wire 1 %c %s $end\n", |
ba7dd8bb | 151 | (char)('!' + i), ch->name); |
4c9ffa83 UH |
152 | } |
153 | ||
6cb45d96 | 154 | g_string_append(header, "$upscope $end\n$enddefinitions $end\n"); |
4c9ffa83 | 155 | |
6cb45d96 | 156 | return header; |
4c9ffa83 UH |
157 | } |
158 | ||
a755b0e1 | 159 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 160 | GString **out) |
4c9ffa83 | 161 | { |
6cb45d96 | 162 | const struct sr_datafeed_meta *meta; |
d5585e32 | 163 | const struct sr_datafeed_logic *logic; |
6cb45d96 BV |
164 | const struct sr_config *src; |
165 | GSList *l; | |
4c9ffa83 | 166 | struct context *ctx; |
fbf1ff5d | 167 | unsigned int i; |
ba6568c5 | 168 | int p, curbit, prevbit, index; |
b050fc48 | 169 | uint8_t *sample; |
2b6363b4 | 170 | gboolean timestamp_written; |
4c9ffa83 | 171 | |
17f63de6 | 172 | *out = NULL; |
d5585e32 | 173 | if (!o || !o->internal) |
191c7e5f | 174 | return SR_ERR_BUG; |
4c9ffa83 | 175 | ctx = o->internal; |
d5585e32 | 176 | |
6cb45d96 BV |
177 | switch (packet->type) { |
178 | case SR_DF_META: | |
179 | meta = packet->payload; | |
180 | for (l = meta->config; l; l = l->next) { | |
181 | src = l->data; | |
182 | if (src->key != SR_CONF_SAMPLERATE) | |
183 | continue; | |
184 | ctx->samplerate = g_variant_get_uint64(src->data); | |
185 | } | |
186 | break; | |
187 | case SR_DF_LOGIC: | |
188 | logic = packet->payload; | |
189 | ||
190 | if (!ctx->header_done) { | |
191 | *out = gen_header(o); | |
192 | ctx->header_done = TRUE; | |
193 | } else { | |
194 | *out = g_string_sized_new(512); | |
195 | } | |
2b78ffea | 196 | |
6cb45d96 BV |
197 | if (!ctx->prevsample) { |
198 | /* Can't allocate this until we know the stream's unitsize. */ | |
199 | ctx->prevsample = g_malloc0(logic->unitsize); | |
2b78ffea | 200 | } |
2b78ffea | 201 | |
6cb45d96 BV |
202 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
203 | sample = logic->data + i; | |
204 | timestamp_written = FALSE; | |
08b488b8 | 205 | |
6cb45d96 BV |
206 | for (p = 0; p < ctx->num_enabled_channels; p++) { |
207 | index = ctx->channel_index[p]; | |
2b6363b4 | 208 | |
6cb45d96 BV |
209 | curbit = ((unsigned)sample[index / 8] |
210 | >> (index % 8)) & 1; | |
211 | prevbit = ((unsigned)ctx->prevsample[index / 8] | |
212 | >> (index % 8)) & 1; | |
fbe2f794 | 213 | |
6cb45d96 BV |
214 | /* VCD only contains deltas/changes of signals. */ |
215 | if (prevbit == curbit && ctx->samplecount > 0) | |
216 | continue; | |
4c9ffa83 | 217 | |
6cb45d96 BV |
218 | /* Output timestamp of subsequent signal changes. */ |
219 | if (!timestamp_written) | |
220 | g_string_append_printf(*out, "#%.0f", | |
221 | (double)ctx->samplecount / | |
222 | ctx->samplerate * ctx->period); | |
2b6363b4 | 223 | |
6cb45d96 BV |
224 | /* Output which signal changed to which value. */ |
225 | g_string_append_c(*out, ' '); | |
226 | g_string_append_c(*out, '0' + curbit); | |
227 | g_string_append_c(*out, '!' + p); | |
2b6363b4 | 228 | |
6cb45d96 BV |
229 | timestamp_written = TRUE; |
230 | } | |
08b488b8 | 231 | |
6cb45d96 BV |
232 | if (timestamp_written) |
233 | g_string_append_c(*out, '\n'); | |
2b6363b4 | 234 | |
6cb45d96 BV |
235 | ctx->samplecount++; |
236 | memcpy(ctx->prevsample, sample, logic->unitsize); | |
237 | } | |
238 | break; | |
239 | case SR_DF_END: | |
240 | /* Write final timestamp as length indicator. */ | |
241 | *out = g_string_sized_new(512); | |
242 | g_string_printf(*out, "#%.0f\n", | |
243 | (double)ctx->samplecount / ctx->samplerate * ctx->period); | |
244 | break; | |
4c9ffa83 UH |
245 | } |
246 | ||
17f63de6 | 247 | return SR_OK; |
d5585e32 BV |
248 | } |
249 | ||
250 | static int cleanup(struct sr_output *o) | |
251 | { | |
252 | struct context *ctx; | |
253 | ||
254 | if (!o || !o->internal) | |
255 | return SR_ERR_ARG; | |
256 | ||
257 | ctx = o->internal; | |
2b78ffea | 258 | g_free(ctx->prevsample); |
6cb45d96 | 259 | g_free(ctx->channel_index); |
d5585e32 | 260 | g_free(ctx); |
4c9ffa83 | 261 | |
e46b8fb1 | 262 | return SR_OK; |
4c9ffa83 UH |
263 | } |
264 | ||
a755b0e1 | 265 | struct sr_output_module output_vcd = { |
cdb3573c | 266 | .id = "vcd", |
a755b0e1 BV |
267 | .name = "VCD", |
268 | .desc = "Value Change Dump", | |
269 | .options = NULL, | |
d494a4aa | 270 | .init = init, |
17f63de6 | 271 | .receive = receive, |
bbe6e336 | 272 | .cleanup = cleanup, |
4c9ffa83 | 273 | }; |