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) {
92 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
95 ctx->probelist[ctx->num_enabled_probes] = 0;
96 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
99 ctx->mark_trigger = -1;
101 if (o->param && o->param[0])
102 ctx->samples_per_line = strtoul(o->param, NULL, 10);
104 ctx->samples_per_line = default_spl;
106 if (!(ctx->header = malloc(512))) {
108 return SIGROK_ERR_MALLOC;
111 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
112 if (o->device->plugin) {
113 num_probes = g_slist_length(o->device->probes);
114 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
115 o->device->plugin_index, DI_CUR_SAMPLERATE));
116 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
121 snprintf(ctx->header + strlen(ctx->header),
122 511 - strlen(ctx->header),
123 "Acquisition with %d/%d probes at %s\n",
124 ctx->num_enabled_probes, num_probes, samplerate_s);
128 ctx->linebuf_len = ctx->samples_per_line * 2;
129 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
132 return SIGROK_ERR_MALLOC;
134 if (!(ctx->linevalues = calloc(1, num_probes))) {
137 return SIGROK_ERR_MALLOC;
143 static int event(struct output *o, int event_type, char **data_out,
144 uint64_t *length_out)
151 switch (event_type) {
153 ctx->mark_trigger = ctx->spl_cnt;
156 outsize = ctx->num_enabled_probes
157 * (ctx->samples_per_line + 20) + 512;
158 if (!(outbuf = calloc(1, outsize)))
159 return SIGROK_ERR_MALLOC;
160 flush_linebufs(ctx, outbuf);
162 *length_out = strlen(outbuf);
171 static int init_bits(struct output *o)
173 return init(o, DEFAULT_BPL_BITS);
176 static int data_bits(struct output *o, char *data_in, uint64_t length_in,
177 char **data_out, uint64_t *length_out)
180 unsigned int outsize, offset, p;
185 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
186 ctx->samples_per_line + 512;
188 if (!(outbuf = calloc(1, outsize + 1)))
189 return SIGROK_ERR_MALLOC;
193 /* The header is still here, this must be the first packet. */
194 strncpy(outbuf, ctx->header, outsize);
199 if (length_in >= ctx->unitsize) {
200 for (offset = 0; offset <= length_in - ctx->unitsize;
201 offset += ctx->unitsize) {
202 memcpy(&sample, data_in + offset, ctx->unitsize);
203 for (p = 0; p < ctx->num_enabled_probes; p++) {
204 if (sample & ((uint64_t) 1 << p))
205 ctx->linebuf[p * ctx->linebuf_len +
206 ctx->line_offset] = '1';
208 ctx->linebuf[p * ctx->linebuf_len +
209 ctx->line_offset] = '0';
214 /* Add a space every 8th bit. */
215 if ((ctx->spl_cnt & 7) == 0) {
216 for (p = 0; p < ctx->num_enabled_probes; p++)
217 ctx->linebuf[p * ctx->linebuf_len +
218 ctx->line_offset] = ' ';
223 if (ctx->spl_cnt >= ctx->samples_per_line) {
224 flush_linebufs(ctx, outbuf);
225 ctx->line_offset = ctx->spl_cnt = 0;
226 ctx->mark_trigger = -1;
230 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
234 *length_out = strlen(outbuf);
239 static int init_hex(struct output *o)
241 return init(o, DEFAULT_BPL_BITS);
244 static int data_hex(struct output *o, char *data_in, uint64_t length_in,
245 char **data_out, uint64_t *length_out)
248 unsigned int outsize, offset, p;
253 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
254 ctx->samples_per_line + 512;
256 if (!(outbuf = calloc(1, outsize + 1)))
257 return SIGROK_ERR_MALLOC;
261 /* The header is still here, this must be the first packet. */
262 strncpy(outbuf, ctx->header, outsize);
267 ctx->line_offset = 0;
268 for (offset = 0; offset <= length_in - ctx->unitsize;
269 offset += ctx->unitsize) {
270 memcpy(&sample, data_in + offset, ctx->unitsize);
271 for (p = 0; p < ctx->num_enabled_probes; p++) {
272 ctx->linevalues[p] <<= 1;
273 if (sample & ((uint64_t) 1 << p))
274 ctx->linevalues[p] |= 1;
275 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
276 ctx->line_offset, "%.2x", ctx->linevalues[p]);
280 /* Add a space after every complete hex byte. */
281 if ((ctx->spl_cnt & 7) == 0) {
282 for (p = 0; p < ctx->num_enabled_probes; p++)
283 ctx->linebuf[p * ctx->linebuf_len +
284 ctx->line_offset + 2] = ' ';
285 ctx->line_offset += 3;
289 if (ctx->spl_cnt >= ctx->samples_per_line) {
290 flush_linebufs(ctx, outbuf);
291 ctx->line_offset = ctx->spl_cnt = 0;
296 *length_out = strlen(outbuf);
301 struct output_format output_text_bits = {
309 struct output_format output_text_hex = {
311 "Text (hexadecimal)",