Author Topic: Verilog generate and buses  (Read 6884 times)

0 Members and 1 Guest are viewing this topic.

Offline EugenioNTopic starter

  • Contributor
  • Posts: 39
Verilog generate and buses
« on: July 24, 2023, 02:16:12 pm »
Hi all!
I found myself stuck into a simple problem I cannot describe in verilog!
I have 12 buses, each 16 bit wide, and I should 'or' them to create a single, 16 bit wide bus:

Code: [Select]
wire [15:0] this_sixteen_bits_data[11:0];
wire [15:0] output_data;

this_sixteen_bits_data[0]=some_logic_instance_0;
this_sixteen_bits_data[1]=some_logic_instance_1;
...
this_sixteen_bits_data[11]=some_logic_instance_11;

assign output_data = this_sixteen_bits_data[0] | this_sixteen_bits_data[1] | ... | this_sixteen_bits_data[11];

Trying not to go this verbose, a generate loop should be the way to go:

Code: [Select]
wire [15:0] this_sixteen_bits_data[11:0];
wire  [15:0] output_data;

genvar i;
generate
for (i=0; i<12; i=i+1) begin : wide_or_genvar
this_sixteen_bits_data[i]=some_logic[i];
output_data = ??? this_sixteen_bits_data[i];
end
endgenerate

How can you describe this parallel wide-or inside a generate loop?

« Last Edit: September 11, 2023, 12:36:41 pm by EugenioN »
 

Offline asmi

  • Super Contributor
  • ***
  • Posts: 3345
  • Country: ca
Re: Verilog generate and buses
« Reply #1 on: July 24, 2023, 02:30:20 pm »
Not sure what you need generate for in this case, or anything else. Just use a simple bitwise OR (|):
Code: [Select]
result = bus[0] | bus[1] | ... | bus[11];
Please do not confuse logical OR (||) with bitwise OR(|). There is also an unary version of OR (|some_var), which is also called "folding" or "reduction" operator, since it "folds" the multibit argument into a single bit. Some more reading here: https://verilogcodes.blogspot.com/2015/10/unary-or-reduction-operators-in-verilog.html
« Last Edit: July 24, 2023, 03:55:00 pm by asmi »
 

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 4842
  • Country: us
Re: Verilog generate and buses
« Reply #2 on: July 24, 2023, 03:43:58 pm »
In addition to what asmi explained, even if you want to do this with a loop you don't need a generate block.  A regular for loop inside a combinatoric process is enough.

Generate for is mostly only needed when you need to create multiple copies of a submodule instance or a top level process. 
 

Offline EugenioNTopic starter

  • Contributor
  • Posts: 39
Re: Verilog generate and buses
« Reply #3 on: July 25, 2023, 09:02:57 am »
Not sure what you need generate for in this case, or anything else. Just use a simple bitwise OR (|):
Code: [Select]
result = bus[0] | bus[1] | ... | bus[11];
Yeah, this is my actual implementation.
I don't like the cut&paste/verbose approach. I was wandering if there is a more concise way to describe it.
 

Offline EugenioNTopic starter

  • Contributor
  • Posts: 39
Re: Verilog generate and buses
« Reply #4 on: July 25, 2023, 09:08:49 am »
In addition to what asmi explained, even if you want to do this with a loop you don't need a generate block.  A regular for loop inside a combinatoric process is enough.

Generate for is mostly only needed when you need to create multiple copies of a submodule instance or a top level process.

I'm actually creating multiple copies of module instances, each generating a 16bit wide result.
At the end, I need to (bitwise) 'or' those results in a single 16bit bus, so the generate loop is a 'free' resource I though I could take advantage of.

Anyhow, never tried the for loop for actual sintesizable logic (testbench only). How can i write down this with for loop?
Code: [Select]
wire [15:0] this_sixteen_bits_data[11:0];
wire [15:0] output_data;

assign output_data = this_sixteen_bits_data[0] | this_sixteen_bits_data[1] | ... | this_sixteen_bits_data[11];
 

Offline Someone

  • Super Contributor
  • ***
  • Posts: 6071
  • Country: au
    • send complaints here
Re: Verilog generate and buses
« Reply #5 on: July 25, 2023, 10:16:06 am »
wor

or a variable in the loop
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 5798
  • Country: dk
Re: Verilog generate and buses
« Reply #6 on: July 25, 2023, 01:06:07 pm »
In addition to what asmi explained, even if you want to do this with a loop you don't need a generate block.  A regular for loop inside a combinatoric process is enough.

Generate for is mostly only needed when you need to create multiple copies of a submodule instance or a top level process.

I'm actually creating multiple copies of module instances, each generating a 16bit wide result.
At the end, I need to (bitwise) 'or' those results in a single 16bit bus, so the generate loop is a 'free' resource I though I could take advantage of.

Anyhow, never tried the for loop for actual sintesizable logic (testbench only). How can i write down this with for loop?
Code: [Select]
wire [15:0] this_sixteen_bits_data[11:0];
wire [15:0] output_data;

assign output_data = this_sixteen_bits_data[0] | this_sixteen_bits_data[1] | ... | this_sixteen_bits_data[11];


something along the the line of this:

Code: [Select]
reg [15:0] output_data;
integer i;
always@(*)
begin
  output_data = 16'h0;
  for(i = 0; i < 16; i=i+1)
    output_data = output_data | this_sixteen_bits_data[i];
end

 

Offline EugenioNTopic starter

  • Contributor
  • Posts: 39
Re: Verilog generate and buses
« Reply #7 on: September 11, 2023, 12:36:09 pm »
something along the the line of this:

Code: [Select]
reg [15:0] output_data;
integer i;
always@(*)
begin
  output_data = 16'h0;
  for(i = 0; i < 12; i=i+1)
    output_data = output_data | this_sixteen_bits_data[i];
end


Worked great, thank you!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->