1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/*
* Copyright (c) 2003-2021 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
# include "sys_priv.h"
# include <stdio.h>
# include <string.h>
# include <math.h>
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 {
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
}
static void error_message(vpiHandle callh, const char* msg)
{
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf(msg, vpi_get_str(vpiName, callh));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
static PLI_INT32 sizetf_64 (ICARUS_VPI_CONST PLI_BYTE8*name)
{
(void)name; /* Parameter is not used. */
return 64;
}
static PLI_INT32 sys_convert_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, callh);
vpiHandle arg;
/* Check that there is an argument. */
if (argv == 0) {
error_message(callh, "%s requires one argument.\n");
return 0;
}
/* In Icarus if we have an argv we have at least one argument. */
arg = vpi_scan(argv);
/* Validate the argument. Only $bitstoreal for now. */
if (!strcmp("$bitstoreal", name) && vpi_get(vpiSize, arg) != 64) {
error_message(callh, "%s requires a 64-bit argument.\n");
return 0;
}
/* Save the argument away to make the calltf faster. */
vpi_put_userdata(callh, (void *) arg);
/* These functions only take one argument. */
check_for_extra_args(argv, callh, name, "one argument", 0);
return 0;
}
static PLI_INT32 sys_bitstoreal_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle arg = (vpiHandle) vpi_get_userdata(callh);
s_vpi_value value;
PLI_UINT32 bits[2];
(void)name; /* Parameter is not used. */
/* 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(callh, &value, 0, vpiNoDelay);
return 0;
}
static PLI_INT32 sys_itor_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle arg = (vpiHandle) vpi_get_userdata(callh);
s_vpi_value value;
(void)name; /* Parameter is not used. */
/* get value */
value.format = vpiIntVal;
vpi_get_value(arg, &value);
/* convert */
value.value.real = value.value.integer;
value.format = vpiRealVal;
/* return converted value */
vpi_put_value(callh, &value, 0, vpiNoDelay);
return 0;
}
static PLI_INT32 sys_realtobits_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle arg = (vpiHandle) vpi_get_userdata(callh);
s_vpi_value value;
static struct t_vpi_vecval res[2];
PLI_UINT32 bits[2];
(void)name; /* Parameter is not used. */
/* get value */
value.format = vpiRealVal;
vpi_get_value(arg, &value);
/* convert */
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(callh, &value, 0, vpiNoDelay);
return 0;
}
static PLI_INT32 sys_rtoi_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle arg = (vpiHandle) vpi_get_userdata(callh);
s_vpi_value value;
static struct t_vpi_vecval res;
double val;
(void)name; /* Parameter is not used. */
/* get value */
value.format = vpiRealVal;
vpi_get_value(arg, &value);
/* If the value is NaN or +/- infinity then return 'bx */
val = value.value.real;
if (val != val || (val && (val == 0.5*val))) {
res.aval = ~(PLI_INT32)0;
res.bval = ~(PLI_INT32)0;
} else {
/* This is not 100% correct since large real values may break this
* code. See the verinum code for a more rigorous implementation. */
if (val >= 0.0) res.aval = (PLI_UINT64) val;
else res.aval = - (PLI_UINT64) -val;
res.bval = 0;
}
value.format = vpiVectorVal;
value.value.vector = &res;
/* return converted value */
vpi_put_value(callh, &value, 0, vpiNoDelay);
return 0;
}
void sys_convert_register(void)
{
s_vpi_systf_data tf_data;
vpiHandle res;
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiRealFunc;
tf_data.user_data = "$bitstoreal";
tf_data.tfname = tf_data.user_data;
tf_data.sizetf = 0;
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = sys_bitstoreal_calltf;
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiRealFunc;
tf_data.user_data = "$itor";
tf_data.tfname = tf_data.user_data;
tf_data.sizetf = 0;
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = sys_itor_calltf;
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiSizedFunc;
tf_data.user_data = "$realtobits";
tf_data.tfname = tf_data.user_data;
tf_data.sizetf = sizetf_64;
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = sys_realtobits_calltf;
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiIntFunc;
tf_data.user_data = "$rtoi";
tf_data.tfname = tf_data.user_data;
tf_data.sizetf = 0;
tf_data.compiletf = sys_convert_compiletf;
tf_data.calltf = sys_rtoi_calltf;
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
}
|