]> sigrok.org Git - libsigrok.git/blame - src/input/vcd.c
input/vcd: Expand the reset() logic
[libsigrok.git] / src / input / vcd.c
CommitLineData
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>
7db06394 5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
99eaa206
PA
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
0157808d
PA
21/* The VCD input module has the following options:
22 *
ba7dd8bb 23 * numchannels: Maximum number of channels to use. The channels are
0157808d
PA
24 * detected in the same order as they are listed
25 * in the $var sections of the VCD file.
26 *
27 * skip: Allows skipping until given timestamp in the file.
28 * This can speed up analyzing of long captures.
176d785d 29 *
0157808d
PA
30 * Value < 0: Skip until first timestamp listed in
31 * the file. (default)
32 *
33 * Value = 0: Do not skip, instead generate samples
34 * beginning from timestamp 0.
35 *
36 * Value > 0: Start at the given timestamp.
37 *
38 * downsample: Divide the samplerate by the given factor.
39 * This can speed up analyzing of long captures.
40 *
6b7ace48
PA
41 * compress: Compress idle periods longer than this value.
42 * This can speed up analyzing of long captures.
43 * Default 0 = don't compress.
44 *
0157808d
PA
45 * Based on Verilog standard IEEE Std 1364-2001 Version C
46 *
47 * Supported features:
48 * - $var with 'wire' and 'reg' types of scalar variables
49 * - $timescale definition for samplerate
50 * - multiple character variable identifiers
51 *
52 * Most important unsupported features:
53 * - vector variables (bit vectors etc.)
54 * - analog, integer and real number variables
55 * - $dumpvars initial value declaration
8be87469 56 * - $scope namespaces
ba7dd8bb 57 * - more than 64 channels
0157808d
PA
58 */
59
6ec6c43b 60#include <config.h>
99eaa206
PA
61#include <stdlib.h>
62#include <glib.h>
63#include <stdio.h>
61a429c9 64#include <string.h>
c1aae900 65#include <libsigrok/libsigrok.h>
99eaa206
PA
66#include "libsigrok-internal.h"
67
3544f848 68#define LOG_PREFIX "input/vcd"
99eaa206 69
9a4fd01a 70#define CHUNK_SIZE (4 * 1024 * 1024)
e4c8a4d7
BV
71
72struct context {
c10ef17c 73 gboolean started;
7db06394 74 gboolean got_header;
f9bc17d4 75 uint64_t prev_timestamp;
e4c8a4d7 76 uint64_t samplerate;
7db06394
BV
77 unsigned int maxchannels;
78 unsigned int channelcount;
e4c8a4d7
BV
79 int downsample;
80 unsigned compress;
81 int64_t skip;
7db06394 82 gboolean skip_until_end;
ba7dd8bb 83 GSList *channels;
db0e5c99
PA
84 size_t bytes_per_sample;
85 size_t samples_in_buffer;
86 uint8_t *buffer;
87 uint8_t *current_levels;
e4c8a4d7
BV
88};
89
ba7dd8bb 90struct vcd_channel {
e4c8a4d7
BV
91 gchar *name;
92 gchar *identifier;
93};
94
e4c8a4d7 95/*
7db06394 96 * Reads a single VCD section from input file and parses it to name/contents.
d9251a2c 97 * e.g. $timescale 1ps $end => "timescale" "1ps"
99eaa206 98 */
7db06394 99static gboolean parse_section(GString *buf, gchar **name, gchar **contents)
99eaa206 100{
7db06394 101 GString *sname, *scontent;
99eaa206 102 gboolean status;
7db06394
BV
103 unsigned int pos;
104
105 *name = *contents = NULL;
106 status = FALSE;
107 pos = 0;
cd1b0e8f 108
1f706c21
WS
109 /* Skip UTF8 BOM */
110 if (buf->len >= 3 && !strncmp(buf->str, "\xef\xbb\xbf", 3))
111 pos = 3;
112
7db06394
BV
113 /* Skip any initial white-space. */
114 while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
115 pos++;
cd1b0e8f 116
99eaa206 117 /* Section tag should start with $. */
7db06394 118 if (buf->str[pos++] != '$')
99eaa206 119 return FALSE;
cd1b0e8f 120
99eaa206 121 sname = g_string_sized_new(32);
7db06394
BV
122 scontent = g_string_sized_new(128);
123
124 /* Read the section tag. */
125 while (pos < buf->len && !g_ascii_isspace(buf->str[pos]))
126 g_string_append_c(sname, buf->str[pos++]);
127
128 /* Skip whitespace before content. */
129 while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
130 pos++;
131
132 /* Read the content. */
133 while (pos < buf->len - 4 && strncmp(buf->str + pos, "$end", 4))
134 g_string_append_c(scontent, buf->str[pos++]);
135
136 if (sname->len && pos < buf->len - 4 && !strncmp(buf->str + pos, "$end", 4)) {
137 status = TRUE;
138 pos += 4;
139 while (pos < buf->len && g_ascii_isspace(buf->str[pos]))
140 pos++;
141 g_string_erase(buf, 0, pos);
142 }
99eaa206 143
99eaa206 144 *name = g_string_free(sname, !status);
7db06394
BV
145 *contents = g_string_free(scontent, !status);
146 if (*contents)
147 g_strchomp(*contents);
148
99eaa206
PA
149 return status;
150}
151
ba7dd8bb 152static void free_channel(void *data)
db9679af 153{
4237ab9e
GS
154 struct vcd_channel *vcd_ch;
155
156 vcd_ch = data;
157 if (!vcd_ch)
158 return;
ba7dd8bb
UH
159 g_free(vcd_ch->name);
160 g_free(vcd_ch->identifier);
161 g_free(vcd_ch);
db9679af
ML
162}
163
99eaa206
PA
164/* Remove empty parts from an array returned by g_strsplit. */
165static void remove_empty_parts(gchar **parts)
166{
167 gchar **src = parts;
168 gchar **dest = parts;
e4c8a4d7 169 while (*src != NULL) {
99eaa206 170 if (**src != '\0')
99eaa206 171 *dest++ = *src;
99eaa206
PA
172 src++;
173 }
cd1b0e8f 174
99eaa206
PA
175 *dest = NULL;
176}
177
e4c8a4d7
BV
178/*
179 * Parse VCD header to get values for context structure.
99eaa206
PA
180 * The context structure should be zeroed before calling this.
181 */
7db06394 182static gboolean parse_header(const struct sr_input *in, GString *buf)
99eaa206 183{
ba7dd8bb 184 struct vcd_channel *vcd_ch;
7db06394
BV
185 uint64_t p, q;
186 struct context *inc;
187 gboolean status;
188 gchar *name, *contents, **parts;
99eaa206 189
7db06394
BV
190 inc = in->priv;
191 name = contents = NULL;
192 status = FALSE;
193 while (parse_section(buf, &name, &contents)) {
99eaa206 194 sr_dbg("Section '%s', contents '%s'.", name, contents);
cd1b0e8f 195
e4c8a4d7 196 if (g_strcmp0(name, "enddefinitions") == 0) {
99eaa206
PA
197 status = TRUE;
198 break;
e4c8a4d7
BV
199 } else if (g_strcmp0(name, "timescale") == 0) {
200 /*
201 * The standard allows for values 1, 10 or 100
202 * and units s, ms, us, ns, ps and fs.
7db06394 203 */
e4c8a4d7 204 if (sr_parse_period(contents, &p, &q) == SR_OK) {
7db06394 205 inc->samplerate = q / p;
e4c8a4d7 206 if (q % p != 0) {
99eaa206 207 /* Does not happen unless time value is non-standard */
0157808d 208 sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 " Hz.",
7db06394 209 q, p, inc->samplerate);
99eaa206 210 }
cd1b0e8f 211
7db06394 212 sr_dbg("Samplerate: %" PRIu64, inc->samplerate);
e4c8a4d7 213 } else {
99eaa206
PA
214 sr_err("Parsing timescale failed.");
215 }
e4c8a4d7 216 } else if (g_strcmp0(name, "var") == 0) {
e85e550d
WS
217 /* Format: $var type size identifier reference [opt. index] $end */
218 unsigned int length;
219
7db06394 220 parts = g_strsplit_set(contents, " \r\n\t", 0);
99eaa206 221 remove_empty_parts(parts);
e85e550d 222 length = g_strv_length(parts);
cd1b0e8f 223
e85e550d
WS
224 if (length != 4 && length != 5)
225 sr_warn("$var section should have 4 or 5 items");
99eaa206 226 else if (g_strcmp0(parts[0], "reg") != 0 && g_strcmp0(parts[0], "wire") != 0)
8be87469 227 sr_info("Unsupported signal type: '%s'", parts[0]);
99eaa206 228 else if (strtol(parts[1], NULL, 10) != 1)
8be87469 229 sr_info("Unsupported signal size: '%s'", parts[1]);
3f48fc82 230 else if (inc->maxchannels && inc->channelcount >= inc->maxchannels)
e85e550d
WS
231 sr_warn("Skipping '%s%s' because only %d channels requested.",
232 parts[3], parts[4] ? : "", inc->maxchannels);
e4c8a4d7 233 else {
ba7dd8bb
UH
234 vcd_ch = g_malloc(sizeof(struct vcd_channel));
235 vcd_ch->identifier = g_strdup(parts[2]);
e85e550d
WS
236 if (length == 4)
237 vcd_ch->name = g_strdup(parts[3]);
238 else
239 vcd_ch->name = g_strconcat(parts[3], parts[4], NULL);
240
241 sr_info("Channel %d is '%s' identified by '%s'.",
242 inc->channelcount, vcd_ch->name, vcd_ch->identifier);
243
ab464eb3 244 sr_channel_new(in->sdi, inc->channelcount++, SR_CHANNEL_LOGIC, TRUE, vcd_ch->name);
7db06394 245 inc->channels = g_slist_append(inc->channels, vcd_ch);
99eaa206 246 }
cd1b0e8f 247
99eaa206
PA
248 g_strfreev(parts);
249 }
cd1b0e8f 250
db0e5c99
PA
251 g_free(name);
252 name = NULL;
253 g_free(contents);
254 contents = NULL;
99eaa206 255 }
99eaa206
PA
256 g_free(name);
257 g_free(contents);
cd1b0e8f 258
db0e5c99
PA
259 /*
260 * Compute how many bytes each sample will have and initialize the
261 * current levels. The current levels will be updated whenever VCD
262 * has changes.
263 */
264 inc->bytes_per_sample = (inc->channelcount + 7) / 8;
265 inc->current_levels = g_malloc0(inc->bytes_per_sample);
266
7db06394
BV
267 inc->got_header = status;
268
99eaa206
PA
269 return status;
270}
271
54ee427d 272static int format_match(GHashTable *metadata, unsigned int *confidence)
99eaa206 273{
7db06394 274 GString *buf, *tmpbuf;
99eaa206 275 gboolean status;
7db06394 276 gchar *name, *contents;
cd1b0e8f 277
7db06394
BV
278 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
279 tmpbuf = g_string_new_len(buf->str, buf->len);
99eaa206 280
e4c8a4d7
BV
281 /*
282 * If we can parse the first section correctly,
99eaa206
PA
283 * then it is assumed to be a VCD file.
284 */
7db06394
BV
285 status = parse_section(tmpbuf, &name, &contents);
286 g_string_free(tmpbuf, TRUE);
99eaa206
PA
287 g_free(name);
288 g_free(contents);
cd1b0e8f 289
54ee427d
GS
290 if (!status)
291 return SR_ERR;
292 *confidence = 1;
293
294 return SR_OK;
99eaa206
PA
295}
296
db0e5c99
PA
297/* Send all accumulated bytes from inc->buffer. */
298static void send_buffer(const struct sr_input *in)
61a429c9 299{
db0e5c99 300 struct context *inc;
61a429c9
PA
301 struct sr_datafeed_packet packet;
302 struct sr_datafeed_logic logic;
cd1b0e8f 303
db0e5c99 304 inc = in->priv;
61a429c9 305
db0e5c99
PA
306 if (inc->samples_in_buffer == 0)
307 return;
cd1b0e8f 308
61a429c9 309 packet.type = SR_DF_LOGIC;
cd1b0e8f 310 packet.payload = &logic;
db0e5c99
PA
311 logic.unitsize = inc->bytes_per_sample;
312 logic.data = inc->buffer;
313 logic.length = inc->bytes_per_sample * inc->samples_in_buffer;
314 sr_session_send(in->sdi, &packet);
315 inc->samples_in_buffer = 0;
316}
317
318/*
319 * Add N copies of the current sample to buffer.
320 * When the buffer fills up, automatically send it.
321 */
322static void add_samples(const struct sr_input *in, size_t count)
323{
324 struct context *inc;
325 size_t samples_per_chunk;
326 size_t space_left, i;
327 uint8_t *p;
328
329 inc = in->priv;
8bc2fa6d 330 samples_per_chunk = CHUNK_SIZE / inc->bytes_per_sample;
cd1b0e8f 331
e4c8a4d7 332 while (count) {
db0e5c99
PA
333 space_left = samples_per_chunk - inc->samples_in_buffer;
334
335 if (space_left > count)
336 space_left = count;
cd1b0e8f 337
db0e5c99
PA
338 p = inc->buffer + inc->samples_in_buffer * inc->bytes_per_sample;
339 for (i = 0; i < space_left; i++) {
340 memcpy(p, inc->current_levels, inc->bytes_per_sample);
341 p += inc->bytes_per_sample;
342 inc->samples_in_buffer++;
343 count--;
344 }
cd1b0e8f 345
db0e5c99
PA
346 if (inc->samples_in_buffer == samples_per_chunk)
347 send_buffer(in);
61a429c9
PA
348 }
349}
350
36dacf17
WS
351/* Set the channel level depending on the identifier and parsed value. */
352static void process_bit(struct context *inc, char *identifier, unsigned int bit)
353{
354 GSList *l;
355 struct vcd_channel *vcd_ch;
356 unsigned int j;
357
358 for (j = 0, l = inc->channels; j < inc->channelcount && l; j++, l = l->next) {
359 vcd_ch = l->data;
360 if (g_strcmp0(identifier, vcd_ch->identifier) == 0) {
361 /* Found our channel. */
362 size_t byte_idx = (j / 8);
363 size_t bit_idx = j - 8 * byte_idx;
364 if (bit)
365 inc->current_levels[byte_idx] |= (uint8_t)1 << bit_idx;
366 else
367 inc->current_levels[byte_idx] &= ~((uint8_t)1 << bit_idx);
368 break;
369 }
370 }
371 if (j == inc->channelcount)
372 sr_dbg("Did not find channel for identifier '%s'.", identifier);
373}
374
7db06394
BV
375/* Parse a set of lines from the data section. */
376static void parse_contents(const struct sr_input *in, char *data)
61a429c9 377{
7db06394 378 struct context *inc;
f9bc17d4 379 uint64_t timestamp;
36dacf17 380 unsigned int bit, i;
7db06394 381 char **tokens;
cd1b0e8f 382
7db06394 383 inc = in->priv;
cd1b0e8f 384
61a429c9 385 /* Read one space-delimited token at a time. */
7db06394
BV
386 tokens = g_strsplit_set(data, " \t\r\n", 0);
387 remove_empty_parts(tokens);
388 for (i = 0; tokens[i]; i++) {
389 if (inc->skip_until_end) {
390 if (!strcmp(tokens[i], "$end")) {
391 /* Done with unhandled/unknown section. */
392 inc->skip_until_end = FALSE;
393 break;
394 }
395 }
396 if (tokens[i][0] == '#' && g_ascii_isdigit(tokens[i][1])) {
61a429c9 397 /* Numeric value beginning with # is a new timestamp value */
7db06394 398 timestamp = strtoull(tokens[i] + 1, NULL, 10);
cd1b0e8f 399
7db06394
BV
400 if (inc->downsample > 1)
401 timestamp /= inc->downsample;
cd1b0e8f 402
e4c8a4d7
BV
403 /*
404 * Skip < 0 => skip until first timestamp.
0157808d
PA
405 * Skip = 0 => don't skip
406 * Skip > 0 => skip until timestamp >= skip.
407 */
7db06394
BV
408 if (inc->skip < 0) {
409 inc->skip = timestamp;
f9bc17d4 410 inc->prev_timestamp = timestamp;
7db06394 411 } else if (inc->skip > 0 && timestamp < (uint64_t)inc->skip) {
f9bc17d4
GS
412 inc->prev_timestamp = inc->skip;
413 } else if (timestamp == inc->prev_timestamp) {
8be87469 414 /* Ignore repeated timestamps (e.g. sigrok outputs these) */
ed367d68
GS
415 } else if (timestamp < inc->prev_timestamp) {
416 sr_err("Invalid timestamp: %" PRIu64 " (smaller than previous timestamp).", timestamp);
417 inc->skip_until_end = TRUE;
418 break;
7db06394 419 } else {
f9bc17d4 420 if (inc->compress != 0 && timestamp - inc->prev_timestamp > inc->compress) {
6b7ace48 421 /* Compress long idle periods */
f9bc17d4 422 inc->prev_timestamp = timestamp - inc->compress;
6b7ace48 423 }
cd1b0e8f 424
61a429c9 425 sr_dbg("New timestamp: %" PRIu64, timestamp);
cd1b0e8f 426
61a429c9 427 /* Generate samples from prev_timestamp up to timestamp - 1. */
f9bc17d4
GS
428 add_samples(in, timestamp - inc->prev_timestamp);
429 inc->prev_timestamp = timestamp;
61a429c9 430 }
7db06394
BV
431 } else if (tokens[i][0] == '$' && tokens[i][1] != '\0') {
432 /*
433 * This is probably a $dumpvars, $comment or similar.
434 * $dump* contain useful data.
435 */
436 if (g_strcmp0(tokens[i], "$dumpvars") == 0
437 || g_strcmp0(tokens[i], "$dumpon") == 0
438 || g_strcmp0(tokens[i], "$dumpoff") == 0
439 || g_strcmp0(tokens[i], "$end") == 0) {
8be87469 440 /* Ignore, parse contents as normally. */
e4c8a4d7 441 } else {
7db06394
BV
442 /* Ignore this and future lines until $end. */
443 inc->skip_until_end = TRUE;
444 break;
8be87469 445 }
34724ffa
WS
446 } else if (strchr("rR", tokens[i][0]) != NULL) {
447 sr_dbg("Real type vector values not supported yet!");
76bc28c3
WS
448 if (!tokens[++i])
449 /* No tokens left, bail out */
450 break;
451 else
452 /* Process next token */
453 continue;
34724ffa
WS
454 } else if (strchr("bB", tokens[i][0]) != NULL) {
455 bit = (tokens[i][1] == '1');
456
457 /*
458 * Bail out if a) char after 'b' is NUL, or b) there is
459 * a second character after 'b', or c) there is no
460 * identifier.
461 */
462 if (!tokens[i][1] || tokens[i][2] || !tokens[++i]) {
463 sr_dbg("Unexpected vector format!");
464 break;
465 }
466
467 process_bit(inc, tokens[i], bit);
7db06394 468 } else if (strchr("01xXzZ", tokens[i][0]) != NULL) {
a66175c2
WS
469 char *identifier;
470
61a429c9 471 /* A new 1-bit sample value */
7db06394
BV
472 bit = (tokens[i][0] == '1');
473
474 /*
475 * The identifier is either the next character, or, if
476 * there was whitespace after the bit, the next token.
477 */
478 if (tokens[i][1] == '\0') {
73f052d3
WS
479 if (!tokens[++i]) {
480 sr_dbg("Identifier missing!");
481 break;
482 }
a66175c2 483 identifier = tokens[i];
7db06394 484 } else {
a66175c2 485 identifier = tokens[i] + 1;
61a429c9 486 }
36dacf17 487 process_bit(inc, identifier, bit);
e4c8a4d7 488 } else {
7db06394 489 sr_warn("Skipping unknown token '%s'.", tokens[i]);
8be87469 490 }
7db06394
BV
491 }
492 g_strfreev(tokens);
493}
494
495static int init(struct sr_input *in, GHashTable *options)
496{
7db06394
BV
497 struct context *inc;
498
d0181813 499 inc = in->priv = g_malloc0(sizeof(struct context));
cd1b0e8f 500
3f48fc82 501 inc->maxchannels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
7db06394
BV
502 inc->downsample = g_variant_get_int32(g_hash_table_lookup(options, "downsample"));
503 if (inc->downsample < 1)
504 inc->downsample = 1;
505
506 inc->compress = g_variant_get_int32(g_hash_table_lookup(options, "compress"));
507 inc->skip = g_variant_get_int32(g_hash_table_lookup(options, "skip"));
508 inc->skip /= inc->downsample;
509
aac29cc1 510 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
7db06394
BV
511 in->priv = inc;
512
8bc2fa6d 513 inc->buffer = g_malloc(CHUNK_SIZE);
db0e5c99 514
7db06394 515 return SR_OK;
61a429c9
PA
516}
517
7db06394
BV
518static gboolean have_header(GString *buf)
519{
520 unsigned int pos;
521 char *p;
522
523 if (!(p = g_strstr_len(buf->str, buf->len, "$enddefinitions")))
524 return FALSE;
525 pos = p - buf->str + 15;
526 while (pos < buf->len - 4 && g_ascii_isspace(buf->str[pos]))
527 pos++;
528 if (!strncmp(buf->str + pos, "$end", 4))
529 return TRUE;
530
531 return FALSE;
532}
533
7066fd46 534static int process_buffer(struct sr_input *in)
99eaa206 535{
99eaa206 536 struct sr_datafeed_packet packet;
2df1e819
BV
537 struct sr_datafeed_meta meta;
538 struct sr_config *src;
7db06394 539 struct context *inc;
2df1e819 540 uint64_t samplerate;
7db06394
BV
541 char *p;
542
7db06394 543 inc = in->priv;
d0181813 544 if (!inc->started) {
bee2b016 545 std_session_send_df_header(in->sdi);
99eaa206 546
7db06394
BV
547 packet.type = SR_DF_META;
548 packet.payload = &meta;
549 samplerate = inc->samplerate / inc->downsample;
550 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate));
551 meta.config = g_slist_append(NULL, src);
552 sr_session_send(in->sdi, &packet);
c01378c9 553 g_slist_free(meta.config);
7db06394 554 sr_config_free(src);
d0181813
BV
555
556 inc->started = TRUE;
99eaa206
PA
557 }
558
7db06394
BV
559 while ((p = g_strrstr_len(in->buf->str, in->buf->len, "\n"))) {
560 *p = '\0';
561 g_strstrip(in->buf->str);
562 if (in->buf->str[0] != '\0')
563 parse_contents(in, in->buf->str);
564 g_string_erase(in->buf, 0, p - in->buf->str + 1);
565 }
99eaa206 566
7db06394
BV
567 return SR_OK;
568}
cd1b0e8f 569
7066fd46
BV
570static int receive(struct sr_input *in, GString *buf)
571{
572 struct context *inc;
573 int ret;
574
575 g_string_append_len(in->buf, buf->str, buf->len);
576
577 inc = in->priv;
578 if (!inc->got_header) {
579 if (!have_header(in->buf))
580 return SR_OK;
73931b7c 581 if (!parse_header(in, in->buf))
7066fd46
BV
582 /* There was a header in there, but it was malformed. */
583 return SR_ERR;
584
585 in->sdi_ready = TRUE;
586 /* sdi is ready, notify frontend. */
587 return SR_OK;
588 }
589
590 ret = process_buffer(in);
591
592 return ret;
593}
594
595static int end(struct sr_input *in)
7db06394
BV
596{
597 struct context *inc;
7066fd46
BV
598 int ret;
599
db0e5c99
PA
600 inc = in->priv;
601
7066fd46
BV
602 if (in->sdi_ready)
603 ret = process_buffer(in);
604 else
605 ret = SR_OK;
99eaa206 606
db0e5c99
PA
607 /* Send any samples that haven't been sent yet. */
608 send_buffer(in);
609
3be42bc2 610 if (inc->started)
bee2b016 611 std_session_send_df_end(in->sdi);
c10ef17c 612
7066fd46
BV
613 return ret;
614}
615
d5cc282f 616static void cleanup(struct sr_input *in)
7066fd46
BV
617{
618 struct context *inc;
619
620 inc = in->priv;
7db06394 621 g_slist_free_full(inc->channels, free_channel);
4237ab9e 622 inc->channels = NULL;
db0e5c99
PA
623 g_free(inc->buffer);
624 inc->buffer = NULL;
625 g_free(inc->current_levels);
626 inc->current_levels = NULL;
99eaa206
PA
627}
628
f4b4725b
SA
629static int reset(struct sr_input *in)
630{
631 struct context *inc = in->priv;
632
633 cleanup(in);
f4b4725b
SA
634 g_string_truncate(in->buf, 0);
635
4237ab9e
GS
636 inc->started = FALSE;
637 inc->got_header = FALSE;
638 inc->prev_timestamp = 0;
639 inc->skip_until_end = FALSE;
640 inc->channelcount = 0;
641 /* The inc->channels list was released in cleanup() above. */
642 inc->buffer = g_malloc(CHUNK_SIZE);
643
f4b4725b
SA
644 return SR_OK;
645}
646
7db06394 647static struct sr_option options[] = {
867293a1
UH
648 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
649 { "skip", "Skip samples until timestamp", "Skip samples until the specified timestamp; "
650 "< 0: Skip until first timestamp listed; 0: Don't skip", NULL, NULL },
651 { "downsample", "Downsampling factor", "Downsample, i.e. divide the samplerate by the specified factor", NULL, NULL },
652 { "compress", "Compress idle periods", "Compress idle periods longer than the specified value", NULL, NULL },
06ad20be 653 ALL_ZERO
7db06394
BV
654};
655
2c240774 656static const struct sr_option *get_options(void)
7db06394
BV
657{
658 if (!options[0].def) {
3f48fc82 659 options[0].def = g_variant_ref_sink(g_variant_new_int32(0));
7db06394
BV
660 options[1].def = g_variant_ref_sink(g_variant_new_int32(-1));
661 options[2].def = g_variant_ref_sink(g_variant_new_int32(1));
662 options[3].def = g_variant_ref_sink(g_variant_new_int32(0));
663 }
664
665 return options;
666}
667
d4c93774 668SR_PRIV struct sr_input_module input_vcd = {
99eaa206 669 .id = "vcd",
7db06394 670 .name = "VCD",
b20eb520 671 .desc = "Value Change Dump data",
c7bc82ff 672 .exts = (const char*[]){"vcd", NULL},
7db06394
BV
673 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
674 .options = get_options,
99eaa206
PA
675 .format_match = format_match,
676 .init = init,
7db06394 677 .receive = receive,
7066fd46 678 .end = end,
7db06394 679 .cleanup = cleanup,
f4b4725b 680 .reset = reset,
99eaa206 681};