/*
* vpi simple test
* rill,2014-03-21
*/#include "vpi_user.h"static PLI_INT32 hello(PLI_BYTE8 * param) {vpi_printf("Hello Rill!\n");return 0;
}// Associate C Function with a New System Task
void registerHelloSystfs(void) {s_vpi_systf_data task_data_s;vpiHandle systf_handle;task_data_s.type = vpiSysTask;task_data_s.sysfunctype = vpiSysTask;task_data_s.tfname = "$hello";task_data_s.calltf = hello;task_data_s.compiletf = 0;task_data_s.sizetf = 0;task_data_s.user_data = 0;systf_handle = vpi_register_systf(&task_data_s);vpi_free_object(systf_handle);
}// Register the new system task here
void (*vlog_startup_routines[]) () = {registerHelloSystfs,0 // last entry must be 0
};
// VPI includes
#include <vpi_user.h>uint32_t vpi_pipe[2]; // [0] - read, [1] - writevoid check_for_command();
void get_command_data();
void return_command_data();//=========================================
void init_pipe(void)
{if(pipe(vpi_pipe) == -1){perror("pipe error\n");exit(1);}
}//=========================================
void check_for_command(char *userdata){vpiHandle systfref, args_iter, argh;struct t_vpi_value argval;int value,i;int n;unsigned char data;//if(DBG_JP_VPI) printf("check_for_command\n");//n = read(rsp_to_vpi_pipe[0], &data, 1);n = read(vpi_pipe[0], &data, 1);if ( ((n < 0) && (errno == EAGAIN)) || (n==0) ){// Nothing in the fifo this time, let's returnreturn;}else if (n < 0){// some sort of errorperror("check_for_command");exit(1);}if (DBG_JP_VPI){printf("jp_vpi: c = %x:",data);print_command_string(data);fflush(stdout);}// Return the command to the sim// Obtain a handle to the argument listsystfref = vpi_handle(vpiSysTfCall, NULL);// Now call iterate with the vpiArgument parameterargs_iter = vpi_iterate(vpiArgument, systfref); // get a handle on the variable passed to the functionargh = vpi_scan(args_iter);// now store the command value back in the simargval.format = vpiIntVal;// Now set the command valuevpi_get_value(argh, &argval);argval.value.integer = (uint32_t) data;// And vpi_put_value() it back into the simvpi_put_value(argh, &argval, NULL, vpiNoDelay);// Cleanup and returnvpi_free_object(args_iter);n = write(vpi_to_rsp_pipe[1],&data,1);if (DBG_JP_VPI) printf("jp_vpi: r");if (DBG_JP_VPI) printf("\n");return;
}void get_command_data(char *userdata){vpiHandle systfref, args_iter, argh;struct t_vpi_value argval;int value,i;int n = 0;uint32_t data;char* recv_buf;recv_buf = (char *) &data; // cast data as our receive char bufferread_command_data_again: n = read(vpi_pipe[0],recv_buf,4);if ((n < 4) && errno==EAGAIN)goto read_command_data_again;else if (n < 4){printf("jp_vpi: get_command_data errno: %d\n",errno);perror("jp_vpi: get_command_data read failed");}if (DBG_JP_VPI) printf("jp_vpi: get_command_data = 0x%.8x\n",data);// Obtain a handle to the argument listsystfref = vpi_handle(vpiSysTfCall, NULL);// Now call iterate with the vpiArgument parameterargs_iter = vpi_iterate(vpiArgument, systfref); // get a handle on the variable passed to the functionargh = vpi_scan(args_iter);// now store the command value back in the simargval.format = vpiIntVal;// Now set the data valuevpi_get_value(argh, &argval);argval.value.integer = (uint32_t) data;// And vpi_put_value() it back into the simvpi_put_value(argh, &argval, NULL, vpiNoDelay);// Cleanup and returnvpi_free_object(args_iter);return;}void return_command_data(char *userdata){vpiHandle systfref, args_iter, argh;struct t_vpi_value argval;int value,i;int n, length;uint32_t data;char* send_buf;// Obtain a handle to the argument listsystfref = vpi_handle(vpiSysTfCall, NULL);// Now call iterate with the vpiArgument parameterargs_iter = vpi_iterate(vpiArgument, systfref); // get a handle on the length variableargh = vpi_scan(args_iter);argval.format = vpiIntVal;// get the value for the length objectvpi_get_value(argh, &argval);// now set lengthlength = argval.value.integer;// get a handle on the object passed to the functionargh = vpi_scan(args_iter);// now store the command value back in the simargval.format = vpiIntVal;// Now set the data valuevpi_get_value(argh, &argval);data = (uint32_t) argval.value.integer;// Cleanup and returnvpi_free_object(args_iter);if (DBG_JP_VPI) printf("jp_vpi: return_command_data %d bytes, 0x%.8x\n",length,data);send_buf = (char *) &data; //cast our long as a char buf// write the data backn = write(vpi_pipe[1],send_buf,length);return;}//=========================================void register_check_for_command() {s_vpi_systf_data data = {vpiSysTask, 0, "$check_for_command", (void *)check_for_command, 0, 0, 0};vpi_register_systf(&data);return;
}void register_get_command_data() {s_vpi_systf_data data = {vpiSysTask, 0, "$get_command_data", (void *)get_command_data, 0, 0, 0};vpi_register_systf(&data);return;
}void register_return_command_data() {s_vpi_systf_data data = {vpiSysTask, 0, "$return_command_data", (void *)return_command_data, 0, 0, 0};vpi_register_systf(&data);return;
}//=========================================
void (*vlog_startup_routines[]) () = {register_check_for_command,register_get_command_data,register_return_command_data,0 // last entry must be 0
};