Advertisement
1WaKa_WaKa1

lru

Apr 5th, 2023 (edited)
1,800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns/1ps
  2.  
  3. module lru (
  4.     input clk,
  5.     input reset,
  6.     input enable,
  7.     input [31:0] data,
  8.     output reg[31:0] out1,
  9.     output reg[31:0] out2,
  10.     output reg[31:0] out3,
  11.     output reg[31:0] out4,
  12.     output reg[31:0] out5,
  13.     output reg[31:0] out6,
  14.     output reg[31:0] out7,
  15.     output reg[31:0] out8          
  16. );
  17.  
  18. reg[2:0] state = 0;
  19. reg[3:0] ages[0:7];
  20. reg[3:0] max_age = 0, preempted_index = 8;
  21. reg[3:0] amount_empty_spaces = 8;
  22. reg[3:0] hit_cache_index = 8;
  23. reg is_value_in_cache = 0;
  24. reg is_value_setted_to_cache = 0;
  25.  
  26.  
  27. parameter INITIAL = 0, IDLE = 1, VALUE_IN_CACHE = 2, NO_VALUE_IN_CACHE_ENOUGH_SPACE = 3, NO_VALUE_IN_CACHE_NO_ENOUGH_SPACE = 4, DONE = 5;
  28. integer i;
  29.  
  30. always @(posedge reset) begin
  31.     if (reset) begin
  32.         out1 <= 32'hffffffff;
  33.         out2 <= 32'hffffffff;
  34.         out3 <= 32'hffffffff;
  35.         out4 <= 32'hffffffff;
  36.         out5 <= 32'hffffffff;
  37.         out6 <= 32'hffffffff;
  38.         out7 <= 32'hffffffff;
  39.         out8 <= 32'hffffffff;
  40.         for (i = 0; i < 8; i = i + 1) begin
  41.             ages[i] <= 8;
  42.         end
  43.         state <= IDLE;
  44.     end
  45. end
  46.  
  47. always @(posedge clk) begin
  48.     case (state)
  49.     INITIAL: begin
  50.         out1 <= 32'hffffffff;
  51.         out2 <= 32'hffffffff;
  52.         out3 <= 32'hffffffff;
  53.         out4 <= 32'hffffffff;
  54.         out5 <= 32'hffffffff;
  55.         out6 <= 32'hffffffff;
  56.         out7 <= 32'hffffffff;
  57.         out8 <= 32'hffffffff;
  58.         for (i = 0; i < 8; i = i + 1) begin
  59.             ages[i] <= 8;
  60.         end
  61.         state <= IDLE;
  62.     end
  63.     IDLE : begin
  64.         if (enable) begin
  65.             if (out1 == data) begin hit_cache_index = 0; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  66.             if (out2 == data) begin hit_cache_index = 1; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  67.             if (out3 == data) begin hit_cache_index = 2; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  68.             if (out4 == data) begin hit_cache_index = 3; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  69.             if (out5 == data) begin hit_cache_index = 4; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  70.             if (out6 == data) begin hit_cache_index = 5; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  71.             if (out7 == data) begin hit_cache_index = 6; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  72.             if (out8 == data) begin hit_cache_index = 7; state <= VALUE_IN_CACHE; is_value_in_cache = 1; end
  73.            
  74.             if (amount_empty_spaces && is_value_in_cache == 0) state <= NO_VALUE_IN_CACHE_ENOUGH_SPACE;
  75.             else state <= NO_VALUE_IN_CACHE_NO_ENOUGH_SPACE;
  76.            
  77.             is_value_in_cache = 0;    
  78.         end
  79.     end
  80.     VALUE_IN_CACHE : begin
  81.         for (i = 0; i < 8; i = i + 1) begin
  82.             if (i == hit_cache_index) begin
  83.                 ages[i] <= 0;
  84.             end else if (ages[i] != 8) begin
  85.                 ages[i] <= ages[i] + 1;
  86.             end
  87.         end
  88.         hit_cache_index <= 8;
  89.         state <= DONE;
  90.     end
  91.     NO_VALUE_IN_CACHE_ENOUGH_SPACE : begin
  92.         for (i = 0; i < 8; i = i + 1) begin
  93.             if (ages[i] != 8) begin
  94.                 ages[i] <= ages[i] + 1;
  95.             end
  96.         end
  97.         for (i = 0; i < 8; i = i + 1) begin
  98.             if (ages[i] >= 8 && is_value_setted_to_cache != 1) begin
  99.                 ages[i] <= 0;
  100.                 case (i)
  101.                     0: begin out1 <= data; is_value_setted_to_cache = 1; end
  102.                     1: begin out2 <= data; is_value_setted_to_cache = 1; end
  103.                     2: begin out3 <= data; is_value_setted_to_cache = 1; end
  104.                     3: begin out4 <= data; is_value_setted_to_cache = 1; end
  105.                     4: begin out5 <= data; is_value_setted_to_cache = 1; end
  106.                     5: begin out6 <= data; is_value_setted_to_cache = 1; end
  107.                     6: begin out7 <= data; is_value_setted_to_cache = 1; end
  108.                     7: begin out8 <= data; is_value_setted_to_cache = 1; end
  109.                 endcase
  110.             end
  111.         end
  112.         is_value_setted_to_cache = 0;
  113.         amount_empty_spaces <= amount_empty_spaces - 1;
  114.         state <= DONE;
  115.     end
  116.     NO_VALUE_IN_CACHE_NO_ENOUGH_SPACE : begin
  117.         for (i = 0; i < 8; i = i + 1) begin
  118.             if (ages[i] != 8) begin
  119.                 ages[i] <= ages[i] + 1;
  120.             end
  121.         end
  122.         $display("ages = [%d, %d, %d, %d, %d, %d, %d, %d]", ages[0], ages[1], ages[2], ages[3], ages[4], ages[5], ages[6], ages[7]);
  123.         $display("max_age: %d", max_age);
  124.         for (i = 0; i < 8; i = i + 1) begin
  125.                 if (ages[i] > max_age) begin
  126.                     max_age = ages[i];
  127.                     preempted_index = i;
  128.                 end
  129.         end
  130.         $display("max_age: %d", max_age);
  131.         $display("preempted_index: %d", preempted_index);
  132.         case (preempted_index)  
  133.             0: begin out1 <= data; end
  134.             1: begin out2 <= data; end
  135.             2: begin out3 <= data; end
  136.             3: begin out4 <= data; end
  137.             4: begin out5 <= data; end
  138.             5: begin out6 <= data; end
  139.             6: begin out7 <= data; end
  140.             7: begin out8 <= data; end
  141.         endcase
  142.         ages[preempted_index] <= 0;
  143.         preempted_index <= 8;
  144.         max_age <= 0;
  145.         state <= DONE;
  146.     end
  147.     DONE : begin
  148.         state <= IDLE;
  149.     end
  150.     endcase
  151. end
  152.  
  153. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement