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