OpenCores
URL https://opencores.org/ocsvn/udp_ip_stack/udp_ip_stack/trunk

Subversion Repositories udp_ip_stack

[/] [udp_ip_stack/] [trunk/] [rtl/] [vhdl/] [UDP_TX.vhd] - Blame information for rev 24

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.