Fixed multiple warnings for -Wall
[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(&vcd->signal[i].irq, i, 1);
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 (!vcd->logindex)
87                 return;
88 //      printf("avr_vcd_flush_log %d\n", vcd->logindex);
89         uint32_t oldbase = 0;   // make sure it's different
90         char out[48];
91
92 #if AVR_VCD_MAX_SIGNALS > 32
93         uint64_t seen = 0;
94 #else
95         uint32_t seen = 0;
96 #endif
97         for (int li = 0; li < vcd->logindex; li++) {
98                 avr_vcd_log_t *l = &vcd->log[li];
99                 uint32_t base = avr_cycles_to_usec(vcd->avr, l->when - vcd->start);
100
101                 // if that trace was seen in this usec already, we fudge the base time
102                 // to make sure the new value is offset by one usec, to make sure we get
103                 // at least a small pulse on the waveform
104                 // This is a bit of a fudge, but it is the only way to represent very 
105                 // short"pulses" that are still visible on the waveform.
106                 if (base == oldbase && seen & (1 << l->signal->irq.irq))
107                         base++; // this forces a new timestamp
108                         
109                 if (base > oldbase || li == 0) {
110                         seen = 0;
111                         fprintf(vcd->output, "#%uld\n", base);
112                         oldbase = base;
113                 }
114                 seen |= (1 << l->signal->irq.irq);      // mark this trace as seen for this timestamp
115                 fprintf(vcd->output, "%s\n", _avr_vcd_get_signal_text(l->signal, out, l->value));
116         }
117         vcd->logindex = 0;
118 }
119
120 static avr_cycle_count_t _avr_vcd_timer(struct avr_t * avr, avr_cycle_count_t when, void * param)
121 {
122         avr_vcd_t * vcd = param;
123         avr_vcd_flush_log(vcd);
124         return when + vcd->period;
125 }
126
127 int avr_vcd_add_signal(avr_vcd_t * vcd, 
128         avr_irq_t * signal_irq,
129         int signal_bit_size,
130         const char * name )
131 {
132         if (vcd->signal_count == AVR_VCD_MAX_SIGNALS)
133                 return -1;
134         avr_vcd_signal_t * s = &vcd->signal[vcd->signal_count++];
135         strncpy(s->name, name, sizeof(s->name));
136         s->size = signal_bit_size;
137         s->alias = ' ' + vcd->signal_count ;
138         avr_connect_irq(signal_irq, &s->irq);
139         return 0;
140 }
141
142
143 int avr_vcd_start(avr_vcd_t * vcd)
144 {
145         if (vcd->output)
146                 avr_vcd_stop(vcd);
147         vcd->output = fopen(vcd->filename, "w");
148         if (vcd->output == NULL) {
149                 perror(vcd->filename);
150                 return -1;
151         }
152                 
153         fprintf(vcd->output, "$timescale 1us $end\n");
154         fprintf(vcd->output, "$scope module logic $end\n");
155
156         for (int i = 0; i < vcd->signal_count; i++) {
157                 fprintf(vcd->output, "$var wire %d %c %s $end\n",
158                         vcd->signal[i].size, vcd->signal[i].alias, vcd->signal[i].name);
159         }
160
161         fprintf(vcd->output, "$upscope $end\n");
162         fprintf(vcd->output, "$enddefinitions $end\n");
163         
164         fprintf(vcd->output, "$dumpvars\n");
165         for (int i = 0; i < vcd->signal_count; i++) {
166                 avr_vcd_signal_t * s = &vcd->signal[i];
167                 char out[48];
168                 fprintf(vcd->output, "%s\n", _avr_vcd_get_signal_text(s, out, s->irq.value));
169         }
170         fprintf(vcd->output, "$end\n");
171         vcd->start = vcd->avr->cycle;
172         avr_cycle_timer_register(vcd->avr, vcd->period, _avr_vcd_timer, vcd);   
173         return 0;
174 }
175
176 int avr_vcd_stop(avr_vcd_t * vcd)
177 {
178         avr_cycle_timer_cancel(vcd->avr, _avr_vcd_timer, vcd);
179
180         avr_vcd_flush_log(vcd);
181         
182         if (vcd->output)
183                 fclose(vcd->output);
184         vcd->output = NULL;
185         return 0;
186 }
187
188