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