]> sigrok.org Git - libsigrok.git/blame - input/vcd.c
Beginnings of VCD input module.
[libsigrok.git] / input / vcd.c
CommitLineData
99eaa206
PA
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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
20/* Based on Verilog standard IEEE Std 1364-2001 Version C */
21
22#include <stdlib.h>
23#include <glib.h>
24#include <stdio.h>
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27
28/* Message logging helpers with driver-specific prefix string. */
29#define DRIVER_LOG_DOMAIN "input/vcd: "
30#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
36
37#define DEFAULT_NUM_PROBES 8
38
39/* Read until specific type of character occurs in file.
40 * Skip input if dest is NULL.
41 * Modes:
42 * 'W' read until whitespace
43 * 'N' read until non-whitespace, and ungetc() the character
44 * '$' read until $end
45 */
46static gboolean read_until(FILE *file, GString *dest, char mode)
47{
48 char prev[4] = "";
49 for(;;)
50 {
51 int c = fgetc(file);
52
53 if (c == EOF)
54 {
55 sr_err("Unexpected EOF.");
56 return FALSE;
57 }
58
59 if (mode == 'W' && g_ascii_isspace(c))
60 return TRUE;
61
62 if (mode == 'N' && !g_ascii_isspace(c))
63 {
64 ungetc(c, file);
65 return TRUE;
66 }
67
68 if (mode == '$')
69 {
70 prev[0] = prev[1]; prev[1] = prev[2]; prev[2] = prev[3]; prev[3] = c;
71 if (prev[0] == '$' && prev[1] == 'e' && prev[2] == 'n' && prev[3] == 'd')
72 {
73 if (dest != NULL)
74 g_string_truncate(dest, dest->len - 3);
75
76 return TRUE;
77 }
78 }
79
80 if (dest != NULL)
81 g_string_append_c(dest, c);
82 }
83}
84
85/* Reads a single VCD section from input file and parses it to structure.
86 * e.g. $timescale 1ps $end => "timescale" "1ps"
87 */
88static gboolean parse_section(FILE *file, gchar **name, gchar **contents)
89{
90 gboolean status;
91 GString *sname, *scontents;
92
93 /* Skip any initial white-space */
94 if (!read_until(file, NULL, 'N')) return FALSE;
95
96 /* Section tag should start with $. */
97 if (fgetc(file) != '$')
98 {
99 sr_err("Expected $ at beginning of section.");
100 return FALSE;
101 }
102
103 /* Read the section tag */
104 sname = g_string_sized_new(32);
105 status = read_until(file, sname, 'W');
106
107 /* Skip whitespace before content */
108 status = status && read_until(file, NULL, 'N');
109
110 /* Read the content */
111 scontents = g_string_sized_new(128);
112 status = status && read_until(file, scontents, '$');
113 g_strchomp(scontents->str);
114
115 /* Release strings if status is FALSE, return them if status is TRUE */
116 *name = g_string_free(sname, !status);
117 *contents = g_string_free(scontents, !status);
118 return status;
119}
120
121struct probe
122{
123 gchar *name;
124 gchar *identifier;
125};
126
127struct context
128{
129 uint64_t samplerate;
130 int maxprobes;
131 int probecount;
132 struct probe probes[SR_MAX_NUM_PROBES];
133};
134
135/* Remove empty parts from an array returned by g_strsplit. */
136static void remove_empty_parts(gchar **parts)
137{
138 gchar **src = parts;
139 gchar **dest = parts;
140 while (*src != NULL)
141 {
142 if (**src != '\0')
143 {
144 *dest++ = *src;
145 }
146
147 src++;
148 }
149
150 *dest = NULL;
151}
152
153/* Parse VCD header to get values for context structure.
154 * The context structure should be zeroed before calling this.
155 */
156static gboolean parse_header(FILE *file, struct context *ctx)
157{
158 gchar *name = NULL, *contents = NULL;
159 gboolean status = FALSE;
160
161 while (parse_section(file, &name, &contents))
162 {
163 sr_dbg("Section '%s', contents '%s'.", name, contents);
164
165 if (g_strcmp0(name, "enddefinitions") == 0)
166 {
167 status = TRUE;
168 break;
169 }
170 else if (g_strcmp0(name, "timescale") == 0)
171 {
172 /* The standard allows for values 1, 10 or 100
173 * and units s, ms, us, ns, ps and fs. */
174 struct sr_rational period;
175 if (sr_parse_period(contents, &period) == SR_OK)
176 {
177 ctx->samplerate = period.q / period.p;
178 if (period.q % period.p != 0)
179 {
180 /* Does not happen unless time value is non-standard */
181 sr_warn("Inexact rounding of samplerate, %" PRIu64 " / %" PRIu64 " to %" PRIu64 ".",
182 period.q, period.p, ctx->samplerate);
183 }
184
185 sr_dbg("Samplerate: %" PRIu64, ctx->samplerate);
186 }
187 else
188 {
189 sr_err("Parsing timescale failed.");
190 }
191 }
192 else if (g_strcmp0(name, "var") == 0)
193 {
194 /* Format: $var type size identifier reference $end */
195 gchar **parts = g_strsplit_set(contents, " \r\n\t", 0);
196 remove_empty_parts(parts);
197
198 if (g_strv_length(parts) != 4)
199 {
200 sr_err("$var section should have 4 items");
201 }
202 else if (g_strcmp0(parts[0], "reg") != 0 && g_strcmp0(parts[0], "wire") != 0)
203 {
204 sr_warn("Unsupported signal type: '%s'", parts[0]);
205 }
206 else if (strtol(parts[1], NULL, 10) != 1)
207 {
208 sr_warn("Unsupported signal size: '%s'", parts[1]);
209 }
210 else if (ctx->probecount >= ctx->maxprobes)
211 {
212 sr_warn("Skipping '%s' because only %d probes requested.", parts[3], ctx->maxprobes);
213 }
214 else
215 {
216 sr_info("Probe %d is '%s' identified by '%s'.", ctx->probecount, parts[3], parts[2]);
217 ctx->probes[ctx->probecount].identifier = g_strdup(parts[2]);
218 ctx->probes[ctx->probecount].name = g_strdup(parts[3]);
219 ctx->probecount++;
220 }
221
222 g_strfreev(parts);
223 }
224
225 g_free(name); name = NULL;
226 g_free(contents); contents = NULL;
227 }
228
229 g_free(name);
230 g_free(contents);
231
232 return status;
233}
234
235static int format_match(const char *filename)
236{
237 FILE *file;
238 gchar *name = NULL, *contents = NULL;
239 gboolean status;
240
241 file = fopen(filename, "r");
242 if (file == NULL)
243 return FALSE;
244
245 /* If we can parse the first section correctly,
246 * then it is assumed to be a VCD file.
247 */
248 status = parse_section(file, &name, &contents);
249 status = status && (*name != '\0');
250
251 g_free(name);
252 g_free(contents);
253 fclose(file);
254
255 return status;
256}
257
258static int init(struct sr_input *in)
259{
260 struct sr_probe *probe;
261 int num_probes, i;
262 char name[SR_MAX_PROBENAME_LEN + 1];
263 char *param;
264 struct context *ctx;
265
266 if (!(ctx = g_try_malloc0(sizeof(*ctx)))) {
267 sr_err("Input format context malloc failed.");
268 return SR_ERR_MALLOC;
269 }
270
271 num_probes = DEFAULT_NUM_PROBES;
272 ctx->samplerate = 0;
273
274 if (in->param) {
275 param = g_hash_table_lookup(in->param, "numprobes");
276 if (param) {
277 num_probes = strtoul(param, NULL, 10);
278 if (num_probes < 1)
279 return SR_ERR;
280 }
281 }
282
283 /* Maximum number of probes to parse from the VCD */
284 ctx->maxprobes = num_probes;
285
286 /* Create a virtual device. */
287 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
288 in->internal = ctx;
289
290 for (i = 0; i < num_probes; i++) {
291 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
292
293 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
294 return SR_ERR;
295 in->sdi->probes = g_slist_append(in->sdi->probes, probe);
296 }
297
298 return SR_OK;
299}
300
301static int loadfile(struct sr_input *in, const char *filename)
302{
303 struct sr_datafeed_header header;
304 struct sr_datafeed_packet packet;
305 struct sr_datafeed_meta_logic meta;
306 struct sr_datafeed_logic logic;
307 FILE *file;
308 struct context *ctx;
309
310 ctx = in->internal;
311
312 if ((file = fopen(filename, "r")) == NULL)
313 return SR_ERR;
314
315 if (!parse_header(file, ctx))
316 {
317 sr_err("VCD parsing failed");
318 fclose(file);
319 return SR_ERR;
320 }
321
322 /* Send header packet to the session bus. */
323 header.feed_version = 1;
324 gettimeofday(&header.starttime, NULL);
325 packet.type = SR_DF_HEADER;
326 packet.payload = &header;
327 sr_session_send(in->sdi, &packet);
328
329 /* Send metadata about the SR_DF_LOGIC packets to come. */
330 packet.type = SR_DF_META_LOGIC;
331 packet.payload = &meta;
332 meta.samplerate = ctx->samplerate;
333 meta.num_probes = ctx->probecount;
334 sr_session_send(in->sdi, &packet);
335
336 /* Chop up the input file into chunks & send it to the session bus. */
337/* packet.type = SR_DF_LOGIC;
338 packet.payload = &logic;
339 logic.unitsize = (num_probes + 7) / 8;
340 logic.data = buffer;
341 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
342 logic.length = size;
343 sr_session_send(in->sdi, &packet);
344 }
345 close(fd);
346*/
347 /* Send end packet to the session bus. */
348 packet.type = SR_DF_END;
349 sr_session_send(in->sdi, &packet);
350
351 g_free(ctx);
352 in->internal = NULL;
353
354 return SR_OK;
355}
356
357SR_PRIV struct sr_input_format input_vcd = {
358 .id = "vcd",
359 .description = "Value Change Dump",
360 .format_match = format_match,
361 .init = init,
362 .loadfile = loadfile,
363};