array.sv 1.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
`default_nettype none

module ArrayOrReduction #(
    parameter SIZE = 16, WIDTH=32) (
    input logic [SIZE-1:0][WIDTH-1:0] inputs,
    output logic [WIDTH-1:0] result
    );

    // Recursively generate a pair-wise reduction
    generate
        if(SIZE <= 0) begin : error_case
            DoesNotExit foo();
        end else if (SIZE == 1) begin : base_case_1
            assign result = inputs[0];
        end else if(SIZE == 2) begin : base_case_2
            assign result = inputs[0] | inputs[1];
        end else begin : recursive_case
            logic [1:0][WIDTH-1:0] subResults;
            ArrayOrReduction #(.WIDTH(WIDTH), .SIZE(SIZE/2)) top(.inputs(inputs[SIZE-1:SIZE/2]), .result(subResults[1]));
            ArrayOrReduction #(.WIDTH(WIDTH), .SIZE(SIZE/2)) bot(.inputs(inputs[SIZE/2-1:0]), .result(subResults[0]));
            assign result = subResults[0] | subResults[1];
        end
    endgenerate

endmodule


module Array #(parameter ELEMENTS=16, WIDTH=32)(
  // $clog2 is a built in function that does the natural log of 2
  input logic [$clog2(ELEMENTS)-1:0] index,
  input logic [WIDTH-1:0] element,
  output logic [ELEMENTS-1:0][WIDTH-1:0] array,
  input logic clock, clear, enable
  );

  localparam ZERO_ELEMENT = {WIDTH{1'b0}};

  always_ff @(posedge clock)
    if(clear)
      array <= {ELEMENTS{ZERO_ELEMENT}};
    else if(enable)
      array[index] <= element;

endmodule