Advertisement
AlexanderAntonov

Untitled

Nov 5th, 2022
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module FindMaxVal_pipelined (
  2.     input clk_i
  3.     , input rst_i
  4.    
  5.     , input [31:0] elem_bi [15:0]
  6.    
  7.     , output logic [31:0] max_elem_bo
  8.     , output logic [3:0] max_index_bo
  9. );
  10.  
  11. //// stage 0 ////
  12.  
  13. // intermediate signals declaration
  14. logic [31:0] max_elem_stage0  [7:0];
  15. logic [31:0] max_index_stage0 [7:0];
  16. logic [31:0] max_elem_stage0_next  [7:0];
  17. logic [31:0] max_index_stage0_next [7:0];
  18.  
  19. // combinational logic
  20. always @*
  21.     begin
  22.     for(integer i=0; i<8; i++)
  23.         begin
  24.         max_elem_stage0_next[i] = 0;
  25.         max_index_stage0_next[i] = 0;
  26.         if (elem_bi[(i<<1)] > elem_bi[(i<<1)+1])
  27.             begin
  28.             max_elem_stage0_next[i] = elem_bi[(i<<1)];
  29.             max_index_stage0_next[i] = i<<1;
  30.             end
  31.         else
  32.             begin
  33.             max_elem_stage0_next[i] = elem_bi[(i<<1)+1];
  34.             max_index_stage0_next[i] = (i<<1)+1;
  35.             end
  36.        end
  37.     end
  38.  
  39. // writing to registers
  40. always @(posedge clk_i)
  41.     begin
  42.     if (rst_i)
  43.         begin
  44.         for (integer i=0; i<8; i++) max_elem_stage0[i] <= 0;
  45.         for (integer i=0; i<8; i++) max_index_stage0[i] <= 0;
  46.         end
  47.     else
  48.         begin
  49.         for (integer i=0; i<8; i++) max_elem_stage0[i] <= max_elem_stage0_next[i];
  50.         for (integer i=0; i<8; i++) max_index_stage0[i] <= max_index_stage0_next[i];
  51.         end
  52.     end
  53.  
  54. //// stage 1 ////
  55.  
  56. // intermediate signals declaration
  57. logic [31:0] max_elem_stage1  [3:0];
  58. logic [31:0] max_index_stage1 [3:0];
  59. logic [31:0] max_elem_stage1_next  [3:0];
  60. logic [31:0] max_index_stage1_next [3:0];
  61.  
  62. // combinational logic
  63. always @*
  64.     begin
  65.     for(integer i=0; i<4; i++)
  66.         begin
  67.         max_elem_stage1_next[i] = 0;
  68.         max_index_stage1_next[i] = 0;
  69.         if (max_elem_stage0[(i<<1)] > max_elem_stage0[(i<<1)+1])
  70.             begin
  71.             max_elem_stage1_next[i] = max_elem_stage0[(i<<1)];
  72.             max_index_stage1_next[i] = max_index_stage0[(i<<1)];
  73.             end
  74.         else
  75.             begin
  76.             max_elem_stage1_next[i] = max_elem_stage0[(i<<1)+1];
  77.             max_index_stage1_next[i] = max_index_stage0[(i<<1)+1];
  78.             end
  79.         end
  80.     end
  81.  
  82. // writing to registers
  83. always @(posedge clk_i)
  84.     begin
  85.     if (rst_i)
  86.         begin
  87.         for (integer i=0; i<4; i++) max_elem_stage1[i] <= 0;
  88.         for (integer i=0; i<4; i++) max_index_stage1[i] <= 0;
  89.         end
  90.     else
  91.         begin
  92.         for (integer i=0; i<4; i++) max_elem_stage1[i] <= max_elem_stage1_next[i];
  93.         for (integer i=0; i<4; i++) max_index_stage1[i] <= max_index_stage1_next[i];
  94.         end
  95.     end
  96.  
  97. //// stage 2 ////
  98.  
  99. // intermediate signals declaration
  100. logic [31:0] max_elem_stage2  [1:0];
  101. logic [31:0] max_index_stage2 [1:0];
  102. logic [31:0] max_elem_stage2_next  [1:0];
  103. logic [31:0] max_index_stage2_next [1:0];
  104.  
  105. // combinational logic
  106. always @*
  107.     begin
  108.     for(integer i=0; i<2; i++)
  109.         begin
  110.         max_elem_stage2_next[i] = 0;
  111.         max_index_stage2_next[i] = 0;
  112.         if (max_elem_stage1[(i<<1)] > max_elem_stage1[(i<<1)+1])
  113.             begin
  114.             max_elem_stage2_next[i] = max_elem_stage1[(i<<1)];
  115.             max_index_stage2_next[i] = max_index_stage1[(i<<1)];
  116.             end
  117.         else
  118.             begin
  119.             max_elem_stage2_next[i] = max_elem_stage1[(i<<1)+1];
  120.             max_index_stage2_next[i] = max_index_stage1[(i<<1)+1];
  121.             end
  122.         end
  123.     end
  124.  
  125. // writing to registers
  126. always @(posedge clk_i)
  127.     begin
  128.     if (rst_i)
  129.         begin
  130.         for (integer i=0; i<2; i++) max_elem_stage2[i] <= 0;
  131.         for (integer i=0; i<2; i++) max_index_stage2[i] <= 0;
  132.         end
  133.     else
  134.         begin
  135.         for (integer i=0; i<2; i++) max_elem_stage2[i] <= max_elem_stage2_next[i];
  136.         for (integer i=0; i<2; i++) max_index_stage2[i] <= max_index_stage2_next[i];
  137.         end
  138.     end
  139.  
  140. //// stage 3 ////
  141.  
  142. // intermediate signals declaration
  143. logic [31:0] max_elem_next;
  144. logic [3:0] max_index_next;
  145.  
  146. // combinational logic
  147. always @*
  148.     begin
  149.     max_elem_next = 0;
  150.     max_index_next = 0;
  151.     if (max_elem_stage2[0] > max_elem_stage2[1])
  152.         begin
  153.         max_elem_next = max_elem_stage2[0];
  154.         max_index_next = max_index_stage2[0];
  155.         end
  156.     else
  157.         begin
  158.         max_elem_next = max_elem_stage2[1];
  159.         max_index_next = max_index_stage2[1];
  160.         end
  161.     end
  162.  
  163. // writing to registers
  164. always @(posedge clk_i)
  165.     begin
  166.     if (rst_i)
  167.         begin
  168.         max_elem_bo <= 0;
  169.         max_index_bo <= 0;
  170.         end
  171.     else
  172.         begin
  173.         max_elem_bo <= max_elem_next;
  174.         max_index_bo <= max_index_next;
  175.         end
  176.     end
  177.  
  178. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement