1

Let's say that I have an array of buses, each one carrying a struct like so:

typedef struct packed {
    logic [31:0] piano_concerto_in_d_minor;
    logic [31:0] piano_concerto_in_c_minor;
    logic [7:0]  nationality;
} rachmaninov_t;

//.........
module russian_composers(/* input/output busses go here */);
    rachmaninov_t [2] rachs;
    rachmaninov_t     output;
    assign rachs[0] = {32'haaaa5555, 32'h10001000, 8'h33};
    assign rachs[1] = {32'h5555aaaa, 32'h80008000, 8'h44};

And I want to bitwise or them together, I would do the following:

    assign output = rachs[0] | rachs[1];
    //value of output is {32'hffffffff, 32'h90009000, 8'h77}
endmodule

Now, what would I do if I had a parameterized number of rachmaninov_t structs and I wanted to bitwise-or all of them together like above? I've already tried

assign output = |rachs;

and it doesn't work (which isn't too surprising).

Could I get a generate block to do this?

John M
  • 1,484
  • 1
  • 14
  • 27

2 Answers2

3

While you can use a generate for this, you can also just use a plain old for loop inside your combinational logic to do this:

parameter NUM = 4; // The number of structs you need to or together
...
int i;

always_comb begin 
  output = '0;
  for (i = 0; i < NUM; i = i + 1) begin
    output |= rachs[i];
  end
end

Just realize this will synthesize into a chain of or; which hopefully the synthesis tool might reform into a larger or or a tree, but Im not sure if it would.

Unn
  • 4,775
  • 18
  • 30
  • I don't really understand the difference between doing this and using a generate. Can you please explain? – John M Oct 28 '15 at 21:09
  • Its not really that different and will synthesize the same. The big differences is in how it will simulate and the syntax. – Unn Oct 28 '15 at 21:33
  • This syntax definately feels more natural than using a generate block. What sort of differences would I see in simulation? – John M Oct 28 '15 at 21:44
  • 1
    Well, with generate you typically are generating a bunch of modules or blocks, all are individual processes. With this solution, everything is in one process that means intermediate results will not be propagated. Mostly the differences is in the details of the simulation rather than how it works functionally. – Unn Oct 28 '15 at 22:08
3

Perhaps you want the bitwise-or reduction method described in 7.12.3 Array reduction methods

assign output = rachs.or();
dave_59
  • 39,096
  • 3
  • 24
  • 63
  • Huh, I never knew about unpacked array reduction operators. Do most synthesis tools support them? – Unn Oct 28 '15 at 21:37
  • Thanks, this is interesting. What is the document you're citing? Can you give a link? – John M Oct 28 '15 at 21:40
  • The [IEEE 1800-2012 SystemVerilog Language Reference Manual](https://standards.ieee.org/getieee/1800/download/1800-2012.pdf). – dave_59 Oct 28 '15 at 22:18