how to get array of values as plusargs in systemverilog?

from http://stackoverflow.com/questions/20519349/how-to-get-array-of-values-as-plusargs-in-systemverilog

可以通过$value$plusargs传入多个string型参数 

$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

 

 

 

posted @ 2014-03-29 10:08  brickisku  阅读(495)  评论(0)    收藏  举报