generic_prescaler.vhd 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. entity generic_prescaler is
  5. generic (
  6. prescaler_bits : integer := 8
  7. );
  8. port
  9. (
  10. clk : in std_logic;
  11. rst : in std_logic;
  12. en : in std_logic;
  13. load : in std_logic;
  14. prescaler_value : in unsigned (prescaler_bits - 1 downto 0);
  15. en_out : out std_logic
  16. );
  17. end entity;
  18. architecture beh of generic_prescaler is
  19. component generic_counter is
  20. generic (
  21. counter_bits : integer
  22. );
  23. port(
  24. clk : in std_logic;
  25. rst : in std_logic;
  26. en : in std_logic;
  27. cnt : out unsigned (counter_bits -1 downto 0)
  28. );
  29. end component;
  30. signal i_match_reg : unsigned (prescaler_bits -1 downto 0);
  31. signal i_counter_val : unsigned (prescaler_bits -1 downto 0);
  32. signal i_match : std_logic;
  33. signal i_match_sync : std_logic := '0';
  34. signal i_reset_counter : std_logic;
  35. -- Signals for the edge detector and output
  36. signal i_match_last : std_logic := '0';
  37. signal i_en_out : std_logic;
  38. signal i_en_out_sync : std_logic := '0';
  39. begin
  40. en_out <= i_en_out;
  41. -- unbuffered match register
  42. i_match_reg <= prescaler_value - 1;
  43. i_match <= '1' when i_match_reg = i_counter_val
  44. else '0';
  45. i_en_out <= i_match_sync and not i_match_last;
  46. i_reset_counter <= (i_match and en) or rst;
  47. i_cntr : generic_counter
  48. generic map(
  49. counter_bits => prescaler_bits
  50. )
  51. port map(
  52. clk => clk,
  53. rst => i_reset_counter,
  54. en => en,
  55. cnt => i_counter_val
  56. );
  57. -- Positive edge detector
  58. process(clk) is
  59. begin
  60. if rising_edge(clk) then
  61. if rst = '1' then
  62. i_match_last <= '0';
  63. i_en_out_sync <= '0';
  64. else
  65. i_match_last <= i_match_sync;
  66. i_en_out_sync <= i_en_out;
  67. end if;
  68. end if;
  69. end process;
  70. -- Sync registers
  71. process(clk) is
  72. begin
  73. if rising_edge(clk) then
  74. if rst = '1' then
  75. i_match_sync <= '0';
  76. -- Buffered match register
  77. -- if load = '1' then
  78. -- i_match_reg <= prescaler_value - 1;
  79. -- end if;
  80. elsif en = '1' then
  81. i_match_sync <= i_match;
  82. end if;
  83. end if;
  84. end process;
  85. end architecture;