| 1 |
2 |
pjf |
----------------------------------------------------------------------------------
|
| 2 |
|
|
-- Company:
|
| 3 |
|
|
-- Engineer: Peter Fall
|
| 4 |
|
|
--
|
| 5 |
|
|
-- Create Date: 5 June 2011
|
| 6 |
|
|
-- Design Name:
|
| 7 |
|
|
-- Module Name: UDP_TX - Behavioral
|
| 8 |
|
|
-- Project Name:
|
| 9 |
|
|
-- Target Devices:
|
| 10 |
|
|
-- Tool versions:
|
| 11 |
|
|
-- Description:
|
| 12 |
|
|
-- handle simple UDP TX
|
| 13 |
|
|
-- doesnt generate the checksum(supposedly optional)
|
| 14 |
|
|
-- Dependencies:
|
| 15 |
|
|
--
|
| 16 |
|
|
-- Revision:
|
| 17 |
|
|
-- Revision 0.01 - File Created
|
| 18 |
|
|
-- Additional Comments:
|
| 19 |
|
|
--
|
| 20 |
|
|
----------------------------------------------------------------------------------
|
| 21 |
|
|
library IEEE;
|
| 22 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
| 23 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
| 24 |
|
|
use work.axi.all;
|
| 25 |
|
|
use work.ipv4_types.all;
|
| 26 |
|
|
|
| 27 |
|
|
entity UDP_TX is
|
| 28 |
|
|
Port (
|
| 29 |
|
|
-- UDP Layer signals
|
| 30 |
|
|
udp_tx_start : in std_logic; -- indicates req to tx UDP
|
| 31 |
|
|
udp_txi : in udp_tx_type; -- UDP tx cxns
|
| 32 |
|
|
udp_tx_result : out std_logic_vector (1 downto 0);-- tx status (changes during transmission)
|
| 33 |
|
|
udp_tx_data_out_ready: out std_logic; -- indicates udp_tx is ready to take data
|
| 34 |
|
|
-- system signals
|
| 35 |
|
|
clk : in STD_LOGIC; -- same clock used to clock mac data and ip data
|
| 36 |
|
|
reset : in STD_LOGIC;
|
| 37 |
|
|
-- IP layer TX signals
|
| 38 |
|
|
ip_tx_start : out std_logic;
|
| 39 |
|
|
ip_tx : out ipv4_tx_type; -- IP tx cxns
|
| 40 |
|
|
ip_tx_result : in std_logic_vector (1 downto 0); -- tx status (changes during transmission)
|
| 41 |
|
|
ip_tx_data_out_ready : in std_logic -- indicates IP TX is ready to take data
|
| 42 |
|
|
);
|
| 43 |
|
|
end UDP_TX;
|
| 44 |
|
|
|
| 45 |
|
|
architecture Behavioral of UDP_TX is
|
| 46 |
|
|
type tx_state_type is (IDLE, SEND_UDP_HDR, SEND_USER_DATA);
|
| 47 |
|
|
|
| 48 |
|
|
type count_mode_type is (RST, INCR, HOLD);
|
| 49 |
|
|
type settable_cnt_type is (RST, SET, INCR, HOLD);
|
| 50 |
|
|
type set_clr_type is (SET, CLR, HOLD);
|
| 51 |
|
|
|
| 52 |
|
|
-- TX state variables
|
| 53 |
|
|
signal udp_tx_state : tx_state_type;
|
| 54 |
|
|
signal tx_count : unsigned (15 downto 0);
|
| 55 |
|
|
signal tx_result_reg : std_logic_vector (1 downto 0);
|
| 56 |
|
|
signal ip_tx_start_reg : std_logic;
|
| 57 |
|
|
signal data_out_ready_reg : std_logic;
|
| 58 |
|
|
|
| 59 |
|
|
-- tx control signals
|
| 60 |
|
|
signal next_tx_state : tx_state_type;
|
| 61 |
|
|
signal set_tx_state : std_logic;
|
| 62 |
|
|
signal next_tx_result : std_logic_vector (1 downto 0);
|
| 63 |
|
|
signal set_tx_result : std_logic;
|
| 64 |
|
|
signal tx_count_val : unsigned (15 downto 0);
|
| 65 |
|
|
signal tx_count_mode : settable_cnt_type;
|
| 66 |
|
|
signal tx_data : std_logic_vector (7 downto 0);
|
| 67 |
|
|
signal set_last : std_logic;
|
| 68 |
|
|
signal set_ip_tx_start : set_clr_type;
|
| 69 |
|
|
signal tx_data_valid : std_logic; -- indicates whether data is valid to tx or not
|
| 70 |
|
|
|
| 71 |
|
|
-- tx temp signals
|
| 72 |
|
|
signal total_length : std_logic_vector (15 downto 0); -- computed combinatorially from header size
|
| 73 |
|
|
|
| 74 |
|
|
|
| 75 |
|
|
-- IP datagram header format
|
| 76 |
|
|
--
|
| 77 |
|
|
-- 0 4 8 16 19 24 31
|
| 78 |
|
|
-- --------------------------------------------------------------------------------------------
|
| 79 |
|
|
-- | source port number | dest port number |
|
| 80 |
|
|
-- | | |
|
| 81 |
|
|
-- --------------------------------------------------------------------------------------------
|
| 82 |
|
|
-- | length (bytes) | checksum |
|
| 83 |
|
|
-- | (header and data combined) | |
|
| 84 |
|
|
-- --------------------------------------------------------------------------------------------
|
| 85 |
|
|
-- | Data |
|
| 86 |
|
|
-- | |
|
| 87 |
|
|
-- --------------------------------------------------------------------------------------------
|
| 88 |
|
|
-- | .... |
|
| 89 |
|
|
-- | |
|
| 90 |
|
|
-- --------------------------------------------------------------------------------------------
|
| 91 |
|
|
|
| 92 |
|
|
begin
|
| 93 |
|
|
-----------------------------------------------------------------------
|
| 94 |
|
|
-- combinatorial process to implement FSM and determine control signals
|
| 95 |
|
|
-----------------------------------------------------------------------
|
| 96 |
|
|
|
| 97 |
|
|
tx_combinatorial : process(
|
| 98 |
|
|
-- input signals
|
| 99 |
|
|
udp_tx_start, udp_txi, clk, ip_tx_result, ip_tx_data_out_ready,
|
| 100 |
|
|
-- state variables
|
| 101 |
|
|
udp_tx_state, tx_count, tx_result_reg, ip_tx_start_reg, data_out_ready_reg,
|
| 102 |
|
|
-- control signals
|
| 103 |
|
|
next_tx_state, set_tx_state, next_tx_result, set_tx_result, tx_count_mode, tx_count_val,
|
| 104 |
|
|
tx_data, set_last, total_length, set_ip_tx_start, tx_data_valid
|
| 105 |
|
|
)
|
| 106 |
|
|
|
| 107 |
|
|
begin
|
| 108 |
|
|
-- set output followers
|
| 109 |
|
|
ip_tx_start <= ip_tx_start_reg;
|
| 110 |
|
|
ip_tx.hdr.protocol <= x"11"; -- UDP protocol
|
| 111 |
|
|
ip_tx.hdr.data_length <= total_length;
|
| 112 |
|
|
ip_tx.hdr.dst_ip_addr <= udp_txi.hdr.dst_ip_addr;
|
| 113 |
|
|
udp_tx_result <= tx_result_reg;
|
| 114 |
|
|
|
| 115 |
|
|
case udp_tx_state is
|
| 116 |
|
|
when SEND_USER_DATA =>
|
| 117 |
|
|
ip_tx.data.data_out <= udp_txi.data.data_out;
|
| 118 |
|
|
tx_data_valid <= udp_txi.data.data_out_valid;
|
| 119 |
|
|
ip_tx.data.data_out_last <= udp_txi.data.data_out_last;
|
| 120 |
|
|
|
| 121 |
|
|
when SEND_UDP_HDR =>
|
| 122 |
|
|
ip_tx.data.data_out <= tx_data;
|
| 123 |
|
|
tx_data_valid <= ip_tx_data_out_ready;
|
| 124 |
|
|
ip_tx.data.data_out_last <= set_last;
|
| 125 |
|
|
|
| 126 |
|
|
when others =>
|
| 127 |
|
|
ip_tx.data.data_out <= (others => '0');
|
| 128 |
|
|
tx_data_valid <= '0';
|
| 129 |
|
|
ip_tx.data.data_out_last <= set_last;
|
| 130 |
|
|
end case;
|
| 131 |
|
|
|
| 132 |
|
|
ip_tx.data.data_out_valid <= tx_data_valid and ip_tx_data_out_ready;
|
| 133 |
|
|
|
| 134 |
|
|
-- set signal defaults
|
| 135 |
|
|
next_tx_state <= IDLE;
|
| 136 |
|
|
set_tx_state <= '0';
|
| 137 |
|
|
tx_count_mode <= HOLD;
|
| 138 |
|
|
tx_data <= x"00";
|
| 139 |
|
|
set_last <= '0';
|
| 140 |
|
|
next_tx_result <= UDPTX_RESULT_NONE;
|
| 141 |
|
|
set_tx_result <= '0';
|
| 142 |
|
|
set_ip_tx_start <= HOLD;
|
| 143 |
|
|
tx_count_val <= (others => '0');
|
| 144 |
|
|
|
| 145 |
|
|
-- set temp signals
|
| 146 |
|
|
total_length <= std_logic_vector(unsigned(udp_txi.hdr.data_length) + 8); -- total length = user data length + header length (bytes)
|
| 147 |
|
|
|
| 148 |
|
|
-- TX FSM
|
| 149 |
|
|
case udp_tx_state is
|
| 150 |
|
|
when IDLE =>
|
| 151 |
|
|
udp_tx_data_out_ready <= '0'; -- in this state, we are unable to accept user data for tx
|
| 152 |
|
|
tx_count_mode <= RST;
|
| 153 |
|
|
if udp_tx_start = '1' then
|
| 154 |
|
|
-- check header count for error if too high
|
| 155 |
|
|
if unsigned(udp_txi.hdr.data_length) > 1472 then
|
| 156 |
|
|
next_tx_result <= UDPTX_RESULT_ERR;
|
| 157 |
|
|
set_tx_result <= '1';
|
| 158 |
|
|
else
|
| 159 |
|
|
-- start to send UDP header
|
| 160 |
|
|
tx_count_mode <= RST;
|
| 161 |
|
|
next_tx_result <= UDPTX_RESULT_SENDING;
|
| 162 |
|
|
set_ip_tx_start <= SET;
|
| 163 |
|
|
set_tx_result <= '1';
|
| 164 |
|
|
next_tx_state <= SEND_UDP_HDR;
|
| 165 |
|
|
set_tx_state <= '1';
|
| 166 |
|
|
end if;
|
| 167 |
|
|
end if;
|
| 168 |
|
|
|
| 169 |
|
|
when SEND_UDP_HDR =>
|
| 170 |
|
|
udp_tx_data_out_ready <= '0'; -- in this state, we are unable to accept user data for tx
|
| 171 |
|
|
if ip_tx_data_out_ready = '1' then
|
| 172 |
|
|
if tx_count = x"0007" then
|
| 173 |
|
|
tx_count_val <= x"0001";
|
| 174 |
|
|
tx_count_mode <= SET;
|
| 175 |
|
|
next_tx_state <= SEND_USER_DATA;
|
| 176 |
|
|
set_tx_state <= '1';
|
| 177 |
|
|
else
|
| 178 |
|
|
tx_count_mode <= INCR;
|
| 179 |
|
|
end if;
|
| 180 |
|
|
case tx_count is
|
| 181 |
|
|
when x"0000" => tx_data <= udp_txi.hdr.src_port (15 downto 8); -- src port
|
| 182 |
|
|
when x"0001" => tx_data <= udp_txi.hdr.src_port (7 downto 0);
|
| 183 |
|
|
when x"0002" => tx_data <= udp_txi.hdr.dst_port (15 downto 8); -- dst port
|
| 184 |
|
|
when x"0003" => tx_data <= udp_txi.hdr.dst_port (7 downto 0);
|
| 185 |
|
|
when x"0004" => tx_data <= total_length (15 downto 8); -- length
|
| 186 |
|
|
when x"0005" => tx_data <= total_length (7 downto 0);
|
| 187 |
|
|
when x"0006" => tx_data <= udp_txi.hdr.checksum (15 downto 8); -- checksum (set by upstream)
|
| 188 |
|
|
when x"0007" => tx_data <= udp_txi.hdr.checksum (7 downto 0);
|
| 189 |
|
|
when others =>
|
| 190 |
|
|
-- shouldnt get here - handle as error
|
| 191 |
|
|
next_tx_result <= IPTX_RESULT_ERR;
|
| 192 |
|
|
set_tx_result <= '1';
|
| 193 |
|
|
end case;
|
| 194 |
|
|
end if;
|
| 195 |
|
|
|
| 196 |
|
|
when SEND_USER_DATA =>
|
| 197 |
|
|
udp_tx_data_out_ready <= '1'; -- in this state, we are always ready to accept user data for tx
|
| 198 |
|
|
if ip_tx_data_out_ready = '1' then
|
| 199 |
|
|
if udp_txi.data.data_out_valid = '1' or tx_count = x"000" then
|
| 200 |
|
|
-- only increment if ready and valid has been subsequently established, otherwise data count moves on too fast
|
| 201 |
|
|
if unsigned(tx_count) = unsigned(udp_txi.hdr.data_length) then
|
| 202 |
|
|
set_last <= '1';
|
| 203 |
|
|
tx_data <= udp_txi.data.data_out;
|
| 204 |
|
|
next_tx_result <= UDPTX_RESULT_SENT;
|
| 205 |
|
|
set_ip_tx_start <= CLR;
|
| 206 |
|
|
set_tx_result <= '1';
|
| 207 |
|
|
next_tx_state <= IDLE;
|
| 208 |
|
|
set_tx_state <= '1';
|
| 209 |
|
|
else
|
| 210 |
|
|
tx_count_mode <= INCR;
|
| 211 |
|
|
tx_data <= udp_txi.data.data_out;
|
| 212 |
|
|
end if;
|
| 213 |
|
|
end if;
|
| 214 |
|
|
end if;
|
| 215 |
|
|
|
| 216 |
|
|
end case;
|
| 217 |
|
|
end process;
|
| 218 |
|
|
|
| 219 |
|
|
-----------------------------------------------------------------------------
|
| 220 |
|
|
-- sequential process to action control signals and change states and outputs
|
| 221 |
|
|
-----------------------------------------------------------------------------
|
| 222 |
|
|
|
| 223 |
|
|
tx_sequential : process (clk,reset,data_out_ready_reg)
|
| 224 |
|
|
begin
|
| 225 |
|
|
if rising_edge(clk) then
|
| 226 |
|
|
data_out_ready_reg <= ip_tx_data_out_ready;
|
| 227 |
|
|
else
|
| 228 |
|
|
data_out_ready_reg <= data_out_ready_reg;
|
| 229 |
|
|
end if;
|
| 230 |
|
|
|
| 231 |
|
|
if rising_edge(clk) then
|
| 232 |
|
|
if reset = '1' then
|
| 233 |
|
|
-- reset state variables
|
| 234 |
|
|
udp_tx_state <= IDLE;
|
| 235 |
|
|
tx_count <= x"0000";
|
| 236 |
|
|
tx_result_reg <= IPTX_RESULT_NONE;
|
| 237 |
|
|
ip_tx_start_reg <= '0';
|
| 238 |
|
|
else
|
| 239 |
|
|
-- Next udp_tx_state processing
|
| 240 |
|
|
if set_tx_state = '1' then
|
| 241 |
|
|
udp_tx_state <= next_tx_state;
|
| 242 |
|
|
else
|
| 243 |
|
|
udp_tx_state <= udp_tx_state;
|
| 244 |
|
|
end if;
|
| 245 |
|
|
|
| 246 |
|
|
-- ip_tx_start_reg processing
|
| 247 |
|
|
case set_ip_tx_start is
|
| 248 |
|
|
when SET => ip_tx_start_reg <= '1';
|
| 249 |
|
|
when CLR => ip_tx_start_reg <= '0';
|
| 250 |
|
|
when HOLD => ip_tx_start_reg <= ip_tx_start_reg;
|
| 251 |
|
|
end case;
|
| 252 |
|
|
|
| 253 |
|
|
-- tx result processing
|
| 254 |
|
|
if set_tx_result = '1' then
|
| 255 |
|
|
tx_result_reg <= next_tx_result;
|
| 256 |
|
|
else
|
| 257 |
|
|
tx_result_reg <= tx_result_reg;
|
| 258 |
|
|
end if;
|
| 259 |
|
|
|
| 260 |
|
|
-- tx_count processing
|
| 261 |
|
|
case tx_count_mode is
|
| 262 |
|
|
when RST => tx_count <= x"0000";
|
| 263 |
|
|
when SET => tx_count <= tx_count_val;
|
| 264 |
|
|
when INCR => tx_count <= tx_count + 1;
|
| 265 |
|
|
when HOLD => tx_count <= tx_count;
|
| 266 |
|
|
end case;
|
| 267 |
|
|
|
| 268 |
|
|
end if;
|
| 269 |
|
|
end if;
|
| 270 |
|
|
end process;
|
| 271 |
|
|
|
| 272 |
|
|
|
| 273 |
|
|
end Behavioral;
|
| 274 |
|
|
|