rspcode.c
11.1 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/********************************************************************
Create RSP code for test vectors
RSP code file format
4 Bytes: #of Test
4 Bytes: Padding to 8 bytes align
4 Bytes: test i Dmem offset(from RSP_CODE_START)
4 Bytes: test i Dmem size
4 Bytes: test i Imem offset(from RSP_CODE_START)
4 Bytes: test i Imem size
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_RSP_CODE 1024
typedef struct {
int num;
int padding;
int dmem_offset[MAX_RSP_CODE];
int dmem_size[MAX_RSP_CODE];
int imem_offset[MAX_RSP_CODE];
int imem_size[MAX_RSP_CODE];
} RSP_CODE;
#define OPTARG "adc:s:n:g:m:iho:"
void usage(void)
{
printf("Usage: rspcode \n");
printf(" create rsp code for test vectors \n");
printf(" -a appending specified rsp code to the new file \n");
printf(" -o old code file \n");
printf(" -c new rsp code \n");
printf(" -d delete part of rspcode(start from 0) \n");
printf(" -s start code to delete\n");
printf(" -n how many code to delete\n");
printf(" -g gng rspcode file\n");
printf(" -m dmem code\n");
printf(" -i imem code \n");
printf(" -h help(this page) \n");
return ;
}
/* Read a big-endian integer from file */
int read_mips_int(FILE *f)
{
int ret = 0, i;
for (i=0; i<4; i++) {
ret <<= 8;
ret |= fgetc(f) & 0xff;
}
return ret;
}
void write_mips_int(FILE *f, int v)
{
int i;
for (i=24; i>=0; i-=8)
fputc((v>>i) & 0xff, f);
}
int read_rsp_code(FILE *f, RSP_CODE *code)
{
int i, total, filesize;
fseek(f, 0, SEEK_END);
filesize = ftell(f);
fseek(f, 0, SEEK_SET);
code->num = read_mips_int(f);
if (code->num > MAX_RSP_CODE || code->num < 0) {
printf("Error: # of rsp(%d) code is wrong \n", code->num);
return -1;
}
fseek(f, 8, SEEK_SET);
total = 8 + 16 * code->num;
for (i=0; i<code->num; i++) {
code->dmem_offset[i] = read_mips_int(f);
code->dmem_size[i] = read_mips_int(f);
code->imem_offset[i] = read_mips_int(f);
code->imem_size[i] = read_mips_int(f);
if ( (code->dmem_offset[i] | code->dmem_size[i] |
code->imem_offset[i] | code->imem_size[i]) & 0x7) {
printf("Error: %d code or offset not align to 8 bytes \n", i);
printf("dmem offset=%d size=%d\n", code->dmem_offset[i], code->dmem_size[i]);
printf("imem offset=%d size=%d\n", code->imem_offset[i], code->imem_size[i]);
return -1;
}
if (code->dmem_size[i] > 0x1000 || code->imem_size[i] > 0x1000 ||
code->dmem_size[i] < 0 || code->imem_size[i] <= 0) {
printf("Error: dmem(%d)/imem(%d) wrong (code No=%d)\n",
code->dmem_size[i], code->imem_size[i], i);
return -1;
}
total += (code->dmem_size[i] + code->imem_size[i]);
}
if (total != filesize) {
printf("Error: file size is wrong exp=%d act=%d", total, filesize);
return -1;
}
return 0;
}
int write_rsp_header_old_code(FILE *fcode, FILE *fold, RSP_CODE *pcode,
int ex_start, int ex_num, int old_code_num)
{
int i, offset, j;
write_mips_int(fcode, pcode->num - ex_num);
write_mips_int(fcode, 0); // padding to 8 bytes
offset = 8 + 16*(pcode->num - ex_num);
for (i=0; i<pcode->num; i++) {
if ((i >= ex_start) && (i < (ex_start+ex_num)))
continue;
write_mips_int(fcode, offset);
write_mips_int(fcode, pcode->dmem_size[i]);
offset += pcode->dmem_size[i];
write_mips_int(fcode, offset);
write_mips_int(fcode, pcode->imem_size[i]);
offset += pcode->imem_size[i];
}
for (i=0; i<old_code_num; i++) {
if ((i >= ex_start) && (i < (ex_start+ex_num)))
continue;
fseek(fold, pcode->dmem_offset[i], SEEK_SET);
for (j=0; j<pcode->dmem_size[i]; j++)
fputc(fgetc(fold), fcode);
fseek(fold, pcode->imem_offset[i], SEEK_SET);
for (j=0; j<pcode->imem_size[i]; j++)
fputc(fgetc(fold), fcode);
}
return 0;
}
int delete_rsp_code(FILE *fold, FILE *fcode, int start, int n)
{
int i, offset, j;
RSP_CODE code;
if (fold == NULL) {
printf("Delete function need previous code file to be specifed\n");
return -1;
}
if (fcode == NULL) {
printf("Delete function need new code file to be specified\n");
return -1;
}
if (read_rsp_code(fold, &code)) {
printf("Reading rsp code from file error\n");
return -1;
}
if ((start + n) > code.num) {
n = code.num - start;
printf("Warning: not enough code, only %d deleted\n", n);
}
write_rsp_header_old_code(fcode, fold, &code, start, n, code.num);
return 0;
}
int append_gng_code(FILE *fold, FILE *fcode, FILE *fgng)
{
int offset, i, dm, im, old_code_num, j, total=0;
RSP_CODE code;
if (fold == NULL) {
code.num = 0;
} else {
if (read_rsp_code(fold, &code)) {
printf("Reading rsp code from file error\n");
return -1;
}
}
if (fcode == NULL) {
printf("Append gng code function need new code file to be specified\n");
return -1;
}
if (fgng == NULL) {
printf("Append gng code need gng code to be specified\n");
return -1;
}
total = 0;
old_code_num = code.num;
while ( (code.num < MAX_RSP_CODE) && !feof(fgng)) {
fseek(fgng, 4, SEEK_CUR);
dm = read_mips_int(fgng);
if (feof(fgng)) {
dm = 0;
break;
}
if (dm > 0x1000 || dm < 0 || (dm & 0x3)) break;
fseek(fgng, 4+dm, SEEK_CUR);
im = read_mips_int(fgng);
if (im > 0x1000 || im <= 0 || (im & 0x3)) break;
fseek(fgng, im, SEEK_CUR);
code.dmem_size[code.num] = (dm+7) & 0x1ff8;
code.imem_size[code.num] = (im+7) & 0x1ff8;
code.num++;
}
if ((dm > 0x1000 || dm < 0 || (dm & 0x3)) ||
(im > 0x1000 || im <= 0 || (im & 0x3))) {
printf("GNG rsp code file wrong(4bytes align <= 4k) \n");
printf("At %d rsp code dm size = %d im size = %d \n", code.num, dm, im);
return -1;
}
write_rsp_header_old_code(fcode, fold, &code, 0, 0, old_code_num);
fseek(fgng, 0, SEEK_SET);
while (!feof(fgng)) {
fseek(fgng, 4, SEEK_CUR);
dm = read_mips_int(fgng);
if (feof(fgng)) break;
for (j=0; j<dm; j++) fputc(fgetc(fgng), fcode);
for (j=dm; j<((dm+7)&0x1ff8); j++) fputc(0, fcode);
fseek(fgng, 4, SEEK_CUR);
im = read_mips_int(fgng);
for (j=0; j<im; j++) fputc(fgetc(fgng), fcode);
for (j=im; j<((im+7)&0x1ff8); j++) fputc(0, fcode);
}
return 0;
}
int append_imem_dmem(FILE *fold, FILE *fcode, FILE *fdmem, FILE *fimem)
{
int offset, i, dm, im;
RSP_CODE code;
if (fold == NULL) {
code.num = 0;
} else {
if (!read_rsp_code(fold, &code)) {
printf("Reading rsp code from file error\n");
return -1;
}
}
if (fcode == NULL) {
printf("Append imem_dmem code function need new code file to be specified\n");
return -1;
}
if (fdmem == NULL) {
dm = 0;
printf("Warning No dmem data \n");
} else {
fseek(fdmem, 0, SEEK_END);
dm = ftell(fdmem);
fseek(fdmem, 0, SEEK_SET);
}
if (fimem == NULL) {
printf("Append imem code not specified \n");
return -1;
}
fseek(fimem, 0, SEEK_END);
im = ftell(fimem);
fseek(fimem, 0, SEEK_SET);
if (code.num >= MAX_RSP_CODE) {
printf("We do not support #of code > %d\n", MAX_RSP_CODE);
return -1;
}
if (dm > 0x1000 || im > 0x1000)
printf("imem(%d) or dmem(%d) code size is too large\n", im, dm);
code.dmem_size[code.num] = (dm + 7) & 0x1ff8; // < 4k and 8 bytes aligned
code.imem_size[code.num] = (im + 7) & 0x1ff8;
code.num++;
write_rsp_header_old_code(fcode, fold, &code, 0, 0, code.num-1);
for (i=0; i<dm; i++)
fputc(fgetc(fdmem), fcode);
for (i=dm; i<code.dmem_size[code.num-1]; i++) fputc(0, fcode);
for (i=0; i<im; i++)
fputc(fgetc(fimem), fcode);
for (i=im; i<code.imem_size[code.num-1]; i++) fputc(0, fcode);
return 0;
}
int main(int argc, char ** argv)
{
int c;
int delete=0, add=0;
int start, n, i, ret = 0 ;
FILE *fold=NULL, *fcode=NULL, *fimem=NULL, *fdmem=NULL, *fgng=NULL;
while ((c = getopt(argc, argv, OPTARG)) != EOF) {
switch (c) {
case 'a':
add = 1;
break;
case 'c':
if ((fcode = fopen(optarg, "wb")) == NULL) {
printf("cannot create the new rsp code file: %s \n", optarg);
ret = -1;
goto out;
}
break;
case 'o':
if ((fold = fopen(optarg, "rb")) == NULL) {
printf("cannot open the old code file: %s \n", optarg);
ret = -1;
goto out;
}
break;
case 'd':
delete = 1;
break;
case 's':
start = atoi(optarg);
if (start < 0) start = 0;
break;
case 'n':
n = atoi(optarg);
if (n < 0) n = 0;
break;
case 'g':
if ((fgng = fopen(optarg, "rb")) == NULL) {
printf("cannot open given gng style rsp code file %s \n", optarg);
ret = -1;
goto out;
}
break;
case 'm':
if ((fdmem = fopen(optarg, "rb")) == NULL) {
printf("cannot open dmem data file %s \n", optarg);
ret = -1;
goto out;
}
break;
case 'i':
if ((fimem = fopen(optarg, "rb")) == NULL) {
printf("cannot open sp imem data file %s \n", optarg);
ret = -1;
goto out;
}
break;
case 'h':
default:
usage();
goto out;
}
}
if (delete && add) {
printf("Sorry, to make less confusion \n");
printf(" delete and add cannot run at the same time\n");
}
if (!(delete || add)) {
usage();
goto out;
}
if (delete) delete_rsp_code(fold, fcode, start, n);
else {
if (fgng)
append_gng_code(fold, fcode, fgng);
else
append_imem_dmem(fold, fcode, fdmem, fimem);
}
out:
if (fold) fclose(fold);
if (fcode) fclose(fcode);
if (fgng) fclose(fgng);
if (fimem) fclose(fimem);
if (fdmem) fclose(fdmem);
return ret;
}