8
8
/* enable program trace mode */
9
9
static bool opt_trace = false;
10
10
11
+ /* RISCV compliance test mode */
12
+ static bool opt_compliance = false;
13
+ static char * signature_out_file ;
14
+
11
15
/* target executable */
12
16
static const char * opt_prog_name = "a.out" ;
13
17
@@ -101,7 +105,8 @@ static void print_usage(const char *filename)
101
105
"RV32I[MA] Emulator which loads an ELF file to execute.\n"
102
106
"Usage: %s [options] [filename]\n"
103
107
"Options:\n"
104
- " --trace : print executable trace\n" ,
108
+ " --trace : print executable trace\n"
109
+ " --compliance [signature filename] : dump signature to the given file for compliance test\n" ,
105
110
filename );
106
111
}
107
112
@@ -118,6 +123,16 @@ static bool parse_args(int argc, char **args)
118
123
opt_trace = true;
119
124
continue ;
120
125
}
126
+ if (!strcmp (arg , "--compliance" )) {
127
+ opt_compliance = true;
128
+ if (i + 1 >= argc ) {
129
+ fprintf (stderr ,
130
+ "Filename for signature output required in compliance mode.\n" );
131
+ return false;
132
+ }
133
+ signature_out_file = args [++ i ];
134
+ continue ;
135
+ }
121
136
/* otherwise, error */
122
137
fprintf (stderr , "Unknown argument '%s'\n" , arg );
123
138
return false;
@@ -129,6 +144,35 @@ static bool parse_args(int argc, char **args)
129
144
return true;
130
145
}
131
146
147
+ void dump_test_signature (struct riscv_t * rv , elf_t * elf )
148
+ {
149
+ uint32_t start = 0 , end = 0 ;
150
+ const struct Elf32_Sym * sym ;
151
+ FILE * f = fopen (signature_out_file , "w" );
152
+ if (!f ) {
153
+ fprintf (stderr , "Cannot open signature output file.\n" );
154
+ return ;
155
+ }
156
+
157
+ /* use the entire .data section as a fallback */
158
+ elf_get_data_section_range (elf , & start , & end );
159
+ /* try and access the exact signature range */
160
+ if ((sym = elf_get_symbol (elf , "begin_signature" )))
161
+ start = sym -> st_value ;
162
+ if ((sym = elf_get_symbol (elf , "end_signature" )))
163
+ end = sym -> st_value ;
164
+
165
+ state_t * s = rv_userdata (rv );
166
+
167
+ /* dump it word by word */
168
+ for (uint32_t addr = start ; addr < end ; addr += 4 ) {
169
+ fprintf (f , "%08x\n" , memory_read_w (s -> mem , addr ));
170
+ }
171
+
172
+ fclose (f );
173
+ }
174
+
175
+
132
176
int main (int argc , char * * args )
133
177
{
134
178
if (!parse_args (argc , args )) {
@@ -177,6 +221,11 @@ int main(int argc, char **args)
177
221
run (rv );
178
222
}
179
223
224
+ /* dump test result in test mode */
225
+ if (opt_compliance ) {
226
+ dump_test_signature (rv , elf );
227
+ }
228
+
180
229
/* finalize the RISC-V runtime */
181
230
elf_delete (elf );
182
231
rv_delete (rv );
0 commit comments