4
votes

I created a module to soemthing simple like add some pipeline delay. I made the data type a parameter so it could handle complex things like structs. I have something like this:

module pipe
   #(parameter type T = int,
     parameter DELAY = 0)
   (
    input clk,
    input T data_in,
    output T data_out);

   T pipe[DELAY];
   always_ff @(posedge clk) begin
     pipe[0] <= data_in;
     for(int i = 1; i<DEPTH; i++) begin
       pipe[i] <= pipe[i-1];
     end
   end
   assign data_out = pipe[DELAY-1];
endmodule

This works great but then I found myself wanting to use it with an unpacked array and I couldn't figure out how to instance it. It would look something like this, but I don't think this is right:

logic [7:0] my_data [16];
logic [7:0] my_data_delayed [16];
pipe #(.T(logic [7:0] [16]), .DELAY(2)) i_pipe
  (
   .clk(clk),
   .data_in(my_data),
   .data_out(my_data_delayed)
  );

Is there a way to get the type of a variable so it can be passed to this parameter?

2

2 Answers

3
votes

You can use typedef do define the data type:

typedef logic [7:0] my_type [16];
my_type my_data;
my_type my_data_delayed;
pipe #(.T(my_type), .DELAY(2)) i_pipe
  (
   .clk(clk),
   .data_in(my_data),
   .data_out(my_data_delayed)
  );

The remaining question is if it will work with your simulator. I tried it out at EDAplayground, worked with VCS but not Riviera-PRO (got a "not supported in this release" message)


I was able to get typedef struct { logic [7:0] data [16]; } my_type; to work. Less clean but it is a workaround.

2
votes

While typing the question I figured out one answer. The type operator (section 6.23 in IEEE 1800-2012) spec does what I need. So my instance would look like this:

logic [7:0] my_data [16];
logic [7:0] my_data_delayed [16];
pipe #(.T(type(my_data)), .DELAY(2)) i_pipe
  (
   .clk(clk),
   .data_in(my_data),
   .data_out(my_data_delayed)
  );

Of course this results in a compile error in my tool so it doesn't look to be universally supported.

My work-around is going to be to make an array of instances of the pipe module.