Belle II KLM Scint Firmware  1
clk_div.vhd
1 ----------------------------------------------------------------------------------
2 --
3 ----------------------------------------------------------------------------------
4 library IEEE;
5 use IEEE.STD_LOGIC_1164.all;
6  use ieee.numeric_std.all;
7  use ieee.std_logic_unsigned.all;
8 library UNISIM;
9  use UNISIM.vcomponents.all;
10 
11 entity clk_div is
12  generic (
13  RATIO : integer := 40
14  );
15  port (
16  clk : in std_logic;
17  rst : in std_logic;
18  strb : in std_logic;
19 
20  clkdiv : out std_logic;
21  hb : out std_logic;
22  sync_strb : out std_logic
23  );
24 end clk_div;
25 
26 architecture beh of clk_div is
27  -- constant half_period : std_logic_vector(31 downto 0) := std_logic_vector(to_unsigned(RATIO/2 - 1, 32));
28  -- constant full_period : std_logic_vector(31 downto 0) := std_logic_vector(to_unsigned(RATIO - 1, 32));
29  -- constant zero_cnt : std_logic_vector(31 downto 0) := (others => '0');
30  constant half_period : integer := RATIO/2 - 1;
31  constant full_period : integer := RATIO - 1;
32  -- signal i_cnt : std_logic_vector(31 downto 0) := (others => '0');
33  signal i_clkdiv : std_logic := '0';
34 
35  -- signal i_hb_cnt : std_logic_vector(31 downto 0) := (others => '0');
36  signal i_hb_cnt : integer range 0 to RATIO - 1 := 0;
37  signal i_hb : std_logic;
38 
39  -- signal i_strb_cnt : std_logic_vector(31 downto 0) := (others => '0');
40  signal i_strb : std_logic := '0';
41  signal i_strb_e : std_logic := '0';
42 begin
43 
44  CLK_PROC : process(clk)
45  variable i_cnt : integer range 0 to half_period := 0;
46  begin
47  if rising_edge(clk) then
48  if rst = '1' then
49  i_cnt := 0;
50  i_clkdiv <= '0';
51  else
52  if i_cnt = half_period then
53  i_cnt := 0;
54  i_clkdiv <= not i_clkdiv;
55  else
56  i_cnt := i_cnt + 1;
57  end if;
58  end if;
59  end if;
60  end process CLK_PROC;
61 
62  clkdiv <= i_clkdiv;
63 
64  HB_PROC : process(clk)
65  begin
66  if rising_edge(clk) then
67  i_hb <= '0';
68  i_hb_cnt <= i_hb_cnt + 1;
69  if i_hb_cnt = half_period then
70  i_hb <= '1';
71  end if;
72  if i_hb_cnt = full_period then
73  i_hb_cnt <= 0;
74  end if;
75  end if;
76  end process;
77 
78  hb <= i_hb;
79 
80  STRB_PROC : process(clk, strb, i_hb_cnt)
81  begin
82  if rising_edge(clk) then
83 
84  if strb = '1' then
85  i_strb_e <= '1';
86  end if;
87 
88  if i_strb_e = '1' and i_hb_cnt = half_period then
89  i_strb <= '1';
90  i_strb_e <= '0';
91  else
92  i_strb <= '0';
93  end if;
94 
95  end if;
96  end process STRB_PROC;
97 
98  sync_strb <= i_strb;
99 
100 end beh;
101 
102