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/>.
27 #define DEFAULT_BPL_BITS 64
28 #define DEFAULT_BPL_HEX 256
31 unsigned int num_enabled_probes;
33 unsigned int unitsize;
44 static void flush_linebufs(struct context *ctx, char *outbuf)
46 static int max_probename_len = 0;
49 if (ctx->linebuf[0] == 0)
52 if (max_probename_len == 0) {
53 /* First time through... */
54 for (i = 0; ctx->probelist[i]; i++) {
55 len = strlen(ctx->probelist[i]);
56 if (len > max_probename_len)
57 max_probename_len = len;
61 for (i = 0; ctx->probelist[i]; i++) {
62 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
63 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
66 /* Mark trigger with a ^ character. */
67 if (ctx->mark_trigger != -1)
68 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
69 ctx->mark_trigger + (ctx->mark_trigger / 8), "");
71 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
74 static int init(struct output *o, int default_spl)
83 if (!(ctx = calloc(1, sizeof(struct context))))
84 return SIGROK_ERR_MALLOC;
87 ctx->num_enabled_probes = 0;
89 for (l = o->device->probes; l; l = l->next) {
93 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
96 ctx->probelist[ctx->num_enabled_probes] = 0;
97 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
100 ctx->mark_trigger = -1;
102 if (o->param && o->param[0]) {
103 ctx->samples_per_line = strtoul(o->param, NULL, 10);
104 if (ctx->samples_per_line < 1)
107 ctx->samples_per_line = default_spl;
109 if (!(ctx->header = malloc(512))) {
111 return SIGROK_ERR_MALLOC;
114 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
115 if (o->device->plugin) {
116 num_probes = g_slist_length(o->device->probes);
117 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
118 o->device->plugin_index, DI_CUR_SAMPLERATE));
119 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
124 snprintf(ctx->header + strlen(ctx->header),
125 511 - strlen(ctx->header),
126 "Acquisition with %d/%d probes at %s\n",
127 ctx->num_enabled_probes, num_probes, samplerate_s);
131 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
132 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
135 return SIGROK_ERR_MALLOC;
137 if (!(ctx->linevalues = calloc(1, num_probes))) {
140 return SIGROK_ERR_MALLOC;
146 static int event(struct output *o, int event_type, char **data_out,
147 uint64_t *length_out)
154 switch (event_type) {
156 ctx->mark_trigger = ctx->spl_cnt;
159 outsize = ctx->num_enabled_probes
160 * (ctx->samples_per_line + 20) + 512;
161 if (!(outbuf = calloc(1, outsize)))
162 return SIGROK_ERR_MALLOC;
163 flush_linebufs(ctx, outbuf);
165 *length_out = strlen(outbuf);
174 static int init_bits(struct output *o)
176 return init(o, DEFAULT_BPL_BITS);
179 static int data_bits(struct output *o, char *data_in, uint64_t length_in,
180 char **data_out, uint64_t *length_out)
183 unsigned int outsize, offset, p;
189 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
190 + ctx->samples_per_line / 8;
191 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
192 / ctx->samples_per_line * max_linelen + 512;
194 if (!(outbuf = calloc(1, outsize + 1)))
195 return SIGROK_ERR_MALLOC;
199 /* The header is still here, this must be the first packet. */
200 strncpy(outbuf, ctx->header, outsize);
205 if (length_in >= ctx->unitsize) {
206 for (offset = 0; offset <= length_in - ctx->unitsize;
207 offset += ctx->unitsize) {
208 memcpy(&sample, data_in + offset, ctx->unitsize);
209 for (p = 0; p < ctx->num_enabled_probes; p++) {
210 c = (sample & ((uint64_t) 1 << p)) ? '1' : '0';
211 ctx->linebuf[p * ctx->linebuf_len +
212 ctx->line_offset] = c;
217 /* Add a space every 8th bit. */
218 if ((ctx->spl_cnt & 7) == 0) {
219 for (p = 0; p < ctx->num_enabled_probes; p++)
220 ctx->linebuf[p * ctx->linebuf_len +
221 ctx->line_offset] = ' ';
226 if (ctx->spl_cnt >= ctx->samples_per_line) {
227 flush_linebufs(ctx, outbuf);
228 ctx->line_offset = ctx->spl_cnt = 0;
229 ctx->mark_trigger = -1;
233 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
237 *length_out = strlen(outbuf);
242 static int init_hex(struct output *o)
244 return init(o, DEFAULT_BPL_BITS);
247 static int data_hex(struct output *o, char *data_in, uint64_t length_in,
248 char **data_out, uint64_t *length_out)
251 unsigned int outsize, offset, p;
257 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
258 + ctx->samples_per_line / 2;
259 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
260 / ctx->samples_per_line * max_linelen + 512;
262 if (!(outbuf = calloc(1, outsize + 1)))
263 return SIGROK_ERR_MALLOC;
267 /* The header is still here, this must be the first packet. */
268 strncpy(outbuf, ctx->header, outsize);
273 ctx->line_offset = 0;
274 for (offset = 0; offset <= length_in - ctx->unitsize;
275 offset += ctx->unitsize) {
276 memcpy(&sample, data_in + offset, ctx->unitsize);
277 for (p = 0; p < ctx->num_enabled_probes; p++) {
278 ctx->linevalues[p] <<= 1;
279 if (sample & ((uint64_t) 1 << p))
280 ctx->linevalues[p] |= 1;
281 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
282 ctx->line_offset, "%.2x", ctx->linevalues[p]);
286 /* Add a space after every complete hex byte. */
287 if ((ctx->spl_cnt & 7) == 0) {
288 for (p = 0; p < ctx->num_enabled_probes; p++)
289 ctx->linebuf[p * ctx->linebuf_len +
290 ctx->line_offset + 2] = ' ';
291 ctx->line_offset += 3;
295 if (ctx->spl_cnt >= ctx->samples_per_line) {
296 flush_linebufs(ctx, outbuf);
297 ctx->line_offset = ctx->spl_cnt = 0;
302 *length_out = strlen(outbuf);
307 struct output_format output_text_bits = {
309 "Bits (takes argument, default 64)",
316 struct output_format output_text_hex = {
318 "Hexadecimal (takes argument, default 256)",