]>
Commit | Line | Data |
---|---|---|
99eaa206 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
99eaa206 | 3 | * |
0157808d | 4 | * Copyright (C) 2012 Petteri Aimonen <jpa@sr.mail.kapsi.fi> |
99eaa206 PA |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
0157808d PA |
20 | /* The VCD input module has the following options: |
21 | * | |
22 | * numprobes: Maximum number of probes to use. The probes are | |
23 | * detected in the same order as they are listed | |
24 | * in the $var sections of the VCD file. | |
25 | * | |
26 | * skip: Allows skipping until given timestamp in the file. | |
27 | * This can speed up analyzing of long captures. | |
28 | * | |
29 | * Value < 0: Skip until first timestamp listed in | |
30 | * the file. (default) | |
31 | * | |
32 | * Value = 0: Do not skip, instead generate samples | |
33 | * beginning from timestamp 0. | |
34 | * | |
35 | * Value > 0: Start at the given timestamp. | |
36 | * | |
37 | * downsample: Divide the samplerate by the given factor. | |
38 | * This can speed up analyzing of long captures. | |
39 | * | |
6b7ace48 PA |
40 | * compress: Compress idle periods longer than this value. |
41 | * This can speed up analyzing of long captures. | |
42 | * Default 0 = don't compress. | |
43 | * | |
0157808d PA |
44 | * Based on Verilog standard IEEE Std 1364-2001 Version C |
45 | * | |
46 | * Supported features: | |
47 | * - $var with 'wire' and 'reg' types of scalar variables | |
48 | * - $timescale definition for samplerate | |
49 | * - multiple character variable identifiers | |
50 | * | |
51 | * Most important unsupported features: | |
52 | * - vector variables (bit vectors etc.) | |
53 | * - analog, integer and real number variables | |
54 | * - $dumpvars initial value declaration | |
8be87469 | 55 | * - $scope namespaces |
e4c8a4d7 | 56 | * - more than 64 probes |
0157808d PA |
57 | */ |
58 | ||
99eaa206 PA |
59 | #include <stdlib.h> |
60 | #include <glib.h> | |
61 | #include <stdio.h> | |
61a429c9 | 62 | #include <string.h> |
99eaa206 PA |
63 | #include "libsigrok.h" |
64 | #include "libsigrok-internal.h" | |
65 | ||
3544f848 | 66 | #define LOG_PREFIX "input/vcd" |
99eaa206 PA |
67 | |
68 | #define DEFAULT_NUM_PROBES 8 | |
e4c8a4d7 BV |
69 | #define CHUNKSIZE 1024 |
70 | ||
71 | struct context { | |
72 | uint64_t samplerate; | |
73 | int maxprobes; | |
74 | int probecount; | |
75 | int downsample; | |
76 | unsigned compress; | |
77 | int64_t skip; | |
78 | GSList *probes; | |
79 | }; | |
80 | ||
81 | struct probe { | |
82 | gchar *name; | |
83 | gchar *identifier; | |
84 | }; | |
85 | ||
99eaa206 PA |
86 | |
87 | /* Read until specific type of character occurs in file. | |
88 | * Skip input if dest is NULL. | |
89 | * Modes: | |
90 | * 'W' read until whitespace | |
91 | * 'N' read until non-whitespace, and ungetc() the character | |
92 | * '$' read until $end | |
93 | */ | |
94 | static gboolean read_until(FILE *file, GString *dest, char mode) | |
95 | { | |
4f2dad42 | 96 | int c; |
99eaa206 | 97 | char prev[4] = ""; |
4f2dad42 | 98 | |
e4c8a4d7 | 99 | for(;;) { |
4f2dad42 | 100 | c = fgetc(file); |
99eaa206 | 101 | |
e4c8a4d7 | 102 | if (c == EOF) { |
8be87469 | 103 | if (mode == '$') |
4f2dad42 | 104 | sr_err("Unexpected EOF."); |
99eaa206 PA |
105 | return FALSE; |
106 | } | |
cd1b0e8f | 107 | |
99eaa206 PA |
108 | if (mode == 'W' && g_ascii_isspace(c)) |
109 | return TRUE; | |
cd1b0e8f | 110 | |
e4c8a4d7 | 111 | if (mode == 'N' && !g_ascii_isspace(c)) { |
99eaa206 PA |
112 | ungetc(c, file); |
113 | return TRUE; | |
114 | } | |
cd1b0e8f | 115 | |
e4c8a4d7 | 116 | if (mode == '$') { |
99eaa206 | 117 | prev[0] = prev[1]; prev[1] = prev[2]; prev[2] = prev[3]; prev[3] = c; |
e4c8a4d7 | 118 | if (prev[0] == '$' && prev[1] == 'e' && prev[2] == 'n' && prev[3] == 'd') { |
99eaa206 PA |
119 | if (dest != NULL) |
120 | g_string_truncate(dest, dest->len - 3); | |
cd1b0e8f | 121 | |
99eaa206 PA |
122 | return TRUE; |
123 | } | |
124 | } | |
125 | ||
126 | if (dest != NULL) | |
127 | g_string_append_c(dest, c); | |
128 | } | |
129 | } | |
130 | ||
e4c8a4d7 BV |
131 | /* |
132 | * Reads a single VCD section from input file and parses it to structure. | |
99eaa206 PA |
133 | * e.g. $timescale 1ps $end => "timescale" "1ps" |
134 | */ | |
135 | static gboolean parse_section(FILE *file, gchar **name, gchar **contents) | |
136 | { | |
137 | gboolean status; | |
138 | GString *sname, *scontents; | |
cd1b0e8f | 139 | |
99eaa206 PA |
140 | /* Skip any initial white-space */ |
141 | if (!read_until(file, NULL, 'N')) return FALSE; | |
cd1b0e8f | 142 | |
99eaa206 | 143 | /* Section tag should start with $. */ |
e4c8a4d7 | 144 | if (fgetc(file) != '$') { |
99eaa206 PA |
145 | sr_err("Expected $ at beginning of section."); |
146 | return FALSE; | |
147 | } | |
cd1b0e8f DE |
148 | |
149 | /* Read the section tag */ | |
99eaa206 PA |
150 | sname = g_string_sized_new(32); |
151 | status = read_until(file, sname, 'W'); | |
cd1b0e8f | 152 | |
99eaa206 PA |
153 | /* Skip whitespace before content */ |
154 | status = status && read_until(file, NULL, 'N'); | |
cd1b0e8f | 155 | |
99eaa206 PA |
156 | /* Read the content */ |
157 | scontents = g_string_sized_new(128); | |
158 | status = status && read_until(file, scontents, '$'); | |
159 | g_strchomp(scontents->str); | |
160 | ||
cd1b0e8f | 161 | /* Release strings if status is FALSE, return them if status is TRUE */ |
99eaa206 PA |
162 | *name = g_string_free(sname, !status); |
163 | *contents = g_string_free(scontents, !status); | |
164 | return status; | |
165 | } | |
166 | ||
db9679af ML |
167 | static void free_probe(void *data) |
168 | { | |
169 | struct probe *probe = data; | |
170 | g_free(probe->name); | |
171 | g_free(probe->identifier); | |
172 | g_free(probe); | |
173 | } | |
174 | ||
61a429c9 PA |
175 | static void release_context(struct context *ctx) |
176 | { | |
db9679af | 177 | g_slist_free_full(ctx->probes, free_probe); |
61a429c9 PA |
178 | g_free(ctx); |
179 | } | |
180 | ||
99eaa206 PA |
181 | /* Remove empty parts from an array returned by g_strsplit. */ |
182 | static void remove_empty_parts(gchar **parts) | |
183 | { | |
184 | gchar **src = parts; | |
185 | gchar **dest = parts; | |
e4c8a4d7 | 186 | while (*src != NULL) { |
99eaa206 | 187 | if (**src != '\0') |
99eaa206 | 188 | *dest++ = *src; |
99eaa206 PA |
189 | src++; |
190 | } | |
cd1b0e8f | 191 | |
99eaa206 PA |
192 | *dest = NULL; |
193 | } | |
194 | ||
e4c8a4d7 BV |
195 | /* |
196 | * Parse VCD header to get values for context structure. | |
99eaa206 PA |
197 | * The context structure should be zeroed before calling this. |
198 | */ | |
199 | static gboolean parse_header(FILE *file, struct context *ctx) | |
200 | { | |
e48a5462 | 201 | uint64_t p, q; |
99eaa206 PA |
202 | gchar *name = NULL, *contents = NULL; |
203 | gboolean status = FALSE; | |
db9679af | 204 | struct probe *probe; |
99eaa206 | 205 | |
e4c8a4d7 | 206 | while (parse_section(file, &name, &contents)) { |
99eaa206 | 207 | sr_dbg("Section '%s', contents '%s'.", name, contents); |
cd1b0e8f | 208 | |
e4c8a4d7 | 209 | if (g_strcmp0(name, "enddefinitions") == 0) { |
99eaa206 PA |
210 | status = TRUE; |
211 | break; | |
e4c8a4d7 BV |
212 | } else if (g_strcmp0(name, "timescale") == 0) { |
213 | /* | |
214 | * The standard allows for values 1, 10 or 100 | |
215 | * and units s, ms, us, ns, ps and fs. | |
216 | * */ | |
217 | if (sr_parse_period(contents, &p, &q) == SR_OK) { | |
e48a5462 | 218 | ctx->samplerate = q / p; |
e4c8a4d7 | 219 | if (q % p != 0) { |
99eaa206 | 220 | /* Does not happen unless time value is non-standard */ |
0157808d | 221 | sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 " Hz.", |
e48a5462 | 222 | q, p, ctx->samplerate); |
99eaa206 | 223 | } |
cd1b0e8f | 224 | |
99eaa206 | 225 | sr_dbg("Samplerate: %" PRIu64, ctx->samplerate); |
e4c8a4d7 | 226 | } else { |
99eaa206 PA |
227 | sr_err("Parsing timescale failed."); |
228 | } | |
e4c8a4d7 | 229 | } else if (g_strcmp0(name, "var") == 0) { |
99eaa206 PA |
230 | /* Format: $var type size identifier reference $end */ |
231 | gchar **parts = g_strsplit_set(contents, " \r\n\t", 0); | |
232 | remove_empty_parts(parts); | |
cd1b0e8f | 233 | |
99eaa206 | 234 | if (g_strv_length(parts) != 4) |
8be87469 | 235 | sr_warn("$var section should have 4 items"); |
99eaa206 | 236 | else if (g_strcmp0(parts[0], "reg") != 0 && g_strcmp0(parts[0], "wire") != 0) |
8be87469 | 237 | sr_info("Unsupported signal type: '%s'", parts[0]); |
99eaa206 | 238 | else if (strtol(parts[1], NULL, 10) != 1) |
8be87469 | 239 | sr_info("Unsupported signal size: '%s'", parts[1]); |
99eaa206 | 240 | else if (ctx->probecount >= ctx->maxprobes) |
99eaa206 | 241 | sr_warn("Skipping '%s' because only %d probes requested.", parts[3], ctx->maxprobes); |
e4c8a4d7 | 242 | else { |
99eaa206 | 243 | sr_info("Probe %d is '%s' identified by '%s'.", ctx->probecount, parts[3], parts[2]); |
db9679af ML |
244 | probe = g_malloc(sizeof(struct probe)); |
245 | probe->identifier = g_strdup(parts[2]); | |
246 | probe->name = g_strdup(parts[3]); | |
247 | ctx->probes = g_slist_append(ctx->probes, probe); | |
99eaa206 PA |
248 | ctx->probecount++; |
249 | } | |
cd1b0e8f | 250 | |
99eaa206 PA |
251 | g_strfreev(parts); |
252 | } | |
cd1b0e8f | 253 | |
99eaa206 PA |
254 | g_free(name); name = NULL; |
255 | g_free(contents); contents = NULL; | |
256 | } | |
cd1b0e8f | 257 | |
99eaa206 PA |
258 | g_free(name); |
259 | g_free(contents); | |
cd1b0e8f | 260 | |
99eaa206 PA |
261 | return status; |
262 | } | |
263 | ||
264 | static int format_match(const char *filename) | |
265 | { | |
266 | FILE *file; | |
267 | gchar *name = NULL, *contents = NULL; | |
268 | gboolean status; | |
cd1b0e8f | 269 | |
99eaa206 PA |
270 | file = fopen(filename, "r"); |
271 | if (file == NULL) | |
272 | return FALSE; | |
273 | ||
e4c8a4d7 BV |
274 | /* |
275 | * If we can parse the first section correctly, | |
99eaa206 PA |
276 | * then it is assumed to be a VCD file. |
277 | */ | |
278 | status = parse_section(file, &name, &contents); | |
279 | status = status && (*name != '\0'); | |
cd1b0e8f | 280 | |
99eaa206 PA |
281 | g_free(name); |
282 | g_free(contents); | |
283 | fclose(file); | |
cd1b0e8f | 284 | |
99eaa206 PA |
285 | return status; |
286 | } | |
287 | ||
543d45c5 | 288 | static int init(struct sr_input *in, const char *filename) |
99eaa206 PA |
289 | { |
290 | struct sr_probe *probe; | |
291 | int num_probes, i; | |
292 | char name[SR_MAX_PROBENAME_LEN + 1]; | |
293 | char *param; | |
294 | struct context *ctx; | |
295 | ||
543d45c5 BV |
296 | (void)filename; |
297 | ||
99eaa206 PA |
298 | if (!(ctx = g_try_malloc0(sizeof(*ctx)))) { |
299 | sr_err("Input format context malloc failed."); | |
300 | return SR_ERR_MALLOC; | |
301 | } | |
302 | ||
303 | num_probes = DEFAULT_NUM_PROBES; | |
304 | ctx->samplerate = 0; | |
0157808d PA |
305 | ctx->downsample = 1; |
306 | ctx->skip = -1; | |
99eaa206 PA |
307 | |
308 | if (in->param) { | |
309 | param = g_hash_table_lookup(in->param, "numprobes"); | |
310 | if (param) { | |
311 | num_probes = strtoul(param, NULL, 10); | |
e4c8a4d7 | 312 | if (num_probes < 1) { |
61a429c9 | 313 | release_context(ctx); |
99eaa206 | 314 | return SR_ERR; |
6c57446d BV |
315 | } else if (num_probes > 64) { |
316 | sr_err("No more than 64 probes supported."); | |
317 | return SR_ERR; | |
61a429c9 | 318 | } |
99eaa206 | 319 | } |
cd1b0e8f | 320 | |
0157808d PA |
321 | param = g_hash_table_lookup(in->param, "downsample"); |
322 | if (param) { | |
323 | ctx->downsample = strtoul(param, NULL, 10); | |
324 | if (ctx->downsample < 1) | |
0157808d | 325 | ctx->downsample = 1; |
0157808d | 326 | } |
cd1b0e8f | 327 | |
6b7ace48 | 328 | param = g_hash_table_lookup(in->param, "compress"); |
e4c8a4d7 | 329 | if (param) |
6b7ace48 | 330 | ctx->compress = strtoul(param, NULL, 10); |
cd1b0e8f | 331 | |
0157808d | 332 | param = g_hash_table_lookup(in->param, "skip"); |
e4c8a4d7 | 333 | if (param) |
0157808d | 334 | ctx->skip = strtoul(param, NULL, 10) / ctx->downsample; |
99eaa206 | 335 | } |
cd1b0e8f | 336 | |
99eaa206 PA |
337 | /* Maximum number of probes to parse from the VCD */ |
338 | ctx->maxprobes = num_probes; | |
339 | ||
340 | /* Create a virtual device. */ | |
341 | in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); | |
342 | in->internal = ctx; | |
343 | ||
344 | for (i = 0; i < num_probes; i++) { | |
345 | snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i); | |
cd1b0e8f | 346 | |
e4c8a4d7 | 347 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name))) { |
61a429c9 | 348 | release_context(ctx); |
99eaa206 | 349 | return SR_ERR; |
61a429c9 | 350 | } |
cd1b0e8f | 351 | |
99eaa206 PA |
352 | in->sdi->probes = g_slist_append(in->sdi->probes, probe); |
353 | } | |
354 | ||
355 | return SR_OK; | |
356 | } | |
357 | ||
61a429c9 PA |
358 | /* Send N samples of the given value. */ |
359 | static void send_samples(const struct sr_dev_inst *sdi, uint64_t sample, uint64_t count) | |
360 | { | |
361 | struct sr_datafeed_packet packet; | |
362 | struct sr_datafeed_logic logic; | |
363 | uint64_t buffer[CHUNKSIZE]; | |
364 | uint64_t i; | |
365 | unsigned chunksize = CHUNKSIZE; | |
cd1b0e8f | 366 | |
61a429c9 PA |
367 | if (count < chunksize) |
368 | chunksize = count; | |
369 | ||
370 | for (i = 0; i < chunksize; i++) | |
61a429c9 | 371 | buffer[i] = sample; |
cd1b0e8f | 372 | |
61a429c9 | 373 | packet.type = SR_DF_LOGIC; |
cd1b0e8f | 374 | packet.payload = &logic; |
61a429c9 PA |
375 | logic.unitsize = sizeof(uint64_t); |
376 | logic.data = buffer; | |
cd1b0e8f | 377 | |
e4c8a4d7 | 378 | while (count) { |
61a429c9 PA |
379 | if (count < chunksize) |
380 | chunksize = count; | |
cd1b0e8f | 381 | |
61a429c9 | 382 | logic.length = sizeof(uint64_t) * chunksize; |
cd1b0e8f | 383 | |
61a429c9 PA |
384 | sr_session_send(sdi, &packet); |
385 | count -= chunksize; | |
386 | } | |
387 | } | |
388 | ||
389 | /* Parse the data section of VCD */ | |
390 | static void parse_contents(FILE *file, const struct sr_dev_inst *sdi, struct context *ctx) | |
391 | { | |
392 | GString *token = g_string_sized_new(32); | |
cd1b0e8f | 393 | |
61a429c9 PA |
394 | uint64_t prev_timestamp = 0; |
395 | uint64_t prev_values = 0; | |
cd1b0e8f | 396 | |
61a429c9 | 397 | /* Read one space-delimited token at a time. */ |
e4c8a4d7 BV |
398 | while (read_until(file, NULL, 'N') && read_until(file, token, 'W')) { |
399 | if (token->str[0] == '#' && g_ascii_isdigit(token->str[1])) { | |
61a429c9 PA |
400 | /* Numeric value beginning with # is a new timestamp value */ |
401 | uint64_t timestamp; | |
402 | timestamp = strtoull(token->str + 1, NULL, 10); | |
cd1b0e8f | 403 | |
0157808d PA |
404 | if (ctx->downsample > 1) |
405 | timestamp /= ctx->downsample; | |
cd1b0e8f | 406 | |
e4c8a4d7 BV |
407 | /* |
408 | * Skip < 0 => skip until first timestamp. | |
0157808d PA |
409 | * Skip = 0 => don't skip |
410 | * Skip > 0 => skip until timestamp >= skip. | |
411 | */ | |
e4c8a4d7 | 412 | if (ctx->skip < 0) { |
0157808d | 413 | ctx->skip = timestamp; |
8be87469 | 414 | prev_timestamp = timestamp; |
e4c8a4d7 | 415 | } else if (ctx->skip > 0 && timestamp < (uint64_t)ctx->skip) { |
8be87469 | 416 | prev_timestamp = ctx->skip; |
0157808d | 417 | } |
e4c8a4d7 | 418 | else if (timestamp == prev_timestamp) { |
8be87469 | 419 | /* Ignore repeated timestamps (e.g. sigrok outputs these) */ |
61a429c9 | 420 | } |
e4c8a4d7 | 421 | else { |
6b7ace48 PA |
422 | if (ctx->compress != 0 && timestamp - prev_timestamp > ctx->compress) |
423 | { | |
424 | /* Compress long idle periods */ | |
425 | prev_timestamp = timestamp - ctx->compress; | |
426 | } | |
cd1b0e8f | 427 | |
61a429c9 | 428 | sr_dbg("New timestamp: %" PRIu64, timestamp); |
cd1b0e8f | 429 | |
61a429c9 | 430 | /* Generate samples from prev_timestamp up to timestamp - 1. */ |
8be87469 | 431 | send_samples(sdi, prev_values, timestamp - prev_timestamp); |
0157808d | 432 | prev_timestamp = timestamp; |
61a429c9 | 433 | } |
e4c8a4d7 | 434 | } else if (token->str[0] == '$' && token->len > 1) { |
61a429c9 | 435 | /* This is probably a $dumpvars, $comment or similar. |
8be87469 | 436 | * $dump* contain useful data, but other tags will be skipped until $end. */ |
e4c8a4d7 BV |
437 | if (g_strcmp0(token->str, "$dumpvars") == 0 |
438 | || g_strcmp0(token->str, "$dumpon") == 0 | |
439 | || g_strcmp0(token->str, "$dumpoff") == 0 | |
440 | || g_strcmp0(token->str, "$end") == 0) { | |
8be87469 | 441 | /* Ignore, parse contents as normally. */ |
e4c8a4d7 | 442 | } else { |
8be87469 PA |
443 | /* Skip until $end */ |
444 | read_until(file, NULL, '$'); | |
445 | } | |
446 | } | |
e4c8a4d7 | 447 | else if (strchr("bBrR", token->str[0]) != NULL) { |
8be87469 PA |
448 | /* A vector value. Skip it and also the following identifier. */ |
449 | read_until(file, NULL, 'N'); | |
450 | read_until(file, NULL, 'W'); | |
e4c8a4d7 | 451 | } else if (strchr("01xXzZ", token->str[0]) != NULL) { |
61a429c9 PA |
452 | /* A new 1-bit sample value */ |
453 | int i, bit; | |
db9679af ML |
454 | GSList *l; |
455 | struct probe *probe; | |
456 | ||
61a429c9 | 457 | bit = (token->str[0] == '1'); |
cd1b0e8f | 458 | |
61a429c9 | 459 | g_string_erase(token, 0, 1); |
e4c8a4d7 | 460 | if (token->len == 0) { |
61a429c9 PA |
461 | /* There was a space between value and identifier. |
462 | * Read in the rest. | |
463 | */ | |
464 | read_until(file, NULL, 'N'); | |
465 | read_until(file, token, 'W'); | |
466 | } | |
cd1b0e8f | 467 | |
e4c8a4d7 | 468 | for (i = 0, l = ctx->probes; i < ctx->probecount && l; i++, l = l->next) { |
db9679af ML |
469 | probe = l->data; |
470 | ||
e4c8a4d7 | 471 | if (g_strcmp0(token->str, probe->identifier) == 0) { |
61a429c9 PA |
472 | /* Found our probe */ |
473 | if (bit) | |
cd1b0e8f | 474 | prev_values |= (uint64_t)1 << i; |
61a429c9 | 475 | else |
cd1b0e8f DE |
476 | prev_values &= ~((uint64_t)1 << i); |
477 | ||
61a429c9 PA |
478 | break; |
479 | } | |
480 | } | |
cd1b0e8f | 481 | |
61a429c9 | 482 | if (i == ctx->probecount) |
8be87469 | 483 | sr_dbg("Did not find probe for identifier '%s'.", token->str); |
e4c8a4d7 | 484 | } else { |
8be87469 PA |
485 | sr_warn("Skipping unknown token '%s'.", token->str); |
486 | } | |
cd1b0e8f | 487 | |
61a429c9 PA |
488 | g_string_truncate(token, 0); |
489 | } | |
cd1b0e8f | 490 | |
61a429c9 PA |
491 | g_string_free(token, TRUE); |
492 | } | |
493 | ||
99eaa206 PA |
494 | static int loadfile(struct sr_input *in, const char *filename) |
495 | { | |
99eaa206 | 496 | struct sr_datafeed_packet packet; |
2df1e819 BV |
497 | struct sr_datafeed_meta meta; |
498 | struct sr_config *src; | |
99eaa206 PA |
499 | FILE *file; |
500 | struct context *ctx; | |
2df1e819 | 501 | uint64_t samplerate; |
99eaa206 PA |
502 | |
503 | ctx = in->internal; | |
504 | ||
505 | if ((file = fopen(filename, "r")) == NULL) | |
506 | return SR_ERR; | |
507 | ||
e4c8a4d7 | 508 | if (!parse_header(file, ctx)) { |
99eaa206 PA |
509 | sr_err("VCD parsing failed"); |
510 | fclose(file); | |
511 | return SR_ERR; | |
512 | } | |
513 | ||
514 | /* Send header packet to the session bus. */ | |
29a27196 | 515 | std_session_send_df_header(in->sdi, LOG_PREFIX); |
99eaa206 PA |
516 | |
517 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
2df1e819 | 518 | packet.type = SR_DF_META; |
99eaa206 | 519 | packet.payload = &meta; |
2df1e819 | 520 | samplerate = ctx->samplerate / ctx->downsample; |
ec4063b8 | 521 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate)); |
2df1e819 | 522 | meta.config = g_slist_append(NULL, src); |
99eaa206 | 523 | sr_session_send(in->sdi, &packet); |
ec4063b8 | 524 | sr_config_free(src); |
99eaa206 | 525 | |
61a429c9 PA |
526 | /* Parse the contents of the VCD file */ |
527 | parse_contents(file, in->sdi, ctx); | |
cd1b0e8f | 528 | |
99eaa206 PA |
529 | /* Send end packet to the session bus. */ |
530 | packet.type = SR_DF_END; | |
531 | sr_session_send(in->sdi, &packet); | |
532 | ||
61a429c9 PA |
533 | fclose(file); |
534 | release_context(ctx); | |
99eaa206 PA |
535 | in->internal = NULL; |
536 | ||
537 | return SR_OK; | |
538 | } | |
539 | ||
540 | SR_PRIV struct sr_input_format input_vcd = { | |
541 | .id = "vcd", | |
542 | .description = "Value Change Dump", | |
543 | .format_match = format_match, | |
544 | .init = init, | |
545 | .loadfile = loadfile, | |
546 | }; |