2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define DEFAULT_BPL_BITS 64
27 #define DEFAULT_BPL_HEX 256
30 unsigned int num_enabled_probes;
32 unsigned int unitsize;
42 static void flush_linebufs(struct context *ctx, char *outbuf)
44 static int max_probename_len = 0;
47 if (ctx->linebuf[0] == 0)
50 if (max_probename_len == 0) {
51 /* First time through... */
52 for (i = 0; ctx->probelist[i]; i++) {
53 len = strlen(ctx->probelist[i]);
54 if (len > max_probename_len)
55 max_probename_len = len;
59 for (i = 0; ctx->probelist[i]; i++) {
60 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
61 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
63 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
66 static int init(struct output *o, int default_spl)
75 ctx = malloc(sizeof(struct context));
77 ctx->num_enabled_probes = 0;
79 for (l = o->device->probes; l; l = l->next) {
82 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
85 ctx->probelist[ctx->num_enabled_probes] = 0;
86 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
90 if (o->param && o->param[0])
91 ctx->samples_per_line = strtoul(o->param, NULL, 10);
93 ctx->samples_per_line = default_spl;
95 ctx->header = malloc(512);
96 num_probes = g_slist_length(o->device->probes);
97 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
98 o->device->plugin_index, DI_CUR_SAMPLERATE));
99 snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ",
100 ctx->num_enabled_probes, num_probes);
102 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
103 return -1; /* FIXME */
104 snprintf(ctx->header + strlen(ctx->header), 512, "%s\n", samplerate_s);
107 ctx->linebuf_len = ctx->samples_per_line * 2;
108 ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len);
109 ctx->linevalues = calloc(1, num_probes);
114 static int event(struct output *o, int event_type, char **data_out,
115 uint64_t *length_out)
122 switch (event_type) {
126 outsize = ctx->num_enabled_probes
127 * (ctx->samples_per_line + 20) + 512;
128 outbuf = calloc(1, outsize);
129 flush_linebufs(ctx, outbuf);
131 *length_out = strlen(outbuf);
140 static int init_bits(struct output *o)
142 return init(o, DEFAULT_BPL_BITS);
145 static int data_bits(struct output *o, char *data_in, uint64_t length_in,
146 char **data_out, uint64_t *length_out)
149 unsigned int outsize, offset, p;
154 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
155 ctx->samples_per_line + 512;
156 outbuf = calloc(1, outsize + 1);
158 /* The header is still here, this must be the first packet. */
159 strncpy(outbuf, ctx->header, outsize);
165 if (length_in >= ctx->unitsize) {
166 for (offset = 0; offset <= length_in - ctx->unitsize;
167 offset += ctx->unitsize) {
168 memcpy(&sample, data_in + offset, ctx->unitsize);
169 for (p = 0; p < ctx->num_enabled_probes; p++) {
170 if (sample & ((uint64_t) 1 << p))
171 ctx->linebuf[p * ctx->linebuf_len +
172 ctx->line_offset] = '1';
174 ctx->linebuf[p * ctx->linebuf_len +
175 ctx->line_offset] = '0';
180 /* Add a space every 8th bit. */
181 if ((ctx->spl_cnt & 7) == 0) {
182 for (p = 0; p < ctx->num_enabled_probes; p++)
183 ctx->linebuf[p * ctx->linebuf_len +
184 ctx->line_offset] = ' ';
189 if (ctx->spl_cnt >= ctx->samples_per_line) {
190 flush_linebufs(ctx, outbuf);
191 ctx->line_offset = ctx->spl_cnt = 0;
195 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
198 *length_out = strlen(outbuf);
203 static int init_hex(struct output *o)
205 return init(o, DEFAULT_BPL_BITS);
208 static int data_hex(struct output *o, char *data_in, uint64_t length_in,
209 char **data_out, uint64_t *length_out)
212 unsigned int outsize, offset, p;
217 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
218 ctx->samples_per_line + 512;
219 outbuf = calloc(1, outsize + 1);
221 /* The header is still here, this must be the first packet. */
222 strncpy(outbuf, ctx->header, outsize);
228 ctx->line_offset = 0;
229 for (offset = 0; offset <= length_in - ctx->unitsize;
230 offset += ctx->unitsize) {
231 memcpy(&sample, data_in + offset, ctx->unitsize);
232 for (p = 0; p < ctx->num_enabled_probes; p++) {
233 ctx->linevalues[p] <<= 1;
234 if (sample & ((uint64_t) 1 << p))
235 ctx->linevalues[p] |= 1;
236 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
237 ctx->line_offset, "%.2x", ctx->linevalues[p]);
241 /* Add a space after every complete hex byte. */
242 if ((ctx->spl_cnt & 7) == 0) {
243 for (p = 0; p < ctx->num_enabled_probes; p++)
244 ctx->linebuf[p * ctx->linebuf_len +
245 ctx->line_offset + 2] = ' ';
246 ctx->line_offset += 3;
250 if (ctx->spl_cnt >= ctx->samples_per_line) {
251 flush_linebufs(ctx, outbuf);
252 ctx->line_offset = ctx->spl_cnt = 0;
257 *length_out = strlen(outbuf);
262 struct output_format output_text_bits = {
270 struct output_format output_text_hex = {
272 "Text (hexadecimal)",