How to get the array of values as arguments in systemverilog, my requirement is I need get an array of commands of undefined size from the command line ,and how to get these arguments to an array/Queue
Eg:
+CMDS=READ,WRITE,READ_N_WRITE
: It should be taken to an array ?
$value$plusargs
does not support arrays, it does support strings. See IEEE Std 1800-2012 § 21.6 "Command line input". Parsing a string in SystemVerilog is only a little cumbersome but still very doable, especially when the separator is represented as a single character. Here is a generic string parser using a SystemVerilog queue for recoding the indexes and string method substr
defined in IEEE Std 1800-2012 § 7.10 "Queue" and § 6.16.8 "Substr"
function void parse(output string out [], input byte separator, input string in);
int index [$]; // queue
foreach(in[i]) begin // find commas
if (in[i]==separator) begin
index.push_back(i-1); // index before comma
index.push_back(i+1); // index after comma
end
end
index.push_front(0); // first index
index.push_back(in.len()-1); // last index
out = new[index.size()/2];
foreach (out[i]) begin
out[i] = in.substr(index[2*i],index[2*i+1]);
/*$display("cmd[%0d] == in.substr(%0d,%0d) == \"%s\"",
i, index[2*i],index[2*i+1], out[i]); */
end
endfunction : parse
Then combine it with $value$plusargs
to parse the input:
string cmd[];
string plusarg_string;
if ( $value$plusargs("CMDS=%s",plusarg_string) ) begin
parse(cmd, ",", plusarg_string);
end
foreach(cmd[i])
$display("CMD[%0d]:'%s'",i,cmd[i]);
Full working example: http://www.edaplayground.com/s/6/570
According to the IEEE Std 1800-2012 (Section 21.6 "Command line input"), the $value$plusargs
system function accepts a string, not an array. You would have to parse the string inside Verilog, which would probably be very cumbersome (refer to Section 6.16 "String data type" for string operators).
Other options might be:
+CMD1=READ +CMD2=WRITE
$readmemh
, although that is limited to numeric data$fopen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With