-
Notifications
You must be signed in to change notification settings - Fork 2
/
vpi_wrapper.c
294 lines (229 loc) · 7.01 KB
/
vpi_wrapper.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* VPI wrapper for elf-loader
*
* Copyright (C) 2015 Olof Kindgren <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <ctype.h>
#include <vpi_user.h>
#include "elf-loader.h"
//install a callback on simulation reset which calls setup
void setup_reset_callbacks(void);
//install a callback on simulation compilation finish
void setup_endofcompile_callbacks(void);
//install a callback on simulation finish
void setup_finish_callbacks(void);
//callback function which closes and clears the socket file descriptors
// on simulation reset
void sim_reset_callback(void);
void sim_endofcompile_callback(void);
void sim_finish_callback(void);
uint8_t *bin_file;
int size;
void elf_load_file(void) {
vpiHandle func_h, args_iter, arg_h;
struct t_vpi_value argval;
char *elf_file_name;
func_h = vpi_handle(vpiSysTfCall, NULL);
args_iter = vpi_iterate(vpiArgument, func_h);
if(!bin_file) {
arg_h = vpi_scan(args_iter);
argval.format = vpiStringVal;
vpi_get_value(arg_h, &argval);
elf_file_name = argval.value.str;
while(isspace(*elf_file_name))
elf_file_name++;
bin_file = load_elf_file(elf_file_name, &size);
if(bin_file)
vpi_printf("elf-loader: %s was loaded\n", elf_file_name);
else
vpi_printf("Error: Failed to load elf file from \"%s\"\n", elf_file_name);
}
//vpi_printf("elf_load_file done\n");
}
void elf_get_size(void) {
vpiHandle func_h;
struct t_vpi_value argval;
if(!bin_file) {
vpi_printf("Error: $elf_load_file must be called first\n");
return;
}
func_h = vpi_handle(vpiSysTfCall, NULL);
argval.format = vpiIntVal;
argval.value.integer = size;
vpi_put_value(func_h, &argval, NULL, vpiNoDelay);
//vpi_printf("elf_get_size done\n");
}
void elf_read_32(void) {
vpiHandle func_h, arg_h, args_iter;
struct t_vpi_value argval;
unsigned int address;
unsigned int data;
//vpi_printf("elf_read_32 start\n");
if(!bin_file) {
vpi_printf("Error: $elf_load_file must be called first\n");
return;
}
func_h = vpi_handle(vpiSysTfCall, NULL);
args_iter = vpi_iterate(vpiArgument, func_h);
// Grab the value of the first argument
arg_h = vpi_scan(args_iter);
argval.format = vpiIntVal;
vpi_get_value(arg_h, &argval);
address = argval.value.integer;
data = read_32(bin_file, address);
argval.value.integer = data;
vpi_put_value(func_h, &argval, NULL, vpiNoDelay);
vpi_free_object(args_iter);
//vpi_printf("elf_read_32 done\n");
}
void elf_read_16(void) {
vpiHandle func_h, arg_h, args_iter;
struct t_vpi_value argval;
unsigned int address;
unsigned short data;
if(!bin_file) {
vpi_printf("Error: $elf_load_file must be called first\n");
return;
}
func_h = vpi_handle(vpiSysTfCall, NULL);
args_iter = vpi_iterate(vpiArgument, func_h);
// Grab the value of the first argument
arg_h = vpi_scan(args_iter);
argval.format = vpiIntVal;
vpi_get_value(arg_h, &argval);
address = argval.value.integer;
data = read_16(bin_file, address);
argval.value.integer = data;
vpi_put_value(func_h, &argval, NULL, vpiNoDelay);
vpi_free_object(args_iter);
//vpi_printf("elf_read_16 done\n");
}
void setup_user_functions(void) {
s_vpi_systf_data task_data_s;
p_vpi_systf_data task_data_p = &task_data_s;
task_data_p->type = vpiSysTask;
task_data_p->sysfunctype = vpiIntFunc;
task_data_p->tfname = "$elf_load_file";
task_data_p->calltf = (void *)elf_load_file;
task_data_p->sizetf = 0;
task_data_p->compiletf = 0;
vpi_register_systf(task_data_p);
task_data_p->type = vpiSysFunc;
task_data_p->tfname = "$elf_get_size";
task_data_p->calltf = (void *)elf_get_size;
task_data_p->compiletf = 0;
vpi_register_systf(task_data_p);
task_data_p->type = vpiSysFunc;
task_data_p->tfname = "$elf_read_32";
task_data_p->calltf = (void *)elf_read_32;
task_data_p->compiletf = 0;
vpi_register_systf(task_data_p);
task_data_p->type = vpiSysFunc;
task_data_p->tfname = "$elf_read_16";
task_data_p->calltf = (void *)elf_read_16;
task_data_p->compiletf = 0;
vpi_register_systf(task_data_p);
}
void setup_reset_callbacks(void)
{
// here we setup and install callbacks for
// the setup and management of connections to
// the simulator upon simulation start and
// reset
static s_vpi_time time_s = {vpiScaledRealTime, 0, 0, 0.0};
static s_vpi_value value_s = {vpiBinStrVal, {}};
static s_cb_data cb_data_s =
{
cbEndOfReset, // or start of simulation - initing socket fds etc
(void *)sim_reset_callback,
NULL,
&time_s,
&value_s,
0,
0
};
cb_data_s.obj = NULL; /* trigger object */
cb_data_s.user_data = NULL;
// actual call to register the callback
vpi_register_cb(&cb_data_s);
}
void sim_reset_callback(void)
{
// nothing to do!
return;
}
void setup_endofcompile_callbacks(void)
{
// here we setup and install callbacks for
// simulation finish
static s_vpi_time time_s = {vpiScaledRealTime, 0, 0, 0.0};
static s_vpi_value value_s = {vpiBinStrVal, {}};
static s_cb_data cb_data_s =
{
cbEndOfCompile, // end of compile
(void *)sim_endofcompile_callback,
NULL,
&time_s,
&value_s,
0,
0
};
cb_data_s.obj = NULL; /* trigger object */
cb_data_s.user_data = NULL;
// actual call to register the callback
vpi_register_cb(&cb_data_s);
}
void sim_endofcompile_callback(void)
{
return;
}
void setup_finish_callbacks(void)
{
// here we setup and install callbacks for
// simulation finish
static s_vpi_time time_s = {vpiScaledRealTime, 0, 0, 0.0};
static s_vpi_value value_s = {vpiBinStrVal, {}};
static s_cb_data cb_data_s =
{
cbEndOfSimulation, // end of simulation
(void *)sim_finish_callback,
NULL,
&time_s,
&value_s,
0,
0
};
cb_data_s.obj = NULL; /* trigger object */
cb_data_s.user_data = NULL;
// actual call to register the callback
vpi_register_cb(&cb_data_s);
}
void sim_finish_callback(void)
{
free(bin_file);
return;
}
void (*vlog_startup_routines[ ] ) (void) = {
#ifdef CDS_VPI
// this installs a callback on simulator reset - something which
// icarus does not do, so we only do it for cadence currently
setup_reset_callbacks,
#endif
setup_endofcompile_callbacks,
setup_finish_callbacks,
setup_user_functions,
0 // last entry must be 0
};