Skip to content

Commit

Permalink
fread() support integral variables not just registers
Browse files Browse the repository at this point in the history
  • Loading branch information
caryr committed Dec 9, 2023
1 parent 5d561f3 commit 987b7d1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ivtest/gold/fread-error-vlog95.gold
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ERROR: vlog95.v:17: $fread's first argument must be a reg or memory.
ERROR: vlog95.v:17: $fread's first argument must be an integral variable or memory.
ERROR: vlog95.v:18: $fread requires a second (file descriptor) argument.
ERROR: vlog95.v:19: $fread's second argument must be numeric.
ERROR: vlog95.v:20: $fread's third argument must be numeric.
Expand Down
12 changes: 6 additions & 6 deletions ivtest/gold/fread-error.gold
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ERROR: ./ivltests/fread-error.v:7: $fread's first argument must be a reg or memory.
ERROR: ./ivltests/fread-error.v:8: $fread requires a second (file descriptor) argument.
ERROR: ./ivltests/fread-error.v:9: $fread's second argument must be numeric.
ERROR: ./ivltests/fread-error.v:10: $fread's third argument must be numeric.
ERROR: ./ivltests/fread-error.v:11: $fread's fourth argument must be numeric.
ERROR: ./ivltests/fread-error.v:12: $fread takes at most four arguments.
ERROR: ./ivltests/fread-error.v:8: $fread's first argument must be an integral variable or memory.
ERROR: ./ivltests/fread-error.v:9: $fread requires a second (file descriptor) argument.
ERROR: ./ivltests/fread-error.v:10: $fread's second argument must be numeric.
ERROR: ./ivltests/fread-error.v:11: $fread's third argument must be numeric.
ERROR: ./ivltests/fread-error.v:12: $fread's fourth argument must be numeric.
ERROR: ./ivltests/fread-error.v:13: $fread takes at most four arguments.
Found 1 extra argument.
5 changes: 3 additions & 2 deletions ivtest/ivltests/fread-error.v
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module top;
integer res, ival;
real rval;
integer res;
reg [7:0] rg;
reg [7:0] mem [3:0];

initial begin
res = $fread(ival, 1); // 1st arg. must be a reg. or memory.
res = $fread(rval, 1); // 1st arg. must be a reg. or memory.
res = $fread(rg); // Too few argument.
res = $fread(mem, "a"); // Not a valid fd.
res = $fread(mem, 1, "a"); // Not a valid start.
Expand Down
13 changes: 5 additions & 8 deletions vpi/sys_fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ static PLI_INT32 sys_fread_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, callh);
vpiHandle arg;
PLI_INT32 type;

/* We must have at least two arguments. */
if (argv == 0) {
Expand All @@ -484,12 +483,11 @@ static PLI_INT32 sys_fread_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
return 0;
}

/* Check that the first required argument is a register or memory. */
type = vpi_get(vpiType, vpi_scan(argv));
if (type != vpiReg && type != vpiMemory) {
/* Check that the first required argument is an integral variable or memory. */
if (! is_int_var_or_mem(vpi_scan(argv))) {
vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
(int)vpi_get(vpiLineNo, callh));
vpi_printf("%s's first argument must be a reg or memory.\n", name);
vpi_printf("%s's first argument must be an integral variable or memory.\n", name);
vpip_set_return_value(1);
vpi_control(vpiFinish, 1);
}
Expand Down Expand Up @@ -612,7 +610,6 @@ static PLI_INT32 sys_fread_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)

/* Get the register/memory. */
mem_reg = vpi_scan(argv);

/* Get the file descriptor. */
arg = vpi_scan(argv);
val.format = vpiIntVal;
Expand All @@ -635,8 +632,8 @@ static PLI_INT32 sys_fread_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
}

/* Are we reading into a memory? */
if (vpi_get(vpiType, mem_reg) == vpiReg) is_mem = 0;
else is_mem = 1;
if (vpi_get(vpiType, mem_reg) == vpiMemory) is_mem = 1;
else is_mem = 0;

/* We only need to get these for memories. */
if (is_mem) {
Expand Down
24 changes: 24 additions & 0 deletions vpi/sys_priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,30 @@ unsigned is_string_obj(vpiHandle obj)

return rtn;
}
/*
* This routine returns 1 if the argument is an integral value or is a memory,
* otherwise it returns 0.
*/
unsigned is_int_var_or_mem(vpiHandle obj)
{
unsigned rtn = 0;

assert(obj);
switch(vpi_get(vpiType, obj)) {
case vpiIntegerVar:
case vpiBitVar:
case vpiByteVar:
case vpiShortIntVar:
case vpiIntVar:
case vpiLongIntVar:
case vpiReg:
case vpiTimeVar:
case vpiMemory:
rtn = 1;
break;
}
return rtn;
}

/*
* This routine returns 1 if the argument supports a valid string value,
Expand Down
1 change: 1 addition & 0 deletions vpi/sys_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extern struct timeformat_info_s timeformat_info;
extern unsigned is_constant_obj(vpiHandle obj);
extern unsigned is_numeric_obj(vpiHandle obj);
extern unsigned is_string_obj(vpiHandle obj);
extern unsigned is_int_var_or_mem(vpiHandle obj);
extern unsigned is_variable(vpiHandle obj);

extern unsigned is_valid_fd_mcd(PLI_UINT32 fd_mcd);
Expand Down

0 comments on commit 987b7d1

Please sign in to comment.