plru.vhdl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use ieee.math_real.all;
  5. entity plru is
  6. generic (
  7. BITS : positive := 2
  8. )
  9. ;
  10. port (
  11. clk : in std_ulogic;
  12. rst : in std_ulogic;
  13. acc : in std_ulogic_vector(BITS-1 downto 0);
  14. acc_en : in std_ulogic;
  15. lru : out std_ulogic_vector(BITS-1 downto 0)
  16. );
  17. end entity plru;
  18. architecture rtl of plru is
  19. constant count : positive := 2 ** BITS - 1;
  20. subtype node_t is integer range 0 to count;
  21. type tree_t is array(node_t) of std_ulogic;
  22. signal tree: tree_t;
  23. begin
  24. -- XXX Check if we can turn that into a little ROM instead that
  25. -- takes the tree bit vector and returns the LRU. See if it's better
  26. -- in term of FPGA resouces usage...
  27. get_lru: process(tree)
  28. variable node : node_t;
  29. begin
  30. node := 0;
  31. for i in 0 to BITS-1 loop
  32. -- report "GET: i:" & integer'image(i) & " node:" & integer'image(node) & " val:" & std_ulogic'image(tree(node));
  33. lru(BITS-1-i) <= tree(node);
  34. if i /= BITS-1 then
  35. node := node * 2;
  36. if tree(node) = '1' then
  37. node := node + 2;
  38. else
  39. node := node + 1;
  40. end if;
  41. end if;
  42. end loop;
  43. end process;
  44. update_lru: process(clk)
  45. variable node : node_t;
  46. variable abit : std_ulogic;
  47. begin
  48. if rising_edge(clk) then
  49. if rst = '1' then
  50. tree <= (others => '0');
  51. elsif acc_en = '1' then
  52. node := 0;
  53. for i in 0 to BITS-1 loop
  54. abit := acc(BITS-1-i);
  55. tree(node) <= not abit;
  56. -- report "UPD: i:" & integer'image(i) & " node:" & integer'image(node) & " val" & std_ulogic'image(not abit);
  57. if i /= BITS-1 then
  58. node := node * 2;
  59. if abit = '1' then
  60. node := node + 2;
  61. else
  62. node := node + 1;
  63. end if;
  64. end if;
  65. end loop;
  66. end if;
  67. end if;
  68. end process;
  69. end;