Initial version

Committed on the Free edition of March Hare Software CVSNT Server.
Upgrade to CVS Suite for more features and support:
http://march-hare.com/cvsnt/


git-svn-id: http://moon:8086/svn/vhdl/trunk@809 cc03376c-175c-47c8-b038-4cd826a8556b
This commit is contained in:
2010-03-21 21:23:50 +00:00
parent 433676de7d
commit 84fa08326f
5 changed files with 1343 additions and 0 deletions
+571
View File
@@ -0,0 +1,571 @@
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.NUMERIC_STD.ALL;
use std.textio.all; -- Imports the standard textio package.
use work.emac_types.all;
use work.utils_pkg.all;
ENTITY emac_tx IS
Generic
(
f_sysclk : real := 100.0;
RAM_SIZE : natural := 2048
);
Port
(
clk : in STD_LOGIC;
rst : in STD_LOGIC;
din : in unsigned(31 downto 0);
ctrl_in : in tx_ctrl_in_t;
ctrl_out : out tx_ctrl_out_t;
mii_tx_clk : in STD_LOGIC;
mii_tx_en : out STD_LOGIC;
mii_tx_er : out STD_LOGIC;
mii_tx : out unsigned(7 downto 0)
);
END emac_tx;
ARCHITECTURE behavior OF emac_tx IS
constant RAM_ADDR_WIDTH : natural := NextExpBaseTwo(RAM_SIZE);
subtype word_ptr_t is unsigned(RAM_ADDR_WIDTH-1 downto 0);
signal fifo_din : unsigned(35 downto 0);
signal fifo_dout : unsigned(35 downto 0);
signal fifo_we : std_logic;
signal fifo_re : std_logic;
signal fifo_empty : std_logic;
signal fifo_full : std_logic;
signal flush_en : std_logic;
signal flush_rdy : std_logic;
signal flush_ptr_set : std_logic;
signal flush_ptr : word_ptr_t;
signal alloc_req_size : unsigned(15 downto 0);
signal fill_size : word_ptr_t;
signal fill_cnt : word_ptr_t;
signal fill_ptr : word_ptr_t;
signal fill_rdy : std_logic;
signal fill_cnt_set : std_logic;
signal fill_cnt_adv : std_logic;
signal fill_ptr_set : std_logic;
signal fill_ptr_adv : std_logic;
signal fill_remain : unsigned(1 downto 0);
signal alloc_OK : std_logic;
signal alloc_req : std_logic;
signal alloc_ack : std_logic;
signal nwords_free : word_ptr_t;
signal alloc_base : word_ptr_t;
signal alloc_size : word_ptr_t;
signal alloc_en : std_logic;
signal free_en : std_logic;
signal commit_en : std_logic;
signal uncommit_en : std_logic;
signal xfer_size : word_ptr_t;
signal xfer_ptr : word_ptr_t;
signal xfer_cnt : word_ptr_t;
signal xfer_ptr_set : std_logic;
signal xfer_ptr_adv : std_logic;
signal xfer_cnt_set : std_logic;
signal xfer_cnt_adv : std_logic;
signal xfer_rdy : std_logic;
signal xfer_tag : unsigned(3 downto 0);
signal xfer_remain : unsigned(1 downto 0);
signal xfer_odd : std_logic;
signal xfer_remain_en : std_logic;
signal ram_en_a : std_logic;
signal ram_we_a : std_logic;
signal ram_addr_a : word_ptr_t;
signal ram_din_a : unsigned(35 downto 0);
signal ram_dout_a : unsigned(35 downto 0);
signal ram_en_b : std_logic;
signal ram_we_b : std_logic;
signal ram_addr_b : word_ptr_t;
signal ram_din_b : unsigned(35 downto 0);
signal ram_dout_b : unsigned(35 downto 0);
signal piso_din_be : unsigned(3 downto 0);
signal piso_din_rdy : std_logic;
signal data_vld : std_logic;
subtype inter_frame_gap_cnt_t is natural range 0 to natural(1.0*f_sysclk)-1;
signal inter_frame_gap_cnt : inter_frame_gap_cnt_t;
signal inter_frame_gap_rdy : std_logic;
alias ram_data_in_a is ram_din_a(31 downto 0);
alias ram_tag_in_a is ram_din_a(35 downto 32);
alias ram_data_out_a is ram_dout_a(31 downto 0);
alias ram_tag_out_a is ram_dout_a(35 downto 32);
alias ram_data_in_b is ram_din_b(31 downto 0);
alias ram_tag_in_b is ram_din_b(35 downto 32);
alias ram_data_out_b is ram_dout_b(31 downto 0);
alias ram_tag_out_b is ram_dout_b(35 downto 32);
type host_state_t is (host_init, host_flush, host_idle, host_alloc, host_setup, host_arm, host_fill, host_commit);
signal host_s, host_sn : host_state_t;
type xfer_state_t is (xfer_init, xfer_idle, xfer_setup, xfer_wait, xfer_active, xfer_free);
signal xfer_s, xfer_sn : xfer_state_t;
type piso_byte_mask_array_t is array (0 to 3) of unsigned (3 downto 0);
constant piso_byte_mask_rom : piso_byte_mask_array_t :=
(
"1111",
"0001",
"0011",
"0111"
);
begin
ctrl_out.alloc_req <= alloc_req;
mii_tx_er <= ctrl_in.tx_er;
ram_tag_in_b <= "0000";
ram_en_b <= not fifo_full or uncommit_en or flush_en;
ram_we_b <= uncommit_en or flush_en;
ram_addr_b <= flush_ptr when (flush_en = '1' or uncommit_en = '1') else xfer_ptr;
xfer_tag <= ram_tag_out_b;
ram_en_a <= '1';
ram_we_a <= commit_en or fill_cnt_adv;
ram_tag_in_a <= fill_remain & "01" when commit_en = '1' else "0000";
ram_data_in_a <= resize(fill_size, 16) & resize(alloc_base, 16) when commit_en = '1' else din;
ram_addr_a <= alloc_base when commit_en = '1' else fill_ptr;
piso_din_be <= (others => '1');
fifo_re <= piso_din_rdy;
data_vld <= not fifo_empty;
fifo_din(35 downto 32) <= piso_byte_mask_rom(to_integer(xfer_remain)) when xfer_remain_en = '1' else "1111";
fifo_din(31 downto 0) <= ram_data_out_b;
------------------------------------------------------------------
host_state_next:
process(clk)
begin
if rising_edge(clk) then
if rst = '1' then
host_s <= host_init;
else
host_s <= host_sn;
end if;
end if;
end process;
host_state:
process(host_s, flush_rdy, alloc_req, alloc_OK, fill_rdy, ctrl_in.data_vld)
begin
flush_en <= '0';
flush_ptr_set <= '0';
alloc_ack <= '0';
fill_cnt_set <= '0';
fill_cnt_adv <= '0';
fill_ptr_set <= '0';
fill_ptr_adv <= '0';
commit_en <= '0';
alloc_en <= '0';
host_sn <= host_s;
case host_s is
when host_init =>
flush_ptr_set <= '1';
host_sn <= host_flush;
when host_flush =>
flush_en <= '1';
if flush_rdy = '1' then
flush_en <= '0';
host_sn <= host_idle;
end if;
when host_idle =>
if alloc_req = '1' then
host_sn <= host_alloc;
end if;
when host_alloc =>
if alloc_OK = '1' then
host_sn <= host_setup;
alloc_ack <= '1';
end if;
when host_setup =>
fill_cnt_set <= '1';
fill_ptr_set <= '1';
host_sn <= host_arm;
when host_arm =>
fill_ptr_adv <= '1';
fill_cnt_adv <= '1';
host_sn <= host_fill;
when host_fill =>
fill_cnt_adv <= ctrl_in.data_vld;
fill_ptr_adv <= not fill_rdy and ctrl_in.data_vld;
if alloc_req = '1' then
host_sn <= host_alloc;
elsif fill_rdy = '1' then
host_sn <= host_commit;
end if;
when host_commit =>
commit_en <= '1';
alloc_en <= '1';
host_sn <= host_idle;
when others =>
host_sn <= host_idle;
end case;
end process;
------------------------------------------------------------------
-- Allocation stuff
------------------------------------------------------------------
alloc_req_size_register:
process(clk)
begin
if rising_edge(clk) then
if ctrl_in.alloc_req_size_vld = '1' then
alloc_req_size <= din(15 downto 0);
end if;
end if;
end process;
------------------------------------------------------------------
alloc_request_logic:
process(clk)
begin
if rising_edge(clk) then
if rst = '1' then
alloc_req <= '0';
elsif ctrl_in.alloc_req_en = '1' and alloc_req = '0' then
alloc_req <= '1';
fill_remain <= alloc_req_size(1 downto 0);
fill_size <= alloc_req_size(word_ptr_t'left+2 downto 2);
if din(1 downto 0) /= "00" then
fill_size <= alloc_req_size(word_ptr_t'left+2 downto 2) + 1;
end if;
ctrl_out.alloc_req_size <= resize(alloc_req_size(word_ptr_t'left downto 0), 16);
elsif alloc_ack = '1' then
alloc_req <= '0';
alloc_size <= fill_size;
end if;
end if;
end process;
------------------------------------------------------------------
alloc_base_logic:
process(clk)
begin
if rising_edge(clk) then
if flush_en = '1' then
alloc_base <= (others => '0');
elsif commit_en = '1' then
alloc_base <= fill_ptr;
end if;
end if;
end process;
------------------------------------------------------------------
alloc_eval_logic:
process(clk)
begin
if rising_edge(clk) then
alloc_OK <= '0';
if alloc_req = '1' then
if fill_size < nwords_free then
alloc_OK <= '1';
end if;
end if;
end if;
end process;
------------------------------------------------------------------
nwords_free_counter:
process(clk)
begin
if rising_edge(clk) then
if flush_en = '1' then
nwords_free <= (others => '1');
elsif alloc_en = '1' then
nwords_free <= nwords_free - fill_size;
elsif free_en = '1' then
nwords_free <= nwords_free + xfer_size;
end if;
end if;
end process;
------------------------------------------------------------------
flush_counter:
process(clk)
begin
if rising_edge(clk) then
if flush_en = '0' then
flush_rdy <= '0';
if flush_ptr_set = '1' then
flush_ptr <= (others => '1');
elsif xfer_ptr_set = '1' then
flush_ptr <= ram_data_out_b(flush_ptr'left downto 0);
end if;
elsif flush_ptr /= 0 then
flush_ptr <= flush_ptr - 1;
else
flush_rdy <= '1';
end if;
end if;
end process;
------------------------------------------------------------------
-- Fill stuff
------------------------------------------------------------------
fill_counter:
process(clk)
begin
if rising_edge(clk) then
if fill_cnt_set = '1' then
fill_rdy <= '0';
fill_cnt <= alloc_size;
elsif fill_cnt_adv = '1' then
if fill_cnt /= 0 then
fill_cnt <= fill_cnt - 1;
else
fill_rdy <= '1';
end if;
end if;
end if;
end process;
fill_pointer:
process(clk)
begin
if rising_edge(clk) then
if fill_ptr_set = '1' then
fill_ptr <= alloc_base;
elsif fill_ptr_adv = '1' then
fill_ptr <= fill_ptr + 1;
end if;
end if;
end process;
------------------------------------------------------------------
-- Transfer stuff
------------------------------------------------------------------
xfer_state_next:
process(clk)
begin
if rising_edge(clk) then
if rst = '1' then
xfer_s <= xfer_init;
else
xfer_s <= xfer_sn;
end if;
end if;
end process;
xfer_state:
process(xfer_s, xfer_tag, xfer_odd, xfer_rdy, inter_frame_gap_rdy, alloc_en, fifo_full)
begin
xfer_cnt_set <= '0';
xfer_cnt_adv <= '0';
xfer_ptr_set <= '0';
xfer_ptr_adv <= '0';
xfer_remain_en <= '0';
fifo_we <= '0';
free_en <= '0';
uncommit_en <= '0';
xfer_sn <= xfer_s;
case xfer_s is
when xfer_init =>
xfer_sn <= xfer_idle;
when xfer_idle =>
if xfer_tag(0) = '1' then
xfer_ptr_set <= '1';
xfer_cnt_set <= '1';
xfer_sn <= xfer_wait;
end if;
when xfer_wait =>
if inter_frame_gap_rdy = '1' then
xfer_sn <= xfer_setup;
xfer_ptr_adv <= '1';
xfer_cnt_adv <= '1';
uncommit_en <= '1';
end if;
when xfer_setup =>
xfer_ptr_adv <= '1';
xfer_cnt_adv <= '1';
xfer_sn <= xfer_active;
when xfer_active =>
fifo_we <= '1';
xfer_cnt_adv <= not fifo_full;
xfer_ptr_adv <= not fifo_full;
if xfer_rdy = '1' then
xfer_remain_en <= xfer_odd;
xfer_cnt_adv <= '0';
xfer_ptr_adv <= '0';
if fifo_full = '0' then
xfer_sn <= xfer_free;
end if;
end if;
when xfer_free =>
free_en <= not alloc_en;
if alloc_en = '0' then
xfer_sn <= xfer_idle;
end if;
when others =>
xfer_sn <= xfer_idle;
end case;
end process;
------------------------------------------------------------------
interframe_gap_counter:
process(clk)
begin
if rising_edge(clk) then
if flush_en = '1' then
inter_frame_gap_rdy <= '1';
elsif fifo_empty = '0' then
inter_frame_gap_rdy <= '0';
inter_frame_gap_cnt <= inter_frame_gap_cnt_t'high;
elsif inter_frame_gap_cnt /= 0 then
inter_frame_gap_cnt <= inter_frame_gap_cnt - 1;
else
inter_frame_gap_rdy <= '1';
end if;
end if;
end process;
------------------------------------------------------------------
xfer_counter:
process(clk)
begin
if rising_edge(clk) then
if xfer_cnt_set = '1' then
xfer_size <= (others => '0');
xfer_rdy <= '0';
xfer_cnt <= ram_data_out_b(xfer_ptr'left+16 downto 16);
elsif xfer_cnt_adv = '1' then
if xfer_cnt /= 0 then
xfer_cnt <= xfer_cnt - 1;
xfer_size <= xfer_size + 1;
else
xfer_rdy <= '1';
end if;
end if;
end if;
end process;
xfer_pointer:
process(clk)
begin
if rising_edge(clk) then
if xfer_ptr_set = '1' then
xfer_ptr <= ram_data_out_b(xfer_ptr'left downto 0);
xfer_remain <= xfer_tag(3 downto 2);
xfer_odd <= '0';
if xfer_tag(3 downto 2) /= "00" then
xfer_odd <= '1';
end if;
elsif xfer_ptr_adv = '1' then
xfer_ptr <= xfer_ptr + 1;
end if;
end if;
end process;
inst_ram : entity work.dpram_2w2r
GENERIC MAP
(
addr_width => RAM_ADDR_WIDTH,
data_width => 36
)
PORT MAP
(
clk_a => clk,
clk_b => clk,
en_a => ram_en_a,
en_b => ram_en_b,
we_a => ram_we_a,
we_b => ram_we_b,
addr_a => ram_addr_a,
addr_b => ram_addr_b,
din_a => ram_din_a,
din_b => ram_din_b,
dout_a => ram_dout_a,
dout_b => ram_dout_b
);
-- Instantiate synchronous FIFO
inst_fifo: entity work.fifo_async
GENERIC MAP
(
addr_width => 4,
data_width => 36,
do_last_read_update => true
)
PORT MAP
(
rst => rst,
clk_w => clk,
clk_r => mii_tx_clk,
we => fifo_we,
re => fifo_re,
fifo_full => fifo_full,
fifo_empty => fifo_empty,
fifo_afull => open,
fifo_aempty => open,
data_w => fifo_din,
data_r => fifo_dout
);
inst_piso : entity work.piso
GENERIC MAP
(
data_width_in => 32,
data_width_out => 8,
msb_first => false
)
PORT MAP
(
rst => rst,
clk => mii_tx_clk,
din_vld => data_vld,
din_rdy => piso_din_rdy,
din_be => fifo_dout(35 downto 32),
din => fifo_dout(31 downto 0),
dout_en => mii_tx_en,
dout => mii_tx
);
end behavior;