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;
43 static void flush_linebufs(struct context *ctx, char *outbuf)
45 static int max_probename_len = 0;
48 if (ctx->linebuf[0] == 0)
51 if (max_probename_len == 0) {
52 /* First time through... */
53 for (i = 0; ctx->probelist[i]; i++) {
54 len = strlen(ctx->probelist[i]);
55 if (len > max_probename_len)
56 max_probename_len = len;
60 for (i = 0; ctx->probelist[i]; i++) {
61 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
62 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
65 /* Mark trigger with ^ */
66 if (ctx->mark_trigger != -1)
67 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
68 ctx->mark_trigger + (ctx->mark_trigger / 8), "");
70 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
73 static int init(struct output *o, int default_spl)
82 ctx = malloc(sizeof(struct context));
84 ctx->num_enabled_probes = 0;
86 for (l = o->device->probes; l; l = l->next) {
89 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
92 ctx->probelist[ctx->num_enabled_probes] = 0;
93 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
96 ctx->mark_trigger = -1;
98 if (o->param && o->param[0])
99 ctx->samples_per_line = strtoul(o->param, NULL, 10);
101 ctx->samples_per_line = default_spl;
103 if (o->device->plugin) {
104 ctx->header = malloc(512);
105 num_probes = g_slist_length(o->device->probes);
106 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
107 o->device->plugin_index, DI_CUR_SAMPLERATE));
108 snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ",
109 ctx->num_enabled_probes, num_probes);
111 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
112 return -1; /* FIXME */
113 snprintf(ctx->header + strlen(ctx->header), 512, "%s\n", samplerate_s);
117 * device has no plugin: this is just a dummy device, the data
123 ctx->linebuf_len = ctx->samples_per_line * 2;
124 ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len);
125 ctx->linevalues = calloc(1, num_probes);
130 static int event(struct output *o, int event_type, char **data_out,
131 uint64_t *length_out)
138 switch (event_type) {
140 ctx->mark_trigger = ctx->spl_cnt;
143 outsize = ctx->num_enabled_probes
144 * (ctx->samples_per_line + 20) + 512;
145 outbuf = calloc(1, outsize);
146 flush_linebufs(ctx, outbuf);
148 *length_out = strlen(outbuf);
157 static int init_bits(struct output *o)
159 return init(o, DEFAULT_BPL_BITS);
162 static int data_bits(struct output *o, char *data_in, uint64_t length_in,
163 char **data_out, uint64_t *length_out)
166 unsigned int outsize, offset, p;
171 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
172 ctx->samples_per_line + 512;
173 outbuf = calloc(1, outsize + 1);
175 /* The header is still here, this must be the first packet. */
176 strncpy(outbuf, ctx->header, outsize);
182 if (length_in >= ctx->unitsize) {
183 for (offset = 0; offset <= length_in - ctx->unitsize;
184 offset += ctx->unitsize) {
185 memcpy(&sample, data_in + offset, ctx->unitsize);
186 for (p = 0; p < ctx->num_enabled_probes; p++) {
187 if (sample & ((uint64_t) 1 << p))
188 ctx->linebuf[p * ctx->linebuf_len +
189 ctx->line_offset] = '1';
191 ctx->linebuf[p * ctx->linebuf_len +
192 ctx->line_offset] = '0';
197 /* Add a space every 8th bit. */
198 if ((ctx->spl_cnt & 7) == 0) {
199 for (p = 0; p < ctx->num_enabled_probes; p++)
200 ctx->linebuf[p * ctx->linebuf_len +
201 ctx->line_offset] = ' ';
206 if (ctx->spl_cnt >= ctx->samples_per_line) {
207 flush_linebufs(ctx, outbuf);
208 ctx->line_offset = ctx->spl_cnt = 0;
209 ctx->mark_trigger = -1;
213 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
216 *length_out = strlen(outbuf);
221 static int init_hex(struct output *o)
223 return init(o, DEFAULT_BPL_BITS);
226 static int data_hex(struct output *o, char *data_in, uint64_t length_in,
227 char **data_out, uint64_t *length_out)
230 unsigned int outsize, offset, p;
235 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
236 ctx->samples_per_line + 512;
237 outbuf = calloc(1, outsize + 1);
239 /* The header is still here, this must be the first packet. */
240 strncpy(outbuf, ctx->header, outsize);
246 ctx->line_offset = 0;
247 for (offset = 0; offset <= length_in - ctx->unitsize;
248 offset += ctx->unitsize) {
249 memcpy(&sample, data_in + offset, ctx->unitsize);
250 for (p = 0; p < ctx->num_enabled_probes; p++) {
251 ctx->linevalues[p] <<= 1;
252 if (sample & ((uint64_t) 1 << p))
253 ctx->linevalues[p] |= 1;
254 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
255 ctx->line_offset, "%.2x", ctx->linevalues[p]);
259 /* Add a space after every complete hex byte. */
260 if ((ctx->spl_cnt & 7) == 0) {
261 for (p = 0; p < ctx->num_enabled_probes; p++)
262 ctx->linebuf[p * ctx->linebuf_len +
263 ctx->line_offset + 2] = ' ';
264 ctx->line_offset += 3;
268 if (ctx->spl_cnt >= ctx->samples_per_line) {
269 flush_linebufs(ctx, outbuf);
270 ctx->line_offset = ctx->spl_cnt = 0;
275 *length_out = strlen(outbuf);
280 struct output_format output_text_bits = {
288 struct output_format output_text_hex = {
290 "Text (hexadecimal)",