X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=output%2Foutput_vcd.c;h=34b150d48ad47bcb6a026f7612d18f9be74080b4;hb=aa0b6b208e2ecaa7e56b020c204b6c3e702fb4ca;hp=fd569e0edc280366cb5af87bc162ce2d2815d275;hpb=607b58de58f22baa026d24ac7b8e0ae123c9ede0;p=libsigrok.git diff --git a/output/output_vcd.c b/output/output_vcd.c index fd569e0e..34b150d4 100644 --- a/output/output_vcd.c +++ b/output/output_vcd.c @@ -33,7 +33,7 @@ struct context { }; const char *vcd_header = "\ -$date\n %s\n$end\n\ +$date\n %s$end\n\ $version\n %s\n$end\n%s\ $timescale\n %i %s\n$end\n\ $scope module %s $end\n\ @@ -57,15 +57,19 @@ static int init(struct output *o) int i, b, num_probes; char *c, *samplerate_s; char wbuf[1000], comment[128]; + time_t t; if (!(ctx = calloc(1, sizeof(struct context)))) return SIGROK_ERR_MALLOC; + o->internal = ctx; ctx->num_enabled_probes = 0; + for (l = o->device->probes; l; l = l->next) { probe = l->data; - if (probe->enabled) - ctx->probelist[ctx->num_enabled_probes++] = probe->name; + if (!probe->enabled) + continue; + ctx->probelist[ctx->num_enabled_probes++] = probe->name; } ctx->probelist[ctx->num_enabled_probes] = 0; @@ -107,7 +111,8 @@ static int init(struct output *o) } /* TODO: Date: File or signals? Make y/n configurable. */ - b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date", + t = time(NULL); + b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, ctime(&t), PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf); /* TODO: Handle snprintf() errors. */ @@ -130,7 +135,9 @@ static int event(struct output *o, int event_type, char **data_out, ctx = o->internal; switch (event_type) { case DF_TRIGGER: - /* TODO */ + /* TODO: can a trigger mark be in a VCD file? */ + *data_out = NULL; + *length_out = 0; break; case DF_END: outlen = strlen("$dumpoff\n$end\n"); @@ -144,6 +151,10 @@ static int event(struct output *o, int event_type, char **data_out, free(o->internal); o->internal = NULL; break; + default: + *data_out = NULL; + *length_out = 0; + break; } return SIGROK_OK; @@ -156,6 +167,7 @@ static int data(struct output *o, char *data_in, uint64_t length_in, unsigned int i, outsize; int p, curbit, prevbit; uint64_t sample, prevsample; + static uint64_t samplecount = 0; char *outbuf, *c; ctx = o->internal; @@ -178,26 +190,25 @@ static int data(struct output *o, char *data_in, uint64_t length_in, /* TODO: Are disabled probes handled correctly? */ 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); + for (p = 0; p < ctx->num_enabled_probes; p++) { - curbit = (sample & ((uint64_t) (1 << p))) != 0; - if (i == 0) { - prevbit = ~curbit; - } else { - memcpy(&prevsample, data_in + i - 1, - ctx->unitsize); - prevbit = - (prevsample & ((uint64_t) (1 << p))) != 0; - } - - /* VCD only contains deltas/changes. */ + curbit = (sample & ((uint64_t) (1 << p))) >> p; + prevbit = (prevsample & ((uint64_t) (1 << p))) >> p; + + /* VCD only contains deltas/changes of signals. */ if (prevbit == curbit) continue; - /* FIXME: Only once per sample? */ - /* TODO: Is 'i' correct here? */ + /* Output which signal changed to which value. */ c = outbuf + strlen(outbuf); - sprintf(c, "#%i\n%i%c\n", i, curbit, (char)('!' + p)); + sprintf(c, "#%" PRIu64 "\n%i%c\n", samplecount, + curbit, (char)('!' + p)); } /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */ @@ -212,6 +223,7 @@ static int data(struct output *o, char *data_in, uint64_t length_in, struct output_format output_vcd = { "vcd", "Value Change Dump (VCD)", + DF_LOGIC, init, data, event,