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
|
|
|
|
2012-11-11 08:36:21 +00:00
|
|
|
/* Message logging helpers with driver-specific prefix string. */
|
|
|
|
#define DRIVER_LOG_DOMAIN "output/vcd: "
|
|
|
|
#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
|
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
struct context {
|
|
|
|
int num_enabled_probes;
|
2012-03-21 18:28:43 +00:00
|
|
|
char *probelist[SR_MAX_NUM_PROBES + 1];
|
2013-04-11 16:49:24 +00:00
|
|
|
int probeindices[SR_MAX_NUM_PROBES + 1];
|
2010-04-04 11:19:20 +00:00
|
|
|
int *prevbits;
|
2011-01-16 03:34:45 +00:00
|
|
|
GString *header;
|
2011-01-16 16:34:49 +00:00
|
|
|
uint64_t prevsample;
|
2011-01-16 16:55:51 +00:00
|
|
|
int period;
|
|
|
|
uint64_t samplerate;
|
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;
|
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-11 16:49:24 +00:00
|
|
|
ctx->probelist[ctx->num_enabled_probes] = probe->name;
|
|
|
|
ctx->probeindices[ctx->num_enabled_probes] = probe->index;
|
|
|
|
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
|
|
|
|
|
|
|
ctx->probelist[ctx->num_enabled_probes] = 0;
|
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-01-25 15:20:40 +00:00
|
|
|
if (o->sdi->driver && sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
|
2013-03-25 19:27:26 +00:00
|
|
|
o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi);
|
|
|
|
ctx->samplerate = g_variant_get_uint64(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);
|
2013-03-25 19:27:26 +00:00
|
|
|
g_variant_unref(gvar);
|
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);
|
2013-03-25 19:27:26 +00:00
|
|
|
g_variant_unref(gvar);
|
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 */
|
2010-04-06 13:02:14 +00:00
|
|
|
for (i = 0; i < ctx->num_enabled_probes; i++) {
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
|
|
|
|
(char)('!' + i), ctx->probelist[i]);
|
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
|
|
|
|
2012-02-11 19:06:46 +00:00
|
|
|
if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
|
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);
|
2012-11-11 08:36:21 +00:00
|
|
|
sr_err("%s: ctx->prevbits 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-09 16:50:34 +00:00
|
|
|
static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi,
|
2013-03-06 22:14:00 +00:00
|
|
|
const struct sr_datafeed_packet *packet)
|
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;
|
2013-03-06 22:14:00 +00:00
|
|
|
GString *text;
|
2011-01-16 03:34:45 +00:00
|
|
|
unsigned int i;
|
2013-04-11 16:49:24 +00:00
|
|
|
int p, curbit, prevbit, index;
|
2011-01-16 16:34:49 +00:00
|
|
|
uint64_t sample;
|
2010-05-09 18:52:36 +00:00
|
|
|
static uint64_t samplecount = 0;
|
2013-03-06 22:14:00 +00:00
|
|
|
gboolean first_sample;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2013-03-06 22:14:00 +00:00
|
|
|
(void)sdi;
|
|
|
|
|
|
|
|
if (!o || !o->internal)
|
|
|
|
return NULL;
|
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) {
|
|
|
|
text = g_string_sized_new(16);
|
|
|
|
g_string_printf(text, "$dumpoff\n$end\n");
|
|
|
|
return text;
|
|
|
|
} else if (packet->type != SR_DF_LOGIC)
|
|
|
|
return NULL;
|
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-03-06 22:14:00 +00:00
|
|
|
text = ctx->header;
|
2010-04-04 11:19:20 +00:00
|
|
|
ctx->header = NULL;
|
2013-03-06 22:14:00 +00:00
|
|
|
first_sample = TRUE;
|
|
|
|
} else {
|
|
|
|
text = g_string_sized_new(512);
|
|
|
|
first_sample = FALSE;
|
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-03-06 22:14:00 +00:00
|
|
|
memcpy(&sample, logic->data + i, logic->unitsize);
|
2011-01-16 16:34:49 +00:00
|
|
|
|
|
|
|
if (first_sample) {
|
|
|
|
/* First packet. We neg to make sure sample is stored. */
|
|
|
|
ctx->prevsample = ~sample;
|
2013-03-06 22:14:00 +00:00
|
|
|
first_sample = FALSE;
|
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-11 16:49:24 +00:00
|
|
|
index = ctx->probeindices[p];
|
|
|
|
curbit = (sample & (((uint64_t) 1) << index)) >> index;
|
|
|
|
prevbit = (ctx->prevsample & (((uint64_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-03-06 22:14:00 +00:00
|
|
|
g_string_append_printf(text, "#%" 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
|
|
|
|
|
|
|
ctx->prevsample = sample;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 22:14:00 +00:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09 16:50:34 +00:00
|
|
|
.recv = receive,
|
|
|
|
.cleanup = cleanup,
|
2010-04-04 11:19:20 +00:00
|
|
|
};
|