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

Subversion Repositories csa

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

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

powered by: WebSVN 2.1.0

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