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