diff --git a/vpi/sys_convert.c b/vpi/sys_convert.c index 7479b7414..096630e0d 100644 --- a/vpi/sys_convert.c +++ b/vpi/sys_convert.c @@ -17,14 +17,45 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #ifdef HAVE_CVS_IDENT -#ident "$Id: sys_convert.c,v 1.2 2003/03/10 23:40:10 steve Exp $" +#ident "$Id: sys_convert.c,v 1.3 2003/03/17 21:59:54 steve Exp $" #endif # include "vpi_user.h" # include "config.h" # include +# include # include +static double bits2double(PLI_UINT32 bits[2]) +{ + union conv { + double rval; + unsigned char bval[sizeof(double)]; + } conv; + +#ifdef WORDS_BIGENDIAN + conv.bval[7] = (bits[0] >> 0) & 0xff; + conv.bval[6] = (bits[0] >> 8) & 0xff; + conv.bval[5] = (bits[0] >> 16) & 0xff; + conv.bval[4] = (bits[0] >> 24) & 0xff; + conv.bval[3] = (bits[1] >> 0) & 0xff; + conv.bval[2] = (bits[1] >> 8) & 0xff; + conv.bval[1] = (bits[1] >> 16) & 0xff; + conv.bval[0] = (bits[1] >> 24) & 0xff; +#else + conv.bval[0] = (bits[0] >> 0) & 0xff; + conv.bval[1] = (bits[0] >> 8) & 0xff; + conv.bval[2] = (bits[0] >> 16) & 0xff; + conv.bval[3] = (bits[0] >> 24) & 0xff; + conv.bval[4] = (bits[1] >> 0) & 0xff; + conv.bval[5] = (bits[1] >> 8) & 0xff; + conv.bval[6] = (bits[1] >> 16) & 0xff; + conv.bval[7] = (bits[1] >> 24) & 0xff; +#endif + + return conv.rval; +} + static void double2bits(double real, PLI_UINT32 bits[2]) { union conv { @@ -60,17 +91,86 @@ static int sizetf_64 (char*x) { return 64; } static int sys_convert_compiletf(char *name) { - vpiHandle call_hand, argv; + vpiHandle call_hand, argv, arg; + int rtn = 0; call_hand = vpi_handle(vpiSysTfCall, 0); argv = vpi_iterate(vpiArgument, call_hand); + arg = vpi_scan(argv); if (!argv) { vpi_printf("ERROR: %s requires a parameter.\n", vpi_get_str(vpiName, call_hand)); - return -1; + rtn = -1; } + if (!strcmp("$bitstoreal", name) && vpi_get(vpiSize, arg) != 64) { + vpi_printf("ERROR: %s requires 64-bit argument.\n", + vpi_get_str(vpiName, call_hand)); + rtn = -1; + } + + /* free iterator */ + vpi_free_object(argv); + + return rtn; +} + +static int sys_bitstoreal_calltf(char *user) +{ + vpiHandle sys, argv, arg; + s_vpi_value value; + + PLI_UINT32 bits[2]; + + /* find argument handle */ + sys = vpi_handle(vpiSysTfCall, 0); + argv = vpi_iterate(vpiArgument, sys); + arg = vpi_scan(argv); + + /* get value */ + value.format = vpiVectorVal; + vpi_get_value(arg, &value); + + /* convert */ + bits[0] = (value.value.vector[0]).aval; + bits[1] = (value.value.vector[1]).aval; + value.value.real = bits2double(bits); + value.format = vpiRealVal; + + /* return converted value */ + vpi_put_value(sys, &value, 0, vpiNoDelay); + + /* free iterator */ + vpi_free_object(argv); + + return 0; +} + +static int sys_itor_calltf(char *user) +{ + vpiHandle sys, argv, arg; + s_vpi_value value; + + /* find argument handle */ + sys = vpi_handle(vpiSysTfCall, 0); + argv = vpi_iterate(vpiArgument, sys); + arg = vpi_scan(argv); + + /* get value */ + value.format = vpiIntVal; + vpi_get_value(arg, &value); + + /* convert */ + value.value.real = (PLI_INT32)value.value.integer; + value.format = vpiRealVal; + + /* return converted value */ + vpi_put_value(sys, &value, 0, vpiNoDelay); + + /* free iterator */ + vpi_free_object(argv); + return 0; } @@ -105,6 +205,9 @@ static int sys_realtobits_calltf(char *user) /* return converted value */ vpi_put_value(sys, &value, 0, vpiNoDelay); + /* free iterator */ + vpi_free_object(argv); + return 0; } @@ -133,6 +236,9 @@ static int sys_rtoi_calltf(char *user) /* return converted value */ vpi_put_value(sys, &value, 0, vpiNoDelay); + /* free iterator */ + vpi_free_object(argv); + return 0; } @@ -144,16 +250,16 @@ void sys_convert_register() tf_data.tfname = "$bitstoreal"; tf_data.sizetf = sizetf_64; tf_data.compiletf = sys_convert_compiletf; - tf_data.calltf = NULL; // sys_bitstoreal_calltf; - tf_data.user_data = NULL; + tf_data.calltf = sys_bitstoreal_calltf; + tf_data.user_data = tf_data.tfname; vpi_register_systf(&tf_data); tf_data.type = vpiSysFunc; tf_data.tfname = "$itor"; tf_data.sizetf = sizetf_64; tf_data.compiletf = sys_convert_compiletf; - tf_data.calltf = NULL; // sys_itor_calltf; - tf_data.user_data = NULL; + tf_data.calltf = sys_itor_calltf; + tf_data.user_data = tf_data.tfname; vpi_register_systf(&tf_data); tf_data.type = vpiSysFunc; @@ -161,7 +267,7 @@ void sys_convert_register() tf_data.sizetf = sizetf_64; tf_data.compiletf = sys_convert_compiletf; tf_data.calltf = sys_realtobits_calltf; - tf_data.user_data = NULL; + tf_data.user_data = tf_data.tfname; vpi_register_systf(&tf_data); tf_data.type = vpiSysFunc; @@ -169,12 +275,15 @@ void sys_convert_register() tf_data.sizetf = sizetf_32; tf_data.compiletf = sys_convert_compiletf; tf_data.calltf = sys_rtoi_calltf; - tf_data.user_data = NULL; + tf_data.user_data = tf_data.tfname; vpi_register_systf(&tf_data); } /* * $Log: sys_convert.c,v $ + * Revision 1.3 2003/03/17 21:59:54 steve + * Implement $itor and $bitstoreal + * * Revision 1.2 2003/03/10 23:40:10 steve * Add support for $rtoi *