2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
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.
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.
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
29 int num_enabled_probes;
39 static const char *vcd_header_comment = "\
40 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
42 static int init(struct sr_output *o)
45 struct sr_probe *probe;
48 char *samplerate_s, *frequency_s, *timestamp;
51 if (!(ctx = calloc(1, sizeof(struct context))))
55 ctx->num_enabled_probes = 0;
57 for (l = o->device->probes; l; l = l->next) {
61 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
63 if (ctx->num_enabled_probes > 94) {
64 g_warning("VCD only supports 94 probes.");
68 ctx->probelist[ctx->num_enabled_probes] = 0;
69 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
70 ctx->header = g_string_sized_new(512);
71 num_probes = g_slist_length(o->device->probes);
75 timestamp = strdup(ctime(&t));
76 timestamp[strlen(timestamp)-1] = 0;
77 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
81 g_string_append_printf(ctx->header, "$version %s %s $end\n",
82 PACKAGE, PACKAGE_VERSION);
84 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
85 ctx->samplerate = *((uint64_t *) o->device->plugin->get_device_info(
86 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
87 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
88 g_string_free(ctx->header, TRUE);
92 g_string_append_printf(ctx->header, vcd_header_comment,
93 ctx->num_enabled_probes, num_probes, samplerate_s);
98 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
99 if (ctx->samplerate > SR_MHZ(1))
100 ctx->period = SR_GHZ(1);
101 else if (ctx->samplerate > SR_KHZ(1))
102 ctx->period = SR_MHZ(1);
104 ctx->period = SR_KHZ(1);
105 if (!(frequency_s = sr_period_string(ctx->period))) {
106 g_string_free(ctx->header, TRUE);
110 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
114 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
116 /* Wires / channels */
117 for (i = 0; i < ctx->num_enabled_probes; i++) {
118 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
119 (char)('!' + i), ctx->probelist[i]);
122 g_string_append(ctx->header, "$upscope $end\n"
123 "$enddefinitions $end\n$dumpvars\n");
125 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
126 g_string_free(ctx->header, TRUE);
128 return SR_ERR_MALLOC;
134 static int event(struct sr_output *o, int event_type, char **data_out,
135 uint64_t *length_out)
141 switch (event_type) {
143 outbuf = strdup("$dumpoff\n$end\n");
145 *length_out = strlen(outbuf);
158 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
159 char **data_out, uint64_t *length_out)
163 int p, curbit, prevbit;
165 static uint64_t samplecount = 0;
167 int first_sample = 0;
170 out = g_string_sized_new(512);
173 /* The header is still here, this must be the first packet. */
174 g_string_append(out, ctx->header->str);
175 g_string_free(ctx->header, TRUE);
180 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
183 memcpy(&sample, data_in + i, ctx->unitsize);
186 /* First packet. We neg to make sure sample is stored. */
187 ctx->prevsample = ~sample;
191 for (p = 0; p < ctx->num_enabled_probes; p++) {
192 curbit = (sample & ((uint64_t) (1 << p))) >> p;
193 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
195 /* VCD only contains deltas/changes of signals. */
196 if (prevbit == curbit)
199 /* Output which signal changed to which value. */
200 g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n",
201 (uint64_t)(((float)samplecount / ctx->samplerate)
202 * ctx->period), curbit, (char)('!' + p));
205 ctx->prevsample = sample;
208 *data_out = out->str;
209 *length_out = out->len;
210 g_string_free(out, FALSE);
215 struct sr_output_format output_vcd = {
217 "Value Change Dump (VCD)",