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