]>
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> | |
fbf1ff5d | 5 | * Copyright (C) 2011 Bert Vermeulen <bert@biot.com> |
4c9ffa83 UH |
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 <sigrok.h> | |
26 | #include "config.h" | |
27 | ||
28 | struct context { | |
29 | int num_enabled_probes; | |
30 | int unitsize; | |
31 | char *probelist[65]; | |
32 | int *prevbits; | |
fbf1ff5d | 33 | GString *header; |
08b488b8 | 34 | uint64_t prevsample; |
b33e7d70 HE |
35 | int period; |
36 | uint64_t samplerate; | |
4c9ffa83 UH |
37 | }; |
38 | ||
39 | const char *vcd_header = "\ | |
fbf1ff5d BV |
40 | $date %s $end\n\ |
41 | $version %s $end\n%s\ | |
42 | $timescale %s $end\n\ | |
4c9ffa83 UH |
43 | $scope module %s $end\n\ |
44 | %s\ | |
45 | $upscope $end\n\ | |
46 | $enddefinitions $end\n\ | |
47 | $dumpvars\n"; | |
48 | ||
7aae7462 BV |
49 | const char *vcd_header_comment = "\ |
50 | $comment\n Acquisition with %d/%d probes at %s\n$end\n"; | |
51 | ||
f50f3f40 | 52 | static int init(struct sr_output *o) |
4c9ffa83 | 53 | { |
4c9ffa83 | 54 | struct context *ctx; |
1afe8989 | 55 | struct sr_probe *probe; |
4c9ffa83 | 56 | GSList *l; |
b33e7d70 | 57 | int num_probes, i; |
fbf1ff5d | 58 | char *samplerate_s, *frequency_s, *timestamp; |
5cca9adb | 59 | time_t t; |
4c9ffa83 | 60 | |
6b5e3cee | 61 | if (!(ctx = calloc(1, sizeof(struct context)))) |
e46b8fb1 | 62 | return SR_ERR_MALLOC; |
757b8c62 | 63 | |
4c9ffa83 UH |
64 | o->internal = ctx; |
65 | ctx->num_enabled_probes = 0; | |
757b8c62 | 66 | |
4c9ffa83 UH |
67 | for (l = o->device->probes; l; l = l->next) { |
68 | probe = l->data; | |
757b8c62 UH |
69 | if (!probe->enabled) |
70 | continue; | |
71 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
4c9ffa83 | 72 | } |
fbf1ff5d BV |
73 | if (ctx->num_enabled_probes > 94) { |
74 | g_warning("VCD only supports 94 probes."); | |
e46b8fb1 | 75 | return SR_ERR; |
fbf1ff5d | 76 | } |
4c9ffa83 UH |
77 | |
78 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
79 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
fbf1ff5d BV |
80 | ctx->header = g_string_sized_new(512); |
81 | num_probes = g_slist_length(o->device->probes); | |
4c9ffa83 | 82 | |
fbf1ff5d BV |
83 | /* timestamp */ |
84 | t = time(NULL); | |
85 | timestamp = strdup(ctime(&t)); | |
86 | timestamp[strlen(timestamp)-1] = 0; | |
87 | g_string_printf(ctx->header, "$date %s $end\n", timestamp); | |
88 | free(timestamp); | |
4c9ffa83 | 89 | |
fbf1ff5d BV |
90 | /* generator */ |
91 | g_string_append_printf(ctx->header, "$version %s %s $end\n", | |
92 | PACKAGE, PACKAGE_VERSION); | |
4c9ffa83 | 93 | |
2bf4aca6 | 94 | if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) { |
b33e7d70 | 95 | ctx->samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
5a2326a7 | 96 | o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); |
a00ba012 | 97 | if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) { |
fbf1ff5d | 98 | g_string_free(ctx->header, TRUE); |
6b5e3cee | 99 | free(ctx); |
e46b8fb1 | 100 | return SR_ERR; |
6b5e3cee | 101 | } |
fbf1ff5d BV |
102 | g_string_append_printf(ctx->header, vcd_header_comment, |
103 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
7aae7462 BV |
104 | free(samplerate_s); |
105 | } | |
4c9ffa83 | 106 | |
fbf1ff5d BV |
107 | /* timescale */ |
108 | /* VCD can only handle 1/10/100 (s - fs), so scale up first */ | |
b33e7d70 HE |
109 | if (ctx->samplerate > MHZ(1)) |
110 | ctx->period = GHZ(1); | |
111 | else if (ctx->samplerate > KHZ(1)) | |
112 | ctx->period = MHZ(1); | |
fbf1ff5d | 113 | else |
b33e7d70 | 114 | ctx->period = KHZ(1); |
a00ba012 | 115 | if (!(frequency_s = sr_period_string(ctx->period))) { |
fbf1ff5d BV |
116 | g_string_free(ctx->header, TRUE); |
117 | free(ctx); | |
e46b8fb1 | 118 | return SR_ERR; |
fbf1ff5d BV |
119 | } |
120 | g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s); | |
121 | free(frequency_s); | |
122 | ||
123 | /* scope */ | |
124 | g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE); | |
125 | ||
4c9ffa83 | 126 | /* Wires / channels */ |
bc010c05 | 127 | for (i = 0; i < ctx->num_enabled_probes; i++) { |
fbf1ff5d BV |
128 | g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n", |
129 | (char)('!' + i), ctx->probelist[i]); | |
4c9ffa83 UH |
130 | } |
131 | ||
fbf1ff5d BV |
132 | g_string_append(ctx->header, "$upscope $end\n" |
133 | "$enddefinitions $end\n$dumpvars\n"); | |
4c9ffa83 | 134 | |
6b5e3cee | 135 | if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) { |
fbf1ff5d | 136 | g_string_free(ctx->header, TRUE); |
6b5e3cee | 137 | free(ctx); |
e46b8fb1 | 138 | return SR_ERR_MALLOC; |
6b5e3cee | 139 | } |
5a8fda15 | 140 | |
e46b8fb1 | 141 | return SR_OK; |
4c9ffa83 UH |
142 | } |
143 | ||
f50f3f40 | 144 | static int event(struct sr_output *o, int event_type, char **data_out, |
4c9ffa83 UH |
145 | uint64_t *length_out) |
146 | { | |
147 | struct context *ctx; | |
148 | char *outbuf; | |
4c9ffa83 UH |
149 | |
150 | ctx = o->internal; | |
99c1fc59 | 151 | switch (event_type) { |
5a2326a7 | 152 | case SR_DF_END: |
fbf1ff5d | 153 | outbuf = strdup("$dumpoff\n$end\n"); |
4c9ffa83 | 154 | *data_out = outbuf; |
fbf1ff5d | 155 | *length_out = strlen(outbuf); |
4c9ffa83 UH |
156 | free(o->internal); |
157 | o->internal = NULL; | |
158 | break; | |
9ab95e54 BV |
159 | default: |
160 | *data_out = NULL; | |
161 | *length_out = 0; | |
162 | break; | |
4c9ffa83 UH |
163 | } |
164 | ||
e46b8fb1 | 165 | return SR_OK; |
4c9ffa83 UH |
166 | } |
167 | ||
f50f3f40 | 168 | static int data(struct sr_output *o, char *data_in, uint64_t length_in, |
4c9ffa83 UH |
169 | char **data_out, uint64_t *length_out) |
170 | { | |
171 | struct context *ctx; | |
fbf1ff5d | 172 | unsigned int i; |
afc8e4de | 173 | int p, curbit, prevbit; |
08b488b8 | 174 | uint64_t sample; |
086eac7c | 175 | static uint64_t samplecount = 0; |
fbf1ff5d | 176 | GString *out; |
08b488b8 | 177 | int first_sample = 0; |
4c9ffa83 UH |
178 | |
179 | ctx = o->internal; | |
fbf1ff5d | 180 | out = g_string_sized_new(512); |
6b5e3cee | 181 | |
4c9ffa83 UH |
182 | if (ctx->header) { |
183 | /* The header is still here, this must be the first packet. */ | |
fbf1ff5d BV |
184 | g_string_append(out, ctx->header->str); |
185 | g_string_free(ctx->header, TRUE); | |
4c9ffa83 | 186 | ctx->header = NULL; |
08b488b8 | 187 | first_sample = 1; |
4c9ffa83 UH |
188 | } |
189 | ||
607b58de | 190 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { |
086eac7c | 191 | samplecount++; |
08b488b8 | 192 | |
607b58de | 193 | memcpy(&sample, data_in + i, ctx->unitsize); |
08b488b8 HE |
194 | |
195 | if (first_sample) { | |
196 | /* First packet. We neg to make sure sample is stored. */ | |
197 | ctx->prevsample = ~sample; | |
198 | first_sample = 0; | |
199 | } | |
200 | ||
4c9ffa83 | 201 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
fbe2f794 | 202 | curbit = (sample & ((uint64_t) (1 << p))) >> p; |
08b488b8 | 203 | prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p; |
fbe2f794 UH |
204 | |
205 | /* VCD only contains deltas/changes of signals. */ | |
607b58de UH |
206 | if (prevbit == curbit) |
207 | continue; | |
4c9ffa83 | 208 | |
fbe2f794 | 209 | /* Output which signal changed to which value. */ |
b33e7d70 | 210 | g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n", |
32c0551b | 211 | (uint64_t)(((float)samplecount / ctx->samplerate) |
b33e7d70 | 212 | * ctx->period), curbit, (char)('!' + p)); |
4c9ffa83 | 213 | } |
08b488b8 HE |
214 | |
215 | ctx->prevsample = sample; | |
4c9ffa83 UH |
216 | } |
217 | ||
fbf1ff5d BV |
218 | *data_out = out->str; |
219 | *length_out = out->len; | |
220 | g_string_free(out, FALSE); | |
4c9ffa83 | 221 | |
e46b8fb1 | 222 | return SR_OK; |
4c9ffa83 UH |
223 | } |
224 | ||
f50f3f40 | 225 | struct sr_output_format output_vcd = { |
4c9ffa83 UH |
226 | "vcd", |
227 | "Value Change Dump (VCD)", | |
5a2326a7 | 228 | SR_DF_LOGIC, |
4c9ffa83 UH |
229 | init, |
230 | data, | |
231 | event, | |
232 | }; |