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