4 Implements a Value Change Dump file outout to generate
5 traces & curves and display them in gtkwave.
7 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
9 This file is part of simavr.
11 simavr is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 simavr is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with simavr. If not, see <http://www.gnu.org/licenses/>.
27 #include "sim_vcd_file.h"
30 void _avr_vcd_notify(struct avr_irq_t * irq, uint32_t value, void * param);
32 int avr_vcd_init(struct avr_t * avr, const char * filename, avr_vcd_t * vcd, uint32_t period)
34 memset(vcd, 0, sizeof(avr_vcd_t));
36 strncpy(vcd->filename, filename, sizeof(vcd->filename));
37 vcd->period = avr_usec_to_cycles(vcd->avr, period);
39 for (int i = 0; i < AVR_VCD_MAX_SIGNALS; i++) {
40 avr_init_irq(&avr->irq_pool, &vcd->signal[i].irq, i, 1, NULL /* TODO IRQ name */);
41 avr_irq_register_notify(&vcd->signal[i].irq, _avr_vcd_notify, vcd);
47 void avr_vcd_close(avr_vcd_t * vcd)
52 void _avr_vcd_notify(struct avr_irq_t * irq, uint32_t value, void * param)
54 avr_vcd_t * vcd = (avr_vcd_t *)param;
57 avr_vcd_signal_t * s = (avr_vcd_signal_t*)irq;
58 if (vcd->logindex == AVR_VCD_LOG_SIZE) {
59 printf("_avr_vcd_notify %s overrun value buffer %d\n", s->name, AVR_VCD_LOG_SIZE);
62 avr_vcd_log_t *l = &vcd->log[vcd->logindex++];
64 l->when = vcd->avr->cycle;
68 static char * _avr_vcd_get_signal_text(avr_vcd_signal_t * s, char * out, uint32_t value)
75 for (int i = s->size; i > 0; i--)
76 *dst++ = value & (1 << (i-1)) ? '1' : '0';
84 static void avr_vcd_flush_log(avr_vcd_t * vcd)
86 #if AVR_VCD_MAX_SIGNALS > 32
91 uint64_t oldbase = 0; // make sure it's different
96 // printf("avr_vcd_flush_log %d\n", vcd->logindex);
99 for (uint32_t li = 0; li < vcd->logindex; li++) {
100 avr_vcd_log_t *l = &vcd->log[li];
101 uint64_t base = avr_cycles_to_nsec(vcd->avr, l->when - vcd->start); // 1ns base
103 // if that trace was seen in this usec already, we fudge the base time
104 // to make sure the new value is offset by one usec, to make sure we get
105 // at least a small pulse on the waveform
106 // This is a bit of a fudge, but it is the only way to represent very
107 // short"pulses" that are still visible on the waveform.
108 if (base == oldbase && seen & (1 << l->signal->irq.irq))
109 base++; // this forces a new timestamp
111 if (base > oldbase || li == 0) {
113 fprintf(vcd->output, "#%llu\n", (long long unsigned int)base);
116 seen |= (1 << l->signal->irq.irq); // mark this trace as seen for this timestamp
117 fprintf(vcd->output, "%s\n", _avr_vcd_get_signal_text(l->signal, out, l->value));
122 static avr_cycle_count_t _avr_vcd_timer(struct avr_t * avr, avr_cycle_count_t when, void * param)
124 avr_vcd_t * vcd = param;
125 avr_vcd_flush_log(vcd);
126 return when + vcd->period;
129 int avr_vcd_add_signal(avr_vcd_t * vcd,
130 avr_irq_t * signal_irq,
134 if (vcd->signal_count == AVR_VCD_MAX_SIGNALS)
136 avr_vcd_signal_t * s = &vcd->signal[vcd->signal_count++];
137 strncpy(s->name, name, sizeof(s->name));
138 s->size = signal_bit_size;
139 s->alias = ' ' + vcd->signal_count ;
140 avr_connect_irq(signal_irq, &s->irq);
145 int avr_vcd_start(avr_vcd_t * vcd)
149 vcd->output = fopen(vcd->filename, "w");
150 if (vcd->output == NULL) {
151 perror(vcd->filename);
155 fprintf(vcd->output, "$timescale 1ns $end\n"); // 1ns base
156 fprintf(vcd->output, "$scope module logic $end\n");
158 for (int i = 0; i < vcd->signal_count; i++) {
159 fprintf(vcd->output, "$var wire %d %c %s $end\n",
160 vcd->signal[i].size, vcd->signal[i].alias, vcd->signal[i].name);
163 fprintf(vcd->output, "$upscope $end\n");
164 fprintf(vcd->output, "$enddefinitions $end\n");
166 fprintf(vcd->output, "$dumpvars\n");
167 for (int i = 0; i < vcd->signal_count; i++) {
168 avr_vcd_signal_t * s = &vcd->signal[i];
170 fprintf(vcd->output, "%s\n", _avr_vcd_get_signal_text(s, out, s->irq.value));
172 fprintf(vcd->output, "$end\n");
173 vcd->start = vcd->avr->cycle;
174 avr_cycle_timer_register(vcd->avr, vcd->period, _avr_vcd_timer, vcd);
178 int avr_vcd_stop(avr_vcd_t * vcd)
180 avr_cycle_timer_cancel(vcd->avr, _avr_vcd_timer, vcd);
182 avr_vcd_flush_log(vcd);