libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
vcd.c
Go to the documentation of this file.
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2010-2012 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"
26 #include "sigrok.h"
27 #include "sigrok-internal.h"
28 
29 struct context {
31  int unitsize;
32  char *probelist[SR_MAX_NUM_PROBES + 1];
33  int *prevbits;
34  GString *header;
35  uint64_t prevsample;
36  int period;
37  uint64_t samplerate;
38 };
39 
40 static const char *vcd_header_comment = "\
41 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
42 
43 static int init(struct sr_output *o)
44 {
45  struct context *ctx;
46  struct sr_probe *probe;
47  GSList *l;
48  int num_probes, i;
49  char *samplerate_s, *frequency_s, *timestamp;
50  time_t t;
51 
52  if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
53  sr_err("vcd out: %s: ctx malloc failed", __func__);
54  return SR_ERR_MALLOC;
55  }
56 
57  o->internal = ctx;
58  ctx->num_enabled_probes = 0;
59 
60  for (l = o->dev->probes; l; l = l->next) {
61  probe = l->data;
62  if (!probe->enabled)
63  continue;
64  ctx->probelist[ctx->num_enabled_probes++] = probe->name;
65  }
66  if (ctx->num_enabled_probes > 94) {
67  sr_err("vcd out: VCD only supports 94 probes.");
68  return SR_ERR;
69  }
70 
71  ctx->probelist[ctx->num_enabled_probes] = 0;
72  ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
73  ctx->header = g_string_sized_new(512);
74  num_probes = g_slist_length(o->dev->probes);
75 
76  /* timestamp */
77  t = time(NULL);
78  timestamp = g_strdup(ctime(&t));
79  timestamp[strlen(timestamp)-1] = 0;
80  g_string_printf(ctx->header, "$date %s $end\n", timestamp);
81  g_free(timestamp);
82 
83  /* generator */
84  g_string_append_printf(ctx->header, "$version %s %s $end\n",
86 
88  ctx->samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
90  if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
91  g_string_free(ctx->header, TRUE);
92  g_free(ctx);
93  return SR_ERR;
94  }
95  g_string_append_printf(ctx->header, vcd_header_comment,
96  ctx->num_enabled_probes, num_probes, samplerate_s);
97  g_free(samplerate_s);
98  }
99 
100  /* timescale */
101  /* VCD can only handle 1/10/100 (s - fs), so scale up first */
102  if (ctx->samplerate > SR_MHZ(1))
103  ctx->period = SR_GHZ(1);
104  else if (ctx->samplerate > SR_KHZ(1))
105  ctx->period = SR_MHZ(1);
106  else
107  ctx->period = SR_KHZ(1);
108  if (!(frequency_s = sr_period_string(ctx->period))) {
109  g_string_free(ctx->header, TRUE);
110  g_free(ctx);
111  return SR_ERR;
112  }
113  g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
114  g_free(frequency_s);
115 
116  /* scope */
117  g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
118 
119  /* Wires / channels */
120  for (i = 0; i < ctx->num_enabled_probes; i++) {
121  g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
122  (char)('!' + i), ctx->probelist[i]);
123  }
124 
125  g_string_append(ctx->header, "$upscope $end\n"
126  "$enddefinitions $end\n$dumpvars\n");
127 
128  if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
129  g_string_free(ctx->header, TRUE);
130  g_free(ctx);
131  sr_err("vcd out: %s: ctx->prevbits malloc failed", __func__);
132  return SR_ERR_MALLOC;
133  }
134 
135  return SR_OK;
136 }
137 
138 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
139  uint64_t *length_out)
140 {
141  uint8_t *outbuf;
142 
143  switch (event_type) {
144  case SR_DF_END:
145  outbuf = (uint8_t *)g_strdup("$dumpoff\n$end\n");
146  *data_out = outbuf;
147  *length_out = strlen((const char *)outbuf);
148  g_free(o->internal);
149  o->internal = NULL;
150  break;
151  default:
152  *data_out = NULL;
153  *length_out = 0;
154  break;
155  }
156 
157  return SR_OK;
158 }
159 
160 static int data(struct sr_output *o, const uint8_t *data_in,
161  uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
162 {
163  struct context *ctx;
164  unsigned int i;
165  int p, curbit, prevbit;
166  uint64_t sample;
167  static uint64_t samplecount = 0;
168  GString *out;
169  int first_sample = 0;
170 
171  ctx = o->internal;
172  out = g_string_sized_new(512);
173 
174  if (ctx->header) {
175  /* The header is still here, this must be the first packet. */
176  g_string_append(out, ctx->header->str);
177  g_string_free(ctx->header, TRUE);
178  ctx->header = NULL;
179  first_sample = 1;
180  }
181 
182  for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
183  samplecount++;
184 
185  memcpy(&sample, data_in + i, ctx->unitsize);
186 
187  if (first_sample) {
188  /* First packet. We neg to make sure sample is stored. */
189  ctx->prevsample = ~sample;
190  first_sample = 0;
191  }
192 
193  for (p = 0; p < ctx->num_enabled_probes; p++) {
194  curbit = (sample & ((uint64_t) (1 << p))) >> p;
195  prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
196 
197  /* VCD only contains deltas/changes of signals. */
198  if (prevbit == curbit)
199  continue;
200 
201  /* Output which signal changed to which value. */
202  g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n",
203  (uint64_t)(((float)samplecount / ctx->samplerate)
204  * ctx->period), curbit, (char)('!' + p));
205  }
206 
207  ctx->prevsample = sample;
208  }
209 
210  *data_out = (uint8_t *)out->str;
211  *length_out = out->len;
212  g_string_free(out, FALSE);
213 
214  return SR_OK;
215 }
216 
218  .id = "vcd",
219  .description = "Value Change Dump (VCD)",
220  .df_type = SR_DF_LOGIC,
221  .init = init,
222  .data = data,
223  .event = event,
224 };