c2b040193a777c09bdc595c95492b57a2521db87
Stefan Schuermans added file headers

Stefan Schuermans authored 12 years ago

1) -- MIPS I system
2) -- Copyright 2011-2012 Stefan Schuermans <stefan@schuermans.info>
3) -- Copyleft GNU public license V2 or later
4) --          http://www.gnu.org/copyleft/gpl.html
5) 
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

6) LIBRARY IEEE;
7) USE IEEE.STD_LOGIC_1164.ALL;
8) USE IEEE.NUMERIC_STD.ALL;
9) 
10) ENTITY e_block_fifo IS
11)     GENERIC (
12)         addr_width: natural;
13)         data_width: natural
14)     );
15)     PORT (
16)         rst:       IN  std_logic;
17)         clk:       IN  std_logic;
18)         o_wr_rdy:  OUT std_logic;
19)         i_wr_data: IN  std_logic_vector(data_width - 1 DOWNTO 0);
20)         i_wr_en:   IN  std_logic;
21)         o_rd_rdy:  OUT std_logic;
22)         o_rd_data: OUT std_logic_vector(data_width - 1 DOWNTO 0);
23)         i_rd_en:   IN  std_logic
24)     );
25) END ENTITY e_block_fifo;
26) 
27) ARCHITECTURE a_block_fifo OF e_block_fifo IS
28) 
29)     COMPONENT e_block_rwram
30)         GENERIC (
31)             addr_width: natural;
32)             data_width: natural := 8
33)         );
34)         PORT (
35)             clk:       IN  std_logic;
36)             i_rd_addr: IN  std_logic_vector(addr_width - 1 DOWNTO 0);
37)             o_rd_data: OUT std_logic_vector(data_width - 1 DOWNTO 0);
38)             i_wr_addr: IN  std_logic_vector(addr_width - 1 DOWNTO 0);
39)             i_wr_data: IN  std_logic_vector(data_width - 1 DOWNTO 0);
40)             i_wr_en:   IN  std_logic
41)         );
42)     END COMPONENT e_block_rwram;
43) 
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

44)     SUBTYPE t_pos  IS natural RANGE 0 TO 2 ** addr_width - 1;
45)     SUBTYPE t_addr IS std_logic_vector(addr_width - 1 DOWNTO 0);
46)     SUBTYPE t_data IS std_logic_vector(data_width - 1 DOWNTO 0);
47) 
48)     SIGNAL r_begin:         t_pos     := 0;
49)     SIGNAL n_begin:         t_pos;
50)     SIGNAL s_begin_addr:    t_addr;
51)     SIGNAL s_begin_sn_addr: t_addr; -- _sn_ means sometimes next
52)     SIGNAL s_rd_rdy:        std_logic;
53)     SIGNAL s_rd_en:         std_logic;
54) 
55)     SIGNAL r_end:            t_pos     := 0;
56)     SIGNAL n_end:            t_pos;
57)     SIGNAL s_end_addr:       t_addr;
58)     SIGNAL r_end_addr_delay: t_addr := (OTHERS => '0');
59)     SIGNAL s_end_an_addr:    t_addr; -- _an_ means always next
60)     SIGNAL s_wr_rdy:         std_logic;
61)     SIGNAL s_wr_en:          std_logic;
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

62) 
63)     FUNCTION next_pos (
64)         pos: natural RANGE 0 TO 2 ** addr_width - 1
65)     ) RETURN natural IS
66)         VARIABLE v_next: natural RANGE 0 TO 2 ** addr_width - 1;
67)     BEGIN
68)         IF pos = 2 ** addr_width - 1 THEN
69)             v_next := 0;
70)         ELSE
71)             v_next := pos + 1;
72)         END IF;
73)         RETURN v_next;
74)     END FUNCTION next_pos;
75) 
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

76)     FUNCTION to_addr (
77)         pos: natural RANGE 0 TO 2 ** addr_width - 1
78)     ) RETURN std_logic_vector IS
79)     BEGIN
80)         RETURN std_logic_vector(to_unsigned(pos, addr_width));
81)     END FUNCTION to_addr;
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

82) 
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

83) BEGIN
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

84) 
85)     i_rwram: e_block_rwram
86)         GENERIC MAP (
87)             addr_width => addr_width,
88)             data_width => data_width
89)         )
90)         PORT MAP (
91)             clk       => clk,
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

92)             i_rd_addr => s_begin_sn_addr,
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

93)             o_rd_data => o_rd_data,
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

94)             i_wr_addr => s_end_addr,
95)             i_wr_data => i_wr_data,
96)             i_wr_en   => s_wr_en
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

97)         );
98) 
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

99)     s_begin_addr    <= to_addr(r_begin);
100)     s_begin_sn_addr <= to_addr(n_begin);
101)     s_rd_rdy        <= '1' WHEN s_begin_addr /= r_end_addr_delay ELSE '0';
102)     s_rd_en         <= s_rd_rdy AND i_rd_en;
103) 
104)     s_end_addr      <= to_addr(r_end);
105)     s_end_an_addr   <= to_addr(next_pos(r_end));
106)     s_wr_rdy        <= '1' WHEN s_end_an_addr /= s_begin_addr ELSE '0';
107)     s_wr_en         <= s_wr_rdy AND i_wr_en;
108) 
109)     o_wr_rdy        <= s_wr_rdy;
110)     o_rd_rdy        <= s_rd_rdy;
111) 
112)     p_next: PROCESS(r_begin, r_end, s_rd_en, s_wr_en)
113)     BEGIN
114)         n_begin <= r_begin;
115)         n_end <= r_end;
116)         IF s_rd_en = '1' THEN
117)             n_begin <= next_pos(r_begin);
118)         END IF;
119)         IF s_wr_en = '1' THEN
120)             n_end <= next_pos(r_end);
121)         END IF;
122)     END PROCESS p_next;
123) 
124)     p_sync: PROCESS(rst, clk)
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

125)     BEGIN
126)         IF rst = '1' THEN
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

127)             r_begin          <= 0;
128)             r_end            <= 0;
129)             r_end_addr_delay <= (OTHERS => '0');
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

130)         ELSIF rising_edge(clk) THEN
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

131)             r_begin          <= n_begin;
132)             r_end            <= n_end;
133)             r_end_addr_delay <= s_end_addr;
Stefan Schuermans added FIFO to UART TX

Stefan Schuermans authored 12 years ago

134)         END IF;
Stefan Schuermans improve synchonous FIFO imp...

Stefan Schuermans authored 12 years ago

135)     END PROCESS p_sync;