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-05-03 19:59:32 +00:00
|
|
|
/* Message logging helpers with subsystem-specific prefix string. */
|
|
|
|
#define LOG_PREFIX "output/vcd: "
|
|
|
|
#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
|
|
|
|
#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
|
|
|
|
#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
|
|
|
|
#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
|
|
|
|
#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
|
|
|
|
#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
|
2012-11-11 08:36:21 +00:00
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
struct context {
|
|
|
|
int num_enabled_probes;
|
2013-04-26 22:16:30 +00:00
|
|
|
GArray *probeindices;
|
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;
|
2013-04-26 23:13:02 +00:00
|
|
|
unsigned int unitsize;
|
2010-04-04 11:19:20 +00:00
|
|
|
};
|
|
|
|
|
2011-02-22 22:12:41 +00:00
|
|
|
static const char *vcd_header_comment = "\
|
2010-05-06 02:56:48 +00:00
|
|
|
$comment\n Acquisition with %d/%d probes at %s\n$end\n";
|
|
|
|
|
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;
|
2011-02-08 16:47:38 +00:00
|
|
|
struct sr_probe *probe;
|
2010-04-04 11:19:20 +00:00
|
|
|
GSList *l;
|
2013-03-25 19:27:26 +00:00
|
|
|
GVariant *gvar;
|
2011-01-16 16:55:51 +00:00
|
|
|
int num_probes, 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
|
|
|
|
2012-02-11 19:06:46 +00:00
|
|
|
if (!(ctx = g_try_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;
|
|
|
|
ctx->num_enabled_probes = 0;
|
2013-04-26 22:16:30 +00:00
|
|
|
ctx->probeindices = g_array_new(FALSE, FALSE, sizeof(int));
|
2011-01-07 18:55:25 +00:00
|
|
|
|
2012-07-20 19:37:36 +00:00
|
|
|
for (l = o->sdi->probes; l; l = l->next) {
|
2010-04-04 11:19:20 +00:00
|
|
|
probe = l->data;
|
2011-01-07 18:55:25 +00:00
|
|
|
if (!probe->enabled)
|
|
|
|
continue;
|
2013-04-26 22:16:30 +00:00
|
|
|
ctx->probeindices = g_array_append_val(
|
|
|
|
ctx->probeindices, probe->index);
|
2013-04-11 16:49:24 +00:00
|
|
|
ctx->num_enabled_probes++;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
2011-01-16 03:34:45 +00:00
|
|
|
if (ctx->num_enabled_probes > 94) {
|
2012-11-11 08:36:21 +00:00
|
|
|
sr_err("VCD only supports 94 probes.");
|
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
|
|
|
|
2013-04-26 23:13:02 +00:00
|
|
|
ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
|
2011-01-16 03:34:45 +00:00
|
|
|
ctx->header = g_string_sized_new(512);
|
2012-07-20 19:37:36 +00:00
|
|
|
num_probes = g_slist_length(o->sdi->probes);
|
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,
|
|
|
|
ctx->num_enabled_probes, num_probes, 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 */
|
2013-04-26 22:00:51 +00:00
|
|
|
for (i = 0, l = o->sdi->probes; l; l = l->next, i++) {
|
|
|
|
probe = l->data;
|
|
|
|
if (!probe->enabled)
|
|
|
|
continue;
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
|
2013-04-26 22:00:51 +00:00
|
|
|
(char)('!' + i), probe->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"
|
|
|
|
"$enddefinitions $end\n$dumpvars\n");
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2013-04-26 23:13:02 +00:00
|
|
|
if (!(ctx->prevsample = g_try_malloc0(ctx->unitsize))) {
|
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);
|
2013-04-26 23:13:02 +00:00
|
|
|
sr_err("%s: ctx->prevsample malloc failed", __func__);
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR_MALLOC;
|
2010-05-08 14:09:25 +00:00
|
|
|
}
|
2010-04-05 14:20:09 +00:00
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
|
|
|
|
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;
|
2010-05-09 18:52:36 +00:00
|
|
|
static uint64_t samplecount = 0;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2013-03-06 22:14:00 +00:00
|
|
|
(void)sdi;
|
|
|
|
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = NULL;
|
2013-03-06 22:14:00 +00:00
|
|
|
if (!o || !o->internal)
|
2013-04-27 15:29:46 +00:00
|
|
|
return SR_ERR_ARG;
|
2010-04-04 11:19:20 +00:00
|
|
|
ctx = o->internal;
|
2013-03-06 22:14:00 +00:00
|
|
|
|
|
|
|
if (packet->type == SR_DF_END) {
|
2013-04-27 15:29:46 +00:00
|
|
|
*out = g_string_new("$dumpoff\n$end\n");
|
|
|
|
return SR_OK;
|
2013-03-06 22:14:00 +00:00
|
|
|
} else if (packet->type != SR_DF_LOGIC)
|
2013-04-27 15:29:46 +00:00
|
|
|
return SR_OK;
|
2010-05-08 14:09:25 +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;
|
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
|
|
|
}
|
|
|
|
|
2013-03-06 22:14:00 +00:00
|
|
|
logic = packet->payload;
|
|
|
|
for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
|
2010-05-09 18:52:36 +00:00
|
|
|
samplecount++;
|
2011-01-16 16:34:49 +00:00
|
|
|
|
2013-04-26 23:13:02 +00:00
|
|
|
sample = logic->data + i;
|
2011-01-16 16:34:49 +00:00
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
for (p = 0; p < ctx->num_enabled_probes; p++) {
|
2013-04-26 22:16:30 +00:00
|
|
|
index = g_array_index(ctx->probeindices, int, p);
|
2013-04-26 23:13:02 +00:00
|
|
|
curbit = (sample[p / 8] & (((uint8_t) 1) << index)) >> index;
|
2013-11-26 15:59:04 +00:00
|
|
|
prevbit = (ctx->prevsample[p / 8] & (((uint8_t) 1) << index)) >> index;
|
2010-05-09 19:05:15 +00:00
|
|
|
|
|
|
|
/* VCD only contains deltas/changes of signals. */
|
2010-05-09 12:42:46 +00:00
|
|
|
if (prevbit == curbit)
|
|
|
|
continue;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2010-05-09 19:05:15 +00:00
|
|
|
/* Output which signal changed to which value. */
|
2013-04-27 15:29:46 +00:00
|
|
|
g_string_append_printf(*out, "#%" PRIu64 "\n%i%c\n",
|
2011-01-18 22:17:55 +00:00
|
|
|
(uint64_t)(((float)samplecount / ctx->samplerate)
|
2011-01-16 16:55:51 +00:00
|
|
|
* ctx->period), curbit, (char)('!' + p));
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
2011-01-16 16:34:49 +00:00
|
|
|
|
2013-04-26 23:13:02 +00:00
|
|
|
memcpy(ctx->prevsample, sample, ctx->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;
|
|
|
|
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)",
|
|
|
|
.df_type = SR_DF_LOGIC,
|
|
|
|
.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
|
|
|
};
|