generic_prescaler.vhd 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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;
  34. signal i_reset_counter : std_logic;
  35. -- Signals for the edge detector and output
  36. signal i_match_last : std_logic;
  37. signal i_en_out : std_logic;
  38. signal i_en_out_sync : std_logic;
  39. begin
  40. en_out <= i_en_out;
  41. i_match <= '1' when i_match_reg = i_counter_val
  42. else '0';
  43. i_en_out <= i_match_sync and not i_match_last;
  44. i_reset_counter <= (i_match and en) or rst;
  45. i_cntr : generic_counter
  46. generic map(
  47. counter_bits => prescaler_bits
  48. )
  49. port map(
  50. clk => clk,
  51. rst => i_reset_counter,
  52. en => en,
  53. cnt => i_counter_val
  54. );
  55. -- Positive edge detector
  56. process(clk) is
  57. begin
  58. if rising_edge(clk) then
  59. if rst = '1' then
  60. i_match_last <= '0';
  61. i_en_out_sync <= '0';
  62. if load = '1' then
  63. i_match_reg <= prescaler_value - 1;
  64. end if;
  65. else
  66. i_match_last <= i_match_sync;
  67. i_en_out_sync <= i_en_out;
  68. end if;
  69. end if;
  70. end process;
  71. -- Output sync
  72. process(clk) is
  73. begin
  74. if rising_edge(clk) then
  75. if rst = '1' then
  76. i_match_sync <= '0';
  77. elsif en = '1' then
  78. i_match_sync <= i_match;
  79. end if;
  80. end if;
  81. end process;
  82. end architecture;