2010-04-04 11:19:20 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the sigrok project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
|
2012-02-13 13:31:51 +00:00
|
|
|
* Copyright (C) 2010-2012 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;
|
|
|
|
int unitsize;
|
2012-03-21 18:28:43 +00:00
|
|
|
char *probelist[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;
|
2012-07-20 19:37:36 +00:00
|
|
|
uint64_t *samplerate;
|
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;
|
|
|
|
ctx->probelist[ctx->num_enabled_probes++] = probe->name;
|
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;
|
|
|
|
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-01-25 15:20:40 +00:00
|
|
|
if (o->sdi->driver && sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
|
2013-01-25 02:17:36 +00:00
|
|
|
o->sdi->driver->config_get(SR_CONF_SAMPLERATE,
|
2012-07-20 19:37:36 +00:00
|
|
|
(const void **)&samplerate, o->sdi);
|
|
|
|
ctx->samplerate = *samplerate;
|
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 */
|
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
|
|
|
}
|
|
|
|
|
2012-03-28 18:00:13 +00:00
|
|
|
static int event(struct sr_output *o, int event_type, uint8_t **data_out,
|
2010-04-04 11:19:20 +00:00
|
|
|
uint64_t *length_out)
|
|
|
|
{
|
2012-03-28 18:00:13 +00:00
|
|
|
uint8_t *outbuf;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
switch (event_type) {
|
2011-01-30 16:58:41 +00:00
|
|
|
case SR_DF_END:
|
2012-03-28 18:00:13 +00:00
|
|
|
outbuf = (uint8_t *)g_strdup("$dumpoff\n$end\n");
|
2010-04-04 11:19:20 +00:00
|
|
|
*data_out = outbuf;
|
2012-03-28 18:00:13 +00:00
|
|
|
*length_out = strlen((const char *)outbuf);
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(o->internal);
|
2010-04-04 11:19:20 +00:00
|
|
|
o->internal = NULL;
|
|
|
|
break;
|
2011-01-13 01:05:39 +00:00
|
|
|
default:
|
|
|
|
*data_out = NULL;
|
|
|
|
*length_out = 0;
|
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
2012-03-28 18:00:13 +00:00
|
|
|
static int data(struct sr_output *o, const uint8_t *data_in,
|
|
|
|
uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
|
2010-04-04 11:19:20 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
2011-01-16 03:34:45 +00:00
|
|
|
unsigned int i;
|
2010-04-09 20:18:46 +00:00
|
|
|
int p, curbit, prevbit;
|
2011-01-16 16:34:49 +00:00
|
|
|
uint64_t sample;
|
2010-05-09 18:52:36 +00:00
|
|
|
static uint64_t samplecount = 0;
|
2011-01-16 03:34:45 +00:00
|
|
|
GString *out;
|
2011-01-16 16:34:49 +00:00
|
|
|
int first_sample = 0;
|
2010-04-04 11:19:20 +00:00
|
|
|
|
|
|
|
ctx = o->internal;
|
2011-01-16 03:34:45 +00:00
|
|
|
out = g_string_sized_new(512);
|
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. */
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_append(out, ctx->header->str);
|
|
|
|
g_string_free(ctx->header, TRUE);
|
2010-04-04 11:19:20 +00:00
|
|
|
ctx->header = NULL;
|
2011-01-16 16:34:49 +00:00
|
|
|
first_sample = 1;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-09 12:42:46 +00:00
|
|
|
for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
|
2010-05-09 18:52:36 +00:00
|
|
|
samplecount++;
|
2011-01-16 16:34:49 +00:00
|
|
|
|
2010-05-09 12:42:46 +00:00
|
|
|
memcpy(&sample, data_in + i, ctx->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;
|
|
|
|
first_sample = 0;
|
|
|
|
}
|
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
for (p = 0; p < ctx->num_enabled_probes; p++) {
|
2010-05-09 19:05:15 +00:00
|
|
|
curbit = (sample & ((uint64_t) (1 << p))) >> p;
|
2011-01-16 16:34:49 +00:00
|
|
|
prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
|
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. */
|
2011-01-16 16:55:51 +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
|
|
|
|
|
|
|
ctx->prevsample = sample;
|
2010-04-04 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2012-03-28 18:00:13 +00:00
|
|
|
*data_out = (uint8_t *)out->str;
|
2011-01-16 03:34:45 +00:00
|
|
|
*length_out = out->len;
|
|
|
|
g_string_free(out, FALSE);
|
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,
|
|
|
|
.data = data,
|
|
|
|
.event = event,
|
2010-04-04 11:19:20 +00:00
|
|
|
};
|