Is there any way you can pass "type" apart from passing as parameter?

Hello !

Is there any way can we pass the “type” variable in struct or pkg instead of passing as parameter ? So that rather than listing multiple type in the parameter list, instead can be passed in the struct or so ?


typedef struct {
    int  width = 1;
    // type T = logic;
} try_s;
 
// parameter try_s try_i = '{ width:8, T=reg};
parameter try_s try_i = '{ width:8};

module struct_try #(parameter try_s try=try_i);
  logic blue;
  logic [try.width-1:0] red;
  // try.T   black;


initial begin
    $display(" width= %0d\n", try.width);
    // $display(" width= %0d, type %s\n", try.width, try.T);
end 

endmodule: struct_try 

Share in your comments !

In reply to desperadorocks:

You can put your typedefs and parameters in a class, and pass the class as type parameter

module bot #(type t)();
  type(t::t1) v1 = 0.5;
  type(t::t2) v2 = 0.5;
  bit [t::width-1:0] vector = '1;
  
  initial begin $display("%m %h",vector,,v1,,v2);
  end 
endmodule

module top;
class A;
  parameter width = 8;
  typedef int t1;
  typedef real t2;
endclass
class B;
  parameter width = 16;
  typedef real t1;
  typedef int t2;
endclass

  bot #(A) i1();
  bot #(B) i2();
endmodule

In reply to dave_59:

Hello Dave,

Thanks for the technique. But is it possible with the classes as well ? Somehting like given below ?


module try;
    class cfg_A;
        typedef int t1;       
        int DEP = 4;
    endclass: cfg_A 
    
    class cfg_B;
        typedef bit t1;        
        int DEP = 8;
    endclass: cfg_B 
    
    class aloha #(type t);
        type (t::t1)[t::DEP-1:0] data;
    
        function display();
            $display("Value of data = %h\n", data);            
        endfunction: display 
    endclass: aloha 
        

    aloha #(cfg_A) al;
    aloha #(cfg_B) bl;

    initial begin 
        al = new(); bl = new();
        al.display(); bl.display();
    end 
endmodule: try

In reply to desperadorocks:

Yes, it should work the same for a parameterized class as a module.

A couple of problems with your code.

DEP must be declared as a parameter:

  • You cannot use variables that get initialized at runtime to declare the size of a variable
  • The BNF only allow a simple type name when declaring the packed array data. You must use an intermediate typedef.
typedef type (t::t1) t_t1;
t_t1 [t::DEP-1:0] data;