-
Notifications
You must be signed in to change notification settings - Fork 1
/
h2vhdl.c
206 lines (187 loc) · 3.68 KB
/
h2vhdl.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
/** API generator for VHDL
*
* (c) 2011, 2012, Martin Strubel <hackfin@section5.ch>
*
* This is a somewhat nasty hack to generate API functions for GHDL
* in a semi automated way.
*
* The API definitions are found in apidef.h
*
*/
#include "ghpi.h"
#include <stdio.h>
#include <string.h>
// The C function prefix
#define PREFIX "sim_"
enum {
TYPE_COMMENT,
TYPE_FUNC,
TYPE_PROC,
};
struct param_desc {
char *name;
char *type;
};
#define RUN_TYPES
#include "apidef.h"
#undef RUN_TYPES
struct ghdl_apidesc {
char *name;
int type;
const char *retitem;
const char *parameters[16];
} g_api[] = {
#include "apidef.h"
{ .name = NULL }
};
int dump_header(FILE *f, char *item, struct ghdl_apidesc *d)
{
char const **p;
int i = 0;
fprintf(f, "%s %s", item, d->name);
p = d->parameters;
if (p[0]) {
if (p[2]) {
fprintf(f, "(%s : %s %s", p[0], p[2], p[1]);
} else {
fprintf(f, "(%s : %s", p[0], p[1]);
}
p += 3; i++;
}
while (p[0]) {
if (p[2]) {
fprintf(f, "; %s : %s %s", p[0], p[2], p[1]);
} else {
fprintf(f, "; %s : %s", p[0], p[1]);
}
p += 3; i++;
}
return i;
}
void dump_comment(FILE *f, struct ghdl_apidesc *d, int which)
{
if (which) { fprintf(f, "--! %s\n", d->name); }
else { fprintf(f, "-- %s\n", d->name); }
}
void dump_func_decl(FILE *f, struct ghdl_apidesc *d)
{
dump_header(f, "function", d);
fprintf(f, ") return %s", d->retitem);
fprintf(f, ";\n");
fprintf(f, "\tattribute foreign of %s :\n"
"\t\tfunction is \"VHPIDIRECT %s%s\";\n\n", d->name, PREFIX, d->name);
}
void dump_proc_decl(FILE *f, struct ghdl_apidesc *d)
{
int i;
i = dump_header(f, "procedure", d);
if (i) {
fprintf(f, ");\n");
} else {
fprintf(f, ";\n");
}
fprintf(f, "\tattribute foreign of %s :\n"
"\t\tprocedure is \"VHPIDIRECT %s%s\";\n\n", d->name, PREFIX, d->name);
}
void dump_func_body(FILE *f, struct ghdl_apidesc *d)
{
int i;
i = dump_header(f, "function", d);
if (i) {
fprintf(f, ") return %s is\n", d->retitem);
} else {
fprintf(f, " return %s is\n", d->retitem);
}
fprintf(f,
"begin\n"
"\tassert false report \"VHPI\" severity failure;\n"
"end function;\n\n");
}
void dump_proc_body(FILE *f, struct ghdl_apidesc *d)
{
int i;
i = dump_header(f, "procedure", d);
if (i) {
fprintf(f, ") is\n");
} else {
fprintf(f, " is\n");
}
fprintf(f,
"begin\n"
"\tassert false report \"VHPI\" severity failure;\n"
"end procedure;\n\n");
}
int dump_decl(FILE *f)
{
struct ghdl_apidesc *d = g_api;
// struct param_desc *p;
while(d->name) {
switch (d->type) {
case TYPE_FUNC:
dump_func_decl(f, d);
break;
case TYPE_PROC:
dump_proc_decl(f, d);
break;
default:
dump_comment(f, d, 1);
}
d++;
}
return 0;
}
int dump_body(FILE *f)
{
struct ghdl_apidesc *d = g_api;
d = g_api;
while(d->name) {
switch (d->type) {
case TYPE_FUNC:
dump_func_body(f, d);
break;
case TYPE_PROC:
dump_proc_body(f, d);
break;
// Omit comments in body
// default:
// dump_comment(f, d, 0);
}
d++;
}
return 0;
}
const char g_line[] =
"----------------------------------------------------------------------\n";
FILE *create_file(const char *fname, const char *rule)
{
FILE *f;
char filename[256];
snprintf(filename, sizeof(filename) - 1, rule, fname);
f = fopen(filename, "w");
if (!f) {
fprintf(stderr, "Failed to open file for writing\n");
}
return f;
}
int main(int argc, char **argv)
{
FILE *f;
if (argc == 2) {
f = create_file(argv[1], "%s_decl.chdl");
if (f) {
dump_decl(f);
fclose(f);
}
f = create_file(argv[1], "%s_body.chdl");
if (f) {
dump_body(f);
fclose(f);
}
} else {
printf(g_line);
return dump_decl(stdout);
printf(g_line);
return dump_body(stdout);
}
return 0;
}