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