~ruther/vhdl-i2c

ref: 7118535deb1f07f92313e19e47e34c31f2e2ed92 vhdl-i2c/src/utils/counter.vhd -rw-r--r-- 1.4 KiB
7118535d — Rutherther chore: add libs folder with vunit ignored 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
library ieee;
use ieee.std_logic_1164.all;

entity counter is

  generic (
    MAX      : natural := 9;
    DIGITS   : natural;
    IN_FREQ  : natural;
    OUT_FREQ : natural);

  port (
    clk_i   : in  std_logic;
    rst_in  : in  std_logic;
    value_o : out std_logic_vector(DIGITS * 4 - 1 downto 0));

end entity counter;

architecture a1 of counter is
  constant MAX_COUNT : natural := IN_FREQ / OUT_FREQ;
  signal curr_count : integer range 0 to MAX_COUNT - 1;
  signal next_count : integer range 0 to MAX_COUNT - 1;

  signal count_clk : std_logic;

  signal carries : std_logic_vector(DIGITS downto 0);
  signal value : std_logic_vector(DIGITS * 4 - 1 downto 0);
begin  -- architecture a1
  value_o <= value;

  next_count <= curr_count - 1 when curr_count > 0 else MAX_COUNT - 1;

  carries(0) <= '1' when curr_count = 0 else '0';

  set_regs: process(clk_i) is
  begin
    if rising_edge(clk_i) then
      if rst_in = '0' then
        curr_count <= MAX_COUNT - 1;
      else
	curr_count <= next_count;
      end if;
    end if;
  end process set_regs;

  counters: for i in 0 to DIGITS - 1 generate
    bcd_counter: entity work.bcd_counter
      generic map (
        MAX => MAX)
      port map (
        clk_i   => clk_i,
        rst_in  => rst_in,
        carry_i => carries(i),
        carry_o => carries(i + 1),
        count_o => value((i + 1) * 4 - 1 downto i * 4));
  end generate counters;

end architecture a1;
Do not follow this link