2010-04-04 11:19:20 +00:00
|
|
|
/*
|
2013-04-23 20:24:30 +00:00
|
|
|
* This file is part of the libsigrok project.
|
2010-04-04 11:19:20 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
|
2013-03-24 10:21:00 +00:00
|
|
|
* Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
|
2010-04-04 11:19:20 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
2012-10-23 22:41:21 +00:00
|
|
|
#include "config.h" /* Needed for PACKAGE and others. */
|
2012-07-04 22:55:07 +00:00
|
|
|
#include "libsigrok.h"
|
|
|
|
#include "libsigrok-internal.h"
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2013-12-23 03:38:35 +00:00
|
|
|
#define LOG_PREFIX "output/vcd"
|
2012-11-11 08:36:21 +00:00
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
struct context {
|
2014-03-24 20:34:20 +00:00
|
|
|
int num_enabled_channels;
|
|
|
|
GArray *channelindices;
|
2011-01-16 03:34:45 +00:00
|
|
|
GString *header;
|
2013-04-26 23:13:02 +00:00
|
|
|
uint8_t *prevsample;
|
2011-01-16 16:55:51 +00:00
|
|
|
int period;
|
|
|
|
uint64_t samplerate;
|
2014-01-30 19:49:36 +00:00
|
|
|
uint64_t samplecount;
|
2010-04-04 11:19:20 +00:00
|
|
|
};
|
|
|
|
|
2014-01-30 21:11:10 +00:00
|
|
|
static const char *const vcd_header_comment =
|
2014-03-24 20:34:20 +00:00
|
|
|
"$comment\n Acquisition with %d/%d channels at %s\n$end\n";
|
2010-05-06 02:56:48 +00:00
|
|
|
|
2011-01-29 15:36:57 +00:00
|
|
|
static int init(struct sr_output *o)
|
2010-04-04 11:19:20 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2010-04-04 11:19:20 +00:00
|
|
|
GSList *l;
|
2013-03-25 19:27:26 +00:00
|
|
|
GVariant *gvar;
|
2014-03-24 20:34:20 +00:00
|
|
|
int num_channels, i;
|
2011-01-16 03:34:45 +00:00
|
|
|
char *samplerate_s, *frequency_s, *timestamp;
|
2010-05-09 12:54:16 +00:00
|
|
|
time_t t;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2014-04-16 13:29:53 +00:00
|
|
|
if (!(ctx = g_malloc0(sizeof(struct context)))) {
|
2012-11-11 08:36:21 +00:00
|
|
|
sr_err("%s: ctx malloc failed", __func__);
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR_MALLOC;
|
2012-02-11 19:06:46 +00:00
|
|
|
}
|
2011-01-07 18:55:25 +00:00
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
o->internal = ctx;
|
2014-03-24 20:34:20 +00:00
|
|
|
ctx->num_enabled_channels = 0;
|
|
|
|
ctx->channelindices = g_array_new(FALSE, FALSE, sizeof(int));
|
2011-01-07 18:55:25 +00:00
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
for (l = o->sdi->channels; l; l = l->next) {
|
|
|
|
ch = l->data;
|
2014-03-24 21:39:42 +00:00
|
|
|
if (ch->type != SR_CHANNEL_LOGIC)
|
2013-12-29 09:57:38 +00:00
|
|
|
continue;
|
2014-03-24 20:34:20 +00:00
|
|
|
if (!ch->enabled)
|
2011-01-07 18:55:25 +00:00
|
|
|
continue;
|
2014-03-24 20:34:20 +00:00
|
|
|
ctx->channelindices = g_array_append_val(
|
|
|
|
ctx->channelindices, ch->index);
|
|
|
|
ctx->num_enabled_channels++;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
2014-03-24 20:34:20 +00:00
|
|
|
if (ctx->num_enabled_channels > 94) {
|
|
|
|
sr_err("VCD only supports 94 channels.");
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-16 03:34:45 +00:00
|
|
|
}
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2011-01-16 03:34:45 +00:00
|
|
|
ctx->header = g_string_sized_new(512);
|
2014-03-24 20:34:20 +00:00
|
|
|
num_channels = g_slist_length(o->sdi->channels);
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2011-01-16 03:34:45 +00:00
|
|
|
/* timestamp */
|
|
|
|
t = time(NULL);
|
2012-02-11 19:06:46 +00:00
|
|
|
timestamp = g_strdup(ctime(&t));
|
2011-01-16 03:34:45 +00:00
|
|
|
timestamp[strlen(timestamp)-1] = 0;
|
|
|
|
g_string_printf(ctx->header, "$date %s $end\n", timestamp);
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(timestamp);
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2011-01-16 03:34:45 +00:00
|
|
|
/* generator */
|
|
|
|
g_string_append_printf(ctx->header, "$version %s %s $end\n",
|
|
|
|
PACKAGE, PACKAGE_VERSION);
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2013-10-31 22:58:33 +00:00
|
|
|
if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
|
|
|
|
&gvar) == SR_OK) {
|
2013-03-25 19:27:26 +00:00
|
|
|
ctx->samplerate = g_variant_get_uint64(gvar);
|
2013-04-30 14:03:37 +00:00
|
|
|
g_variant_unref(gvar);
|
2011-01-29 15:43:45 +00:00
|
|
|
if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_free(ctx->header, TRUE);
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(ctx);
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2010-05-08 14:09:25 +00:00
|
|
|
}
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_append_printf(ctx->header, vcd_header_comment,
|
2014-03-24 20:34:20 +00:00
|
|
|
ctx->num_enabled_channels, num_channels, samplerate_s);
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(samplerate_s);
|
2010-05-06 02:56:48 +00:00
|
|
|
}
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2011-01-16 03:34:45 +00:00
|
|
|
/* timescale */
|
|
|
|
/* VCD can only handle 1/10/100 (s - fs), so scale up first */
|
2011-02-22 16:57:03 +00:00
|
|
|
if (ctx->samplerate > SR_MHZ(1))
|
|
|
|
ctx->period = SR_GHZ(1);
|
|
|
|
else if (ctx->samplerate > SR_KHZ(1))
|
|
|
|
ctx->period = SR_MHZ(1);
|
2011-01-16 03:34:45 +00:00
|
|
|
else
|
2011-02-22 16:57:03 +00:00
|
|
|
ctx->period = SR_KHZ(1);
|
2011-01-29 15:43:45 +00:00
|
|
|
if (!(frequency_s = sr_period_string(ctx->period))) {
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_free(ctx->header, TRUE);
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(ctx);
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-16 03:34:45 +00:00
|
|
|
}
|
|
|
|
g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(frequency_s);
|
2011-01-16 03:34:45 +00:00
|
|
|
|
|
|
|
/* scope */
|
|
|
|
g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
|
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
/* Wires / channels */
|
2014-03-24 20:34:20 +00:00
|
|
|
for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
|
|
|
|
ch = l->data;
|
2014-03-24 21:39:42 +00:00
|
|
|
if (ch->type != SR_CHANNEL_LOGIC)
|
2013-12-29 09:57:38 +00:00
|
|
|
continue;
|
2014-03-24 20:34:20 +00:00
|
|
|
if (!ch->enabled)
|
2013-04-26 22:00:51 +00:00
|
|
|
continue;
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
|
2014-03-24 20:34:20 +00:00
|
|
|
(char)('!' + i), ch->name);
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_append(ctx->header, "$upscope $end\n"
|
2014-01-30 21:11:10 +00:00
|
|
|
"$enddefinitions $end\n");
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-20 20:51:09 +00:00
|
|
|
static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
|
|
|
|
GString **out)
|
2010-04-04 11:19:20 +00:00
|
|
|
{
|
2013-03-06 22:14:00 +00:00
|
|
|
const struct sr_datafeed_logic *logic;
|
2010-04-04 11:19:20 +00:00
|
|
|
struct context *ctx;
|
2011-01-16 03:34:45 +00:00
|
|
|
unsigned int i;
|
2013-04-11 16:49:24 +00:00
|
|
|
int p, curbit, prevbit, index;
|
2013-04-26 23:13:02 +00:00
|
|
|
uint8_t *sample;
|
2014-01-30 20:57:49 +00:00
|
|
|
gboolean timestamp_written;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = NULL;
|
2013-03-06 22:14:00 +00:00
|
|
|
if (!o || !o->internal)
|
2014-01-30 21:11:10 +00:00
|
|
|
return SR_ERR_BUG;
|
2010-04-04 11:19:20 +00:00
|
|
|
ctx = o->internal;
|
2013-03-06 22:14:00 +00:00
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
if (ctx->header) {
|
|
|
|
/* The header is still here, this must be the first packet. */
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = ctx->header;
|
2010-04-04 11:19:20 +00:00
|
|
|
ctx->header = NULL;
|
2014-01-30 19:49:36 +00:00
|
|
|
ctx->samplecount = 0;
|
2013-03-06 22:14:00 +00:00
|
|
|
} else {
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = g_string_sized_new(512);
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 23:56:49 +00:00
|
|
|
if (packet->type != SR_DF_LOGIC) {
|
|
|
|
if (packet->type == SR_DF_END)
|
|
|
|
/* Write final timestamp as length indicator. */
|
|
|
|
g_string_append_printf(*out, "#%.0f\n",
|
|
|
|
(double)ctx->samplecount /
|
|
|
|
ctx->samplerate * ctx->period);
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-06 22:14:00 +00:00
|
|
|
logic = packet->payload;
|
2014-04-16 13:29:53 +00:00
|
|
|
|
|
|
|
if (!ctx->prevsample) {
|
|
|
|
/* Can't allocate this until we know the stream's unitsize. */
|
|
|
|
if (!(ctx->prevsample = g_malloc0(logic->unitsize))) {
|
|
|
|
g_free(ctx);
|
|
|
|
sr_err("%s: ctx->prevsample malloc failed", __func__);
|
|
|
|
return SR_ERR_MALLOC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 22:14:00 +00:00
|
|
|
for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
|
2013-04-26 23:13:02 +00:00
|
|
|
sample = logic->data + i;
|
2014-01-30 20:57:49 +00:00
|
|
|
timestamp_written = FALSE;
|
2011-01-16 16:34:49 +00:00
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
for (p = 0; p < ctx->num_enabled_channels; p++) {
|
|
|
|
index = g_array_index(ctx->channelindices, int, p);
|
2014-01-30 20:57:49 +00:00
|
|
|
|
|
|
|
curbit = ((unsigned)sample[index / 8]
|
|
|
|
>> (index % 8)) & 1;
|
|
|
|
prevbit = ((unsigned)ctx->prevsample[index / 8]
|
|
|
|
>> (index % 8)) & 1;
|
2010-05-09 19:05:15 +00:00
|
|
|
|
|
|
|
/* VCD only contains deltas/changes of signals. */
|
2014-01-30 19:49:36 +00:00
|
|
|
if (prevbit == curbit && ctx->samplecount > 0)
|
2010-05-09 12:42:46 +00:00
|
|
|
continue;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2014-01-30 20:57:49 +00:00
|
|
|
/* Output timestamp of subsequent signal changes. */
|
|
|
|
if (!timestamp_written)
|
|
|
|
g_string_append_printf(*out, "#%.0f",
|
|
|
|
(double)ctx->samplecount /
|
|
|
|
ctx->samplerate * ctx->period);
|
|
|
|
|
2010-05-09 19:05:15 +00:00
|
|
|
/* Output which signal changed to which value. */
|
2014-01-30 20:57:49 +00:00
|
|
|
g_string_append_c(*out, ' ');
|
|
|
|
g_string_append_c(*out, '0' + curbit);
|
|
|
|
g_string_append_c(*out, '!' + p);
|
|
|
|
|
|
|
|
timestamp_written = TRUE;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
2011-01-16 16:34:49 +00:00
|
|
|
|
2014-01-30 20:57:49 +00:00
|
|
|
if (timestamp_written)
|
|
|
|
g_string_append_c(*out, '\n');
|
|
|
|
|
2014-01-30 19:49:36 +00:00
|
|
|
ctx->samplecount++;
|
2014-04-16 13:29:53 +00:00
|
|
|
memcpy(ctx->prevsample, sample, logic->unitsize);
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
return SR_OK;
|
2013-03-06 22:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cleanup(struct sr_output *o)
|
|
|
|
{
|
|
|
|
struct context *ctx;
|
|
|
|
|
|
|
|
if (!o || !o->internal)
|
|
|
|
return SR_ERR_ARG;
|
|
|
|
|
|
|
|
ctx = o->internal;
|
2014-04-16 13:29:53 +00:00
|
|
|
g_free(ctx->prevsample);
|
|
|
|
g_array_free(ctx->channelindices, TRUE);
|
2013-03-06 22:14:00 +00:00
|
|
|
g_free(ctx);
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2011-01-29 15:36:57 +00:00
|
|
|
struct sr_output_format output_vcd = {
|
2011-04-06 19:51:36 +00:00
|
|
|
.id = "vcd",
|
2011-04-06 17:42:49 +00:00
|
|
|
.description = "Value Change Dump (VCD)",
|
|
|
|
.init = init,
|
2013-04-27 15:29:46 +00:00
|
|
|
.receive = receive,
|
2013-04-09 16:50:34 +00:00
|
|
|
.cleanup = cleanup,
|
2010-04-04 11:19:20 +00:00
|
|
|
};
|