vcd: change VCD time base to 1 ns to handle higher AVR speeds (e.g. 20 MHz / max...
[simavr] / simavr / sim / sim_vcd_file.c
1 /*
2         sim_vcd_file.c
3
4         Implements a Value Change Dump file outout to generate
5         traces & curves and display them in gtkwave.
6
7         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
8
9         This file is part of simavr.
10
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.
15
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.
20
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/>.
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include "sim_vcd_file.h"
28 #include "sim_avr.h"
29
30 void _avr_vcd_notify(struct avr_irq_t * irq, uint32_t value, void * param);
31
32 int avr_vcd_init(struct avr_t * avr, const char * filename, avr_vcd_t * vcd, uint32_t period)
33 {
34         memset(vcd, 0, sizeof(avr_vcd_t));
35         vcd->avr = avr;
36         strncpy(vcd->filename, filename, sizeof(vcd->filename));
37         vcd->period = avr_usec_to_cycles(vcd->avr, period);
38         
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);
42         }
43         
44         return 0;
45 }
46
47 void avr_vcd_close(avr_vcd_t * vcd)
48 {
49         avr_vcd_stop(vcd);
50 }
51
52 void _avr_vcd_notify(struct avr_irq_t * irq, uint32_t value, void * param)
53 {
54         avr_vcd_t * vcd = (avr_vcd_t *)param;
55         if (!vcd->output)
56                 return;
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);
60                 return;
61         }
62         avr_vcd_log_t *l = &vcd->log[vcd->logindex++];
63         l->signal = s;
64         l->when = vcd->avr->cycle;
65         l->value = value;
66 }
67
68 static char * _avr_vcd_get_signal_text(avr_vcd_signal_t * s, char * out, uint32_t value)
69 {
70         char * dst = out;
71                 
72         if (s->size > 1)
73                 *dst++ = 'b';
74         
75         for (int i = s->size; i > 0; i--)
76                 *dst++ = value & (1 << (i-1)) ? '1' : '0';
77         if (s->size > 1)
78                 *dst++ = ' ';
79         *dst++ = s->alias;
80         *dst = 0;
81         return out;
82 }
83
84 static void avr_vcd_flush_log(avr_vcd_t * vcd)
85 {
86 #if AVR_VCD_MAX_SIGNALS > 32
87         uint64_t seen = 0;
88 #else
89         uint32_t seen = 0;
90 #endif
91         uint64_t oldbase = 0;   // make sure it's different
92         char out[48];
93
94         if (!vcd->logindex)
95                 return;
96 //      printf("avr_vcd_flush_log %d\n", vcd->logindex);
97
98
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
102
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
110                         
111                 if (base > oldbase || li == 0) {
112                         seen = 0;
113                         fprintf(vcd->output, "#%llu\n", (long long unsigned int)base);
114                         oldbase = base;
115                 }
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));
118         }
119         vcd->logindex = 0;
120 }
121
122 static avr_cycle_count_t _avr_vcd_timer(struct avr_t * avr, avr_cycle_count_t when, void * param)
123 {
124         avr_vcd_t * vcd = param;
125         avr_vcd_flush_log(vcd);
126         return when + vcd->period;
127 }
128
129 int avr_vcd_add_signal(avr_vcd_t * vcd, 
130         avr_irq_t * signal_irq,
131         int signal_bit_size,
132         const char * name )
133 {
134         if (vcd->signal_count == AVR_VCD_MAX_SIGNALS)
135                 return -1;
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);
141         return 0;
142 }
143
144
145 int avr_vcd_start(avr_vcd_t * vcd)
146 {
147         if (vcd->output)
148                 avr_vcd_stop(vcd);
149         vcd->output = fopen(vcd->filename, "w");
150         if (vcd->output == NULL) {
151                 perror(vcd->filename);
152                 return -1;
153         }
154                 
155         fprintf(vcd->output, "$timescale 1ns $end\n");  // 1ns base
156         fprintf(vcd->output, "$scope module logic $end\n");
157
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);
161         }
162
163         fprintf(vcd->output, "$upscope $end\n");
164         fprintf(vcd->output, "$enddefinitions $end\n");
165         
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];
169                 char out[48];
170                 fprintf(vcd->output, "%s\n", _avr_vcd_get_signal_text(s, out, s->irq.value));
171         }
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);   
175         return 0;
176 }
177
178 int avr_vcd_stop(avr_vcd_t * vcd)
179 {
180         avr_cycle_timer_cancel(vcd->avr, _avr_vcd_timer, vcd);
181
182         avr_vcd_flush_log(vcd);
183         
184         if (vcd->output)
185                 fclose(vcd->output);
186         vcd->output = NULL;
187         return 0;
188 }
189
190