OpenCores
URL https://opencores.org/ocsvn/csa/csa/trunk

Subversion Repositories csa

[/] [csa/] [trunk/] [bench/] [csa_pli.c] - Blame information for rev 41

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 simon111
/*
2
 * =====================================================================================
3
 *
4
 *       Filename:  read_ikey.c
5
 *
6
 *    Description:  this is a pli module to read the input key
7
 *
8
 *        Version:  1.0
9
 *        Created:  07/10/2008 09:18:10 PM
10
 *       Revision:  none
11
 *       Compiler:  gcc
12
 *
13
 *         Author:  mengxipeng@gmail.com
14
 *        Company:  mengxipeng
15
 *
16
 * =====================================================================================
17
 */
18
 
19
#include <string.h>
20 20 simon111
#include <stdio.h>
21 36 simon111
#include <errno.h>
22 13 simon111
#include <vpi_user.h>
23
 
24
 
25 20 simon111
char  data[120*8*8];
26 13 simon111
 
27 36 simon111
 
28
static int read_data_compile(char* used_data)
29
{
30
        vpiHandle systf_h;
31
        vpiHandle trarg_itr; // argument iterator
32
        vpiHandle trarg_h;   // argument handle;
33
        PLI_INT32 trarg_type; // argument type
34
        PLI_INT32 reg_size; // argument type
35
 
36
        systf_h=vpi_handle(vpiSysTfCall,NULL);
37
        if(!systf_h)
38
        {
39
                vpi_printf("ERROR: could not obtain the handle to systf call\n");
40 41 simon111
                //tf_dofinish();
41 36 simon111
                return 0;
42
        }
43
 
44
        trarg_itr=vpi_iterate(vpiArgument, systf_h);
45
        if(!trarg_itr)
46
        {
47
                vpi_printf("ERROR: could not obtain the iterate to argument\n");
48 41 simon111
                //tf_dofinish();
49 36 simon111
                return 0;
50
        }
51
 
52
        // get the first argument
53
        trarg_h=vpi_scan(trarg_itr);
54
        if(vpi_get(vpiType,trarg_h)!=vpiConstant)
55
        {
56
                if(vpi_get(vpiConstType,trarg_h)!=vpiStringConst)
57
                {
58
                        vpi_printf("the first argument type is incorrect (not a string constant)\n");
59 41 simon111
                        //tf_dofinish();
60 36 simon111
                        return 0;
61
                }
62
        }
63
 
64
        // get the next argumnent
65
        trarg_h=vpi_scan(trarg_itr);
66
        trarg_type=vpi_get(vpiType,trarg_h);
67 41 simon111
        if(trarg_type==vpiConstant || trarg_type == vpiIntegerVar)
68 36 simon111
        // offset ?
69
        {
70 41 simon111
                if(trarg_type==vpiConstant
71
                        && vpi_get(vpiConstType,trarg_h)!=vpiDecConst
72
                        && vpi_get(vpiConstType,trarg_h)!=vpiBinaryConst      // iverilog have this bug
73
                        )
74 36 simon111
                {
75 41 simon111
                        vpi_printf("[%d]the offset must be dec constant [%d] \n",__LINE__,vpi_get(vpiConstType,trarg_h));
76
                        //tf_dofinish();
77 36 simon111
                        return 0;
78
                }
79
                trarg_h=vpi_scan(trarg_itr);
80
                trarg_type=vpi_get(vpiType,trarg_h);
81
        }
82
 
83
        if(trarg_type!=vpiReg)
84
        {
85 41 simon111
                vpi_printf("[%d]error:the current argument's type must be reg [%d]\n",__LINE__,trarg_type);
86
                //tf_dofinish();
87 36 simon111
                return 0;
88
        }
89
 
90
        reg_size=vpi_get(vpiSize,trarg_h);
91
 
92
        trarg_h=vpi_scan(trarg_itr);
93
        if(trarg_h)
94
        // size argument
95
        {
96
                s_vpi_value  value_s;
97
                trarg_type=vpi_get(vpiType,trarg_h);
98
                if(trarg_type!=vpiConstant || vpi_get(vpiConstType,trarg_h)!=vpiBinaryConst)
99
                {
100
                        vpi_printf("error:size type must be a binary constant");
101 41 simon111
                        //tf_dofinish();
102 36 simon111
                        return 0;
103
                }
104
                value_s.format=vpiIntVal;
105
                vpi_get_value(trarg_h,&value_s);
106
                if(value_s.value.integer*8>reg_size)
107
                {
108
                        vpi_printf("warning:size is beyond the length of the register");
109
                }
110
        }
111
 
112
        return 0;
113
}
114
 
