module array_product (
    input logic [9:0] SW,
    output logic [6:0] HEX5, HEX4, HEX3, HEX2, HEX1, HEX0
);
    wire [3:0] a = SW[7:4];
    wire [3:0] b = SW[3:0];
    wire [7:0] subtotal [0:4];
    assign subtotal[0] = 8'b0;

    genvar row, col;
    generate
        for (row = 0; row < 4; row = row + 1) begin : rows
            wire [3:0] masked = a & {4{b[row]}};
            wire [7:0] partial = {4'b0, masked}; // TODO: align this partial product with its binary column.
            wire [8:0] carry;
            assign carry[0] = 1'b0;
            for (col = 0; col < 8; col = col + 1) begin : cells
                wire lhs = subtotal[row][col];
                wire rhs = partial[col];
                wire propagate = lhs ^ rhs;
                assign subtotal[row+1][col] = propagate ^ carry[col];
                assign carry[col+1] = (lhs & rhs) | (propagate & carry[col]);
            end
        end
    endgenerate

    function automatic logic [6:0] segments(input logic [3:0] nibble);
        begin
            case (nibble)
                4'h0: segments = 7'h40;
                4'h1: segments = 7'h79;
                4'h2: segments = 7'h24;
                4'h3: segments = 7'h30;
                4'h4: segments = 7'h19;
                4'h5: segments = 7'h12;
                4'h6: segments = 7'h02;
                4'h7: segments = 7'h78;
                4'h8: segments = 7'h00;
                4'h9: segments = 7'h10;
                4'ha: segments = 7'h08;
                4'hb: segments = 7'h03;
                4'hc: segments = 7'h46;
                4'hd: segments = 7'h21;
                4'he: segments = 7'h06;
                4'hf: segments = 7'h0e;
                default: segments = 7'h7f;
            endcase
        end
    endfunction

    assign HEX5 = segments(subtotal[4][7:4]);
    assign HEX4 = segments(subtotal[4][3:0]);
    assign HEX3 = 7'h7f;
    assign HEX2 = segments(a);
    assign HEX1 = 7'h7f;
    assign HEX0 = segments(b);
endmodule
