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

Subversion Repositories csa

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

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
                if(vpi_get(vpiConstType,trarg_h)!=vpiBinaryConst)
71
                {
72
                        vpi_printf("the offset must be dec constant [%d] \n",vpi_get(vpiConstType,trarg_h));
73
                        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
                i=0;
181
                if(read_size >= 32)
182 35 simon111
                {
183 36 simon111
                        for(i=0;i<read_size;i+=32)
184
                        {
185
                                read=fread(&dword,4,1,fp);
186
                                if(1!=read)
187
                                {
188
                                        vpi_printf("warning read fail [%d] \n",read);
189
                                        break;
190
                                }
191
                                value_s.value.vector[i/32].bval=0x00;
192
                                value_s.value.vector[i/32].aval=dword;
193
                        }
194 35 simon111
                }
195 36 simon111
                if(i<read_size)
196
                {
197
                        PLI_INT32 reduant=read_size-i;
198
                        PLI_INT32 reduant_mask;
199
                        int n;
200
                        reduant_mask=0;
201
                        for(n=0;n<reduant;n++)
202
                        {
203
                                reduant_mask |= (1<<n);
204
                        }
205
                        read=fread(&dword,reduant/8,1,fp);
206
                        if(1!=read)
207
                        {
208
                                vpi_printf("warning fail \n");
209
                        }
210
                        value_s.value.vector[i/32].bval&=~reduant_mask;
211
                        value_s.value.vector[i/32].aval&=~reduant_mask;
212
                        value_s.value.vector[i/32].aval|=reduant_mask&dword;
213
                }
214
                vpi_put_value(arg_handle_reg, &value_s,NULL, vpiNoDelay);
215 13 simon111
        }
216 36 simon111
        fclose(fp);
217 13 simon111
        return 0;
218
}
219
 
220 36 simon111
 
221
// thia vpi funciton read_data is used to read a binary to a verilog register;
222
//    usage: read_data ( file name,   [ offset ] ,  register name ,[size ]);
223
//              note offset and size argument's unit is byte
224
 
225 13 simon111
static void read_data_register()
226
{
227
        s_vpi_systf_data tf_data;
228
        tf_data.type      = vpiSysTask;
229
        tf_data.tfname    = "$read_data";
230
        tf_data.calltf    = read_data;
231 36 simon111
        tf_data.compiletf = read_data_compile;
232 13 simon111
        tf_data.sizetf    = 0;
233
        vpi_register_systf(&tf_data);
234
}
235
 
236
static int write_data(char *xx)
237
{
238
        vpiHandle    systf_handle;
239
        vpiHandle    arg_itr;
240
        vpiHandle    arg_handle;
241
        s_vpi_value  value_s;
242
 
243
        FILE        *fp;
244
        char         str[120];
245
        int          i;
246
        int          n;
247
 
248
        systf_handle = vpi_handle(vpiSysTfCall, NULL);
249
        arg_itr = vpi_iterate(vpiArgument, systf_handle);
250
        if (arg_itr == NULL)
251
        {
252
                vpi_printf("ERROR: $pow failed to obtain systf arg handles\n");
253
                return(0);
254
        }
255
 
256
        /* read file name */
257
        arg_handle = vpi_scan(arg_itr);
258
        value_s.format = vpiStringVal;
259
        vpi_get_value(arg_handle, &value_s);
260
        strcpy(str,value_s.value.str);
261
 
262 15 simon111
        arg_handle = vpi_scan(arg_itr);
263
        value_s.format = vpiStringVal;
264
        vpi_get_value(arg_handle, &value_s);
265
        if(!strcmp("a",value_s.value.str))
266 35 simon111
                fp=fopen(str,"ab");
267 15 simon111
        else
268 35 simon111
                fp=fopen(str,"wb");
269 13 simon111
        if(fp)
270
        {
271 35 simon111
                unsigned int word;
272
                int b;
273
                int i;
274
                int vector_size;
275
                value_s.format = vpiVectorVal;
276 13 simon111
                arg_handle = vpi_scan(arg_itr);
277 35 simon111
                vector_size = vpi_get(vpiSize, arg_handle);
278 13 simon111
                vpi_get_value(arg_handle, &value_s);
279 35 simon111
                for(i=0;i<vector_size/32;i++)
280
                {
281
                        fwrite(&value_s.value.vector[i].aval,4,1,fp);
282
                }
283
                fwrite(&value_s.value.vector[i].aval,(vector_size%32)/8,1,fp);
284 13 simon111
                fclose(fp);
285
        }
286
        else
287
        {
288
                vpi_printf("can't open the output file in %s \n", __FUNCTION__);
289
        }
290
 
291
        return 0;
292
}
293
 
294
static void write_data_register()
295
{
296
        s_vpi_systf_data tf_data;
297
        tf_data.type      = vpiSysTask;
298
        tf_data.tfname    = "$write_data";
299
        tf_data.calltf    = write_data;
300
        tf_data.compiletf = 0;
301
        tf_data.sizetf    = 0;
302
        vpi_register_systf(&tf_data);
303
}
304
 
305
void (*vlog_startup_routines[])() = {
306
        read_data_register,
307
        write_data_register,
308
 
309
};
310
 

powered by: WebSVN 2.1.0

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