]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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> | |
25 | #include "config.h" /* Needed for PACKAGE and others. */ | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | ||
29 | #define LOG_PREFIX "output/vcd" | |
30 | ||
31 | struct context { | |
32 | int num_enabled_channels; | |
33 | GArray *channelindices; | |
34 | uint8_t *prevsample; | |
35 | gboolean header_done; | |
36 | int period; | |
37 | int *channel_index; | |
38 | uint64_t samplerate; | |
39 | uint64_t samplecount; | |
40 | }; | |
41 | ||
42 | static const char *const vcd_header_comment = | |
43 | "$comment\n Acquisition with %d/%d channels at %s\n$end\n"; | |
44 | ||
45 | static int init(struct sr_output *o) | |
46 | { | |
47 | struct context *ctx; | |
48 | struct sr_channel *ch; | |
49 | GSList *l; | |
50 | int num_enabled_channels, i; | |
51 | ||
52 | num_enabled_channels = 0; | |
53 | for (l = o->sdi->channels; l; l = l->next) { | |
54 | ch = l->data; | |
55 | if (ch->type != SR_CHANNEL_LOGIC) | |
56 | continue; | |
57 | if (!ch->enabled) | |
58 | continue; | |
59 | num_enabled_channels++; | |
60 | } | |
61 | if (num_enabled_channels > 94) { | |
62 | sr_err("VCD only supports 94 channels."); | |
63 | return SR_ERR; | |
64 | } | |
65 | ||
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); | |
97 | num_channels = g_slist_length(o->sdi->channels); | |
98 | ||
99 | /* timestamp */ | |
100 | t = time(NULL); | |
101 | timestamp = g_strdup(ctime(&t)); | |
102 | timestamp[strlen(timestamp)-1] = 0; | |
103 | g_string_printf(header, "$date %s $end\n", timestamp); | |
104 | g_free(timestamp); | |
105 | ||
106 | /* generator */ | |
107 | g_string_append_printf(header, "$version %s %s $end\n", | |
108 | PACKAGE, PACKAGE_VERSION); | |
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); | |
117 | } | |
118 | } | |
119 | if (ctx->samplerate != 0) { | |
120 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
121 | g_string_append_printf(header, " at %s", samplerate_s); | |
122 | g_free(samplerate_s); | |
123 | } | |
124 | g_string_append_printf(header, "\n$end\n"); | |
125 | ||
126 | /* timescale */ | |
127 | /* VCD can only handle 1/10/100 (s - fs), so scale up first */ | |
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); | |
132 | else | |
133 | ctx->period = SR_KHZ(1); | |
134 | frequency_s = sr_period_string(ctx->period); | |
135 | g_string_append_printf(header, "$timescale %s $end\n", frequency_s); | |
136 | g_free(frequency_s); | |
137 | ||
138 | /* scope */ | |
139 | g_string_append_printf(header, "$scope module %s $end\n", PACKAGE); | |
140 | ||
141 | /* Wires / channels */ | |
142 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
143 | ch = l->data; | |
144 | if (ch->type != SR_CHANNEL_LOGIC) | |
145 | continue; | |
146 | if (!ch->enabled) | |
147 | continue; | |
148 | g_string_append_printf(header, "$var wire 1 %c %s $end\n", | |
149 | (char)('!' + i), ch->name); | |
150 | } | |
151 | ||
152 | g_string_append(header, "$upscope $end\n$enddefinitions $end\n"); | |
153 | ||
154 | return header; | |
155 | } | |
156 | ||
157 | static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet, | |
158 | GString **out) | |
159 | { | |
160 | const struct sr_datafeed_meta *meta; | |
161 | const struct sr_datafeed_logic *logic; | |
162 | const struct sr_config *src; | |
163 | GSList *l; | |
164 | struct context *ctx; | |
165 | unsigned int i; | |
166 | int p, curbit, prevbit, index; | |
167 | uint8_t *sample; | |
168 | gboolean timestamp_written; | |
169 | ||
170 | *out = NULL; | |
171 | if (!o || !o->internal) | |
172 | return SR_ERR_BUG; | |
173 | ctx = o->internal; | |
174 | ||
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 | } | |
194 | ||
195 | if (!ctx->prevsample) { | |
196 | /* Can't allocate this until we know the stream's unitsize. */ | |
197 | ctx->prevsample = g_malloc0(logic->unitsize); | |
198 | } | |
199 | ||
200 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { | |
201 | sample = logic->data + i; | |
202 | timestamp_written = FALSE; | |
203 | ||
204 | for (p = 0; p < ctx->num_enabled_channels; p++) { | |
205 | index = ctx->channel_index[p]; | |
206 | ||
207 | curbit = ((unsigned)sample[index / 8] | |
208 | >> (index % 8)) & 1; | |
209 | prevbit = ((unsigned)ctx->prevsample[index / 8] | |
210 | >> (index % 8)) & 1; | |
211 | ||
212 | /* VCD only contains deltas/changes of signals. */ | |
213 | if (prevbit == curbit && ctx->samplecount > 0) | |
214 | continue; | |
215 | ||
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); | |
221 | ||
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); | |
226 | ||
227 | timestamp_written = TRUE; | |
228 | } | |
229 | ||
230 | if (timestamp_written) | |
231 | g_string_append_c(*out, '\n'); | |
232 | ||
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; | |
243 | } | |
244 | ||
245 | return SR_OK; | |
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; | |
256 | g_free(ctx->prevsample); | |
257 | g_free(ctx->channel_index); | |
258 | g_free(ctx); | |
259 | ||
260 | return SR_OK; | |
261 | } | |
262 | ||
263 | struct sr_output_format output_vcd = { | |
264 | .id = "vcd", | |
265 | .description = "Value Change Dump (VCD)", | |
266 | .init = init, | |
267 | .receive = receive, | |
268 | .cleanup = cleanup, | |
269 | }; |