115 13 simon111
static int read_data(char *fn)
116
{
117
        vpiHandle    systf_handle;
118
        vpiHandle    arg_itr;
119
        vpiHandle    arg_handle;
120 36 simon111
        vpiHandle    arg_handle_reg;
121
        PLI_INT32    arg_type;
122
        PLI_INT32    arg_size;
123
        PLI_INT32    read_size;
124 13 simon111
        s_vpi_value  value_s;
125
 
126
        FILE        *fp;
127
 
128
        systf_handle = vpi_handle(vpiSysTfCall, NULL);
129
        arg_itr = vpi_iterate(vpiArgument, systf_handle);
130
        if (arg_itr == NULL)
131
        {
132 36 simon111
                vpi_printf("ERROR: failed to obtain systf arg handles\n");
133 41 simon111
                //tf_dofinish();
134 13 simon111
                return(0);
135
        }
136
 
137
        /* read file name */
138
        arg_handle = vpi_scan(arg_itr);
139
        value_s.format = vpiStringVal;
140
        vpi_get_value(arg_handle, &value_s);
141 36 simon111
        fp=fopen(value_s.value.str,"rb");
142
        if(!fp)
143
        {
144
                vpi_printf("ERROR: failed to open the file [%s]\n",value_s.value.str);
145 41 simon111
                //tf_dofinish();
146 36 simon111
                return 0;
147
        }
148 13 simon111
 
149 36 simon111
        arg_handle = vpi_scan(arg_itr);
150
        arg_type=vpi_get(vpiType,arg_handle);
151
        arg_handle_reg=arg_handle;
152 41 simon111
        if(arg_type==vpiConstant || arg_type == vpiIntegerVar)
153
        // offset ?
154 13 simon111
        {
155 36 simon111
                value_s.format = vpiIntVal;
156
                vpi_get_value(arg_handle, &value_s);
157
                if(0<fseek(fp,value_s.value.integer,SEEK_SET))
158
                {
159
                        vpi_printf("warning: failed to seek the offset\n");
160
                }
161 35 simon111
 
162 36 simon111
                arg_handle_reg = vpi_scan(arg_itr);
163
        }
164 35 simon111
 
165 36 simon111
 
166
        // calute the size
167
        read_size=vpi_get(vpiSize,arg_handle_reg);
168
        arg_handle = vpi_scan(arg_itr);
169
        if(arg_handle)
170
        {
171 41 simon111
 
172
                vpi_printf("[%d] go here\n",__LINE__);
173 36 simon111
                value_s.format = vpiIntVal;
174
                vpi_get_value(arg_handle, &value_s);
175
                if(value_s.value.integer<read_size*8)
176
                        read_size=value_s.value.integer*8;
177
        }
178
 
179
        // read the value to vector
180
        {
181
                unsigned int dword;
182
                int i;
183
                int read;
184 35 simon111
                value_s.format = vpiVectorVal;
185 36 simon111
                vpi_get_value(arg_handle_reg, &value_s);
186 37 simon111
                for(i=0;i+32<=read_size;i+=32)
187 35 simon111
                {
188 37 simon111
                        read=fread(&dword,4,1,fp);
189
                        if(1!=read)
190 36 simon111
                        {
191 37 simon111
                                vpi_printf("warning read fail [%d] \n",read);
192
                                break;
193 36 simon111
                        }
194 37 simon111
                        value_s.value.vector[i/32].bval=0x00;
195
                        value_s.value.vector[i/32].aval=dword;
196 35 simon111
                }
197 36 simon111
                if(i<read_size)
198
                {
199
                        PLI_INT32 reduant=read_size-i;
200
                        PLI_INT32 reduant_mask;
201
                        int n;
202
                        reduant_mask=0;
203
                        for(n=0;n<reduant;n++)
204
                        {
205
                                reduant_mask |= (1<<n);
206
                        }
207
                        read=fread(&dword,reduant/8,1,fp);
208
                        if(1!=read)
209
                        {
210
                                vpi_printf("warning fail \n");
211
                        }
212
                        value_s.value.vector[i/32].bval&=~reduant_mask;
213
                        value_s.value.vector[i/32].aval&=~reduant_mask;
214
                        value_s.value.vector[i/32].aval|=reduant_mask&dword;
215
                }
216
                vpi_put_value(arg_handle_reg, &value_s,NULL, vpiNoDelay);
217 13 simon111
        }
218 36 simon111
        fclose(fp);
219 13 simon111
        return 0;
220
}
221
 
