]>
Commit | Line | Data |
---|---|---|
4c9ffa83 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
4c9ffa83 UH |
3 | * |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
13d8e03c | 5 | * Copyright (C) 2013 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> | |
545f9786 | 25 | #include "config.h" /* Needed for PACKAGE and others. */ |
45c59c8b BV |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
4c9ffa83 | 28 | |
3544f848 | 29 | #define LOG_PREFIX "output/vcd" |
a944a84b | 30 | |
4c9ffa83 UH |
31 | struct context { |
32 | int num_enabled_probes; | |
a1be7b6c | 33 | GArray *probeindices; |
fbf1ff5d | 34 | GString *header; |
b050fc48 | 35 | uint8_t *prevsample; |
b33e7d70 HE |
36 | int period; |
37 | uint64_t samplerate; | |
b050fc48 | 38 | unsigned int unitsize; |
4c9ffa83 UH |
39 | }; |
40 | ||
d078d2e5 | 41 | static const char *vcd_header_comment = "\ |
7aae7462 BV |
42 | $comment\n Acquisition with %d/%d probes at %s\n$end\n"; |
43 | ||
f50f3f40 | 44 | static int init(struct sr_output *o) |
4c9ffa83 | 45 | { |
4c9ffa83 | 46 | struct context *ctx; |
1afe8989 | 47 | struct sr_probe *probe; |
4c9ffa83 | 48 | GSList *l; |
ec4063b8 | 49 | GVariant *gvar; |
b33e7d70 | 50 | int num_probes, i; |
fbf1ff5d | 51 | char *samplerate_s, *frequency_s, *timestamp; |
5cca9adb | 52 | time_t t; |
4c9ffa83 | 53 | |
133a37bf | 54 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
a944a84b | 55 | sr_err("%s: ctx malloc failed", __func__); |
e46b8fb1 | 56 | return SR_ERR_MALLOC; |
133a37bf | 57 | } |
757b8c62 | 58 | |
4c9ffa83 UH |
59 | o->internal = ctx; |
60 | ctx->num_enabled_probes = 0; | |
a1be7b6c | 61 | ctx->probeindices = g_array_new(FALSE, FALSE, sizeof(int)); |
757b8c62 | 62 | |
5c3c1241 | 63 | for (l = o->sdi->probes; l; l = l->next) { |
4c9ffa83 | 64 | probe = l->data; |
757b8c62 UH |
65 | if (!probe->enabled) |
66 | continue; | |
a1be7b6c ML |
67 | ctx->probeindices = g_array_append_val( |
68 | ctx->probeindices, probe->index); | |
ba6568c5 | 69 | ctx->num_enabled_probes++; |
4c9ffa83 | 70 | } |
fbf1ff5d | 71 | if (ctx->num_enabled_probes > 94) { |
a944a84b | 72 | sr_err("VCD only supports 94 probes."); |
e46b8fb1 | 73 | return SR_ERR; |
fbf1ff5d | 74 | } |
4c9ffa83 | 75 | |
b050fc48 | 76 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; |
fbf1ff5d | 77 | ctx->header = g_string_sized_new(512); |
5c3c1241 | 78 | num_probes = g_slist_length(o->sdi->probes); |
4c9ffa83 | 79 | |
fbf1ff5d BV |
80 | /* timestamp */ |
81 | t = time(NULL); | |
133a37bf | 82 | timestamp = g_strdup(ctime(&t)); |
fbf1ff5d BV |
83 | timestamp[strlen(timestamp)-1] = 0; |
84 | g_string_printf(ctx->header, "$date %s $end\n", timestamp); | |
133a37bf | 85 | g_free(timestamp); |
4c9ffa83 | 86 | |
fbf1ff5d BV |
87 | /* generator */ |
88 | g_string_append_printf(ctx->header, "$version %s %s $end\n", | |
89 | PACKAGE, PACKAGE_VERSION); | |
4c9ffa83 | 90 | |
d3c74a6f BV |
91 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, |
92 | &gvar) == SR_OK) { | |
ec4063b8 | 93 | ctx->samplerate = g_variant_get_uint64(gvar); |
af51a771 | 94 | g_variant_unref(gvar); |
a00ba012 | 95 | if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) { |
fbf1ff5d | 96 | g_string_free(ctx->header, TRUE); |
133a37bf | 97 | g_free(ctx); |
e46b8fb1 | 98 | return SR_ERR; |
6b5e3cee | 99 | } |
fbf1ff5d BV |
100 | g_string_append_printf(ctx->header, vcd_header_comment, |
101 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
133a37bf | 102 | g_free(samplerate_s); |
7aae7462 | 103 | } |
4c9ffa83 | 104 | |
fbf1ff5d BV |
105 | /* timescale */ |
106 | /* VCD can only handle 1/10/100 (s - fs), so scale up first */ | |
59df0c77 UH |
107 | if (ctx->samplerate > SR_MHZ(1)) |
108 | ctx->period = SR_GHZ(1); | |
109 | else if (ctx->samplerate > SR_KHZ(1)) | |
110 | ctx->period = SR_MHZ(1); | |
fbf1ff5d | 111 | else |
59df0c77 | 112 | ctx->period = SR_KHZ(1); |
a00ba012 | 113 | if (!(frequency_s = sr_period_string(ctx->period))) { |
fbf1ff5d | 114 | g_string_free(ctx->header, TRUE); |
133a37bf | 115 | g_free(ctx); |
e46b8fb1 | 116 | return SR_ERR; |
fbf1ff5d BV |
117 | } |
118 | g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s); | |
133a37bf | 119 | g_free(frequency_s); |
fbf1ff5d BV |
120 | |
121 | /* scope */ | |
122 | g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE); | |
123 | ||
4c9ffa83 | 124 | /* Wires / channels */ |
d601c0e9 ML |
125 | for (i = 0, l = o->sdi->probes; l; l = l->next, i++) { |
126 | probe = l->data; | |
127 | if (!probe->enabled) | |
128 | continue; | |
fbf1ff5d | 129 | g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n", |
d601c0e9 | 130 | (char)('!' + i), probe->name); |
4c9ffa83 UH |
131 | } |
132 | ||
fbf1ff5d BV |
133 | g_string_append(ctx->header, "$upscope $end\n" |
134 | "$enddefinitions $end\n$dumpvars\n"); | |
4c9ffa83 | 135 | |
b050fc48 | 136 | if (!(ctx->prevsample = g_try_malloc0(ctx->unitsize))) { |
fbf1ff5d | 137 | g_string_free(ctx->header, TRUE); |
133a37bf | 138 | g_free(ctx); |
b050fc48 | 139 | sr_err("%s: ctx->prevsample malloc failed", __func__); |
e46b8fb1 | 140 | return SR_ERR_MALLOC; |
6b5e3cee | 141 | } |
5a8fda15 | 142 | |
e46b8fb1 | 143 | return SR_OK; |
4c9ffa83 UH |
144 | } |
145 | ||
17f63de6 BV |
146 | static int receive(struct sr_output *o, const struct sr_dev_inst *sdi, |
147 | const struct sr_datafeed_packet *packet, GString **out) | |
4c9ffa83 | 148 | { |
d5585e32 | 149 | const struct sr_datafeed_logic *logic; |
4c9ffa83 | 150 | struct context *ctx; |
fbf1ff5d | 151 | unsigned int i; |
ba6568c5 | 152 | int p, curbit, prevbit, index; |
b050fc48 | 153 | uint8_t *sample; |
086eac7c | 154 | static uint64_t samplecount = 0; |
4c9ffa83 | 155 | |
d5585e32 BV |
156 | (void)sdi; |
157 | ||
17f63de6 | 158 | *out = NULL; |
d5585e32 | 159 | if (!o || !o->internal) |
17f63de6 | 160 | return SR_ERR_ARG; |
4c9ffa83 | 161 | ctx = o->internal; |
d5585e32 BV |
162 | |
163 | if (packet->type == SR_DF_END) { | |
17f63de6 BV |
164 | *out = g_string_new("$dumpoff\n$end\n"); |
165 | return SR_OK; | |
d5585e32 | 166 | } else if (packet->type != SR_DF_LOGIC) |
17f63de6 | 167 | return SR_OK; |
6b5e3cee | 168 | |
4c9ffa83 UH |
169 | if (ctx->header) { |
170 | /* The header is still here, this must be the first packet. */ | |
17f63de6 | 171 | *out = ctx->header; |
4c9ffa83 | 172 | ctx->header = NULL; |
d5585e32 | 173 | } else { |
17f63de6 | 174 | *out = g_string_sized_new(512); |
4c9ffa83 UH |
175 | } |
176 | ||
d5585e32 BV |
177 | logic = packet->payload; |
178 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { | |
086eac7c | 179 | samplecount++; |
08b488b8 | 180 | |
b050fc48 | 181 | sample = logic->data + i; |
08b488b8 | 182 | |
4c9ffa83 | 183 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
a1be7b6c | 184 | index = g_array_index(ctx->probeindices, int, p); |
b050fc48 | 185 | curbit = (sample[p / 8] & (((uint8_t) 1) << index)) >> index; |
34539700 | 186 | prevbit = (ctx->prevsample[p / 8] & (((uint8_t) 1) << index)) >> index; |
fbe2f794 UH |
187 | |
188 | /* VCD only contains deltas/changes of signals. */ | |
607b58de UH |
189 | if (prevbit == curbit) |
190 | continue; | |
4c9ffa83 | 191 | |
fbe2f794 | 192 | /* Output which signal changed to which value. */ |
17f63de6 | 193 | g_string_append_printf(*out, "#%" PRIu64 "\n%i%c\n", |
32c0551b | 194 | (uint64_t)(((float)samplecount / ctx->samplerate) |
b33e7d70 | 195 | * ctx->period), curbit, (char)('!' + p)); |
4c9ffa83 | 196 | } |
08b488b8 | 197 | |
b050fc48 | 198 | memcpy(ctx->prevsample, sample, ctx->unitsize); |
4c9ffa83 UH |
199 | } |
200 | ||
17f63de6 | 201 | return SR_OK; |
d5585e32 BV |
202 | } |
203 | ||
204 | static int cleanup(struct sr_output *o) | |
205 | { | |
206 | struct context *ctx; | |
207 | ||
208 | if (!o || !o->internal) | |
209 | return SR_ERR_ARG; | |
210 | ||
211 | ctx = o->internal; | |
212 | g_free(ctx); | |
4c9ffa83 | 213 | |
e46b8fb1 | 214 | return SR_OK; |
4c9ffa83 UH |
215 | } |
216 | ||
f50f3f40 | 217 | struct sr_output_format output_vcd = { |
cdb3573c | 218 | .id = "vcd", |
d494a4aa UH |
219 | .description = "Value Change Dump (VCD)", |
220 | .df_type = SR_DF_LOGIC, | |
221 | .init = init, | |
17f63de6 | 222 | .receive = receive, |
bbe6e336 | 223 | .cleanup = cleanup, |
4c9ffa83 | 224 | }; |