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