-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvisualise.c
259 lines (214 loc) · 6.94 KB
/
visualise.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
Visualise the running CPU by interpretting the output of GHDL and drawing
the state of the CPU at each clock tick in a series of HTML files.
This program parses the VHDL source files for the CPU, and works out the
nesting of the various components. This is used for outputting the nested <div>
blocks in the HTML.
When running, it looks for specially formatted report statements in the simulation
output, and includes those in their respective divs.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int frame_number=0;
struct entity {
char *name;
char *class;
struct entity *children;
struct entity *next;
};
struct entity *model=NULL;
struct signal {
char *node;
char *name;
char value[1024];
int changed;
};
#define MAX_SIGNALS 65536
int signal_count=0;
struct signal signals[MAX_SIGNALS];
int log_signal(char *node,char *signal,char *type,char *value)
{
for(int i=0;i<signal_count;i++)
{
if (!strcmp(signals[i].node,node))
if (!strcmp(signals[i].name,signal))
{
if (strcasecmp(value,signals[i].value))
signals[i].changed=1;
strcpy(signals[i].value,value);
return 0;
}
}
if (signal_count>=MAX_SIGNALS) return -1;
signals[signal_count].node=strdup(node);
signals[signal_count].name=strdup(signal);
signals[signal_count].changed=1;
strcpy(signals[signal_count++].value,value);
return 0;
}
int vhdl_structure_discover(int depth,char *name,char *file,struct entity **e)
{
char filename[1024];
snprintf(filename,1024,"%s.vhdl",file);
FILE *f=fopen(filename,"r");
if (!f) {
fprintf(stderr,"ERROR: Could not open '%s'\n",filename);
return -1;
}
for(int i=0;i<depth;i++) fprintf(stderr," ");
fprintf(stderr," '%s' is a '%s'\n",name,file);
struct entity *ee=calloc(sizeof(struct entity),1);
struct entity **eee=e;
ee->name=strdup(name);
ee->class=strdup(file);
while(*eee) {
eee=&(*eee)->next;
}
*eee=ee;
char line[1024];
// Write template for visualiation for this VHDL file
char templatefile[1024];
snprintf(templatefile,1024,"vis_templates/%s.vhdl",file);
FILE *o=fopen(templatefile,"w");
if (!o) {
fprintf(stderr,"ERROR: Could not open '%s'\n",templatefile);
return -1;
}
fprintf(o,
" process (cpuclock) is\n"
" variable ignored : boolean;\n"
" begin\n"
" if rising_edge(cpuclock) then\n");
line[0]=0; fgets(line,1024,f);
while(line[0]) {
char name[1024];
char class[1024];
if (sscanf(line,"%*[ ]%[^:]: entity work.%s",
name,class)==2) {
vhdl_structure_discover(depth+1,name,class,&ee->children);
}
if (sscanf(line,"%*[ ]%[^ ] : in %[^:;\( \r\n];",name,class)==2)
{
if (strcmp(name,"entity_name"))
fprintf(o," ignored := visualise(entity_name,\"%s\",%s);\n",
name,name);
}
if (sscanf(line,"%*[ ]signal %[^ ] : %[^:;\( \r\n];",name,class)==2)
{
if (strcmp(name,"entity_name"))
fprintf(o," ignored := visualise(entity_name,\"%s\",%s);\n",
name,name);
}
line[0]=0; fgets(line,1024,f);
}
fclose(f);
fprintf(o," end if;\n");
fprintf(o," end process;\n");
fclose(o);
return 0;
}
int emit_entity(FILE *f,int depth,char *prefix,struct entity *e)
{
char node[1024];
if (prefix[0])
snprintf(node,1024,"%s.%s",prefix,e->name);
else
snprintf(node,1024,"%s",e->name);
fprintf(stderr,"emit_entity('%s')\n",node);
fprintf(f,"<a name=\"%s\"></a>\n",node);
for(int i=0;i<depth;i++) fprintf(f," ");
fprintf(f,"<div class=entity id=\"%s\">\n",node);
for(int i=0;i<=depth;i++) fprintf(f," ");
fprintf(f,"<div class=entitytitle>%s ",node);
fprintf(f,"<div class=navitem><a href=\"frame%d.html#%s\"><</a></div>"
" <div class=navitem><a href=\"frame%d.html#%s\">></a></div>\n",
frame_number?frame_number-1:0,node,frame_number+1,node);
fprintf(f,"</div>\n");
for(int i=0;i<=depth;i++) fprintf(f," ");
fprintf(f,"<div class=signallist>\n");
for(int i=0;i<signal_count;i++) {
if (!strcmp(signals[i].node,node)) {
for(int i=0;i<=depth;i++) fprintf(f," ");
fprintf(f,"<div class=signal%s id=\"%s.%s\">%s = %s</div>\n",
signals[i].changed?"updated":"",
signals[i].node,signals[i].name,
signals[i].name,signals[i].value);
}
}
for(int i=0;i<=depth;i++) fprintf(f," ");
fprintf(f,"</div>\n");
// Process children
struct entity *c=e->children;
if (c) {
emit_entity(f,depth+1,node,c);
}
for(int i=0;i<depth;i++) fprintf(f," ");
fprintf(f,"</div>\n");
if (e->next) {
emit_entity(f,depth,prefix,e->next);
}
return 0;
}
int generate_frame(long long timestep)
{
char filename[1024];
snprintf(filename,1024,"html/frame%d.html",frame_number);
fprintf(stderr,"Writing to %s\n",filename);
FILE *f=fopen(filename,"w");
if (!f) return -1;
fprintf(f,"<html><head>\n");
fprintf(f," <link rel=\"stylesheet\" type=\"text/css\" href=\"frame.css\"></head>\n");
fprintf(f,"<body>\n");
#if 0
// Include navigation between frames
fprintf(f,"<div class=navbar><a href=\"frame%d.html\"+window.location.hash>Previous frame</a> <a href=\"frame%d.html\"+window.location.hash>Next frame</a></div>\n",
frame_number?frame_number-1:0,frame_number+1);
fprintf(f,"<div class=navbarunder><a href=\"frame%d.html\"+window.location.hash>Previous frame</a> <a href=\"frame%d.html\"+window.location.hash>Next frame</a></div>\n",
frame_number?frame_number-1:0,frame_number+1);
#endif
fprintf(f,"<div class=navbar>%lld ns</div>\n",
timestep);
emit_entity(f,0,"",model);
fprintf(f,"</body>\n</html>\n");
fclose(f);
for(int i=0;i<signal_count;i++)
fprintf(stderr,"signal #%d : '%s' '%s' '%s'\n",
i,signals[i].node,signals[i].name,signals[i].value);
// Clear change flags on signals
for(int i=0;i<signal_count;i++) signals[i].changed=0;
frame_number++;
return 0;
}
int main(int argc,char **argv)
{
// Build model of the system
vhdl_structure_discover(0,"gs4502b","gs4502b",&model);
fprintf(stderr,"Read model.\n");
// Watch for VISUALISE lines in output of ghdl, and generate our visualisations
// from that, whenever time advances.
// The lines have a format like:
// visualise.vhdl:75:5:@1625ns:(report note): VISUALISE:gs4502b:mem_ports_in(2):mem_port_in:false
char line[1024],module[1024],signal[1024],type[1024],value[1024];
long long timestamp;
long long last_timestamp=-1;
line[0]=0; fgets(line,1024,stdin);
while(line[0]) {
int r=sscanf(line,
"%*[^:]:%*d:%*d:@%lldns:(report note):"
" VISUALISE:%[^:]:%[^:]:%[^:]:%[^\r\n]",
×tamp,module,signal,type,value);
if (r==5) {
fprintf(stderr,"%lldns:%s:%s:%s:%s\n",
timestamp,module,signal,type,value);
log_signal(module,signal,type,value);
if (timestamp!=last_timestamp&&last_timestamp>-1) {
fprintf(stderr,"Time has advanced to %lldns\n",timestamp);
generate_frame(last_timestamp);
}
last_timestamp=timestamp;
}
line[0]=0; fgets(line,1024,stdin);
}
return 0;
}