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