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