2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
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 3 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #define DEFAULT_BPL_BITS 64
29 #define DEFAULT_BPL_HEX 192
30 #define DEFAULT_BPL_ASCII 74
39 unsigned int num_enabled_probes;
41 unsigned int unitsize;
54 static void flush_linebufs(struct context *ctx, char *outbuf)
56 static int max_probename_len = 0;
59 if (ctx->linebuf[0] == 0)
62 if (max_probename_len == 0) {
63 /* First time through... */
64 for (i = 0; ctx->probelist[i]; i++) {
65 len = strlen(ctx->probelist[i]);
66 if (len > max_probename_len)
67 max_probename_len = len;
71 for (i = 0; ctx->probelist[i]; i++) {
72 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
73 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
76 /* Mark trigger with a ^ character. */
77 if (ctx->mark_trigger != -1)
79 int space_offset = ctx->mark_trigger / 8;
81 if (ctx->mode == MODE_ASCII)
84 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
85 ctx->mark_trigger + space_offset, "");
88 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
91 static int init(struct output *o, int default_spl, enum outputmode mode)
100 if (!(ctx = calloc(1, sizeof(struct context))))
101 return SIGROK_ERR_MALLOC;
104 ctx->num_enabled_probes = 0;
106 for (l = o->device->probes; l; l = l->next) {
110 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
113 ctx->probelist[ctx->num_enabled_probes] = 0;
114 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
115 ctx->line_offset = 0;
117 ctx->mark_trigger = -1;
120 if (o->param && o->param[0]) {
121 ctx->samples_per_line = strtoul(o->param, NULL, 10);
122 if (ctx->samples_per_line < 1)
125 ctx->samples_per_line = default_spl;
127 if (!(ctx->header = malloc(512))) {
129 return SIGROK_ERR_MALLOC;
132 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
133 num_probes = g_slist_length(o->device->probes);
134 if (o->device->plugin) {
135 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
136 o->device->plugin_index, DI_CUR_SAMPLERATE));
137 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
142 snprintf(ctx->header + strlen(ctx->header),
143 511 - strlen(ctx->header),
144 "Acquisition with %d/%d probes at %s\n",
145 ctx->num_enabled_probes, num_probes, samplerate_s);
149 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
150 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
153 return SIGROK_ERR_MALLOC;
155 if (!(ctx->linevalues = calloc(1, num_probes))) {
158 return SIGROK_ERR_MALLOC;
164 static int event(struct output *o, int event_type, char **data_out,
165 uint64_t *length_out)
172 switch (event_type) {
174 ctx->mark_trigger = ctx->spl_cnt;
179 outsize = ctx->num_enabled_probes
180 * (ctx->samples_per_line + 20) + 512;
181 if (!(outbuf = calloc(1, outsize)))
182 return SIGROK_ERR_MALLOC;
183 flush_linebufs(ctx, outbuf);
185 *length_out = strlen(outbuf);
198 static int init_bits(struct output *o)
200 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
203 static int data_bits(struct output *o, char *data_in, uint64_t length_in,
204 char **data_out, uint64_t *length_out)
207 unsigned int outsize, offset, p;
213 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
214 + ctx->samples_per_line / 8;
216 * Calculate space needed for probes. Set aside 512 bytes for
217 * extra output, e.g. trigger.
219 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
220 * (ctx->num_enabled_probes * max_linelen);
222 if (!(outbuf = calloc(1, outsize + 1)))
223 return SIGROK_ERR_MALLOC;
227 /* The header is still here, this must be the first packet. */
228 strncpy(outbuf, ctx->header, outsize);
232 /* Ensure first transition. */
233 memcpy(&ctx->prevsample, data_in, ctx->unitsize);
234 ctx->prevsample = ~ctx->prevsample;
237 if (length_in >= ctx->unitsize) {
238 for (offset = 0; offset <= length_in - ctx->unitsize;
239 offset += ctx->unitsize) {
240 memcpy(&sample, data_in + offset, ctx->unitsize);
241 for (p = 0; p < ctx->num_enabled_probes; p++) {
242 c = (sample & ((uint64_t) 1 << p)) ? '1' : '0';
243 ctx->linebuf[p * ctx->linebuf_len +
244 ctx->line_offset] = c;
249 /* Add a space every 8th bit. */
250 if ((ctx->spl_cnt & 7) == 0) {
251 for (p = 0; p < ctx->num_enabled_probes; p++)
252 ctx->linebuf[p * ctx->linebuf_len +
253 ctx->line_offset] = ' ';
258 if (ctx->spl_cnt >= ctx->samples_per_line) {
259 flush_linebufs(ctx, outbuf);
260 ctx->line_offset = ctx->spl_cnt = 0;
261 ctx->mark_trigger = -1;
265 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
269 *length_out = strlen(outbuf);
274 static int init_hex(struct output *o)
276 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
279 static int data_hex(struct output *o, char *data_in, uint64_t length_in,
280 char **data_out, uint64_t *length_out)
283 unsigned int outsize, offset, p;
289 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
290 + ctx->samples_per_line / 2;
291 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
292 / ctx->samples_per_line * max_linelen + 512;
294 if (!(outbuf = calloc(1, outsize + 1)))
295 return SIGROK_ERR_MALLOC;
299 /* The header is still here, this must be the first packet. */
300 strncpy(outbuf, ctx->header, outsize);
305 ctx->line_offset = 0;
306 for (offset = 0; offset <= length_in - ctx->unitsize;
307 offset += ctx->unitsize) {
308 memcpy(&sample, data_in + offset, ctx->unitsize);
309 for (p = 0; p < ctx->num_enabled_probes; p++) {
310 ctx->linevalues[p] <<= 1;
311 if (sample & ((uint64_t) 1 << p))
312 ctx->linevalues[p] |= 1;
313 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
314 ctx->line_offset, "%.2x", ctx->linevalues[p]);
318 /* Add a space after every complete hex byte. */
319 if ((ctx->spl_cnt & 7) == 0) {
320 for (p = 0; p < ctx->num_enabled_probes; p++)
321 ctx->linebuf[p * ctx->linebuf_len +
322 ctx->line_offset + 2] = ' ';
323 ctx->line_offset += 3;
327 if (ctx->spl_cnt >= ctx->samples_per_line) {
328 flush_linebufs(ctx, outbuf);
329 ctx->line_offset = ctx->spl_cnt = 0;
334 *length_out = strlen(outbuf);
339 static int init_ascii(struct output *o)
341 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
344 static int data_ascii(struct output *o, char *data_in, uint64_t length_in,
345 char **data_out, uint64_t *length_out)
348 unsigned int outsize, offset, p;
354 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
355 + ctx->samples_per_line / 8;
357 * Calculate space needed for probes. Set aside 512 bytes for
358 * extra output, e.g. trigger.
360 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
361 * (ctx->num_enabled_probes * max_linelen);
363 if (!(outbuf = calloc(1, outsize + 1)))
364 return SIGROK_ERR_MALLOC;
368 /* The header is still here, this must be the first packet. */
369 strncpy(outbuf, ctx->header, outsize);
374 if (length_in >= ctx->unitsize) {
375 for (offset = 0; offset <= length_in - ctx->unitsize;
376 offset += ctx->unitsize) {
377 memcpy(&sample, data_in + offset, ctx->unitsize);
379 char tmpval[ctx->num_enabled_probes];
381 for (p = 0; p < ctx->num_enabled_probes; p++) {
382 uint64_t curbit = (sample & ((uint64_t) 1 << p));
383 uint64_t prevbit = (ctx->prevsample &
384 ((uint64_t) 1 << p));
386 if (curbit < prevbit && ctx->line_offset > 0) {
387 ctx->linebuf[p * ctx->linebuf_len +
388 ctx->line_offset-1] = '\\';
391 if (curbit > prevbit) {
402 if (ctx->spl_cnt >= ctx->samples_per_line) {
403 flush_linebufs(ctx, outbuf);
404 ctx->line_offset = ctx->spl_cnt = 0;
405 ctx->mark_trigger = -1;
408 for (p = 0; p < ctx->num_enabled_probes; p++) {
409 ctx->linebuf[p * ctx->linebuf_len +
410 ctx->line_offset] = tmpval[p];
416 ctx->prevsample = sample;
419 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
423 *length_out = strlen(outbuf);
429 struct output_format output_text_bits = {
431 "Bits (takes argument, default 64)",
438 struct output_format output_text_hex = {
440 "Hexadecimal (takes argument, default 192)",
447 struct output_format output_text_ascii = {
449 "ASCII (takes argument, default 74)",