]>
Commit | Line | Data |
---|---|---|
4c9ffa83 UH |
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; | |
4c9ffa83 UH |
33 | }; |
34 | ||
35 | const char *vcd_header = "\ | |
36 | $date\n %s\n$end\n\ | |
7aae7462 | 37 | $version\n %s\n$end\n%s\ |
4c9ffa83 UH |
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 | ||
7aae7462 BV |
45 | const char *vcd_header_comment = "\ |
46 | $comment\n Acquisition with %d/%d probes at %s\n$end\n"; | |
47 | ||
5a8fda15 | 48 | static int init(struct output *o) |
4c9ffa83 UH |
49 | { |
50 | /* Maximum header length */ | |
51 | #define MAX_HEADER_LEN 2048 | |
52 | ||
53 | struct context *ctx; | |
54 | struct probe *probe; | |
55 | GSList *l; | |
56 | uint64_t samplerate; | |
57 | int i, b, num_probes; | |
25e7d9b1 | 58 | char *c, *samplerate_s; |
7aae7462 | 59 | char wbuf[1000], comment[128]; |
4c9ffa83 | 60 | |
6b5e3cee | 61 | if (!(ctx = calloc(1, sizeof(struct context)))) |
e31b636d | 62 | return SIGROK_ERR_MALLOC; |
4c9ffa83 UH |
63 | o->internal = ctx; |
64 | ctx->num_enabled_probes = 0; | |
65 | for (l = o->device->probes; l; l = l->next) { | |
66 | probe = l->data; | |
67 | if (probe->enabled) | |
68 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
69 | } | |
70 | ||
71 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
72 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
73 | ||
74 | /* TODO: Allow for configuration via o->param. */ | |
75 | ||
6b5e3cee UH |
76 | if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) { |
77 | free(ctx); | |
e31b636d | 78 | return SIGROK_ERR_MALLOC; |
6b5e3cee | 79 | } |
4c9ffa83 | 80 | num_probes = g_slist_length(o->device->probes); |
4c9ffa83 | 81 | |
6b5e3cee | 82 | comment[0] = '\0'; |
7aae7462 BV |
83 | if (o->device->plugin) { |
84 | /* TODO: Handle num_probes == 0, too many probes, etc. */ | |
6b5e3cee | 85 | /* TODO: Error handling. */ |
7aae7462 BV |
86 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
87 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
6b5e3cee UH |
88 | if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) { |
89 | free(ctx->header); | |
90 | free(ctx); | |
7aae7462 | 91 | return SIGROK_ERR; |
6b5e3cee UH |
92 | } |
93 | /* TODO: Handle sprintf() errors. */ | |
94 | snprintf(comment, 127, vcd_header_comment, | |
95 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
7aae7462 BV |
96 | free(samplerate_s); |
97 | } | |
4c9ffa83 UH |
98 | |
99 | /* Wires / channels */ | |
100 | wbuf[0] = '\0'; | |
bc010c05 | 101 | for (i = 0; i < ctx->num_enabled_probes; i++) { |
4c9ffa83 | 102 | c = (char *)&wbuf + strlen((char *)&wbuf); |
6b5e3cee UH |
103 | /* TODO: Needs fixing for very large number of probes. */ |
104 | /* TODO: Handle sprintf() errors. */ | |
bc010c05 | 105 | sprintf(c, "$var wire 1 %c channel%s $end\n", |
99c1fc59 | 106 | (char)('!' + i), ctx->probelist[i]); |
4c9ffa83 UH |
107 | } |
108 | ||
99c1fc59 | 109 | /* TODO: Date: File or signals? Make y/n configurable. */ |
4c9ffa83 | 110 | b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date", |
6b5e3cee UH |
111 | PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf); |
112 | /* TODO: Handle snprintf() errors. */ | |
4c9ffa83 | 113 | |
6b5e3cee UH |
114 | if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) { |
115 | free(ctx->header); | |
116 | free(ctx); | |
e31b636d | 117 | return SIGROK_ERR_MALLOC; |
6b5e3cee | 118 | } |
5a8fda15 | 119 | |
6b5e3cee | 120 | return SIGROK_OK; |
4c9ffa83 UH |
121 | } |
122 | ||
123 | static int event(struct output *o, int event_type, char **data_out, | |
124 | uint64_t *length_out) | |
125 | { | |
126 | struct context *ctx; | |
127 | char *outbuf; | |
128 | int outlen; | |
129 | ||
130 | ctx = o->internal; | |
99c1fc59 | 131 | switch (event_type) { |
4c9ffa83 | 132 | case DF_TRIGGER: |
6b5e3cee | 133 | /* TODO */ |
4c9ffa83 UH |
134 | break; |
135 | case DF_END: | |
136 | outlen = strlen("$dumpoff\n$end\n"); | |
6b5e3cee | 137 | if (!(outbuf = malloc(outlen + 1))) |
e31b636d | 138 | return SIGROK_ERR_MALLOC; |
6b5e3cee UH |
139 | /* TODO: Bug? Drop the + 1? */ |
140 | /* TODO: Handle snprintf() errors. */ | |
bc010c05 | 141 | snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n"); |
4c9ffa83 UH |
142 | *data_out = outbuf; |
143 | *length_out = outlen; | |
144 | free(o->internal); | |
145 | o->internal = NULL; | |
146 | break; | |
147 | } | |
148 | ||
149 | return SIGROK_OK; | |
150 | } | |
151 | ||
152 | static int data(struct output *o, char *data_in, uint64_t length_in, | |
153 | char **data_out, uint64_t *length_out) | |
154 | { | |
155 | struct context *ctx; | |
607b58de | 156 | unsigned int i, outsize; |
afc8e4de | 157 | int p, curbit, prevbit; |
4c9ffa83 UH |
158 | uint64_t sample, prevsample; |
159 | char *outbuf, *c; | |
160 | ||
161 | ctx = o->internal; | |
e273a904 HE |
162 | outsize = 0; |
163 | if (ctx->header) | |
164 | outsize = strlen(ctx->header); | |
6b5e3cee UH |
165 | |
166 | /* FIXME: Use realloc(). */ | |
167 | if (!(outbuf = calloc(1, outsize + 1 + 10000))) | |
168 | return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */ | |
169 | ||
170 | outbuf[0] = '\0'; | |
4c9ffa83 UH |
171 | if (ctx->header) { |
172 | /* The header is still here, this must be the first packet. */ | |
173 | strncpy(outbuf, ctx->header, outsize); | |
174 | free(ctx->header); | |
175 | ctx->header = NULL; | |
4c9ffa83 UH |
176 | } |
177 | ||
178 | /* TODO: Are disabled probes handled correctly? */ | |
179 | ||
607b58de UH |
180 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { |
181 | memcpy(&sample, data_in + i, ctx->unitsize); | |
4c9ffa83 UH |
182 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
183 | curbit = (sample & ((uint64_t) (1 << p))) != 0; | |
607b58de | 184 | if (i == 0) { |
4c9ffa83 UH |
185 | prevbit = ~curbit; |
186 | } else { | |
607b58de | 187 | memcpy(&prevsample, data_in + i - 1, |
99c1fc59 UH |
188 | ctx->unitsize); |
189 | prevbit = | |
190 | (prevsample & ((uint64_t) (1 << p))) != 0; | |
4c9ffa83 UH |
191 | } |
192 | ||
607b58de UH |
193 | /* VCD only contains deltas/changes. */ |
194 | if (prevbit == curbit) | |
195 | continue; | |
4c9ffa83 | 196 | |
607b58de UH |
197 | /* FIXME: Only once per sample? */ |
198 | /* TODO: Is 'i' correct here? */ | |
199 | c = outbuf + strlen(outbuf); | |
200 | sprintf(c, "#%i\n%i%c\n", i, curbit, (char)('!' + p)); | |
4c9ffa83 UH |
201 | } |
202 | ||
99c1fc59 | 203 | /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */ |
4c9ffa83 UH |
204 | } |
205 | ||
206 | *data_out = outbuf; | |
207 | *length_out = strlen(outbuf); | |
208 | ||
209 | return SIGROK_OK; | |
210 | } | |
211 | ||
212 | struct output_format output_vcd = { | |
213 | "vcd", | |
214 | "Value Change Dump (VCD)", | |
215 | init, | |
216 | data, | |
217 | event, | |
218 | }; |