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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
/*
* Copyright (c) 2012-2021 Stephen Williams (steve@icarus.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 <assert.h>
# include <ctype.h>
# include <math.h>
# include <stdlib.h>
# include <string.h>
static PLI_INT32 one_arg_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle arg;
argv = vpi_iterate(vpiArgument, callh);
if (argv == 0) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s requires a single argument.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
arg = vpi_scan(argv);
if (arg == 0) return 0;
arg = vpi_scan(argv);
if (arg != 0) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s has too many arguments.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
return 0;
}
static PLI_INT32 two_arg_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle arg;
argv = vpi_iterate(vpiArgument, callh);
if (argv == 0) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s missing arguments.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
arg = vpi_scan(argv);
if (argv == 0) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s missing object argument.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
arg = vpi_scan(argv);
if (argv == 0) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s missing method argument.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
arg = vpi_scan(argv);
if (arg != 0) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s has too many arguments.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
return 0;
}
static PLI_INT32 len_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle arg;
(void)name; /* Parameter is not used. */
argv = vpi_iterate(vpiArgument, callh);
assert(argv);
arg = vpi_scan(argv);
assert(arg);
vpi_free_object(argv);
int res = vpi_get(vpiSize, arg);
s_vpi_value value;
value.format = vpiIntVal;
value.value.integer = res;
vpi_put_value(callh, &value, 0, vpiNoDelay);
return 0;
}
static PLI_INT32 atoi_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle arg;
s_vpi_value value;
(void)name; /* Parameter not used */
argv = vpi_iterate(vpiArgument, callh);
assert(argv);
arg = vpi_scan(argv);
assert(arg);
vpi_free_object(argv);
int res = 0;
value.format = vpiStringVal;
vpi_get_value(arg, &value);
const char*bufp = value.value.str;
while (bufp && *bufp) {
if (isdigit(*bufp)) {
res = (res * 10) + (*bufp - '0');
bufp += 1;
} else if (*bufp == '_') {
bufp += 1;
} else {
bufp = 0;
}
}
value.format = vpiIntVal;
value.value.integer = res;
vpi_put_value(callh, &value, 0, vpiNoDelay);
return 0;
}
static PLI_INT32 atoreal_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle arg;
s_vpi_value value;
(void)name; /* Parameter not used */
argv = vpi_iterate(vpiArgument, callh);
assert(argv);
arg = vpi_scan(argv);
assert(arg);
vpi_free_object(argv);
double res = 0;
value.format = vpiStringVal;
vpi_get_value(arg, &value);
res = strtod(value.value.str, 0);
value.format = vpiRealVal;
value.value.real = res;
vpi_put_value(callh, &value, 0, vpiNoDelay);
return 0;
}
static PLI_INT32 atohex_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle arg;
s_vpi_value value;
(void)name; /* Parameter not used */
argv = vpi_iterate(vpiArgument, callh);
assert(argv);
arg = vpi_scan(argv);
assert(arg);
vpi_free_object(argv);
int res = 0;
value.format = vpiStringVal;
vpi_get_value(arg, &value);
const char*bufp = value.value.str;
while (bufp && *bufp) {
switch (*bufp) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
res = (res * 16) + (*bufp - '0');
bufp += 1;
break;
case 'a': case 'A':
case 'b': case 'B':
case 'c': case 'C':
case 'd': case 'D':
case 'e': case 'E':
case 'f': case 'F':
res = (res * 16) + (toupper(*bufp) - 'A' + 10);
bufp += 1;
break;
case '_':
bufp += 1;
break;
default:
bufp = 0;
break;
}
}
value.format = vpiIntVal;
value.value.integer = res;
vpi_put_value(callh, &value, 0, vpiNoDelay);
return 0;
}
/*
* Convert a val to a text string that represents the value. The base
* is the integer base to use for the conversion. The base will be one
* of the values 2, 8, 10, or 16. If the input value is negative, then
* extract the sign out (and put a '-' in the beginning of the string and
* write the digits for the abs(val). For example, if the input value
* is -11 and base is 8, then the text output will be "-13".
*/
static void value_to_text_base(char*text, long val, long base)
{
static const char digit_map[16] = "0123456789abcdef";
char* ptr;
assert(base <= 16);
if (val == 0) {
text[0] = '0';
text[1] = 0;
return;
}
if (val < 0) {
*text++ = '-';
val = 0-val;
}
ptr = text;
while (val != 0) {
long digit = val % base;
val /= base;
*(ptr++) = digit_map[digit];
*ptr = 0;
}
/* Now the text string is valid, but digits are reversed. */
ptr -= 1;
while (text < ptr) {
char tmp = *ptr;
*ptr = *text;
*text = tmp;
ptr -= 1;
text += 1;
}
}
/*
* This function implements the <string>.itoa(arg) method.
* The first argument to this task is the <string> object, and the second
* is the value to interpret.
*/
static PLI_INT32 xtoa_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle obj, arg;
s_vpi_value value;
char text_buf[64];
long use_base;
use_base = strtoul(name, 0, 10);
argv = vpi_iterate(vpiArgument, callh);
assert(argv);
obj = vpi_scan(argv);
assert(obj);
arg = vpi_scan(argv);
assert(arg);
vpi_free_object(argv);
value.format = vpiIntVal;
vpi_get_value(arg, &value);
value_to_text_base(text_buf, value.value.integer, use_base);
value.format = vpiStringVal;
value.value.str = text_buf;
vpi_put_value(obj, &value, 0, vpiNoDelay);
return 0;
}
static PLI_INT32 realtoa_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv;
vpiHandle obj, arg;
s_vpi_value value;
char text_buf[64];
(void) name;
argv = vpi_iterate(vpiArgument, callh);
assert(argv);
obj = vpi_scan(argv);
assert(obj);
arg = vpi_scan(argv);
assert(arg);
vpi_free_object(argv);
value.format = vpiRealVal;
vpi_get_value(arg, &value);
snprintf(text_buf, sizeof text_buf, "%g", value.value.real);
value.format = vpiStringVal;
value.value.str = text_buf;
vpi_put_value(obj, &value, 0, vpiNoDelay);
return 0;
}
void v2009_string_register(void)
{
s_vpi_systf_data tf_data;
vpiHandle res;
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiIntFunc;
tf_data.tfname = "$ivl_string_method$len";
tf_data.calltf = len_calltf;
tf_data.compiletf = one_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "$ivl_string_method$len";
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiIntFunc;
tf_data.tfname = "$ivl_string_method$atoi";
tf_data.calltf = atoi_calltf;
tf_data.compiletf = one_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "$ivl_string_method$atoi";
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiRealFunc;
tf_data.tfname = "$ivl_string_method$atoreal";
tf_data.calltf = atoreal_calltf;
tf_data.compiletf = one_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "$ivl_string_method$atoreal";
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiIntFunc;
tf_data.tfname = "$ivl_string_method$atohex";
tf_data.calltf = atohex_calltf;
tf_data.compiletf = one_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "$ivl_string_method$atohex";
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysTask;
tf_data.tfname = "$ivl_string_method$itoa";
tf_data.calltf = xtoa_calltf;
tf_data.compiletf = two_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "10";
res = vpi_register_systf(&tf_data);
tf_data.type = vpiSysTask;
tf_data.tfname = "$ivl_string_method$hextoa";
tf_data.calltf = xtoa_calltf;
tf_data.compiletf = two_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "16";
res = vpi_register_systf(&tf_data);
tf_data.type = vpiSysTask;
tf_data.tfname = "$ivl_string_method$octtoa";
tf_data.calltf = xtoa_calltf;
tf_data.compiletf = two_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "8";
res = vpi_register_systf(&tf_data);
tf_data.type = vpiSysTask;
tf_data.tfname = "$ivl_string_method$bintoa";
tf_data.calltf = xtoa_calltf;
tf_data.compiletf = two_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "2";
res = vpi_register_systf(&tf_data);
tf_data.type = vpiSysTask;
tf_data.tfname = "$ivl_string_method$realtoa";
tf_data.calltf = realtoa_calltf;
tf_data.compiletf = two_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "$ivl_string_method$realtoa";
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
}
|