iverilog/vpi/sys_convert.c

185 lines
4.7 KiB
C
Raw Normal View History

2003-03-07 03:44:33 +01:00
/*
* Copyright (c) 2003 Michael Ruff (mruff at chiaro.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
2003-03-11 00:40:10 +01:00
#ident "$Id: sys_convert.c,v 1.2 2003/03/10 23:40:10 steve Exp $"
2003-03-07 03:44:33 +01:00
#endif
# include "vpi_user.h"
# include "config.h"
# include <stdio.h>
# include <math.h>
static void double2bits(double real, PLI_UINT32 bits[2])
{
union conv {
double rval;
unsigned char bval[sizeof(double)];
} conv;
conv.rval = real;
#ifdef WORDS_BIGENDIAN
bits[0] = conv.bval[7]
| (conv.bval[6] << 8)
| (conv.bval[5] <<16)
| (conv.bval[4] <<24);
bits[1] = conv.bval[3]
| (conv.bval[2] << 8)
| (conv.bval[1] <<16)
| (conv.bval[0] <<24);
#else
bits[0] = conv.bval[0]
| (conv.bval[1] << 8)
| (conv.bval[2] <<16)
| (conv.bval[3] <<24);
bits[1] = conv.bval[4]
| (conv.bval[5] << 8)
| (conv.bval[6] <<16)
| (conv.bval[7] <<24);
#endif
}
2003-03-11 00:40:10 +01:00
static int sizetf_32 (char*x) { return 32; }
2003-03-07 03:44:33 +01:00
static int sizetf_64 (char*x) { return 64; }
2003-03-11 00:40:10 +01:00
static int sys_convert_compiletf(char *name)
2003-03-07 03:44:33 +01:00
{
vpiHandle call_hand, argv;
call_hand = vpi_handle(vpiSysTfCall, 0);
argv = vpi_iterate(vpiArgument, call_hand);
if (!argv) {
vpi_printf("ERROR: %s requires a parameter.\n",
vpi_get_str(vpiName, call_hand));
return -1;
}
return 0;
}
2003-03-11 00:40:10 +01:00
static int sys_realtobits_calltf(char *user)
2003-03-07 03:44:33 +01:00
{
vpiHandle sys, argv, arg;
s_vpi_value value;
static struct t_vpi_vecval res[2];
PLI_UINT32 bits[2];
/* find argument handle */
sys = vpi_handle(vpiSysTfCall, 0);
argv = vpi_iterate(vpiArgument, sys);
arg = vpi_scan(argv);
2003-03-11 00:40:10 +01:00
/* get value */
2003-03-07 03:44:33 +01:00
value.format = vpiRealVal;
vpi_get_value(arg, &value);
2003-03-11 00:40:10 +01:00
/* convert */
2003-03-07 03:44:33 +01:00
double2bits(value.value.real, bits);
res[0].aval = bits[0];
res[0].bval = 0;
res[1].aval = bits[1];
res[1].bval = 0;
value.format = vpiVectorVal;
value.value.vector = res;
/* return converted value */
vpi_put_value(sys, &value, 0, vpiNoDelay);
return 0;
}
2003-03-11 00:40:10 +01:00
static int sys_rtoi_calltf(char *user)
{
vpiHandle sys, argv, arg;
s_vpi_value value;
static struct t_vpi_vecval res;
/* find argument handle */
sys = vpi_handle(vpiSysTfCall, 0);
argv = vpi_iterate(vpiArgument, sys);
arg = vpi_scan(argv);
/* get value */
value.format = vpiRealVal;
vpi_get_value(arg, &value);
/* convert */
res.aval = (unsigned)value.value.real;
res.bval = 0;
value.format = vpiVectorVal;
value.value.vector = &res;
/* return converted value */
vpi_put_value(sys, &value, 0, vpiNoDelay);
return 0;
}
2003-03-07 03:44:33 +01:00
void sys_convert_register()
{
s_vpi_systf_data tf_data;
2003-03-11 00:40:10 +01:00
2003-03-07 03:44:33 +01:00
tf_data.type = vpiSysFunc;
tf_data.tfname = "$bitstoreal";
tf_data.sizetf = sizetf_64;
2003-03-11 00:40:10 +01:00
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = NULL; // sys_bitstoreal_calltf;
tf_data.user_data = NULL;
2003-03-07 03:44:33 +01:00
vpi_register_systf(&tf_data);
tf_data.type = vpiSysFunc;
tf_data.tfname = "$itor";
tf_data.sizetf = sizetf_64;
2003-03-11 00:40:10 +01:00
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = NULL; // sys_itor_calltf;
tf_data.user_data = NULL;
2003-03-07 03:44:33 +01:00
vpi_register_systf(&tf_data);
tf_data.type = vpiSysFunc;
tf_data.tfname = "$realtobits";
tf_data.sizetf = sizetf_64;
2003-03-11 00:40:10 +01:00
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = sys_realtobits_calltf;
tf_data.user_data = NULL;
2003-03-07 03:44:33 +01:00
vpi_register_systf(&tf_data);
tf_data.type = vpiSysFunc;
tf_data.tfname = "$rtoi";
tf_data.sizetf = sizetf_32;
2003-03-11 00:40:10 +01:00
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = sys_rtoi_calltf;
tf_data.user_data = NULL;
2003-03-07 03:44:33 +01:00
vpi_register_systf(&tf_data);
}
/*
* $Log: sys_convert.c,v $
2003-03-11 00:40:10 +01:00
* Revision 1.2 2003/03/10 23:40:10 steve
* Add support for $rtoi
*
2003-03-07 03:44:33 +01:00
* Revision 1.1 2003/03/07 02:44:34 steve
* Implement $realtobits.
*
*/