00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <glib.h>
00025 #include "config.h"
00026 #include "sigrok.h"
00027 #include "sigrok-internal.h"
00028
00029 struct context {
00030 int num_enabled_probes;
00031 int unitsize;
00032 char *probelist[SR_MAX_NUM_PROBES + 1];
00033 int *prevbits;
00034 GString *header;
00035 uint64_t prevsample;
00036 int period;
00037 uint64_t samplerate;
00038 };
00039
00040 static const char *vcd_header_comment = "\
00041 $comment\n Acquisition with %d/%d probes at %s\n$end\n";
00042
00043 static int init(struct sr_output *o)
00044 {
00045 struct context *ctx;
00046 struct sr_probe *probe;
00047 GSList *l;
00048 int num_probes, i;
00049 char *samplerate_s, *frequency_s, *timestamp;
00050 time_t t;
00051
00052 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
00053 sr_err("vcd out: %s: ctx malloc failed", __func__);
00054 return SR_ERR_MALLOC;
00055 }
00056
00057 o->internal = ctx;
00058 ctx->num_enabled_probes = 0;
00059
00060 for (l = o->dev->probes; l; l = l->next) {
00061 probe = l->data;
00062 if (!probe->enabled)
00063 continue;
00064 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
00065 }
00066 if (ctx->num_enabled_probes > 94) {
00067 sr_err("vcd out: VCD only supports 94 probes.");
00068 return SR_ERR;
00069 }
00070
00071 ctx->probelist[ctx->num_enabled_probes] = 0;
00072 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
00073 ctx->header = g_string_sized_new(512);
00074 num_probes = g_slist_length(o->dev->probes);
00075
00076
00077 t = time(NULL);
00078 timestamp = g_strdup(ctime(&t));
00079 timestamp[strlen(timestamp)-1] = 0;
00080 g_string_printf(ctx->header, "$date %s $end\n", timestamp);
00081 g_free(timestamp);
00082
00083
00084 g_string_append_printf(ctx->header, "$version %s %s $end\n",
00085 PACKAGE, PACKAGE_VERSION);
00086
00087 if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
00088 ctx->samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
00089 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
00090 if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) {
00091 g_string_free(ctx->header, TRUE);
00092 g_free(ctx);
00093 return SR_ERR;
00094 }
00095 g_string_append_printf(ctx->header, vcd_header_comment,
00096 ctx->num_enabled_probes, num_probes, samplerate_s);
00097 g_free(samplerate_s);
00098 }
00099
00100
00101
00102 if (ctx->samplerate > SR_MHZ(1))
00103 ctx->period = SR_GHZ(1);
00104 else if (ctx->samplerate > SR_KHZ(1))
00105 ctx->period = SR_MHZ(1);
00106 else
00107 ctx->period = SR_KHZ(1);
00108 if (!(frequency_s = sr_period_string(ctx->period))) {
00109 g_string_free(ctx->header, TRUE);
00110 g_free(ctx);
00111 return SR_ERR;
00112 }
00113 g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s);
00114 g_free(frequency_s);
00115
00116
00117 g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE);
00118
00119
00120 for (i = 0; i < ctx->num_enabled_probes; i++) {
00121 g_string_append_printf(ctx->header, "$var wire 1 %c %s $end\n",
00122 (char)('!' + i), ctx->probelist[i]);
00123 }
00124
00125 g_string_append(ctx->header, "$upscope $end\n"
00126 "$enddefinitions $end\n$dumpvars\n");
00127
00128 if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) {
00129 g_string_free(ctx->header, TRUE);
00130 g_free(ctx);
00131 sr_err("vcd out: %s: ctx->prevbits malloc failed", __func__);
00132 return SR_ERR_MALLOC;
00133 }
00134
00135 return SR_OK;
00136 }
00137
00138 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
00139 uint64_t *length_out)
00140 {
00141 uint8_t *outbuf;
00142
00143 switch (event_type) {
00144 case SR_DF_END:
00145 outbuf = (uint8_t *)g_strdup("$dumpoff\n$end\n");
00146 *data_out = outbuf;
00147 *length_out = strlen((const char *)outbuf);
00148 g_free(o->internal);
00149 o->internal = NULL;
00150 break;
00151 default:
00152 *data_out = NULL;
00153 *length_out = 0;
00154 break;
00155 }
00156
00157 return SR_OK;
00158 }
00159
00160 static int data(struct sr_output *o, const uint8_t *data_in,
00161 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
00162 {
00163 struct context *ctx;
00164 unsigned int i;
00165 int p, curbit, prevbit;
00166 uint64_t sample;
00167 static uint64_t samplecount = 0;
00168 GString *out;
00169 int first_sample = 0;
00170
00171 ctx = o->internal;
00172 out = g_string_sized_new(512);
00173
00174 if (ctx->header) {
00175
00176 g_string_append(out, ctx->header->str);
00177 g_string_free(ctx->header, TRUE);
00178 ctx->header = NULL;
00179 first_sample = 1;
00180 }
00181
00182 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
00183 samplecount++;
00184
00185 memcpy(&sample, data_in + i, ctx->unitsize);
00186
00187 if (first_sample) {
00188
00189 ctx->prevsample = ~sample;
00190 first_sample = 0;
00191 }
00192
00193 for (p = 0; p < ctx->num_enabled_probes; p++) {
00194 curbit = (sample & ((uint64_t) (1 << p))) >> p;
00195 prevbit = (ctx->prevsample & ((uint64_t) (1 << p))) >> p;
00196
00197
00198 if (prevbit == curbit)
00199 continue;
00200
00201
00202 g_string_append_printf(out, "#%" PRIu64 "\n%i%c\n",
00203 (uint64_t)(((float)samplecount / ctx->samplerate)
00204 * ctx->period), curbit, (char)('!' + p));
00205 }
00206
00207 ctx->prevsample = sample;
00208 }
00209
00210 *data_out = (uint8_t *)out->str;
00211 *length_out = out->len;
00212 g_string_free(out, FALSE);
00213
00214 return SR_OK;
00215 }
00216
00217 struct sr_output_format output_vcd = {
00218 .id = "vcd",
00219 .description = "Value Change Dump (VCD)",
00220 .df_type = SR_DF_LOGIC,
00221 .init = init,
00222 .data = data,
00223 .event = event,
00224 };