aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/gna/issue1908/sr_synchronizer.vhdl
blob: d0afc900fe48a01c48faabea42fcd8365705956c (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-----------------------------------------------------------------------------------
-- * Libs
-----------------------------------------------------------------------------------
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.NUMERIC_STD.ALL;

-----------------------------------------------------------------------------------
-- * Entity
-----------------------------------------------------------------------------------
ENTITY sr_synchronizer IS
  GENERIC (
    G_INIT       : std_logic := '0';   -- Reset value
    G_STAGES     : natural   := 2      -- Number of synchronization stages
  );
  PORT (
     CLK_I       : in   std_logic;
     RSTN_I      : in   std_logic;

     X_I         : in   std_logic;
     X_SYNC_O    : out  std_logic
  );
END sr_synchronizer;

-----------------------------------------------------------------------------------
-- * Architecture Begins
-----------------------------------------------------------------------------------
ARCHITECTURE synthetizable OF sr_synchronizer IS
  -----------------------------------------------------------------------------------
  -- * Types
  -----------------------------------------------------------------------------------
  type shift_regsiter_t is array (G_STAGES-1 downto 0) of std_logic;

  -----------------------------------------------------------------------------------
  -- * Signals
  -----------------------------------------------------------------------------------
  signal r_rst_cnt    : natural range 0 to G_STAGES;  -- To reset the shift-register stages
  signal r_x_sync_o   : shift_regsiter_t;             -- Shift register
  signal s_x_i        : std_logic;

-----------------------------------------------------------------------------------
-- * Architecture synthetizable
-----------------------------------------------------------------------------------
BEGIN
  -----------------------------------------------------------------------------------
  -- * Generics Constraints Checking
  -----------------------------------------------------------------------------------
  assert(G_INIT='0' or G_INIT='1')
    report "-ERROR- In 'sr_synchronizer' component, the generic parameter 'G_INIT' is bad defined." &
      " Must be '1' or '0' of std_logic type." severity failure;

  assert(G_STAGES > 0)
    report "-ERROR- In 'sr_synchronizer' component, the generic parameter 'G_STAGES' is bad defined." &
      " Must be 1 at least." severity failure;

  -----------------------------------------------------------------------------------
  -- * Mapping IO
  -----------------------------------------------------------------------------------
  X_SYNC_O  <= r_x_sync_o(r_x_sync_o'high);
  s_x_i     <= X_I;

  -----------------------------------------------------------------------------------
  -- * Process
  -----------------------------------------------------------------------------------
    -- ** Shift Register
    -----------------------------------------------------------------------------------
    shift_register: process(CLK_I)
    begin
      if rising_edge(CLK_I) then
        if RSTN_I='0' then
          r_rst_cnt <= G_STAGES;                                                  -- Reset value of reset counter
        else
          if r_rst_cnt=0 then                                                     -- In case all shift register stages has performed
            r_x_sync_o <=                                                         -- reset, change shift register input to 's_x_i'
              r_x_sync_o(r_x_sync_o'high-1 downto r_x_sync_o'low) & s_x_i;        -- value
          else
            r_rst_cnt  <= r_rst_cnt-1;                                            -- In case not all shift register stages has performed
            r_x_sync_o <=                                                         -- reset, change shift register input to 'G_INIT'
              r_x_sync_o(r_x_sync_o'high-1 downto r_x_sync_o'low) & G_INIT;       -- value and, decrement reset counter value
          end if;
       end if; -- RSTN_I
      end if; -- rising_edge(CLK_I)
    end process;

-----------------------------------------------------------------------------------
-- * Architecture Ends
-----------------------------------------------------------------------------------
END synthetizable;