Line 16... |
Line 16... |
* =====================================================================================
|
* =====================================================================================
|
*/
|
*/
|
|
|
#include <string.h>
|
#include <string.h>
|
#include <stdio.h>
|
#include <stdio.h>
|
|
#include <errno.h>
|
#include <vpi_user.h>
|
#include <vpi_user.h>
|
|
|
|
|
char data[120*8*8];
|
char data[120*8*8];
|
|
|
|
|
|
static int read_data_compile(char* used_data)
|
|
{
|
|
vpiHandle systf_h;
|
|
vpiHandle trarg_itr; // argument iterator
|
|
vpiHandle trarg_h; // argument handle;
|
|
PLI_INT32 trarg_type; // argument type
|
|
PLI_INT32 reg_size; // argument type
|
|
|
|
systf_h=vpi_handle(vpiSysTfCall,NULL);
|
|
if(!systf_h)
|
|
{
|
|
vpi_printf("ERROR: could not obtain the handle to systf call\n");
|
|
tf_dofinish();
|
|
return 0;
|
|
}
|
|
|
|
trarg_itr=vpi_iterate(vpiArgument, systf_h);
|
|
if(!trarg_itr)
|
|
{
|
|
vpi_printf("ERROR: could not obtain the iterate to argument\n");
|
|
tf_dofinish();
|
|
return 0;
|
|
}
|
|
|
|
// get the first argument
|
|
trarg_h=vpi_scan(trarg_itr);
|
|
if(vpi_get(vpiType,trarg_h)!=vpiConstant)
|
|
{
|
|
if(vpi_get(vpiConstType,trarg_h)!=vpiStringConst)
|
|
{
|
|
vpi_printf("the first argument type is incorrect (not a string constant)\n");
|
|
tf_dofinish();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// get the next argumnent
|
|
trarg_h=vpi_scan(trarg_itr);
|
|
trarg_type=vpi_get(vpiType,trarg_h);
|
|
if(trarg_type==vpiConstant)
|
|
// offset ?
|
|
{
|
|
if(vpi_get(vpiConstType,trarg_h)!=vpiBinaryConst)
|
|
{
|
|
vpi_printf("the offset must be dec constant [%d] \n",vpi_get(vpiConstType,trarg_h));
|
|
tf_dofinish();
|
|
return 0;
|
|
}
|
|
trarg_h=vpi_scan(trarg_itr);
|
|
trarg_type=vpi_get(vpiType,trarg_h);
|
|
}
|
|
|
|
if(trarg_type!=vpiReg)
|
|
{
|
|
vpi_printf("error:the current argument's type must be reg [%d]",trarg_type);
|
|
tf_dofinish();
|
|
return 0;
|
|
}
|
|
|
|
reg_size=vpi_get(vpiSize,trarg_h);
|
|
|
|
trarg_h=vpi_scan(trarg_itr);
|
|
if(trarg_h)
|
|
// size argument
|
|
{
|
|
s_vpi_value value_s;
|
|
trarg_type=vpi_get(vpiType,trarg_h);
|
|
if(trarg_type!=vpiConstant || vpi_get(vpiConstType,trarg_h)!=vpiBinaryConst)
|
|
{
|
|
vpi_printf("error:size type must be a binary constant");
|
|
tf_dofinish();
|
|
return 0;
|
|
}
|
|
value_s.format=vpiIntVal;
|
|
vpi_get_value(trarg_h,&value_s);
|
|
if(value_s.value.integer*8>reg_size)
|
|
{
|
|
vpi_printf("warning:size is beyond the length of the register");
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
static int read_data(char *fn)
|
static int read_data(char *fn)
|
{
|
{
|
vpiHandle systf_handle;
|
vpiHandle systf_handle;
|
vpiHandle arg_itr;
|
vpiHandle arg_itr;
|
vpiHandle arg_handle;
|
vpiHandle arg_handle;
|
|
vpiHandle arg_handle_reg;
|
|
PLI_INT32 arg_type;
|
|
PLI_INT32 arg_size;
|
|
PLI_INT32 read_size;
|
s_vpi_value value_s;
|
s_vpi_value value_s;
|
|
|
FILE *fp;
|
FILE *fp;
|
FILE *fp1;
|
|
char str[120];
|
|
int i;
|
|
int n;
|
|
|
|
systf_handle = vpi_handle(vpiSysTfCall, NULL);
|
systf_handle = vpi_handle(vpiSysTfCall, NULL);
|
arg_itr = vpi_iterate(vpiArgument, systf_handle);
|
arg_itr = vpi_iterate(vpiArgument, systf_handle);
|
if (arg_itr == NULL)
|
if (arg_itr == NULL)
|
{
|
{
|
vpi_printf("ERROR: $pow failed to obtain systf arg handles\n");
|
vpi_printf("ERROR: failed to obtain systf arg handles\n");
|
|
tf_dofinish();
|
return(0);
|
return(0);
|
}
|
}
|
|
|
/* read file name */
|
/* read file name */
|
arg_handle = vpi_scan(arg_itr);
|
arg_handle = vpi_scan(arg_itr);
|
value_s.format = vpiStringVal;
|
value_s.format = vpiStringVal;
|
vpi_get_value(arg_handle, &value_s);
|
vpi_get_value(arg_handle, &value_s);
|
strcpy(str,value_s.value.str);
|
fp=fopen(value_s.value.str,"rb");
|
|
if(!fp)
|
fp=fopen(str,"rb");
|
|
if(fp)
|
|
{
|
{
|
int i;
|
vpi_printf("ERROR: failed to open the file [%s]\n",value_s.value.str);
|
int b;
|
tf_dofinish();
|
unsigned int word;
|
return 0;
|
int vector_size;
|
}
|
//fscanf(fp,"%s",&data);
|
|
arg_handle = vpi_scan(arg_itr);
|
arg_handle = vpi_scan(arg_itr);
|
|
arg_type=vpi_get(vpiType,arg_handle);
|
|
arg_handle_reg=arg_handle;
|
|
if(arg_type==vpiConstant)
|
|
{
|
|
value_s.format = vpiIntVal;
|
|
vpi_get_value(arg_handle, &value_s);
|
|
if(0<fseek(fp,value_s.value.integer,SEEK_SET))
|
|
{
|
|
vpi_printf("warning: failed to seek the offset\n");
|
|
}
|
|
|
vector_size = vpi_get(vpiSize, arg_handle);
|
arg_handle_reg = vpi_scan(arg_itr);
|
vpi_printf("vector_size=%d \n", vector_size);
|
}
|
|
|
value_s.format = vpiVectorVal;
|
|
|
// calute the size
|
|
read_size=vpi_get(vpiSize,arg_handle_reg);
|
|
arg_handle = vpi_scan(arg_itr);
|
|
if(arg_handle)
|
|
{
|
|
value_s.format = vpiIntVal;
|
vpi_get_value(arg_handle, &value_s);
|
vpi_get_value(arg_handle, &value_s);
|
for (i=0;i<vector_size/32;i++)
|
if(value_s.value.integer<read_size*8)
|
|
read_size=value_s.value.integer*8;
|
|
}
|
|
|
|
// read the value to vector
|
|
{
|
|
unsigned int dword;
|
|
int i;
|
|
int read;
|
|
value_s.format = vpiVectorVal;
|
|
vpi_get_value(arg_handle_reg, &value_s);
|
|
i=0;
|
|
if(read_size >= 32)
|
{
|
{
|
fread(&word,4,1,fp);
|
for(i=0;i<read_size;i+=32)
|
value_s.value.vector[i].bval=0;
|
{
|
value_s.value.vector[i].aval=word;
|
read=fread(&dword,4,1,fp);
|
|
if(1!=read)
|
|
{
|
|
vpi_printf("warning read fail [%d] \n",read);
|
|
break;
|
}
|
}
|
vpi_put_value(arg_handle, &value_s,NULL, vpiNoDelay);
|
value_s.value.vector[i/32].bval=0x00;
|
fclose(fp);
|
value_s.value.vector[i/32].aval=dword;
|
}
|
}
|
else
|
}
|
|
if(i<read_size)
|
{
|
{
|
vpi_printf("can't open the input file in %s \n", __FUNCTION__);
|
PLI_INT32 reduant=read_size-i;
|
|
PLI_INT32 reduant_mask;
|
|
int n;
|
|
reduant_mask=0;
|
|
for(n=0;n<reduant;n++)
|
|
{
|
|
reduant_mask |= (1<<n);
|
}
|
}
|
|
read=fread(&dword,reduant/8,1,fp);
|
|
if(1!=read)
|
|
{
|
|
vpi_printf("warning fail \n");
|
|
}
|
|
value_s.value.vector[i/32].bval&=~reduant_mask;
|
|
value_s.value.vector[i/32].aval&=~reduant_mask;
|
|
value_s.value.vector[i/32].aval|=reduant_mask&dword;
|
|
}
|
|
vpi_put_value(arg_handle_reg, &value_s,NULL, vpiNoDelay);
|
|
}
|
|
fclose(fp);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|
|
// thia vpi funciton read_data is used to read a binary to a verilog register;
|
|
// usage: read_data ( file name, [ offset ] , register name ,[size ]);
|
|
// note offset and size argument's unit is byte
|
|
|
static void read_data_register()
|
static void read_data_register()
|
{
|
{
|
s_vpi_systf_data tf_data;
|
s_vpi_systf_data tf_data;
|
tf_data.type = vpiSysTask;
|
tf_data.type = vpiSysTask;
|
tf_data.tfname = "$read_data";
|
tf_data.tfname = "$read_data";
|
tf_data.calltf = read_data;
|
tf_data.calltf = read_data;
|
tf_data.compiletf = 0;
|
tf_data.compiletf = read_data_compile;
|
tf_data.sizetf = 0;
|
tf_data.sizetf = 0;
|
vpi_register_systf(&tf_data);
|
vpi_register_systf(&tf_data);
|
}
|
}
|
|
|
static int write_data(char *xx)
|
static int write_data(char *xx)
|
Line 133... |
Line 273... |
int i;
|
int i;
|
int vector_size;
|
int vector_size;
|
value_s.format = vpiVectorVal;
|
value_s.format = vpiVectorVal;
|
arg_handle = vpi_scan(arg_itr);
|
arg_handle = vpi_scan(arg_itr);
|
vector_size = vpi_get(vpiSize, arg_handle);
|
vector_size = vpi_get(vpiSize, arg_handle);
|
vpi_printf("str:%s vector_size=%d\n",str,vector_size);
|
|
vpi_get_value(arg_handle, &value_s);
|
vpi_get_value(arg_handle, &value_s);
|
for(i=0;i<vector_size/32;i++)
|
for(i=0;i<vector_size/32;i++)
|
{
|
{
|
vpi_printf("i=%d\n",i);
|
|
fwrite(&value_s.value.vector[i].aval,4,1,fp);
|
fwrite(&value_s.value.vector[i].aval,4,1,fp);
|
}
|
}
|
fwrite(&value_s.value.vector[i].aval,(vector_size%32)/8,1,fp);
|
fwrite(&value_s.value.vector[i].aval,(vector_size%32)/8,1,fp);
|
fclose(fp);
|
fclose(fp);
|
}
|
}
|