-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3X3 Matrix Multiplication
More file actions
40 lines (33 loc) · 1.08 KB
/
Copy path3X3 Matrix Multiplication
File metadata and controls
40 lines (33 loc) · 1.08 KB
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
module matrix3x3(a, b, y);
input [8:0] a, b; // 8x8 input matrices (1-bit elements)
output reg [17:0] y; // 8x8 output matrix (2-bit elements)
reg [2:0] a1[0:2][0:2];
reg [2:0] b1[0:2][0:2];
reg [2:0] y1[0:2][0:2];
integer i, j, k;
always @(a or b)
begin
// Unpack the 1-bit input matrices a and b into 3-bit matrices a1 and b1
for (i = 0; i < 3; i=i+1) begin
for (j = 0; j < 3; j=j+1) begin
a1[i][j] = a[i*3 + j];
b1[i][j] = b[i*3 + j];
end
end
// Matrix multiplication
for (i = 0; i < 3; i=i+1) begin
for (j = 0; j < 3; j=j+1) begin
y1[i][j] = 3'b000; // Initialize each element in the output matrix to 0
for (k = 0; k < 3; k=k+1) begin
y1[i][j] = y1[i][j] + (a1[i][k] * b1[k][j]);
end
end
end
// Pack the 2-bit output matrix y1 into the 128-bit output y
for (i = 0; i < 3; i=i+1) begin
for (j = 0; j < 3; j=j+1) begin
y[i*6+ j*2 +: 2] = y1[i][j];
end
end
end
endmodule