Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize array via function within package #570

Open
logicmonkey opened this issue Dec 20, 2021 · 3 comments
Open

Initialize array via function within package #570

logicmonkey opened this issue Dec 20, 2021 · 3 comments
Labels

Comments

@logicmonkey
Copy link

package mypkg;
  localparam ELEM = 4;
  typedef int myints_t [ELEM-1:0]; // create a type that is an array of ints

  localparam myints_t myarr = init_arr(ELEM); // initialise constant as an array of values set by a function

  function automatic myints_t init_arr(int N); // an easy function
  begin
    for (int i=0; i<N; i++)
      init_arr[i] = i;
  end
  endfunction
endpackage

module dummy;
import mypkg::*;
  initial begin
    for (int i=0; i<mypkg::ELEM; i++)
      $display(mypkg::myarr[i]);
    $display(mypkg::ELEM);
    $finish;
  end
endmodule

Expected output (as per commercial simulator):

xcelium> run
          0
          1
          2
          3
          4
Simulation complete via $finish(1) at time 0 FS + 0

Actual output (some rev 12 hash build run with iverilog -g2012)

0
1
0
1
          4

@martinwhitaker
Copy link
Collaborator

Icarus Verilog does not currently support unpacked arrays as (local) parameters or function return values, or indeed, most other places where they can't be used in traditional Verilog. But it should warn you about this and fail to compile your code, rather than producing incorrect results.

@logicmonkey
Copy link
Author

logicmonkey commented Dec 21, 2021

Understood. Thanks, Martin.
So longer term, I have a kind of wish list (all work on 2 commercial simulators I have access to). I have bundled them:

package mypkg;
  localparam ELEM = 4;

  // ATTEMPT 0 - unpacked array of ints
  typedef int myuints_t [ELEM-1:0];
  localparam myuints_t ATTEMPT0 = init_uarr(ELEM);

  function automatic myuints_t init_uarr(int N); // an easy function
    for (int i=0; i<N; i++)
      init_uarr[i] = i;
  endfunction

  // ATTEMPT 1 - packed array of logic vectors
  typedef logic  [$clog2(ELEM)-1:0] elem_t;     // pack this
  typedef elem_t [ELEM-1:0]         mypints_t;  // and this
  localparam mypints_t ATTEMPT1 = init_parr(ELEM);

  function automatic mypints_t init_parr(int N); // an easy function
    for (int i=0; i<N; i++)
      init_parr[i] = i;
  endfunction

  // ATTEMPT 2 - unpacked array of ints (attempt0) explicitly assigned
  localparam myuints_t ATTEMPT2 = '{3, 2, 1, 0};

  // ATTEMPT 3 - assign packed logic vector array
  localparam mypints_t ATTEMPT3 = 8'b11_10_01_00;

  // ATTEMPT 4 - assign packed logic vector array
  localparam logic [ELEM-1:0][$clog2(ELEM)-1:0] ATTEMPT4 = 8'b11_10_01_00;

endpackage

module dummy;
import mypkg::*;
  initial begin
    for (int i=0; i<mypkg::ELEM; i++)
      $display(mypkg::ATTEMPT0[i]);
    for (int i=0; i<mypkg::ELEM; i++)
      $display(mypkg::ATTEMPT1[i]);
    for (int i=0; i<mypkg::ELEM; i++)
      $display(mypkg::ATTEMPT2[i]);
    for (int i=0; i<mypkg::ELEM; i++)
      $display(mypkg::ATTEMPT3[i]);
    for (int i=0; i<mypkg::ELEM; i++)
      $display(mypkg::ATTEMPT4[i]);
    $finish;
  end
endmodule

@martinwhitaker
Copy link
Collaborator

Yes, having looked, I now find that multi-dimensional packed arrays are not supported in parameters/localparams.
Do you actually need the array to be a localparam (i.e. do you need to use it in a constant expression)? Using a variable instead, e.g.

package mypkg;
  localparam ELEM = 4;

  typedef logic [ELEM-1:0][$clog2(ELEM)-1:0] parray_t ;

  parray_t array = { 2'd3, 2'd2, 2'd1, 2'd0 };
endpackage

almost works, although it hits another bug when array is imported into a module (which may be easier to fix).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants