]> sigrok.org Git - libsigrok.git/blob - output/vcd.c
input/output modules: Adjust to GVariant-based sr_config_* functions
[libsigrok.git] / output / vcd.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2013 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" /* Needed for PACKAGE and others. */
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28
29 /* Message logging helpers with driver-specific prefix string. */
30 #define DRIVER_LOG_DOMAIN "output/vcd: "
31 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
36 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
37
38 struct context {
39         int num_enabled_probes;
40         char *probelist[SR_MAX_NUM_PROBES + 1];
41         int *prevbits;
42         GString *header;
43         uint64_t prevsample;
44         int period;
45         uint64_t samplerate;
46 };
47
48 static const char *vcd_header_comment = "\
49 $comment\n  Acquisition with %d/%d probes at %s\n$end\n";
50
51 static int init(struct sr_output *o)
52 {
53         struct context *ctx;
54         struct sr_probe *probe;
55         GSList *l;
56         GVariant *gvar;
57         int num_probes, i;
58         char *samplerate_s, *frequency_s, *timestamp;
59         time_t t;
60
61         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
62                 sr_err("%s: ctx malloc failed", __func__);
63                 return SR_ERR_MALLOC;
64         }
65
66         o->internal = ctx;
67         ctx->num_enabled_probes = 0;
68
69         for (l = o->sdi->probes; l; l = l->next) {
70                 probe = l->data;
71                 if (!probe->enabled)
72                         continue;
73                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
74         }
75         if (ctx->num_enabled_probes > 94) {
76                 sr_err("VCD only supports 94 probes.");
77                 return SR_ERR;
78         }
79
80         ctx->probelist[ctx->num_enabled_probes] = 0;
81         ctx->header = g_string_sized_new(512);
82         num_probes = g_slist_length(o->sdi->probes);
83
84         /* timestamp */
85         t = time(NULL);
86         timestamp = g_strdup(ctime(&t));
87         timestamp[strlen(timestamp)-1] = 0;
88         g_string_printf(ctx->header, "$date %s $end\n", timestamp);
89         g_free(timestamp);
90
91         /* generator */
92         g_string_append_printf(ctx->header, "$version %s %s $end\n",
93                         PACKAGE, PACKAGE_VERSION);
94
95         if (o->sdi->driver && sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
96                 o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi);
97                 ctx->samplerate = g_variant_get_uint64(gvar);
98                 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
99                         g_string_free(ctx->header, TRUE);
100                         g_free(ctx);
101                         g_variant_unref(gvar);
102                         return SR_ERR;
103                 }
104                 g_string_append_printf(ctx->header, vcd_header_comment,
105                                  ctx->num_enabled_probes, num_probes, samplerate_s);
106                 g_free(samplerate_s);
107                 g_variant_unref(gvar);
108         }
109
110         /* timescale */
111         /* VCD can only handle 1/10/100 (s - fs), so scale up first */
112         if (ctx->samplerate > SR_MHZ(1))
113                 ctx->period = SR_GHZ(1);
114         else if (ctx->samplerate > SR_KHZ(1))
115                 ctx->period = SR_MHZ(1);
116         else
117                 ctx->period = SR_KHZ(1);
118         if (!(frequency_s = sr_period_string(ctx->period))) {
119                 g_string_free(ctx->header, TRUE);
120                 g_free(ctx);
121                 return SR_ERR;
122         }
123         g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
124         g_free(frequency_s);
125
126         /* scope */
127         g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
128
129         /* Wires / channels */
130         for (i = 0; i < ctx->num_enabled_probes; i++) {
131                 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
132                                 (char)('!' + i), ctx->probelist[i]);
133         }
134
135         g_string_append(ctx->header, "$upscope $end\n"
136                         "$enddefinitions $end\n$dumpvars\n");
137
138         if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
139                 g_string_free(ctx->header, TRUE);
140                 g_free(ctx);
141                 sr_err("%s: ctx->prevbits malloc failed", __func__);
142                 return SR_ERR_MALLOC;
143         }
144
145         return SR_OK;
146 }
147
148 static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi,
149                 const struct sr_datafeed_packet *packet)
150 {
151         const struct sr_datafeed_logic *logic;
152         struct context *ctx;
153         GString *text;
154         unsigned int i;
155         int p, curbit, prevbit;
156         uint64_t sample;
157         static uint64_t samplecount = 0;
158         gboolean first_sample;
159
160         (void)sdi;
161
162         if (!o || !o->internal)
163                 return NULL;
164         ctx = o->internal;
165
166         if (packet->type == SR_DF_END) {
167                 text = g_string_sized_new(16);
168                 g_string_printf(text, "$dumpoff\n$end\n");
169                 return text;
170         } else if (packet->type != SR_DF_LOGIC)
171                 return NULL;
172
173         if (ctx->header) {
174                 /* The header is still here, this must be the first packet. */
175                 text = ctx->header;
176                 ctx->header = NULL;
177                 first_sample = TRUE;
178         } else {
179                 text = g_string_sized_new(512);
180                 first_sample = FALSE;
181         }
182
183         logic = packet->payload;
184         for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
185                 samplecount++;
186
187                 memcpy(&sample, logic->data + i, logic->unitsize);
188
189                 if (first_sample) {
190                         /* First packet. We neg to make sure sample is stored. */
191                         ctx->prevsample = ~sample;
192                         first_sample = FALSE;
193                 }
194
195                 for (p = 0; p < ctx->num_enabled_probes; p++) {
196                         curbit = (sample & ((uint64_t) (1 << p))) >> p;
197                         prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
198
199                         /* VCD only contains deltas/changes of signals. */
200                         if (prevbit == curbit)
201                                 continue;
202
203                         /* Output which signal changed to which value. */
204                         g_string_append_printf(text, "#%" PRIu64 "\n%i%c\n",
205                                         (uint64_t)(((float)samplecount / ctx->samplerate)
206                                         * ctx->period), curbit, (char)('!' + p));
207                 }
208
209                 ctx->prevsample = sample;
210         }
211
212         return text;
213 }
214
215 static int cleanup(struct sr_output *o)
216 {
217         struct context *ctx;
218
219         if (!o || !o->internal)
220                 return SR_ERR_ARG;
221
222         ctx = o->internal;
223         g_free(ctx);
224
225         return SR_OK;
226 }
227
228 struct sr_output_format output_vcd = {
229         .id = "vcd",
230         .description = "Value Change Dump (VCD)",
231         .df_type = SR_DF_LOGIC,
232         .init = init,
233         .recv = receive,
234         .cleanup = cleanup,
235 };