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 ^ */
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 ctx = malloc(sizeof(struct context));
85 ctx->num_enabled_probes = 0;
87 for (l = o->device->probes; l; l = l->next) {
90 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
93 ctx->probelist[ctx->num_enabled_probes] = 0;
94 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
97 ctx->mark_trigger = -1;
99 if (o->param && o->param[0])
100 ctx->samples_per_line = strtoul(o->param, NULL, 10);
102 ctx->samples_per_line = default_spl;
104 ctx->header = malloc(512);
105 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
106 if (o->device->plugin) {
107 num_probes = g_slist_length(o->device->probes);
108 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
109 o->device->plugin_index, DI_CUR_SAMPLERATE));
110 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
112 snprintf(ctx->header + strlen(ctx->header), 511 - strlen(ctx->header),
113 "Acquisition with %d/%d probes at %s\n", ctx->num_enabled_probes,
114 num_probes, samplerate_s);
118 ctx->linebuf_len = ctx->samples_per_line * 2;
119 ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len);
120 ctx->linevalues = calloc(1, num_probes);
125 static int event(struct output *o, int event_type, char **data_out,
126 uint64_t *length_out)
133 switch (event_type) {
135 ctx->mark_trigger = ctx->spl_cnt;
138 outsize = ctx->num_enabled_probes
139 * (ctx->samples_per_line + 20) + 512;
140 outbuf = calloc(1, outsize);
141 flush_linebufs(ctx, outbuf);
143 *length_out = strlen(outbuf);
152 static int init_bits(struct output *o)
154 return init(o, DEFAULT_BPL_BITS);
157 static int data_bits(struct output *o, char *data_in, uint64_t length_in,
158 char **data_out, uint64_t *length_out)
161 unsigned int outsize, offset, p;
166 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
167 ctx->samples_per_line + 512;
168 outbuf = calloc(1, outsize + 1);
170 /* The header is still here, this must be the first packet. */
171 strncpy(outbuf, ctx->header, outsize);
177 if (length_in >= ctx->unitsize) {
178 for (offset = 0; offset <= length_in - ctx->unitsize;
179 offset += ctx->unitsize) {
180 memcpy(&sample, data_in + offset, ctx->unitsize);
181 for (p = 0; p < ctx->num_enabled_probes; p++) {
182 if (sample & ((uint64_t) 1 << p))
183 ctx->linebuf[p * ctx->linebuf_len +
184 ctx->line_offset] = '1';
186 ctx->linebuf[p * ctx->linebuf_len +
187 ctx->line_offset] = '0';
192 /* Add a space every 8th bit. */
193 if ((ctx->spl_cnt & 7) == 0) {
194 for (p = 0; p < ctx->num_enabled_probes; p++)
195 ctx->linebuf[p * ctx->linebuf_len +
196 ctx->line_offset] = ' ';
201 if (ctx->spl_cnt >= ctx->samples_per_line) {
202 flush_linebufs(ctx, outbuf);
203 ctx->line_offset = ctx->spl_cnt = 0;
204 ctx->mark_trigger = -1;
208 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
211 *length_out = strlen(outbuf);
216 static int init_hex(struct output *o)
218 return init(o, DEFAULT_BPL_BITS);
221 static int data_hex(struct output *o, char *data_in, uint64_t length_in,
222 char **data_out, uint64_t *length_out)
225 unsigned int outsize, offset, p;
230 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
231 ctx->samples_per_line + 512;
232 outbuf = calloc(1, outsize + 1);
234 /* The header is still here, this must be the first packet. */
235 strncpy(outbuf, ctx->header, outsize);
241 ctx->line_offset = 0;
242 for (offset = 0; offset <= length_in - ctx->unitsize;
243 offset += ctx->unitsize) {
244 memcpy(&sample, data_in + offset, ctx->unitsize);
245 for (p = 0; p < ctx->num_enabled_probes; p++) {
246 ctx->linevalues[p] <<= 1;
247 if (sample & ((uint64_t) 1 << p))
248 ctx->linevalues[p] |= 1;
249 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
250 ctx->line_offset, "%.2x", ctx->linevalues[p]);
254 /* Add a space after every complete hex byte. */
255 if ((ctx->spl_cnt & 7) == 0) {
256 for (p = 0; p < ctx->num_enabled_probes; p++)
257 ctx->linebuf[p * ctx->linebuf_len +
258 ctx->line_offset + 2] = ' ';
259 ctx->line_offset += 3;
263 if (ctx->spl_cnt >= ctx->samples_per_line) {
264 flush_linebufs(ctx, outbuf);
265 ctx->line_offset = ctx->spl_cnt = 0;
270 *length_out = strlen(outbuf);
275 struct output_format output_text_bits = {
283 struct output_format output_text_hex = {
285 "Text (hexadecimal)",