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