222 36 simon111
 
223
// thia vpi funciton read_data is used to read a binary to a verilog register;
224
//    usage: read_data ( file name,   [ offset ] ,  register name ,[size ]);
225
//              note offset and size argument's unit is byte
226
 
227 13 simon111
static void read_data_register()
228
{
229
        s_vpi_systf_data tf_data;
230
        tf_data.type      = vpiSysTask;
231
        tf_data.tfname    = "$read_data";
232
        tf_data.calltf    = read_data;
233 36 simon111
        tf_data.compiletf = read_data_compile;
234 13 simon111
        tf_data.sizetf    = 0;
235
        vpi_register_systf(&tf_data);
236
}
237
 
238 37 simon111
// write system call
239
// usage $write_data( file name , [ flags ,] reg)
240
 
241
static int write_data_compile(char* used_data)
242
{
243
        vpiHandle systf_h;
244
        vpiHandle trarg_itr; // argument iterator
245
        vpiHandle trarg_h;   // argument handle;
246
        PLI_INT32 trarg_type; // argument type
247
        s_vpi_value  value_s;
248
 
249
 
250
        systf_h=vpi_handle(vpiSysTfCall,NULL);
251
        if(!systf_h)
252
        {
253
                vpi_printf("ERROR: could not obtain the handle to systf call\n");
254 41 simon111
                //tf_dofinish();
255 37 simon111
                return 0;
256
        }
257
 
258
        trarg_itr=vpi_iterate(vpiArgument, systf_h);
259
        if(!trarg_itr)
260
        {
261
                vpi_printf("ERROR: could not obtain the iterate to argument\n");
262 41 simon111
                //tf_dofinish();
263 37 simon111
                return 0;
264
        }
265
 
266
 
267
        // get the first argument
268
        trarg_h=vpi_scan(trarg_itr);
269
        if(vpi_get(vpiType,trarg_h)!=vpiConstant&&vpi_get(vpiConstType,trarg_h)!=vpiStringConst)
270
        {
271
                vpi_printf("the first argument type is incorrect (not a string constant)\n");
272 41 simon111
                //tf_dofinish();
273 37 simon111
                return 0;
274
        }
275
 
276
        // get flag
277
        trarg_h=vpi_scan(trarg_itr);
278
        trarg_type=vpi_get(vpiType,trarg_h);
279
        if(trarg_type==vpiConstant&&vpi_get(vpiConstType,trarg_h)==vpiStringConst)
280
        // flag
281
        {
282
                value_s.format=vpiStringVal;
283
                vpi_get_value(trarg_h,&value_s);
284
 
285
                if(
286
                           strcasecmp(value_s.value.str,"A")
287
                           )
288
                {
289
                        vpi_printf("error, not only support addpend A flag[%s]\n",value_s.value.str);
290 41 simon111
                        //tf_dofinish();
291 37 simon111
                        return 0;
292
                }
293
 
294
                trarg_h=vpi_scan(trarg_itr);
295
                trarg_type=vpi_get(vpiType,trarg_h);
296
        }
297
 
298 41 simon111
        if(
299
                trarg_type!=vpiConstant
300
                &&trarg_type!=vpiReg
301
                &&trarg_type!=vpiPartSelect
302
                //&&trarg_type!=vpiRegBit
303
                &&trarg_type!=vpiNet
304
                )
305 37 simon111
        {
306 41 simon111
                vpi_printf("[%d]error, the last argument is not a valid val [%d]\n",__LINE__,trarg_type);
307
                //tf_dofinish();
308 37 simon111
                return 0;
309
        }
310
        if(trarg_type==vpiConstant)
311
        {
312
                if(0!=vpi_get(vpiConstType,trarg_h))
313
                {
314
                        vpi_printf("error, the last argument is not a sub 0 type constant val \n");
315 41 simon111
                        //tf_dofinish();
316 37 simon111
                        return 0;
317
                }
318
        }
319
 
320
        return 0;
321
}
322
 
