X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=output%2Foutput_vcd.c;h=d8ce6ef063e9ad53eb068916d8505c9d5f8d0705;hb=c4fffe1e9606e3e63a4cd0b760a37beb1f122cc5;hp=e4ac7babffe1a780f0398a03a733fcb4c9255590;hpb=6b5e3ceefcdee5e942b9cbff5c697016dacee774;p=libsigrok.git diff --git a/output/output_vcd.c b/output/output_vcd.c index e4ac7bab..d8ce6ef0 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,6 +57,7 @@ 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; @@ -107,7 +108,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. */ @@ -153,9 +155,10 @@ static int data(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out) { struct context *ctx; - unsigned int offset, outsize; + unsigned int i, outsize; int p, curbit, prevbit; uint64_t sample, prevsample; + static uint64_t samplecount = 0; char *outbuf, *c; ctx = o->internal; @@ -177,28 +180,26 @@ static int data(struct output *o, char *data_in, uint64_t length_in, /* TODO: Are disabled probes handled correctly? */ - for (offset = 0; offset <= length_in - ctx->unitsize; - offset += ctx->unitsize) { - memcpy(&sample, data_in + offset, ctx->unitsize); + 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 (offset == 0) { - prevbit = ~curbit; - } else { - memcpy(&prevsample, data_in + offset - 1, - ctx->unitsize); - prevbit = - (prevsample & ((uint64_t) (1 << p))) != 0; - } - - if (prevbit != curbit) { - /* FIXME: Only once per sample? */ - c = outbuf + strlen(outbuf); - sprintf(c, "#%i\n", offset * 1 /* TODO */); - - c = outbuf + strlen(outbuf); - sprintf(c, "%i%c\n", curbit, (char)('!' + p)); - } + 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; + + /* Output which signal changed to which value. */ + c = outbuf + strlen(outbuf); + sprintf(c, "#%" PRIu64 "\n%i%c\n", samplecount, + curbit, (char)('!' + p)); } /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */