]> sigrok.org Git - libsigrok.git/blobdiff - output/output_vcd.c
Drop obsolete vcd_header.
[libsigrok.git] / output / output_vcd.c
index 2157c1ba5c1cd4ab1fa4cba9109e7d2799991dad..1c5d723d1abd9664ab40368d953c719851cfe809 100644 (file)
@@ -31,33 +31,25 @@ struct context {
        char *probelist[65];
        int *prevbits;
        GString *header;
+       uint64_t prevsample;
+       int period;
+       uint64_t samplerate;
 };
 
-const char *vcd_header = "\
-$date %s $end\n\
-$version %s $end\n%s\
-$timescale %s $end\n\
-$scope module %s $end\n\
-%s\
-$upscope $end\n\
-$enddefinitions $end\n\
-$dumpvars\n";
-
-const char *vcd_header_comment = "\
+static const char *vcd_header_comment = "\
 $comment\n  Acquisition with %d/%d probes at %s\n$end\n";
 
-static int init(struct output *o)
+static int init(struct sr_output *o)
 {
        struct context *ctx;
-       struct probe *probe;
+       struct sr_probe *probe;
        GSList *l;
-       uint64_t samplerate;
-       int num_probes, period, i;
+       int num_probes, i;
        char *samplerate_s, *frequency_s, *timestamp;
        time_t t;
 
        if (!(ctx = calloc(1, sizeof(struct context))))
-               return SIGROK_ERR_MALLOC;
+               return SR_ERR_MALLOC;
 
        o->internal = ctx;
        ctx->num_enabled_probes = 0;
@@ -70,7 +62,7 @@ static int init(struct output *o)
        }
        if (ctx->num_enabled_probes > 94) {
                g_warning("VCD only supports 94 probes.");
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        ctx->probelist[ctx->num_enabled_probes] = 0;
@@ -89,13 +81,13 @@ static int init(struct output *o)
        g_string_append_printf(ctx->header, "$version %s %s $end\n",
                        PACKAGE, PACKAGE_VERSION);
 
-       if (o->device->plugin) {
-               samplerate = *((uint64_t *) o->device->plugin->get_device_info(
-                               o->device->plugin_index, DI_CUR_SAMPLERATE));
-               if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
+       if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
+               ctx->samplerate = *((uint64_t *) o->device->plugin->get_device_info(
+                               o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
+               if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
                        g_string_free(ctx->header, TRUE);
                        free(ctx);
-                       return SIGROK_ERR;
+                       return SR_ERR;
                }
                g_string_append_printf(ctx->header, vcd_header_comment,
                                 ctx->num_enabled_probes, num_probes, samplerate_s);
@@ -104,16 +96,16 @@ static int init(struct output *o)
 
        /* timescale */
        /* VCD can only handle 1/10/100 (s - fs), so scale up first */
-       if (samplerate > MHZ(1))
-               period = GHZ(1);
-       else if (samplerate > KHZ(1))
-               period = MHZ(1);
+       if (ctx->samplerate > SR_MHZ(1))
+               ctx->period = SR_GHZ(1);
+       else if (ctx->samplerate > SR_KHZ(1))
+               ctx->period = SR_MHZ(1);
        else
-               period = KHZ(1);
-       if (!(frequency_s = sigrok_period_string(period))) {
+               ctx->period = SR_KHZ(1);
+       if (!(frequency_s = sr_period_string(ctx->period))) {
                g_string_free(ctx->header, TRUE);
                free(ctx);
-               return SIGROK_ERR;
+               return SR_ERR;
        }
        g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
        free(frequency_s);
@@ -133,13 +125,13 @@ static int init(struct output *o)
        if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
                g_string_free(ctx->header, TRUE);
                free(ctx);
-               return SIGROK_ERR_MALLOC;
+               return SR_ERR_MALLOC;
        }
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
-static int event(struct output *o, int event_type, char **data_out,
+static int event(struct sr_output *o, int event_type, char **data_out,
                 uint64_t *length_out)
 {
        struct context *ctx;
@@ -147,7 +139,7 @@ static int event(struct output *o, int event_type, char **data_out,
 
        ctx = o->internal;
        switch (event_type) {
-       case DF_END:
+       case SR_DF_END:
                outbuf = strdup("$dumpoff\n$end\n");
                *data_out = outbuf;
                *length_out = strlen(outbuf);
@@ -160,18 +152,19 @@ static int event(struct output *o, int event_type, char **data_out,
                break;
        }
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
-static int data(struct output *o, char *data_in, uint64_t length_in,
+static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
                char **data_out, uint64_t *length_out)
 {
        struct context *ctx;
        unsigned int i;
        int p, curbit, prevbit;
-       uint64_t sample, prevsample;
+       uint64_t sample;
        static uint64_t samplecount = 0;
        GString *out;
+       int first_sample = 0;
 
        ctx = o->internal;
        out = g_string_sized_new(512);
@@ -181,41 +174,48 @@ static int data(struct output *o, char *data_in, uint64_t length_in,
                g_string_append(out, ctx->header->str);
                g_string_free(ctx->header, TRUE);
                ctx->header = NULL;
+               first_sample = 1;
        }
 
        for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
                samplecount++;
+
                memcpy(&sample, data_in + i, ctx->unitsize);
-               if (i == 0)
-                       prevsample = sample;
-               else
-                       memcpy(&prevsample, data_in + i - 1, ctx->unitsize);
+
+               if (first_sample) {
+                       /* First packet. We neg to make sure sample is stored. */
+                       ctx->prevsample = ~sample;
+                       first_sample = 0;
+               }
 
                for (p = 0; p < ctx->num_enabled_probes; p++) {
                        curbit = (sample & ((uint64_t) (1 << p))) >> p;
-                       prevbit = (prevsample & ((uint64_t) (1 << p))) >> p;
+                       prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
 
                        /* VCD only contains deltas/changes of signals. */
                        if (prevbit == curbit)
                                continue;
 
                        /* Output which signal changed to which value. */
-                       g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n", samplecount,
-                                       curbit, (char)('!' + p));
+                       g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n",
+                                       (uint64_t)(((float)samplecount / ctx->samplerate)
+                                       * ctx->period), curbit, (char)('!' + p));
                }
+
+               ctx->prevsample = sample;
        }
 
        *data_out = out->str;
        *length_out = out->len;
        g_string_free(out, FALSE);
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
-struct output_format output_vcd = {
+struct sr_output_format output_vcd = {
        "vcd",
        "Value Change Dump (VCD)",
-       DF_LOGIC,
+       SR_DF_LOGIC,
        init,
        data,
        event,