323 13 simon111
static int write_data(char *xx)
324
{
325
        vpiHandle    systf_handle;
326
        vpiHandle    arg_itr;
327 37 simon111
        vpiHandle    arg_handle_fn;
328 13 simon111
        vpiHandle    arg_handle;
329
        s_vpi_value  value_s;
330
 
331
        FILE        *fp;
332
 
333
        systf_handle = vpi_handle(vpiSysTfCall, NULL);
334 37 simon111
        if(systf_handle== NULL)
335
        {
336 40 simon111
                vpi_printf("ERROR: failed to obtain systf call handles\n");
337 41 simon111
                //tf_dofinish();
338 37 simon111
                return(0);
339
        }
340 13 simon111
        arg_itr = vpi_iterate(vpiArgument, systf_handle);
341
        if (arg_itr == NULL)
342
        {
343 40 simon111
                vpi_printf("ERROR: failed to obtain systf arg handles\n");
344 41 simon111
                //tf_dofinish();
345 13 simon111
                return(0);
346
        }
347
 
348
        /* read file name */
349
        arg_handle = vpi_scan(arg_itr);
350
        value_s.format = vpiStringVal;
351
        vpi_get_value(arg_handle, &value_s);
352
 
353 15 simon111
        arg_handle = vpi_scan(arg_itr);
354 37 simon111
        if(vpi_get(vpiType,arg_handle)==vpiConstant&&vpi_get(vpiConstType,arg_handle)==vpiStringConst)
355
        // flags
356
        {
357
                fp=fopen(value_s.value.str,"ab");// if have flag, then open the file with append mode
358
                arg_handle = vpi_scan(arg_itr);
359
        }
360 15 simon111
        else
361 13 simon111
        {
362 37 simon111
                fp=fopen(value_s.value.str,"wb");
363
        }
364
        if(!fp)
365
        {
366
                vpi_printf("can not open file to write\n");
367 41 simon111
                //tf_dofinish();
368 37 simon111
                return 0;
369
        }
370
 
371
        // write data
372
        {
373 35 simon111
                unsigned int word;
374
                int b;
375
                int i;
376
                int vector_size;
377
                value_s.format = vpiVectorVal;
378
                vector_size = vpi_get(vpiSize, arg_handle);
379 13 simon111
                vpi_get_value(arg_handle, &value_s);
380 37 simon111
                for(i=0;i+32<=vector_size;i+=32)
381 35 simon111
                {
382 37 simon111
                        if(1!=fwrite(&value_s.value.vector[i/32].aval,4,1,fp))
383
                        {
384
                                vpi_printf("[%d]warning:write fail \n",__LINE__);
385
                        }
386 35 simon111
                }
387 37 simon111
                if(i<vector_size)
388
                {
389
                        int n;
390
                        unsigned char b;
391
                        unsigned int  last=value_s.value.vector[i/32].aval;
392
                        unsigned int  mask=0xff;
393
                        PLI_INT32 reduant=vector_size-i;
394
                        for(n=0;n<reduant;n+=8)
395
                        {
396
                                b=(last&(mask<<n))>>n;
397
                                if(1!=fwrite(&b,1,1,fp))
398
                                {
399
                                        vpi_printf("[%d]warning:write fail \n",__LINE__);
400
                                }
401
                        }
402
                }
403
                //fwrite(&value_s.value.vector[i].aval,(vector_size%32)/8,1,fp);
404 13 simon111
                fclose(fp);
405
        }
406
 
407
        return 0;
408
}
409
 
410
static void write_data_register()
411
{
412
        s_vpi_systf_data tf_data;
413
        tf_data.type      = vpiSysTask;
414
        tf_data.tfname    = "$write_data";
415
        tf_data.calltf    = write_data;
416 37 simon111
        tf_data.compiletf = write_data_compile;
417 13 simon111
        tf_data.sizetf    = 0;
418
        vpi_register_systf(&tf_data);
419
}
420
 
421
void (*vlog_startup_routines[])() = {
422
        read_data_register,
423
        write_data_register,
424
 
425
};
426
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.