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