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
26 #include "sigrok-internal.h"
30 int num_enabled_probes;
40 static const char *vcd_header_comment = "\
41 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
43 static int init(struct sr_output *o)
46 struct sr_probe *probe;
49 char *samplerate_s, *frequency_s, *timestamp;
52 if (!(ctx = calloc(1, sizeof(struct context))))
56 ctx->num_enabled_probes = 0;
58 for (l = o->device->probes; l; l = l->next) {
62 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
64 if (ctx->num_enabled_probes > 94) {
65 sr_warn("VCD only supports 94 probes.");
69 ctx->probelist[ctx->num_enabled_probes] = 0;
70 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
71 ctx->header = g_string_sized_new(512);
72 num_probes = g_slist_length(o->device->probes);
76 timestamp = strdup(ctime(&t));
77 timestamp[strlen(timestamp)-1] = 0;
78 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
82 g_string_append_printf(ctx->header, "$version %s %s $end\n",
83 PACKAGE, PACKAGE_VERSION);
85 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
86 ctx->samplerate = *((uint64_t *) o->device->plugin->get_device_info(
87 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
88 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
89 g_string_free(ctx->header, TRUE);
93 g_string_append_printf(ctx->header, vcd_header_comment,
94 ctx->num_enabled_probes, num_probes, samplerate_s);
99 /* VCD can only handle 1/10/100 (s - fs), so scale up first */
100 if (ctx->samplerate > SR_MHZ(1))
101 ctx->period = SR_GHZ(1);
102 else if (ctx->samplerate > SR_KHZ(1))
103 ctx->period = SR_MHZ(1);
105 ctx->period = SR_KHZ(1);
106 if (!(frequency_s = sr_period_string(ctx->period))) {
107 g_string_free(ctx->header, TRUE);
111 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
115 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
117 /* Wires / channels */
118 for (i = 0; i < ctx->num_enabled_probes; i++) {
119 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
120 (char)('!' + i), ctx->probelist[i]);
123 g_string_append(ctx->header, "$upscope $end\n"
124 "$enddefinitions $end\n$dumpvars\n");
126 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
127 g_string_free(ctx->header, TRUE);
129 return SR_ERR_MALLOC;
135 static int event(struct sr_output *o, int event_type, char **data_out,
136 uint64_t *length_out)
142 switch (event_type) {
144 outbuf = strdup("$dumpoff\n$end\n");
146 *length_out = strlen(outbuf);
159 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
160 char **data_out, uint64_t *length_out)
164 int p, curbit, prevbit;
166 static uint64_t samplecount = 0;
168 int first_sample = 0;
171 out = g_string_sized_new(512);
174 /* The header is still here, this must be the first packet. */
175 g_string_append(out, ctx->header->str);
176 g_string_free(ctx->header, TRUE);
181 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
184 memcpy(&sample, data_in + i, ctx->unitsize);
187 /* First packet. We neg to make sure sample is stored. */
188 ctx->prevsample = ~sample;
192 for (p = 0; p < ctx->num_enabled_probes; p++) {
193 curbit = (sample & ((uint64_t) (1 << p))) >> p;
194 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
196 /* VCD only contains deltas/changes of signals. */
197 if (prevbit == curbit)
200 /* Output which signal changed to which value. */
201 g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n",
202 (uint64_t)(((float)samplecount / ctx->samplerate)
203 * ctx->period), curbit, (char)('!' + p));
206 ctx->prevsample = sample;
209 *data_out = out->str;
210 *length_out = out->len;
211 g_string_free(out, FALSE);
216 struct sr_output_format output_vcd = {
218 .description = "Value Change Dump (VCD)",
219 .df_type = SR_DF_LOGIC,