Search code examples
command-line-argumentsverilogsystem-verilog

How to get array of values as plusargs?


How to get the array of values as arguments. I need get an array of commands of undefined size from the command line. How to get these arguments into an array or queue?

Eg:

+CMDS=READ,WRITE,READ_N_WRITE

It should be taken to an array.


Solution

  • $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