Belle II KLM Scint Firmware  1
mem.vhd
1 ------------------------------------------------------------------------
2 ---- SIMPLE DUAL PORT BRAM WITH COMMON CLOCK ---------------------------
3 ------------------------------------------------------------------------
4 library ieee;
5 use ieee.std_logic_1164.all;
6 use ieee.std_logic_unsigned.all;
7 
8 entity bram_sdp_cc is
9 generic (
10  DATA : integer := 16;
11  ADDR : integer := 10
12 );
13 port (
14  -- Port A
15  clk : in std_logic;
16  wea : in std_logic;
17  addra : in std_logic_vector(ADDR-1 downto 0);
18  dina : in std_logic_vector(DATA-1 downto 0);
19  -- Port B
20  addrb : in std_logic_vector(ADDR-1 downto 0);
21  doutb : out std_logic_vector(DATA-1 downto 0)
22 );
23 end bram_sdp_cc;
24 
25 architecture read_first of bram_sdp_cc is
26  -- Shared memory
27  type mem_type is array ( (2**ADDR)-1 downto 0 ) of std_logic_vector(DATA-1 downto 0);
28  signal mem : mem_type := (others => (others => '0'));
29 begin
30 
31 process(clk)
32 begin
33  if(clk'event and clk='1') then
34  if(wea='1') then
35  mem(conv_integer(addra)) <= dina;
36  end if;
37  doutb <= mem(conv_integer(addrb));
38  end if;
39 end process;
40 
41 end read_first;
42 ------------------------------------------------------------------------
43 
44 ------------------------------------------------------------------------
45 ---- SIMPLE DUAL PORT BRAM ---------------------------------------------
46 ------------------------------------------------------------------------
47 library ieee;
48 use ieee.std_logic_1164.all;
49 use ieee.std_logic_unsigned.all;
50 
51 entity bram_sdp is
52 generic (
53  DATA : integer := 16;
54  ADDR : integer := 10
55 );
56 port (
57  -- Port A
58  clka : in std_logic;
59  wea : in std_logic;
60  addra : in std_logic_vector(ADDR-1 downto 0);
61  dina : in std_logic_vector(DATA-1 downto 0);
62  -- Port B
63  clkb : in std_logic;
64  addrb : in std_logic_vector(ADDR-1 downto 0);
65  doutb : out std_logic_vector(DATA-1 downto 0)
66 );
67 end bram_sdp;
68 
69 architecture rtl of bram_sdp is
70  -- Shared memory
71  type mem_type is array ( (2**ADDR)-1 downto 0 ) of std_logic_vector(DATA-1 downto 0);
72  signal mem : mem_type := (others => (others => '0'));
73 begin
74 
75 process(clka)
76 begin
77  if(clka'event and clka='1') then
78  if(wea='1') then
79  mem(conv_integer(addra)) <= dina;
80  end if;
81  end if;
82 end process;
83 
84 process(clkb)
85 begin
86  if(clkb'event and clkb='1') then
87  doutb <= mem(conv_integer(addrb));
88  end if;
89 end process;
90 
91 end rtl;
92 ------------------------------------------------------------------------
93 
94 
95 
96 ------------------------------------------------------------------------
97 ---- FIFO with common clock
98 ------------------------------------------------------------------------
99 library ieee;
100 use ieee.std_logic_1164.all;
101 use ieee.std_logic_unsigned.all;
102 
103 entity fifo_cc is
104 generic(
105  DATA_WIDTH : natural := 16;
106  DEPTH : natural := 5
107 );
108 
109 port(
110  clk : in std_logic;
111  rst : in std_logic;
112  din : in std_logic_vector(DATA_WIDTH-1 downto 0);
113  wen : in std_logic;
114  ren : in std_logic;
115  dout : out std_logic_vector(DATA_WIDTH-1 downto 0);
116  full : out std_logic;
117  empty : out std_logic
118 );
119 end fifo_cc;
120 
121 architecture fifo_cc_arch of fifo_cc is
122 
123  signal i_ren : std_logic;
124  signal i_full : std_logic;
125  signal i_empty : std_logic;
126  signal i_empty_r : std_logic;
127 
128  signal i_waddr : std_logic_vector(DEPTH-1 downto 0) := (others => '0');
129  signal i_raddr : std_logic_vector(DEPTH-1 downto 0) := (others => '0');
130  signal i_cnt : std_logic_vector(DEPTH-1 downto 0) := (others => '0');
131 
132  constant ZERO_ADDR : std_logic_vector(DEPTH-1 downto 0) := (others =>'0');
133  constant MAX_ADDR : std_logic_vector(DEPTH-1 downto 0) := (others =>'1');
134 
135 begin
136 
137  ---- instantiate BRAM with FIFO content
138  bram_i : entity work.bram_sdp_cc
139  generic map(
140  DATA => DATA_WIDTH,
141  ADDR => DEPTH
142  )
143  port map (
144  clk => clk,
145  wea => wen,
146  addra => i_waddr,
147  dina => din,
148  addrb => i_raddr,
149  doutb => dout
150  );
151  ----
152 
153  ---- generate full and empty signals
154  i_full <= '1' when i_cnt = MAX_ADDR else '0';
155  i_empty <= '1' when i_cnt = ZERO_ADDR else '0';
156 
157  full <= i_full;
158 
159  empty <= i_empty or i_empty_r;
160 
161  process(clk)
162  begin
163  if rising_edge(clk) then
164  i_empty_r <= i_empty;
165  end if;
166  end process;
167  ----
168 
169  i_ren <= ren;
170 
171  ---- count number of words in FIFO
172  FIFO_CNT_PROC : process(clk)
173  begin
174  if rising_edge(clk) then
175  if rst = '1' then
176  i_cnt <= (others => '0');
177  else
178  if wen = '1' and i_ren = '0' and i_full = '0' then
179  i_cnt <= i_cnt + '1';
180  elsif wen = '0' and i_ren = '1' and i_empty = '0' then
181  i_cnt <= i_cnt - '1';
182  end if;
183  end if;
184  end if;
185  end process FIFO_CNT_PROC;
186  ----
187 
188  ---- manage read/write addresses for BRAM
189  RW_ADDR_PROC : process(clk)
190  begin
191  if rising_edge(clk) then
192  if rst = '1' then
193  i_waddr <= (others => '0');
194  i_raddr <= (others => '0');
195  else
196  if (wen = '1' and i_full = '0') then
197  if i_waddr = MAX_ADDR then
198  i_waddr <= (others => '0');
199  else
200  i_waddr <= i_waddr + 1;
201  end if;
202  end if;
203 
204  if (i_ren = '1' and i_empty = '0') then
205  if i_raddr = MAX_ADDR then
206  i_raddr <= (others => '0');
207  else
208  i_raddr <= i_raddr + 1;
209  end if;
210  end if;
211 
212  end if;
213  end if;
214  end process RW_ADDR_PROC;
215  ----
216 
217  -- synthesis translate_off
218  --process (clk) is
219  --begin
220  --if rising_edge(clk) then
221  --if wen = '1' and i_full = '1' then
222  --report "ERROR:: trying to write full FIFO" severity failure;
223  --end if;
224 
225  --if ren = '1' and i_empty = '1' then
226  --report "ERROR:: trying to read empty FIFO" severity failure;
227  --end if;
228  --end if;
229  --end process;
230  -- synthesis translate_on
231 
232 end fifo_cc_arch;
233 ------------------------------------------------------------------------
234 
235 
236 
Definition: mem.vhd:103
Definition: mem.vhd:51