]> sigrok.org Git - libsigrok.git/blob - output/output_vcd.c
output: if device has no plugin, don't report samplerate
[libsigrok.git] / output / 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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <sigrok.h>
25 #include "config.h"
26
27 struct context {
28         int num_enabled_probes;
29         int unitsize;
30         char *probelist[65];
31         int *prevbits;
32         char *header;
33 };
34
35 const char *vcd_header = "\
36 $date\n  %s\n$end\n\
37 $version\n  %s\n$end\n%s\
38 $timescale\n  %i %s\n$end\n\
39 $scope module %s $end\n\
40 %s\
41 $upscope $end\n\
42 $enddefinitions $end\n\
43 $dumpvars\n";
44
45 const char *vcd_header_comment = "\
46 $comment\n  Acquisition with %d/%d probes at %s\n$end\n";
47
48
49 static int init(struct output *o)
50 {
51 /* Maximum header length */
52 #define MAX_HEADER_LEN 2048
53
54         struct context *ctx;
55         struct probe *probe;
56         GSList *l;
57         uint64_t samplerate;
58         int i, b, num_probes;
59         char *c, *samplerate_s;
60         char wbuf[1000], comment[128];
61
62         ctx = malloc(sizeof(struct context));
63         if (ctx == NULL)
64                 return SIGROK_ERR_MALLOC;
65         o->internal = ctx;
66         ctx->num_enabled_probes = 0;
67         for (l = o->device->probes; l; l = l->next) {
68                 probe = l->data;
69                 if (probe->enabled)
70                         ctx->probelist[ctx->num_enabled_probes++] = probe->name;
71         }
72
73         ctx->probelist[ctx->num_enabled_probes] = 0;
74         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
75
76         /* TODO: Allow for configuration via o->param. */
77
78         ctx->header = calloc(1, MAX_HEADER_LEN + 1);
79         if (ctx->header == NULL)
80                 return SIGROK_ERR_MALLOC;
81         num_probes = g_slist_length(o->device->probes);
82
83         if (o->device->plugin) {
84                 /* TODO: Handle num_probes == 0, too many probes, etc. */
85                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
86                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
87                 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
88                         return SIGROK_ERR;
89                 snprintf(comment, 127, vcd_header_comment, ctx->num_enabled_probes,
90                                 num_probes, samplerate_s);
91                 free(samplerate_s);
92         }
93         else
94                 comment[0] = '\0';
95
96         /* Wires / channels */
97         wbuf[0] = '\0';
98         for (i = 0; i < ctx->num_enabled_probes; i++) {
99                 c = (char *)&wbuf + strlen((char *)&wbuf);
100                 sprintf(c, "$var wire 1 %c channel%s $end\n",
101                         (char)('!' + i), ctx->probelist[i]);
102         }
103
104         /* TODO: Date: File or signals? Make y/n configurable. */
105         b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
106                         PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
107         /* TODO: Handle snprintf errors. */
108
109         ctx->prevbits = calloc(sizeof(int), num_probes);
110         if (ctx->prevbits == NULL)
111                 return SIGROK_ERR_MALLOC;
112
113         return 0;
114 }
115
116 static int event(struct output *o, int event_type, char **data_out,
117                  uint64_t *length_out)
118 {
119         struct context *ctx;
120         char *outbuf;
121         int outlen;
122
123         ctx = o->internal;
124         switch (event_type) {
125         case DF_TRIGGER:
126                 break;
127         case DF_END:
128                 outlen = strlen("$dumpoff\n$end\n");
129                 outbuf = malloc(outlen + 1);
130                 if (outbuf == NULL)
131                         return SIGROK_ERR_MALLOC;
132                 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
133                 *data_out = outbuf;
134                 *length_out = outlen;
135                 free(o->internal);
136                 o->internal = NULL;
137                 break;
138         }
139
140         return SIGROK_OK;
141 }
142
143 static int data(struct output *o, char *data_in, uint64_t length_in,
144                 char **data_out, uint64_t *length_out)
145 {
146         struct context *ctx;
147         unsigned int offset, outsize;
148         int p, curbit, prevbit;
149         uint64_t sample, prevsample;
150         char *outbuf, *c;
151
152         ctx = o->internal;
153         outsize = 0;
154         if (ctx->header)
155                 outsize = strlen(ctx->header);
156         outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
157         if (outbuf == NULL)
158                 return SIGROK_ERR_MALLOC;
159         if (ctx->header) {
160                 /* The header is still here, this must be the first packet. */
161                 strncpy(outbuf, ctx->header, outsize);
162                 free(ctx->header);
163                 ctx->header = NULL;
164         } else {
165                 outbuf[0] = 0;
166         }
167
168         /* TODO: Are disabled probes handled correctly? */
169
170         for (offset = 0; offset <= length_in - ctx->unitsize;
171              offset += ctx->unitsize) {
172                 memcpy(&sample, data_in + offset, ctx->unitsize);
173                 for (p = 0; p < ctx->num_enabled_probes; p++) {
174                         curbit = (sample & ((uint64_t) (1 << p))) != 0;
175                         if (offset == 0) {
176                                 prevbit = ~curbit;
177                         } else {
178                                 memcpy(&prevsample, data_in + offset - 1,
179                                        ctx->unitsize);
180                                 prevbit =
181                                     (prevsample & ((uint64_t) (1 << p))) != 0;
182                         }
183
184                         if (prevbit != curbit) {
185                                 /* FIXME: Only once per sample? */
186                                 c = outbuf + strlen(outbuf);
187                                 sprintf(c, "#%i\n", offset * 1 /* TODO */);
188
189                                 c = outbuf + strlen(outbuf);
190                                 sprintf(c, "%i%c\n", curbit,
191                                         (char)('!' + p /* FIXME? */));
192                         }
193                 }
194
195                 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
196         }
197
198         *data_out = outbuf;
199         *length_out = strlen(outbuf);
200
201         return SIGROK_OK;
202 }
203
204 struct output_format output_vcd = {
205         "vcd",
206         "Value Change Dump (VCD)",
207         init,
208         data,
209         event,
210 };