Advertisement
Guest User

Untitled

a guest
Nov 27th, 2023
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. //Matrix multipication: 3X3 , each elements is 8-bits wide
  2. //Upon exiting reset mode the module executes mat_c=mat_a X mat_b
  3.  
  4. module matrix_mul(clk,rstn,a,b,c,done);
  5. //Input declerations
  6. input logic clk;
  7. input logic rstn;
  8. input logic signed [71:0] a;
  9. input logic signed [71:0] b;
  10.  
  11. //Output declerations
  12. output logic done;
  13. output logic signed [71:0] c;
  14.  
  15. //Internal signals
  16. integer i,j,k; //Used in for loops
  17. logic first_cycle; //Indicates initiation of a multiplication procedure
  18. logic done; //Rises to logic high when result is ready to be transformed into 1D form
  19.  
  20. logic signed [7:0] mat_a [2:0][2:0]; //Internal 2D representation of the 72-bit input vector a
  21. logic signed [7:0] mat_b [2:0][2:0]; //Internal 2D representation of the 72-bit input vector b
  22. logic signed [7:0] mat_c [2:0][2:0]; //Internal 2D representation of the resulting matrix, i.e. 3X3 matrix with each cell comprising 8 bits
  23.  
  24. //HDL code
  25.  
  26. always @(posedge clk or negedge rstn)
  27. if (!rstn) begin
  28. first_cycle<=1'b1;
  29. done<=1'b0;
  30. for (i=0; i<3; i=i+1)
  31. for (j=0; j<3 ;j=j+1) begin
  32. mat_a[i][j]<='0;
  33. mat_b[i][j]<='0;
  34. mat_c[i][j]<='0;
  35. end
  36. end
  37. else if (first_cycle) begin //1D->2D
  38. for (i=0; i<3; i=i+1)
  39. for (j=0; j<3; j=j+1) begin
  40. mat_a[i][j]<=a[(3*i+j)*8:+8];
  41. mat_b[i][j]<=b[(3*i+j)*8:+8];
  42. mat_c[i][j]<='0;
  43. end
  44. first_cycle<=1'b0;
  45. end
  46. else if (!done) begin //Execute the matrix multipication
  47. for (i=0;i<3; i=i+1)
  48. for (j=0; j<3; j=j+1)
  49. for (k=0; k<3; k=k+1)
  50. mat_c[i][j]<=mat_c[i][j]+mat_a[i][k]*mat_b[k][j];
  51. done<=1'b1;
  52. end
  53. else if (done) begin //2D->1D
  54. for (i=0; i<3; i=i+1)
  55. for (j=0; j<3; j=j+1) begin
  56. c[(3*i+j)*8+:8]<=mat_c[i][j];
  57. end
  58. end
  59.  
  60. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement