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

Subversion Repositories udp_ip_stack

[/] [udp_ip_stack/] [trunk/] [rtl/] [vhdl/] [IPv4_TX.vhd] - Blame information for rev 2

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:    16:20:42 06/01/2011 
6
-- Design Name: 
7
-- Module Name:    IPv4_TX - Behavioral 
8
-- Project Name: 
9
-- Target Devices: 
10
-- Tool versions: 
11
-- Description: 
12
--              handle simple IP TX
13
--              doesnt handle segmentation
14
--              dest MAC addr resolution through ARP layer
15
--              Handle IPv4 protocol
16
-- Dependencies: 
17
--
18
-- Revision: 
19
-- Revision 0.01 - File Created
20
-- Revision 0.02 - fixed up setting of tx_result control defaults
21
-- Additional Comments: 
22
--
23
----------------------------------------------------------------------------------
24
library IEEE;
25
use IEEE.STD_LOGIC_1164.ALL;
26
use IEEE.NUMERIC_STD.ALL;
27
use work.axi.all;
28
use work.ipv4_types.all;
29
use work.arp_types.all;
30
 
31
entity IPv4_TX is
32
    Port (
33
                        -- IP Layer signals
34
                        ip_tx_start                             : in std_logic;
35
                        ip_tx                                           : in ipv4_tx_type;                                                              -- IP tx cxns
36
                        ip_tx_result                    : out std_logic_vector (1 downto 0);             -- tx status (changes during transmission)
37
                        ip_tx_data_out_ready    : out std_logic;                                                                        -- indicates IP TX is ready to take data
38
 
39
                        -- system signals
40
                        clk                                             : in  STD_LOGIC;                                                                        -- same clock used to clock mac data and ip data
41
                        reset                                   : in  STD_LOGIC;
42
                        our_ip_address          : in STD_LOGIC_VECTOR (31 downto 0);
43
                        our_mac_address                 : in std_logic_vector (47 downto 0);
44
                        -- ARP lookup signals
45
                        arp_req_req                             : out arp_req_req_type;
46
                        arp_req_rslt                    : in arp_req_rslt_type;
47
                        -- MAC layer TX signals
48
                        mac_tx_req                              : out std_logic;                                                                        -- indicates that ip wants access to channel (stays up for as long as tx)
49
                        mac_tx_granted                  : in std_logic;                                                                 -- indicates that access to channel has been granted            
50
                        mac_data_out_ready      : in std_logic;                                                                 -- indicates system ready to consume data
51
                        mac_data_out_valid      : out std_logic;                                                                -- indicates data out is valid
52
                        mac_data_out_last               : out std_logic;                                                                        -- with data out valid indicates the last byte of a frame
53
                        mac_data_out                    : out std_logic_vector (7 downto 0)              -- ethernet frame (from dst mac addr through to last byte of frame)      
54
                        );
55
end IPv4_TX;
56
 
57
architecture Behavioral of IPv4_TX is
58
 
59
        type tx_state_type is (
60
                        IDLE,
61
                        WAIT_MAC,                               -- waiting for response from ARP for mac lookup
62
                        WAIT_CHN,                               -- waiting for tx access to MAC channel
63
                        SEND_ETH_HDR,                   -- sending the ethernet header
64
                        SEND_IP_HDR,                    -- sending the IP header
65
                        SEND_USER_DATA                  -- sending the users data
66
                        );
67
 
68
        type crc_state_type is (IDLE,TOT_LEN,ID,FLAGS,TTL,CKS,SAH,SAL,DAH,DAL,FINAL,WAIT_END);
69
 
70
        type count_mode_type is (RST, INCR, HOLD);
71
        type settable_cnt_type is (RST, SET, INCR, HOLD);
72
        type set_clr_type is (SET, CLR, HOLD);
73
 
74
        -- Configuration
75
 
76
        constant IP_TTL                 : std_logic_vector (7 downto 0) := x"80";
77
 
78
        -- TX state variables
79
        signal tx_state                 : tx_state_type;
80
        signal tx_count                         : unsigned (11 downto 0);
81
        signal tx_result_reg            : std_logic_vector (1 downto 0);
82
        signal tx_mac                           : std_logic_vector (47 downto 0);
83
        signal tx_mac_chn_reqd  : std_logic;
84
        signal tx_hdr_cks                       : std_logic_vector (23 downto 0);
85
        signal mac_lookup_req   : std_logic;
86
        signal crc_state                        : crc_state_type;
87
        signal arp_req_ip_reg   : std_logic_vector (31 downto 0);
88
        signal mac_data_out_ready_reg   : std_logic;
89
 
90
        -- tx control signals
91
        signal next_tx_state    : tx_state_type;
92
        signal set_tx_state             : std_logic;
93
        signal next_tx_result   : std_logic_vector (1 downto 0);
94
        signal set_tx_result            : std_logic;
95
        signal set_tx_mac                       : std_logic;
96
        signal tx_count_val             : unsigned (11 downto 0);
97
        signal tx_count_mode            : settable_cnt_type;
98
        signal tx_data                          : std_logic_vector (7 downto 0);
99
        signal set_last                 : std_logic;
100
        signal set_chn_reqd             : set_clr_type;
101
        signal set_mac_lku_req  : set_clr_type;
102
        signal tx_data_valid            : std_logic;                    -- indicates whether data is valid to tx or not
103
 
104
        -- tx temp signals
105
        signal total_length             : std_logic_vector (15 downto 0);        -- computed combinatorially from header size
106
 
107
 
108
        FUNCTION inv_if_one(s1:std_logic_vector;en:std_logic) return std_logic_vector is
109
                  --this function inverts all the bits of a vector if
110
                  --'en' is '1'.
111
        VARIABLE Z : std_logic_vector(s1'high downto s1'low);
112
        BEGIN
