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