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
|
/*
* Copyright (c) 2002-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 <string.h>
# include <stdlib.h>
# include <assert.h>
/*
* Compare the +arguments passed to the simulator with the argument
* passed to the $test$plusargs. If there is a simulator argument that
* is like this argument, then return true. Otherwise return false.
*/
static PLI_INT32 sys_test_plusargs_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
s_vpi_value val;
s_vpi_vlog_info info;
int idx;
int flag = 0;
size_t slen, len;
(void)name; /* Parameter is not used. */
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, callh);
val.format = vpiStringVal;
vpi_get_value(vpi_scan(argv), &val);
slen = strlen(val.value.str);
vpi_get_vlog_info(&info);
/* Look for a +arg that matches the prefix supplied. */
for (idx = 0 ; idx < info.argc ; idx += 1) {
/* Skip arguments that are not +args. */
if (info.argv[idx][0] != '+')
continue;
len = strlen(info.argv[idx]+1);
if (len < slen)
continue;
if (strncmp(val.value.str, info.argv[idx]+1, slen) != 0)
continue;
flag = 1;
break;
}
val.format = vpiIntVal;
val.value.integer = flag;
vpi_put_value(callh, &val, 0, vpiNoDelay);
vpi_free_object(argv);
return 0;
}
static PLI_INT32 sys_value_plusargs_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, callh);
vpiHandle arg;
/* Check that there are arguments. */
if (argv == 0) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s requires two arguments.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
/* Check that the first argument is a string. */
arg = vpi_scan(argv);
assert(arg != 0);
if ( ! is_string_obj(arg)) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s's first argument must be a string.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
arg = vpi_scan(argv);
if (! arg) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s's requires a second variable argument.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
switch (vpi_get(vpiType, arg)) {
case vpiReg:
case vpiIntegerVar:
case vpiBitVar:
case vpiByteVar:
case vpiShortIntVar:
case vpiIntVar:
case vpiLongIntVar:
case vpiRealVar:
case vpiTimeVar:
case vpiStringVar:
break;
default:
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s's second argument must be a variable, found a %s.\n",
name, vpi_get_str(vpiType, arg));
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
/* Make sure there are no extra arguments. */
check_for_extra_args(argv, callh, name, "two arguments", 0);
return 0;
}
static PLI_INT32 sys_value_plusargs_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
s_vpi_vlog_info info;
s_vpi_value fmt;
s_vpi_value res;
char msg[64];
char*cp;
int idx;
int flag = 0;
size_t slen, len;
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, callh);
fmt.format = vpiStringVal;
vpi_get_value(vpi_scan(argv), &fmt);
/* Check for the start of a format string. */
cp = strchr(fmt.value.str, '%');
if (cp == 0) {
snprintf(msg, sizeof(msg), "ERROR: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s %s is missing a format code.\n", msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ", fmt.value.str);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
/* This is the length of string we will look for. */
slen = cp - fmt.value.str;
/* Skip a zero. */
cp += 1;
if (*cp == '0') cp += 1;
/* Check the format code. */
switch (*cp) {
case 'd':
case 'D':
case 'o':
case 'O':
case 'h':
case 'H':
case 'x':
case 'X':
case 'b':
case 'B':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 's':
case 'S':
break;
default:
snprintf(msg, sizeof(msg), "ERROR: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s %s has an invalid format string:\n", msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ", fmt.value.str);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
return 0;
}
/* Warn if there is any trailing garbage. */
if (*(cp+1) != '\0') {
snprintf(msg, sizeof(msg), "WARNING: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s Skipping trailing garbage in %s's format string:\n",
msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ", fmt.value.str);
*(cp+1) = '\0';
}
vpi_get_vlog_info(&info);
/* Look for a +arg that matches the prefix supplied. */
for (idx = 0 ; idx < info.argc ; idx += 1) {
char*sp, *tp, *end;
size_t sp_len;
/* Skip arguments that are not +args. */
if (info.argv[idx][0] != '+')
continue;
len = strlen(info.argv[idx]+1);
if (len < slen)
continue;
if (strncmp(fmt.value.str, info.argv[idx]+1, slen) != 0)
continue;
sp = info.argv[idx]+1+slen;
sp_len = strlen(sp);
switch (*cp) {
case 'd':
case 'D':
res.format = vpiDecStrVal;
/* A decimal string can set the value to "x" or "z". */
if (sp_len == strspn(sp, "xX_") ||
sp_len == strspn(sp, "zZ_")) {
res.value.str = sp;
/* A decimal string must contain only these characters.
* A decimal string can not start with an "_" character.
* A "-" can only be at the start of the string. */
} else if (sp_len != strspn(sp, "-0123456789_") ||
*sp == '_' ||
((tp = strrchr(sp, '-')) && tp != sp)) {
res.value.str = "x";
snprintf(msg, sizeof(msg), "WARNING: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s Invalid decimal value passed to %s:\n",
msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ", sp);
} else {
res.value.str = sp;
}
break;
case 'o':
case 'O':
res.format = vpiOctStrVal;
/* An octal string must contain only these characters.
* An octal string can not start with an "_" character.
* A "-" can only be at the start of the string. */
if (sp_len != strspn(sp, "-01234567_xXzZ") ||
*sp == '_' || ((tp = strrchr(sp, '-')) && tp != sp)) {
res.value.str = "x";
snprintf(msg, sizeof(msg), "WARNING: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s Invalid octal value passed to %s:\n",
msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ", sp);
} else {
res.value.str = sp;
}
break;
case 'h':
case 'H':
case 'x':
case 'X':
res.format = vpiHexStrVal;
/* A hex. string must contain only these characters.
* A hex. string can not start with an "_" character.
* A "-" can only be at the start of the string. */
if (sp_len != strspn(sp, "-0123456789aAbBcCdDeEfF_xXzZ") ||
*sp == '_' || ((tp = strrchr(sp, '-')) && tp != sp)) {
res.value.str = "x";
snprintf(msg, sizeof(msg), "WARNING: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s Invalid hex value passed to %s:\n",
msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ", sp);
} else {
res.value.str = sp;
}
break;
case 'b':
case 'B':
res.format = vpiBinStrVal;
/* A binary string must contain only these characters.
* A binary string can not start with an "_" character.
* A "-" can only be at the start of the string. */
if (sp_len != strspn(sp, "-01_xXzZ") ||
*sp == '_' || ((tp = strrchr(sp, '-')) && tp != sp)) {
res.value.str = "x";
snprintf(msg, sizeof(msg), "WARNING: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s Invalid binary value passed to %s:\n",
msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ", sp);
} else {
res.value.str = sp;
}
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
res.format = vpiRealVal;
res.value.real = strtod(sp, &end);
/* If we didn't get a full conversion print a warning. */
if (*end) {
/* We had an invalid value passed. */
if (end == sp) {
snprintf(msg, sizeof(msg), "WARNING: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s Invalid real value passed to "
"%s:\n", msg, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ",
sp);
/* We have extra garbage at the end. */
} else {
snprintf(msg, sizeof(msg), "WARNING: %s:%d:",
vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
msg[sizeof(msg)-1] = 0;
vpi_printf("%s Extra character(s) \"%s\" found "
"in %s's real string:\n",
msg, end, name);
vpi_printf("%*s \"%s\".\n", (int)strlen(msg), " ",
sp);
}
}
break;
case 's':
case 'S':
res.format = vpiStringVal;
res.value.str = sp;
break;
default:
assert(0);
}
vpi_put_value(vpi_scan(argv), &res, 0, vpiNoDelay);
flag = 1;
break;
}
res.format = vpiIntVal;
res.value.integer = flag;
vpi_put_value(callh, &res, 0, vpiNoDelay);
vpi_free_object(argv);
return 0;
}
void sys_plusargs_register(void)
{
s_vpi_systf_data tf_data;
vpiHandle res;
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiIntFunc;
tf_data.tfname = "$test$plusargs";
tf_data.calltf = sys_test_plusargs_calltf;
tf_data.compiletf = sys_one_string_arg_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "$test$plusargs";
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
tf_data.type = vpiSysFunc;
tf_data.sysfunctype = vpiIntFunc;
tf_data.tfname = "$value$plusargs";
tf_data.calltf = sys_value_plusargs_calltf;
tf_data.compiletf = sys_value_plusargs_compiletf;
tf_data.sizetf = 0;
tf_data.user_data = "$value$plusargs";
res = vpi_register_systf(&tf_data);
vpip_make_systf_system_defined(res);
}
|