]> sigrok.org Git - libsigrok.git/blame - output/output_vcd.c
More consistent spelling of "samplerate".
[libsigrok.git] / output / output_vcd.c
CommitLineData
4c9ffa83
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
24#include <sigrok.h>
25#include "config.h"
26
27struct context {
28 int num_enabled_probes;
29 int unitsize;
30 char *probelist[65];
31 int *prevbits;
32 char *header;
33 char *data;
34};
35
36const char *vcd_header = "\
37$date\n %s\n$end\n\
38$version\n %s\n$end\n\
39$comment\n Acquisition with %d/%d probes at %s\n$end\n\
40$timescale\n %i %s\n$end\n\
41$scope module %s $end\n\
42%s\
43$upscope $end\n\
44$enddefinitions $end\n\
45$dumpvars\n";
46
47static void init(struct output *o)
48{
49/* Maximum header length */
50#define MAX_HEADER_LEN 2048
51
52 struct context *ctx;
53 struct probe *probe;
54 GSList *l;
55 uint64_t samplerate;
56 int i, b, num_probes;
57 char *c;
58 char sbuf[10], wbuf[1000];
59
60 ctx = malloc(sizeof(struct context));
61 o->internal = ctx;
62 ctx->num_enabled_probes = 0;
63 for (l = o->device->probes; l; l = l->next) {
64 probe = l->data;
65 if (probe->enabled)
66 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
67 }
68
69 ctx->probelist[ctx->num_enabled_probes] = 0;
70 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
71
72 /* TODO: Allow for configuration via o->param. */
73
74 ctx->header = calloc(1, MAX_HEADER_LEN + 1);
75 num_probes = g_slist_length(o->device->probes);
76 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
4c100f32 77 o->device->plugin_index, DI_CUR_SAMPLERATE));
4c9ffa83
UH
78
79 /* Samplerate string */
80 if (samplerate >= GHZ(1))
81 snprintf(sbuf, 10, "%"PRIu64" GHz", samplerate / 1000000000);
82 else if (samplerate >= MHZ(1))
83 snprintf(sbuf, 10, "%"PRIu64" MHz", samplerate / 1000000);
84 else if (samplerate >= KHZ(1))
85 snprintf(sbuf, 10, "%"PRIu64" KHz", samplerate / 1000);
86 else
87 snprintf(sbuf, 10, "%"PRIu64" Hz", samplerate);
88
89 /* Wires / channels */
90 wbuf[0] = '\0';
91 for (i = 0; i < num_probes; i++) {
92 c = (char *)&wbuf + strlen((char *)&wbuf);
93 sprintf(c, "$var wire 1 %c channel%i $end\n",
94 (char)('!' + i), i);
95 }
96
97 /* TODO: date: File or signals? Make y/n configurable. */
98 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
99 PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
100 (char *)&sbuf, 1, "ns", PACKAGE, (char *)&wbuf);
101
102 ctx->prevbits = calloc(sizeof(int), num_probes);
103}
104
105static int event(struct output *o, int event_type, char **data_out,
106 uint64_t *length_out)
107{
108 struct context *ctx;
109 char *outbuf;
110 int outlen;
111
112 ctx = o->internal;
113 switch(event_type) {
114 case DF_TRIGGER:
115 break;
116 case DF_END:
117 outlen = strlen("$dumpoff\n$end\n");
118 outbuf = malloc(outlen + 1);
119 snprintf(outbuf, outlen, "$dumpoff\n$end\n");
120 *data_out = outbuf;
121 *length_out = outlen;
122 free(o->internal);
123 o->internal = NULL;
124 break;
125 }
126
127 return SIGROK_OK;
128}
129
130static int data(struct output *o, char *data_in, uint64_t length_in,
131 char **data_out, uint64_t *length_out)
132{
133 struct context *ctx;
134 int offset, outsize, p, curbit, prevbit;
135 uint64_t sample, prevsample;
136 char *outbuf, *c;
137
138 ctx = o->internal;
139 outsize = strlen(ctx->header);
140 outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
141 if (ctx->header) {
142 /* The header is still here, this must be the first packet. */
143 strncpy(outbuf, ctx->header, outsize);
144 free(ctx->header);
145 ctx->header = NULL;
146 } else {
147 outbuf[0] = 0;
148 }
149
150 /* TODO: Are disabled probes handled correctly? */
151
152 for (offset = 0; offset <= length_in - ctx->unitsize;
153 offset += ctx->unitsize) {
154 memcpy(&sample, data_in + offset, ctx->unitsize);
155 for (p = 0; p < ctx->num_enabled_probes; p++) {
156 curbit = (sample & ((uint64_t) (1 << p))) != 0;
157 if (offset == 0) {
158 prevbit = ~curbit;
159 } else {
160 memcpy(&prevsample, data_in + offset - 1, ctx->unitsize);
161 prevbit = (prevsample & ((uint64_t) (1 << p))) != 0;
162 }
163
164 if (prevbit != curbit) {
165 /* FIXME: Only once per sample? */
166 c = outbuf + strlen(outbuf);
167 sprintf(c, "#%i\n", offset * 1 /* TODO */);
168
169 c = outbuf + strlen(outbuf);
170 sprintf(c, "%i%c\n", curbit, (char)('!' + p /* FIXME? */));
171 }
172 }
173
174 /* TODO: Do a realloc() here if strlen(outbuf) is almost "full"... */
175 }
176
177 *data_out = outbuf;
178 *length_out = strlen(outbuf);
179
180 return SIGROK_OK;
181}
182
183struct output_format output_vcd = {
184 "vcd",
185 "Value Change Dump (VCD)",
186 init,
187 data,
188 event,
189};