113
        FOR i IN (s1'low) to s1'high LOOP
114
            Z(i) := en XOR s1(i);
115
        END LOOP;
116
        RETURN Z;
117
        END inv_if_one; -- end function
118
 
119
 
120
-- IP datagram header format
121
--
122
--      0          4          8                      16      19             24                    31
123
--      --------------------------------------------------------------------------------------------
124
--      | Version  | *Header  |    Service Type      |        Total Length including header        |
125
--      |   (4)    |  Length  |     (ignored)        |                 (in bytes)                  |
126
--      --------------------------------------------------------------------------------------------
127
--      |           Identification                   | Flags |       Fragment Offset               |
128
--      |                                            |       |      (in 32 bit words)              |
129
--      --------------------------------------------------------------------------------------------
130
--      |    Time To Live     |       Protocol       |             Header Checksum                 |
131
--      |     (ignored)       |                      |                                             |
132
--      --------------------------------------------------------------------------------------------
133
--      |                                   Source IP Address                                      |
134
--      |                                                                                          |
135
--      --------------------------------------------------------------------------------------------
136
--      |                                 Destination IP Address                                   |
137
--      |                                                                                          |
138
--      --------------------------------------------------------------------------------------------
139
--      |                          Options (if any - ignored)               |       Padding        |
140
--      |                                                                   |      (if needed)     |
141
--      --------------------------------------------------------------------------------------------
142
--      |                                          Data                                            |
143
--      |                                                                                          |
144
--      --------------------------------------------------------------------------------------------
145
--      |                                          ....                                            |
146
--      |                                                                                          |
147
--      --------------------------------------------------------------------------------------------
148
--
149
-- * - in 32 bit words 
150
 
151
begin
152
        -----------------------------------------------------------------------
153
        -- combinatorial process to implement FSM and determine control signals
154
        -----------------------------------------------------------------------
155
 
156
        tx_combinatorial : process(
157
                -- input signals
158
                ip_tx_start, ip_tx, clk, our_ip_address, our_mac_address, arp_req_rslt,
159
                mac_tx_granted, mac_data_out_ready,
160
                -- state variables
161
                tx_state, tx_count, tx_result_reg, tx_mac, tx_mac_chn_reqd,
162
                mac_lookup_req, tx_hdr_cks, arp_req_ip_reg, mac_data_out_ready_reg,
163
                -- control signals
164
                next_tx_state, set_tx_state, next_tx_result, set_tx_result, set_tx_mac, tx_count_mode,
165
                tx_data, set_last, set_chn_reqd, set_mac_lku_req, total_length,
166
                tx_data_valid, tx_count_val
167
                )
168
        begin
169
                -- set output followers
170
                ip_tx_result <= tx_result_reg;
171
                mac_tx_req <= tx_mac_chn_reqd;
172
                arp_req_req.lookup_req <= mac_lookup_req;
173
                arp_req_req.ip <= arp_req_ip_reg;
174
 
175
                case tx_state is
176
                        when SEND_ETH_HDR | SEND_IP_HDR =>
177
                                mac_data_out <= tx_data;
178
                                tx_data_valid <= mac_data_out_ready;    -- generated internally
179
                                mac_data_out_last <= set_last;
180
 
181
                        when SEND_USER_DATA =>
182
                                mac_data_out <= ip_tx.data.data_out;
183
                                tx_data_valid <= ip_tx.data.data_out_valid;
184
                                mac_data_out_last <= ip_tx.data.data_out_last;
185
 
186
                        when others =>
187
                                mac_data_out <= (others => '0');
188
                                tx_data_valid <= '0';                                            -- not transmitting during this phase
189
                                mac_data_out_last <= '0';
190
                end case;
191
 
192
                mac_data_out_valid <= tx_data_valid and mac_data_out_ready;
193
 
194
                -- set signal defaults
195
                next_tx_state <= IDLE;
196
                set_tx_state <= '0';
197
                tx_count_mode <= HOLD;
198
                tx_data <= x"00";
199
                set_last <= '0';
200
                set_tx_mac <= '0';
201
                set_chn_reqd <= HOLD;
202
                set_mac_lku_req <= HOLD;
203
                next_tx_result <= IPTX_RESULT_NONE;
204
                set_tx_result <= '0';
205
                tx_count_val <= (others => '0');
206
 
207
                -- set temp signals
208
                total_length <= std_logic_vector(unsigned(ip_tx.hdr.data_length) + 20);         -- total length = user data length + header length (bytes)
209
 
210
                -- TX FSM
211
                case tx_state is
212
                        when IDLE =>
213
                                ip_tx_data_out_ready <= '0';             -- in this state, we are unable to accept user data for tx
214
                                tx_count_mode <= RST;
215
                                set_chn_reqd <= CLR;
216
                                if ip_tx_start = '1' then
217
                                        -- check header count for error if too high
218
                                        if unsigned(ip_tx.hdr.data_length) > 1480 then
219
                                                next_tx_result <= IPTX_RESULT_ERR;
220
                                                set_tx_result <= '1';
221
                                        else
222
                                                -- TODO - check if we already have the mac addr for this ip, if so, bypass the WAIT_MAC state
223
 
224
                                                -- req the mac address for this ip
225
                                                set_mac_lku_req <= SET;
226
                                                next_tx_result <= IPTX_RESULT_SENDING;
227
                                                set_tx_result <= '1';
228
                                                next_tx_state <= WAIT_MAC;
229
                                                set_tx_state <= '1';
230
                                        end if;
231
                                else
232
                                        set_mac_lku_req <= CLR;
233
                                end if;
234
 
235
                        when WAIT_MAC =>
236
                                ip_tx_data_out_ready <= '0';             -- in this state, we are unable to accept user data for tx
237
                                if arp_req_rslt.got_mac = '1' then
238
                                        -- save the MAC we got back from the ARP lookup
239
                                        set_tx_mac <= '1';
240
                                        set_chn_reqd <= SET;
241
                                        set_mac_lku_req <= CLR;
242
                                        -- check for optimise when already have the channel
243
                                        if mac_tx_granted = '1' then
244
                                                -- ready to send data
245
                                                next_tx_state <= SEND_ETH_HDR;
246
                                                set_tx_state <= '1';
247
                                        else
248
                                                next_tx_state <= WAIT_CHN;
249
                                                set_tx_state <= '1';
250
                                        end if;
251
                                elsif arp_req_rslt.got_err = '1' then
252
                                        set_mac_lku_req <= CLR;
253
                                        next_tx_result <= IPTX_RESULT_ERR;
254
                                        set_tx_result <= '1';
255
                                        next_tx_state <= IDLE;
256
                                        set_tx_state <= '1';
257
                                end if;
258
 
259
                        when WAIT_CHN =>
260
                                ip_tx_data_out_ready <= '0';             -- in this state, we are unable to accept user data for tx
261
                                if mac_tx_granted = '1' then
262
                                        -- ready to send data
263
                                        next_tx_state <= SEND_ETH_HDR;
264
                                        set_tx_state <= '1';
265
                                end if;
266
                                -- probably should handle a timeout here
267
 
268
                        when SEND_ETH_HDR =>
269
                                ip_tx_data_out_ready <= '0';             -- in this state, we are unable to accept user data for tx
270
                                if mac_data_out_ready = '1' then
271
                                        if tx_count = x"00d" then
272
                                                tx_count_mode <= RST;
273
                                                next_tx_state <= SEND_IP_HDR;
274
                                                set_tx_state <= '1';
275
                                        else
276
                                                tx_count_mode <= INCR;
277
                                        end if;
278
                                        case tx_count is
279
                                                when x"000"  => tx_data <= tx_mac (47 downto 40);                       -- trg = mac from ARP lookup
280
                                                when x"001"  => tx_data <= tx_mac (39 downto 32);
281
                                                when x"002"  => tx_data <= tx_mac (31 downto 24);
282
                                                when x"003"  => tx_data <= tx_mac (23 downto 16);
283
                                                when x"004"  => tx_data <= tx_mac (15 downto 8);
284
                                                when x"005"  => tx_data <= tx_mac (7 downto 0);
285
                                                when x"006"  => tx_data <= our_mac_address (47 downto 40);      -- src = our mac
286
                                                when x"007"  => tx_data <= our_mac_address (39 downto 32);
287
                                                when x"008"  => tx_data <= our_mac_address (31 downto 24);
288
                                                when x"009"  => tx_data <= our_mac_address (23 downto 16);
289
                                                when x"00a"  => tx_data <= our_mac_address (15 downto 8);
290
                                                when x"00b"  => tx_data <= our_mac_address (7 downto 0);
291
                                                when x"00c"  => tx_data <= x"08";                                                                       -- pkt type = 0800 : IP                                         
292
                                                when x"00d"  => tx_data <= x"00";
293
                                                when others =>
294
                                                        -- shouldnt get here - handle as error
295
                                                        next_tx_result <= IPTX_RESULT_ERR;
296
                                                        set_tx_result <= '1';
297
                                                        next_tx_state <= IDLE;
298
                                                        set_tx_state <= '1';
299
                                        end case;
300
                                end if;
301
 
302
                        when SEND_IP_HDR =>
303
                                ip_tx_data_out_ready <= '0';             -- in this state, we are unable to accept user data for tx
304
                                if mac_data_out_ready    = '1' then
305
                                        if tx_count = x"013" then
306
                                                tx_count_val <= x"001";
307
                                                tx_count_mode <= SET;
308
                                                next_tx_state <= SEND_USER_DATA;
309
                                                set_tx_state <= '1';
310
                                        else
311
                                                tx_count_mode <= INCR;
312
                                        end if;
313
                                        case tx_count is
314
                                                when x"000"  => tx_data <= x"45";                                                                       -- v4, 5 words in hdr
315
                                                when x"001"  => tx_data <= x"00";                                                                       -- service type
316
                                                when x"002"  => tx_data <= total_length (15 downto 8);          -- total length
317
                                                when x"003"  => tx_data <= total_length (7 downto 0);
318
                                                when x"004"  => tx_data <= x"00";                                                                       -- identification
319
                                                when x"005"  => tx_data <= x"00";
320
                                                when x"006"  => tx_data <= x"00";                                                                       -- flags and fragment offset
321
                                                when x"007"  => tx_data <= x"00";
322
                                                when x"008"  => tx_data <= IP_TTL;                                                                      -- TTL
323
                                                when x"009"  => tx_data <= ip_tx.hdr.protocol;                                  -- protocol
324
                                                when x"00a"  => tx_data <= tx_hdr_cks (15 downto 8);                    -- HDR checksum
325
                                                when x"00b"  => tx_data <= tx_hdr_cks (7 downto 0);                      -- HDR checksum
326
                                                when x"00c" => tx_data <= our_ip_address (31 downto 24);                -- src ip
327
                                                when x"00d" => tx_data <= our_ip_address (23 downto 16);
328
                                                when x"00e" => tx_data <= our_ip_address (15 downto 8);
329
                                                when x"00f" => tx_data <= our_ip_address (7 downto 0);
330
                                                when x"010" => tx_data <= ip_tx.hdr.dst_ip_addr (31 downto 24); -- dst ip
331
                                                when x"011" => tx_data <= ip_tx.hdr.dst_ip_addr (23 downto 16);
332
                                                when x"012" => tx_data <= ip_tx.hdr.dst_ip_addr (15 downto 8);
333
                                                when x"013" => tx_data <= ip_tx.hdr.dst_ip_addr (7 downto 0);
334
                                                when others =>
335
                                                        -- shouldnt get here - handle as error
336
                                                        next_tx_result <= IPTX_RESULT_ERR;
337
                                                        set_tx_result <= '1';
338
                                                        next_tx_state <= IDLE;
339
                                                        set_tx_state <= '1';
340
                                        end case;
341
                                end if;
342
 
343
                        when SEND_USER_DATA =>
344
                                ip_tx_data_out_ready <= mac_data_out_ready and mac_data_out_ready_reg;          -- in this state, we are always ready to accept user data for tx
345
                                if mac_data_out_ready = '1' then
346
                                        if ip_tx.data.data_out_valid = '1' or tx_count = x"000" then
347
                                                -- only increment if ready and valid has been subsequently established, otherwise data count moves on too fast
348
                                                if unsigned(tx_count) = unsigned(ip_tx.hdr.data_length) then
349
                                                        set_last <= '1';
350
                                                        set_chn_reqd <= CLR;
351
                                                        tx_data <= ip_tx.data.data_out;
352
                                                        next_tx_result <= IPTX_RESULT_SENT;
353
                                                        set_tx_result <= '1';
354
                                                        next_tx_state <= IDLE;
355
                                                        set_tx_state <= '1';
356
                                                else
357
                                                        tx_count_mode <= INCR;
358
                                                        tx_data <= ip_tx.data.data_out;
359
                                                end if;
360
                                        end if;
361
                                end if;
362
 
363
                end case;
364
        end process;
365
 
366
        -----------------------------------------------------------------------------
367
        -- sequential process to action control signals and change states and outputs
368
        -----------------------------------------------------------------------------
369
 
370
        tx_sequential : process (clk,reset,mac_data_out_ready_reg)
371
        begin
372
                if rising_edge(clk) then
373
                        mac_data_out_ready_reg <= mac_data_out_ready;
374
                else
375
                        mac_data_out_ready_reg <= mac_data_out_ready_reg;
376
                end if;
377
 
378
                if rising_edge(clk) then
379
                        if reset = '1' then
380
                                -- reset state variables
381
                                tx_state <= IDLE;
382
                                tx_count <= x"000";
383
                                tx_result_reg <= IPTX_RESULT_NONE;
384
                                tx_mac <= (others => '0');
385
                                tx_mac_chn_reqd <= '0';
386
                        else
387
                                -- Next tx_state processing
388
                                if set_tx_state = '1' then
389
                                        tx_state <= next_tx_state;
390
                                else
391
                                        tx_state <= tx_state;
392
                                end if;
393
 
394
                                -- tx result processing
395
                                if set_tx_result = '1' then
396
                                        tx_result_reg <= next_tx_result;
397
                                else
398
                                        tx_result_reg <= tx_result_reg;
399
                                end if;
400
 
401
                                -- control arp lookup request
402
                                case set_mac_lku_req is
403
                                        when SET =>
404
                                                arp_req_ip_reg <= ip_tx.hdr.dst_ip_addr;
405
                                                mac_lookup_req <= '1';
406
 
407
                                        when CLR =>
408
                                                mac_lookup_req <= '0';
409
                                                arp_req_ip_reg <= arp_req_ip_reg;
410
 
411
                                        when HOLD =>
412
                                                mac_lookup_req <= mac_lookup_req;
413
                                                arp_req_ip_reg <= arp_req_ip_reg;
414
                                end case;
415
 
416
                                -- save MAC
417
                                if set_tx_mac = '1' then
418
                                        tx_mac <= arp_req_rslt.mac;
419
                                else
420
                                        tx_mac <= tx_mac;
421
                                end if;
422
 
423
                                -- control access request to mac tx chn
424
                                case set_chn_reqd is
425
                                        when SET => tx_mac_chn_reqd <= '1';
426
                                        when CLR => tx_mac_chn_reqd <= '0';
427
                                        when HOLD => tx_mac_chn_reqd <= tx_mac_chn_reqd;
428
                                end case;
429
 
430
                                -- tx_count processing
431
                                case tx_count_mode is
432
                                        when RST  =>    tx_count <= x"000";
433
                                        when SET  =>    tx_count <= tx_count_val;
434
                                        when INCR =>    tx_count <= tx_count + 1;
435
                                        when HOLD =>    tx_count <= tx_count;
436
                                end case;
437
 
438
                        end if;
439
                end if;
440
        end process;
441
 
442
        -----------------------------------------------------------------------------
443
        -- Process to calculate CRC in parallel with pkt out processing
444
        -- this process must yield a valid CRC before it is required to be used in the hdr
445
        -----------------------------------------------------------------------------
446
 
447
        crc : process (clk,reset)
448
        begin
449
                if rising_edge(clk) then
450
                        case crc_state is
451
                                when IDLE =>
452
                                        if ip_tx_start = '1' then
453
                                                tx_hdr_cks <= x"004500";                        -- vers & hdr len & service
454
                                                crc_state <= TOT_LEN;
455
                                        end if;
456
 
457
                                when TOT_LEN =>
458
                                        tx_hdr_cks <= std_logic_vector (unsigned(tx_hdr_cks) + unsigned(total_length));
459
                                        crc_state <= ID;
460
 
461
                                when ID =>
462
                                        tx_hdr_cks <= tx_hdr_cks;
463
                                        crc_state <= FLAGS;
464
 
465
                                when FLAGS =>
466
                                        tx_hdr_cks <= tx_hdr_cks;
467
                                        crc_state <= TTL;
468
 
469
                                when TTL =>
470
                                        tx_hdr_cks <= std_logic_vector (unsigned(tx_hdr_cks) + unsigned(IP_TTL & ip_tx.hdr.protocol));
471
                                        crc_state <= CKS;
472
 
473
                                when CKS =>
474
                                        tx_hdr_cks <= tx_hdr_cks;
475
                                        crc_state <= SAH;
476
 
477
                                when SAH =>
478
                                        tx_hdr_cks <= std_logic_vector (unsigned(tx_hdr_cks) + unsigned(our_ip_address(31 downto 16)));
479
                                        crc_state <= SAL;
480
 
481
                                when SAL =>
482
                                        tx_hdr_cks <= std_logic_vector (unsigned(tx_hdr_cks) + unsigned(our_ip_address(15 downto 0)));
483
                                        crc_state <= DAH;
484
 
485
                                when DAH =>
486
                                        tx_hdr_cks <= std_logic_vector (unsigned(tx_hdr_cks) + unsigned(ip_tx.hdr.dst_ip_addr(31 downto 16)));
487
                                        crc_state <= DAL;
488
 
489
                                when DAL =>
490
                                        tx_hdr_cks <= std_logic_vector (unsigned(tx_hdr_cks) + unsigned(ip_tx.hdr.dst_ip_addr(15 downto 0)));
491
                                        crc_state <= FINAL;
492
 
493
                                when FINAL =>
494
                                        tx_hdr_cks <= inv_if_one(std_logic_vector (unsigned(tx_hdr_cks) + unsigned(tx_hdr_cks(23 downto 16))),'1');
495
                                        crc_state <= WAIT_END;
496
 
497
                                when WAIT_END =>
498
                                        tx_hdr_cks <= tx_hdr_cks;
499
                                        if ip_tx_start = '0' then
500
                                                crc_state <= IDLE;
501
                                        else
502
                                                crc_state <= WAIT_END;
503
                                        end if;
504
 
505
 
506
                        end case;
507
                end if;
508
  end process;
509
 
510
 
511
end Behavioral;
512
 

powered by: WebSVN 2.1.0

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