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