`timescale 1ns / 1ps // Output-stationary N x N systolic array for C = A x B. // A is M x K and B is K x N, both signed INT8. Each processing element (PE) owns one // INT32 accumulator for C[i][j]. Column k of A enters from the left and row k of B from // the top, one k per clock, so a tile of K beats takes K cycles plus the array latency. // // Interface (one beat per cycle when in_valid = 1): // a_col[8*i +: 8] = A[i][k] for i = 0..N-1 // b_row[8*j +: 8] = B[k][j] for j = 0..N-1 // in_first marks k = 0 of a tile, in_last marks k = K-1 (both high when K = 1). // out_valid pulses for one cycle when the whole tile is in c_out: // c_out[32*(i*N+j) +: 32] = C[i][j] // Latency from the in_last beat to out_valid is 2N-1 cycles. // Rule: leave at least 2N-1 cycles between two in_last beats (K >= 2N-1 when tiles // run back to back), or a new tile overwrites results before out_valid. module systolic_array #( parameter int N = 4 ) ( input logic clk, input logic rst, // synchronous, active high: clears the control flags input logic in_valid, input logic in_first, input logic in_last, input logic [N*8-1:0] a_col, input logic [N*8-1:0] b_row, output logic out_valid, output logic [N*N*32-1:0] c_out ); // Signals at the inputs of PE(i, j). logic signed [7:0] a_h [N][N]; // operand from the left logic signed [7:0] b_v [N][N]; // operand from above logic [2:0] f_h [N][N]; // flags {last, first, valid}, travel with a logic signed [31:0] res [N][N]; // finished results logic done [N][N]; // PE captured its result on the last edge // Input skew: row i of A and column i of B are delayed by i cycles, so that // A[i][k] and B[k][j] meet in PE(i, j) on the same edge. for (genvar i = 0; i < N; i++) begin : g_skew if (i == 0) begin : g_direct assign a_h[0][0] = a_col[7:0]; assign b_v[0][0] = b_row[7:0]; assign f_h[0][0] = {in_last, in_first, in_valid}; end else begin : g_delay logic signed [7:0] a_d [i]; logic signed [7:0] b_d [i]; logic [2:0] f_d [i]; always_ff @(posedge clk) begin a_d[0] <= a_col[8*i +: 8]; b_d[0] <= b_row[8*i +: 8]; for (int s = 1; s < i; s++) begin a_d[s] <= a_d[s-1]; b_d[s] <= b_d[s-1]; end end always_ff @(posedge clk) begin if (rst) begin for (int s = 0; s < i; s++) f_d[s] <= '0; end else begin f_d[0] <= {in_last, in_first, in_valid}; for (int s = 1; s < i; s++) f_d[s] <= f_d[s-1]; end end assign a_h[i][0] = a_d[i-1]; assign b_v[0][i] = b_d[i-1]; assign f_h[i][0] = f_d[i-1]; end end // The PE grid. for (genvar i = 0; i < N; i++) begin : g_row for (genvar j = 0; j < N; j++) begin : g_col logic signed [15:0] prod; // INT8 x INT8 always fits in 16 bits logic signed [31:0] acc; logic signed [31:0] sum; assign prod = a_h[i][j] * b_v[i][j]; assign sum = (f_h[i][j][1] ? 32'sd0 : acc) + 32'(prod); // Data registers: no reset, the flags say when they hold valid data. always_ff @(posedge clk) begin if (f_h[i][j][0]) begin acc <= sum; if (f_h[i][j][2]) res[i][j] <= sum; end end always_ff @(posedge clk) begin if (rst) done[i][j] <= 1'b0; else done[i][j] <= f_h[i][j][0] && f_h[i][j][2]; end // Pass a and the flags to the right, b downwards. if (j < N-1) begin : g_right always_ff @(posedge clk) a_h[i][j+1] <= a_h[i][j]; always_ff @(posedge clk) begin if (rst) f_h[i][j+1] <= '0; else f_h[i][j+1] <= f_h[i][j]; end end if (i < N-1) begin : g_down always_ff @(posedge clk) b_v[i+1][j] <= b_v[i][j]; end assign c_out[32*(i*N+j) +: 32] = res[i][j]; end end // PE(N-1, N-1) is the last to finish a tile. assign out_valid = done[N-1][N-1]; endmodule