X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=output%2Fvcd.c;h=27cc15336bf1422595ffce92be83ce4d99d7bbc7;hb=db9679afc9f46bdf038efabd3ac69983d15f89cf;hp=b60890ae63627bcafe38a1fb3b3826641f6f84a7;hpb=13d8e03c4f9fb6fe9c5f682ab957173effe42d43;p=libsigrok.git diff --git a/output/vcd.c b/output/vcd.c index b60890ae..27cc1533 100644 --- a/output/vcd.c +++ b/output/vcd.c @@ -1,5 +1,5 @@ /* - * This file is part of the sigrok project. + * This file is part of the libsigrok project. * * Copyright (C) 2010 Uwe Hermann * Copyright (C) 2013 Bert Vermeulen @@ -38,11 +38,12 @@ struct context { int num_enabled_probes; char *probelist[SR_MAX_NUM_PROBES + 1]; - int *prevbits; + int probeindices[SR_MAX_NUM_PROBES + 1]; GString *header; - uint64_t prevsample; + uint8_t *prevsample; int period; uint64_t samplerate; + unsigned int unitsize; }; static const char *vcd_header_comment = "\ @@ -53,7 +54,7 @@ static int init(struct sr_output *o) struct context *ctx; struct sr_probe *probe; GSList *l; - uint64_t *samplerate; + GVariant *gvar; int num_probes, i; char *samplerate_s, *frequency_s, *timestamp; time_t t; @@ -70,13 +71,16 @@ static int init(struct sr_output *o) probe = l->data; if (!probe->enabled) continue; - ctx->probelist[ctx->num_enabled_probes++] = probe->name; + ctx->probelist[ctx->num_enabled_probes] = probe->name; + ctx->probeindices[ctx->num_enabled_probes] = probe->index; + ctx->num_enabled_probes++; } if (ctx->num_enabled_probes > 94) { sr_err("VCD only supports 94 probes."); return SR_ERR; } + ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; ctx->probelist[ctx->num_enabled_probes] = 0; ctx->header = g_string_sized_new(512); num_probes = g_slist_length(o->sdi->probes); @@ -93,17 +97,18 @@ static int init(struct sr_output *o) PACKAGE, PACKAGE_VERSION); if (o->sdi->driver && sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) { - o->sdi->driver->config_get(SR_CONF_SAMPLERATE, - (const void **)&samplerate, o->sdi); - ctx->samplerate = *samplerate; + o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi); + ctx->samplerate = g_variant_get_uint64(gvar); if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) { g_string_free(ctx->header, TRUE); g_free(ctx); + g_variant_unref(gvar); return SR_ERR; } g_string_append_printf(ctx->header, vcd_header_comment, ctx->num_enabled_probes, num_probes, samplerate_s); g_free(samplerate_s); + g_variant_unref(gvar); } /* timescale */ @@ -134,10 +139,10 @@ static int init(struct sr_output *o) g_string_append(ctx->header, "$upscope $end\n" "$enddefinitions $end\n$dumpvars\n"); - if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) { + if (!(ctx->prevsample = g_try_malloc0(ctx->unitsize))) { g_string_free(ctx->header, TRUE); g_free(ctx); - sr_err("%s: ctx->prevbits malloc failed", __func__); + sr_err("%s: ctx->prevsample malloc failed", __func__); return SR_ERR_MALLOC; } @@ -151,10 +156,9 @@ static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi, struct context *ctx; GString *text; unsigned int i; - int p, curbit, prevbit; - uint64_t sample; + int p, curbit, prevbit, index; + uint8_t *sample; static uint64_t samplecount = 0; - gboolean first_sample; (void)sdi; @@ -173,27 +177,20 @@ static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi, /* The header is still here, this must be the first packet. */ text = ctx->header; ctx->header = NULL; - first_sample = TRUE; } else { text = g_string_sized_new(512); - first_sample = FALSE; } logic = packet->payload; for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { samplecount++; - memcpy(&sample, logic->data + i, logic->unitsize); - - if (first_sample) { - /* First packet. We neg to make sure sample is stored. */ - ctx->prevsample = ~sample; - first_sample = FALSE; - } + sample = logic->data + i; for (p = 0; p < ctx->num_enabled_probes; p++) { - curbit = (sample & ((uint64_t) (1 << p))) >> p; - prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p; + index = ctx->probeindices[p % 8]; + curbit = (sample[p / 8] & (((uint8_t) 1) << index)) >> index; + prevbit = (ctx->prevsample[p / 8] & (((uint64_t) 1) << index)) >> index; /* VCD only contains deltas/changes of signals. */ if (prevbit == curbit) @@ -205,7 +202,7 @@ static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi, * ctx->period), curbit, (char)('!' + p)); } - ctx->prevsample = sample; + memcpy(ctx->prevsample, sample, ctx->unitsize); } return text;