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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/*
* Copyright (c) 2010-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 "vpi_config.h"
# include "sv_vpi_user.h"
# include <stdlib.h>
# include <string.h>
# include <assert.h>
/*
* The compiletf routine for the enumeration next() and prev() methods.
*/
static PLI_INT32 ivl_enum_method_next_prev_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle arg_enum, arg_var, arg_count;
/* These routines must have arguments. */
if (argv == 0) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("No arguments given for enum method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
/* Check for the enumeration type argument. */
arg_enum = vpi_scan(argv);
if (arg_enum == 0) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("No enumeration type argument given for enum "
"method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
if (vpi_get(vpiType, arg_enum) != vpiEnumTypespec) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("The first argument to enum method %s() must be an "
"enumeration type, found a %s.\n", name,
vpi_get_str(vpiType, arg_enum));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
/* Check for the enumeration variable. */
arg_var = vpi_scan(argv);
if (arg_var == 0) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("No enumeration variable argument given for enum "
"method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
switch(vpi_get(vpiType, arg_var)) {
case vpiBitVar:
case vpiByteVar:
case vpiIntegerVar:
case vpiIntVar:
case vpiLongIntVar:
case vpiReg:
case vpiShortIntVar:
break;
default:
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("The second argument to enum method %s() must be an "
"enumeration variable, found a %s.\n", name,
vpi_get_str(vpiType, arg_var));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
/* We can have an optional numeric count value. */
arg_count = vpi_scan(argv);
if (arg_count) {
switch(vpi_get(vpiType, arg_count)) {
case vpiBitVar:
case vpiByteVar:
case vpiIntegerVar:
case vpiIntVar:
case vpiLongIntVar:
case vpiMemoryWord:
case vpiNet:
case vpiPartSelect:
case vpiRealVar:
case vpiReg:
case vpiShortIntVar:
case vpiTimeVar:
break;
case vpiConstant:
case vpiParameter:
if (vpi_get(vpiConstType, arg_count) != vpiStringConst) break;
// fallthrough
default:
vpi_printf("%s:%d: compiler error: ",
vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("The second argument to enum method %s() must be "
"numeric, found a %s.\n", name,
vpi_get_str(vpiType, arg_count));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
}
/* If we have a count argument then check to see if any extra
* arguments were given. */
if (arg_count && (vpi_scan(argv) != 0)) {
vpi_free_object(argv);
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("Extra argument(s) given to enum method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
/* The method return value and the enum variable must be the
* same size */
if (vpi_get(vpiSize, sys) != vpi_get(vpiSize, arg_var)) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("The enum method %s() return width (%d) must match "
"the enum variable width (%d).\n", name,
(int) vpi_get(vpiSize, sys),
(int) vpi_get(vpiSize, arg_var));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
return 0;
}
/*
* Compare it two values are equal to each other.
*/
static int compare_value_eequal(s_vpi_value*ref1, s_vpi_value*ref2,
PLI_INT32 wid)
{
/* Two integer values are easy. */
if (ref1->format == vpiIntVal && ref2->format == vpiIntVal)
return ref1->value.integer == ref2->value.integer;
/* For two vectors compare them word by word. */
if (ref1->format == vpiVectorVal && ref2->format == vpiVectorVal) {
PLI_INT32 words = (wid-1)/32 + 1;
PLI_INT32 idx;
for (idx = 0 ; idx < words ; idx += 1) {
if (ref1->value.vector[idx].aval !=
ref2->value.vector[idx].aval) return 0;
if (ref1->value.vector[idx].bval !=
ref2->value.vector[idx].bval) return 0;
}
return 1;
}
/* Swap the order so the code below can be used. */
if (ref1->format == vpiVectorVal && ref2->format == vpiIntVal) {
s_vpi_value*tmp = ref1;
ref1 = ref2;
ref2 = tmp;
}
/* Compare an integer to a vector. */
if (ref1->format == vpiIntVal && ref2->format == vpiVectorVal) {
PLI_INT32 aval = ref1->value.integer;
PLI_INT32 words = (wid-1)/32 + 1;
PLI_INT32 idx;
for (idx = 0 ; idx < words ; idx += 1) {
if (aval != ref2->value.vector[idx].aval) return 0;
if (ref2->value.vector[idx].bval) return 0;
aval = 0;
}
return 1;
}
/* Unsupported types. */
vpi_printf("XXXX formats are: %d vs %d\n", ref1->format, ref2->format);
assert(0);
return 0;
}
/*
* If the current value is not found in the enumeration. Then we need to
* return X/0 for the next()/prev() value.
*/
static void fill_handle_with_init(vpiHandle arg, int is_two_state)
{
s_vpi_value val;
PLI_INT32 words = ((vpi_get(vpiSize, arg) - 1) / 32) + 1;
PLI_INT32 idx;
p_vpi_vecval val_ptr = (p_vpi_vecval) malloc(words*sizeof(s_vpi_vecval));
PLI_INT32 fill = (is_two_state) ? 0x0 : 0xffffffff;
assert(val_ptr);
/* Fill the vector with X. */
for (idx=0; idx < words; idx += 1) {
val_ptr[idx].aval = fill;
val_ptr[idx].bval = fill;
}
/* Put the vector to the argument. */
val.format = vpiVectorVal;
val.value.vector = val_ptr;
vpi_put_value(arg, &val, 0, vpiNoDelay);
free(val_ptr);
}
/*
* Implement the next()/prev() enumeration methods.
*/
static PLI_INT32 ivl_enum_method_next_prev_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle arg_enum = vpi_scan(argv);
vpiHandle arg_var = vpi_scan(argv);
vpiHandle arg_count = vpi_scan(argv);
vpiHandle enum_list;
vpiHandle cur;
PLI_INT32 var_width = vpi_get(vpiSize, arg_var);
PLI_UINT32 enum_size = vpi_get(vpiSize, arg_enum);
PLI_UINT32 count = 1;
PLI_UINT32 loc = 0;
s_vpi_value cur_val, var_val;
/* Get the count value if one was given. */
if (arg_count) {
s_vpi_value count_val;
/* Get the count value. */
count_val.format = vpiIntVal;
vpi_get_value(arg_count, &count_val);
count = count_val.value.integer;
/* Remove any multiple loops around the enumeration. */
count %= enum_size;
/* Free the argument iterator. */
vpi_free_object(argv);
}
/* Get the current value. */
var_val.format = vpiObjTypeVal;
vpi_get_value(arg_var, &var_val);
/* If the count is zero then just return the current value. */
if (count == 0) {
vpi_put_value(sys, &var_val, 0, vpiNoDelay);
return 0;
}
/* If the current value is a vector, then make a safe copy of
it so that other vpi_get_value() calls don't trash the value. */
if (var_val.format == vpiVectorVal) {
PLI_INT32 idx;
PLI_INT32 words = (var_width - 1)/32 + 1;
p_vpi_vecval nvec = malloc(words*sizeof(s_vpi_vecval));
for (idx = 0 ; idx < words ; idx += 1) {
nvec[idx].aval = var_val.value.vector[idx].aval;
nvec[idx].bval = var_val.value.vector[idx].bval;
}
var_val.value.vector = nvec;
}
/* Search for the current value in the enumeration list. */
enum_list = vpi_iterate(vpiEnumConst, arg_enum);
assert(enum_list);
do {
cur = vpi_scan(enum_list);
if (cur == 0) break;
cur_val.format = vpiObjTypeVal;
vpi_get_value(cur, &cur_val);
assert(var_width == vpi_get(vpiSize, cur));
loc += 1;
} while (! compare_value_eequal(&cur_val, &var_val, var_width));
/* If the variable is a vector then free the copy we created above. */
if (var_val.format == vpiVectorVal) free(var_val.value.vector);
/* The current value was not found in the list so return X/0. */
if (cur == 0) {
/* This only works correctly since we don't really define the
* the correct base typespec. */
int is_two_state = vpi_get(vpiBaseTypespec, arg_enum) != vpiReg;
fill_handle_with_init(sys, is_two_state);
} else {
if (strcmp(name, "$ivl_enum_method$next") == 0) {
/* Check to see if we need to wrap to the beginning. */
if (loc + count > enum_size) {
/* Free the current iterator before creating a new
* one that is at the beginning of the list. */
vpi_free_object(enum_list);
enum_list = vpi_iterate(vpiEnumConst, arg_enum);
assert(enum_list);
count -= (enum_size - loc);
}
} else if (strcmp(name, "$ivl_enum_method$prev") == 0) {
/* Because of wrapping the count could be either before
* or after the current element. */
if (loc <= count ) {
/* The element we want is after the current
* element. */
count = enum_size - count;
} else {
/* The element we want is before the current
* element (at the beginning of the list). Free
* the current iterator before creating a new
* one that is at the beginning of the list. */
vpi_free_object(enum_list);
enum_list = vpi_iterate(vpiEnumConst, arg_enum);
assert(enum_list);
count = loc - count;
}
} else assert(0);
/* Find the correct element. */
while (count) {
cur = vpi_scan(enum_list);
count -= 1;
}
assert(cur != 0);
/* Free the iterator. */
vpi_free_object(enum_list);
/* Get the value and return it. */
cur_val.format = vpiObjTypeVal;
vpi_get_value(cur, &cur_val);
vpi_put_value(sys, &cur_val, 0, vpiNoDelay);
}
return 0;
}
/*
* The compiletf routine for the enumeration name() method.
*/
static PLI_INT32 ivl_enum_method_name_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle arg_enum, arg_var;
/* This routine must have arguments. */
if (argv == 0) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("No arguments given for enum method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
/* Check for the enumeration type argument. */
arg_enum = vpi_scan(argv);
if (arg_enum == 0) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("No enumeration type argument given for enum "
"method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
if (vpi_get(vpiType, arg_enum) != vpiEnumTypespec) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("The first argument to enum method %s() must be an "
"enumeration type, found a %s.\n", name,
vpi_get_str(vpiType, arg_enum));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
/* Check for the enumeration variable. */
arg_var = vpi_scan(argv);
if (arg_var == 0) {
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("No enumeration variable argument given for enum "
"method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
switch(vpi_get(vpiType, arg_var)) {
case vpiBitVar:
case vpiByteVar:
case vpiIntegerVar:
case vpiIntVar:
case vpiLongIntVar:
case vpiReg:
case vpiShortIntVar:
break;
default:
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("The second argument to enum method %s() must be an "
"enumeration variable, found a %s.\n", name,
vpi_get_str(vpiType, arg_var));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
/* Only two arguments are supported. */
if (vpi_scan(argv) != 0) {
vpi_free_object(argv);
vpi_printf("%s:%d: compiler error: ", vpi_get_str(vpiFile, sys),
(int) vpi_get(vpiLineNo,sys));
vpi_printf("Extra argument(s) given to enum method %s().\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
return 0;
}
/*
* Implement the name() enumeration method.
*/
static PLI_INT32 ivl_enum_method_name_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle arg_enum = vpi_scan(argv);
vpiHandle arg_var = vpi_scan(argv);
vpiHandle enum_list;
vpiHandle cur;
PLI_INT32 var_width = vpi_get(vpiSize, arg_var);
s_vpi_value cur_val, var_val;
(void)name; /* Parameter is not used. */
/* Free the argument iterator. */
vpi_free_object(argv);
/* Get the current value. */
var_val.format = vpiObjTypeVal;
vpi_get_value(arg_var, &var_val);
/* If the current value is a vector, then make a safe copy of
it so that other vpi_get_value() calls don't trash the value. */
if (var_val.format == vpiVectorVal) {
PLI_INT32 idx;
PLI_INT32 words = (var_width - 1)/32 + 1;
p_vpi_vecval nvec = malloc(words*sizeof(s_vpi_vecval));
for (idx = 0 ; idx < words ; idx += 1) {
nvec[idx].aval = var_val.value.vector[idx].aval;
nvec[idx].bval = var_val.value.vector[idx].bval;
}
var_val.value.vector = nvec;
}
/* Search for the current value in the enumeration list. */
enum_list = vpi_iterate(vpiEnumConst, arg_enum);
assert(enum_list);
do {
cur = vpi_scan(enum_list);
if (cur == 0) break;
cur_val.format = vpiObjTypeVal;
vpi_get_value(cur, &cur_val);
assert(var_width == vpi_get(vpiSize, cur));
} while (! compare_value_eequal(&cur_val, &var_val, var_width));
/* If the variable is a vector then free the copy we created above. */
if (var_val.format == vpiVectorVal) free(var_val.value.vector);
/* The current value was not found in the list so return an empty
* string. */
cur_val.format = vpiStringVal;
if (cur == 0) {
cur_val.value.str = "";
} else {
cur_val.value.str = vpi_get_str(vpiName, cur);
/* Free the iterator. */
vpi_free_object(enum_list);
}
/* Return the appropriate string value. */
vpi_put_value(sys, &cur_val, 0, vpiNoDelay);
return 0;
}
void v2009_enum_register(void)
{
s_vpi_systf_data tf_data;
vpiHandle res;
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiOtherFunc;
tf_data.calltf = ivl_enum_method_name_calltf;
tf_data.compiletf = ivl_enum_method_name_compiletf;
tf_data.sizetf = 0;
tf_data.tfname = "$ivl_enum_method$name";
tf_data.user_data = "$ivl_enum_method$name";
res = vpi_register_systf(&tf_data);
/* This is not a user defined system function so hide it. */
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiOtherFunc;
tf_data.calltf = ivl_enum_method_next_prev_calltf;
tf_data.compiletf = ivl_enum_method_next_prev_compiletf;
tf_data.sizetf = 0;
tf_data.tfname = "$ivl_enum_method$next";
tf_data.user_data = "$ivl_enum_method$next";
res = vpi_register_systf(&tf_data);
/* This is not a user defined system function so hide it. */
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiOtherFunc;
tf_data.calltf = ivl_enum_method_next_prev_calltf;
tf_data.compiletf = ivl_enum_method_next_prev_compiletf;
tf_data.sizetf = 0;
tf_data.tfname = "$ivl_enum_method$prev";
tf_data.user_data = "$ivl_enum_method$prev";
res = vpi_register_systf(&tf_data);
/* This is not a user defined system function so hide it. */
vpip_make_systf_system_defined(res);
}
|