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>
|
2011-01-16 03:34:45 +00:00
|
|
|
* Copyright (C) 2011 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>
|
|
|
|
#include <sigrok.h>
|
2011-04-14 07:46:53 +00:00
|
|
|
#include <sigrok-internal.h>
|
2010-04-04 11:19:20 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
struct context {
|
|
|
|
int num_enabled_probes;
|
|
|
|
int unitsize;
|
|
|
|
char *probelist[65];
|
|
|
|
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;
|
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
|
|
|
|
2010-05-08 14:09:25 +00:00
|
|
|
if (!(ctx = calloc(1, sizeof(struct context))))
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR_MALLOC;
|
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
|
|
|
|
2010-04-04 11:19:20 +00:00
|
|
|
for (l = o->device->probes; l; l = l->next) {
|
|
|
|
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) {
|
2011-04-14 07:46:53 +00:00
|
|
|
sr_warn("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);
|
|
|
|
num_probes = g_slist_length(o->device->probes);
|
2010-04-04 11:19:20 +00:00
|
|
|
|
2011-01-16 03:34:45 +00:00
|
|
|
/* timestamp */
|
|
|
|
t = time(NULL);
|
|
|
|
timestamp = strdup(ctime(&t));
|
|
|
|
timestamp[strlen(timestamp)-1] = 0;
|
|
|
|
g_string_printf(ctx->header, "$date %s $end\n", timestamp);
|
|
|
|
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
|
|
|
|
2011-02-08 17:19:38 +00:00
|
|
|
if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
|
2011-01-16 16:55:51 +00:00
|
|
|
ctx->samplerate = *((uint64_t *) o->device->plugin->get_device_info(
|
2011-01-30 16:58:41 +00:00
|
|
|
o->device->plugin_index, SR_DI_CUR_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);
|
2010-05-08 14:09:25 +00:00
|
|
|
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);
|
2010-05-06 02:56:48 +00:00
|
|
|
free(samplerate_s);
|
|
|
|
}
|
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);
|
|
|
|
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);
|
|
|
|
free(frequency_s);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2010-05-08 14:09:25 +00:00
|
|
|
if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
|
2011-01-16 03:34:45 +00:00
|
|
|
g_string_free(ctx->header, TRUE);
|
2010-05-08 14:09:25 +00:00
|
|
|
free(ctx);
|
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
|
|
|
}
|
|
|
|
|
2011-01-29 15:36:57 +00:00
|
|
|
static int event(struct sr_output *o, int event_type, char **data_out,
|
2010-04-04 11:19:20 +00:00
|
|
|
uint64_t *length_out)
|
|
|
|
{
|
|
|
|
struct context *ctx;
|
|
|
|
char *outbuf;
|
|
|
|
|
|
|
|
ctx = o->internal;
|
2010-04-15 18:36:04 +00:00
|
|
|
switch (event_type) {
|
2011-01-30 16:58:41 +00:00
|
|
|
case SR_DF_END:
|
2011-01-16 03:34:45 +00:00
|
|
|
outbuf = strdup("$dumpoff\n$end\n");
|
2010-04-04 11:19:20 +00:00
|
|
|
*data_out = outbuf;
|
2011-01-16 03:34:45 +00:00
|
|
|
*length_out = strlen(outbuf);
|
2010-04-04 11:19:20 +00:00
|
|
|
free(o->internal);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-20 17:24:25 +00:00
|
|
|
static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
|
2010-04-04 11:19:20 +00:00
|
|
|
char **data_out, uint64_t *length_out)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-01-16 03:34:45 +00:00
|
|
|
*data_out = out->str;
|
|
|
|
*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
|
|
|
};
|