简体   繁体   中英

Parameterized generate block

I want to create a parameterised priority logic block in my test bench. Truth table looks like

Here, I want to parameterize the number of inputs and outputs. How can I implement this in a generate block?

This is what I have done for now:

module PL

#(

parameter N=2

)

(

input en,

input [N-1:0] in,

output [N-1:0] out

);

assign out[0] = en? (in[0]):0;

if(N==2)

assign out[1] = en ? (!in[0] && in[1]) :0;

.

.

.

Add another signal to mask off the lower priority inputs. Like this:

module PL #(
parameter N = 2 )(
input          en,
input  [N-1:0] in,
output [N-1:0] out );

wire [N:0] mask;
assign mask[N] = 0;
genvar i;
generate
  for ( i=0; i<N; i=i+1 )
  begin: g1
    assign out[(N-1)-i] = ( en == 1 && mask[N-i] == 0 ) ?
                          in[(N-1)-i] : 1'b0;
    assign mask[(N-1)-i] = mask[N-i] | in[(N-1)-i];
  end
endgenerate
endmodule

But I prefer using a simple for-loop in this case:

module PL #(
parameter N = 2 )(
input          en,
input  [N-1:0] in,
output [N-1:0] out );

always @*
begin: blk1
  integer i;
  out = 0;
  if ( en )
  begin
    for ( i=0; i<N; i=i+1 )
      if ( in[i] )
        out = 32'd1 << i;
  end
end
endmodule

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM