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

Subversion Repositories udp_ip_stack

[/] [udp_ip_stack/] [trunk/] [rtl/] [vhdl/] [arp.vhd] - Blame information for rev 4

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:    12:00:04 05/31/2011 
6
-- Design Name: 
7
-- Module Name:    arp - Behavioral 
8
-- Project Name: 
9
-- Target Devices: 
10
-- Tool versions: 
11
-- Description:
12
--              handle simple IP lookup in cache
13
--              request cache fill through ARP protocol if required
14
--              cache is simple 1 deep
15
--              Handle ARP protocol
16
--              Respond to ARP requests and replies
17
--              Ignore pkts that are not ARP
18
--              Ignore pkts that are not addressed to us
19
--
20
-- Dependencies: 
21
--
22
-- Revision: 
23
-- Revision 0.01 - File Created
24 4 pjf
-- Revision 0.02 - Added req for mac tx and wait for grant
25
-- Revision 0.03 - Added data_out_first
26
 
27 2 pjf
-- Additional Comments: 
28
--
29
----------------------------------------------------------------------------------
30
library IEEE;
31
use IEEE.STD_LOGIC_1164.ALL;
32
use IEEE.NUMERIC_STD.ALL;
33
use work.arp_types.all;
34
 
35
entity arp is
36
    Port (
37
                        -- lookup request signals
38
                        arp_req_req                     : in arp_req_req_type;
39
                        arp_req_rslt            : out arp_req_rslt_type;
40
                        -- MAC layer RX signals
41
                        data_in_clk             : in  STD_LOGIC;
42
                        reset                           : in  STD_LOGIC;
43
                        data_in                                 : in  STD_LOGIC_VECTOR (7 downto 0);             -- ethernet frame (from dst mac addr through to last byte of frame)
44
                        data_in_valid           : in  STD_LOGIC;                                                                        -- indicates data_in valid on clock
45
                        data_in_last            : in  STD_LOGIC;                                                                        -- indicates last data in frame
46
                        -- MAC layer TX signals
47
                        mac_tx_req                      : out std_logic;                                                                        -- indicates that ip wants access to channel (stays up for as long as tx)
48
                        mac_tx_granted          : in std_logic;                                                                 -- indicates that access to channel has been granted            
49
                        data_out_clk            : in std_logic;
50
                        data_out_ready          : in std_logic;                                                                 -- indicates system ready to consume data
51
                        data_out_valid          : out std_logic;                                                                        -- indicates data out is valid
52 4 pjf
                        data_out_first          : out std_logic;                                                                        -- with data out valid indicates the first byte of a frame
53 2 pjf
                        data_out_last           : out std_logic;                                                                        -- with data out valid indicates the last byte of a frame
54
                        data_out                                : out std_logic_vector (7 downto 0);             -- ethernet frame (from dst mac addr through to last byte of frame)
55
                        -- system signals
56
                        our_mac_address         : in STD_LOGIC_VECTOR (47 downto 0);
57
                        our_ip_address  : in STD_LOGIC_VECTOR (31 downto 0);
58
                        req_count                       : out STD_LOGIC_VECTOR(7 downto 0)                       -- count of arp pkts received
59
                        );
60
end arp;
61
 
62
architecture Behavioral of arp is
63
 
64
        type req_state_type is (IDLE,LOOKUP,REQUEST,WAIT_REPLY);
65
        type rx_state_type is (IDLE,PARSE,PROCESS_ARP,WAIT_END);
66
        type rx_event_type is (NO_EVENT,DATA);
67
        type count_mode_type is (RST,INCR,HOLD);
68
        type arp_oper_type is (NOP,REQUEST,REPLY);
69
        type set_clr_type is (SET, CLR, HOLD);
70
 
71
        type tx_state_type is (IDLE,WAIT_MAC,SEND);
72
 
73
 
74
        type arp_entry_type is record
75
                ip                              : std_logic_vector (31 downto 0);
76
                mac                             : std_logic_vector (47 downto 0);
77
                is_valid                        : std_logic;
78
                reply_required  : std_logic;
79
        end record;
80
 
81
        -- state variables
82
        signal req_state                        : req_state_type;
83
        signal req_ip_addr              : std_logic_vector (31 downto 0);                -- IP address to lookup
84
        signal mac_addr_found   : STD_LOGIC_VECTOR (47 downto 0);                -- mac address found
85
        signal mac_addr_valid_reg: std_logic;
86
        signal send_request_needed              : std_logic;
87
        signal tx_mac_chn_reqd  : std_logic;
88
 
89
        signal rx_state                         : rx_state_type;
90
        signal rx_count                         : unsigned (7 downto 0);
91
        signal arp_operation            : arp_oper_type;
92
        signal arp_req_count            : unsigned (7 downto 0);
93
        signal arp_entry                        : arp_entry_type;                               -- arp entry store
94
        signal new_arp_entry            : arp_entry_type;
95
        signal tx_state                 : tx_state_type;
96
        signal tx_count                         : unsigned (7 downto 0);
97
 
98
-- FIXME        - remove these debug state signals
99
        signal arp_err_data             : std_logic_vector (7 downto 0);
100
        signal set_err_data             : std_logic;
101
 
102
  attribute keep : string;
103
  attribute keep of arp_err_data             : signal is "true";
104
 
105
        -- requester control signals
106
        signal next_req_state   : req_state_type;
107
        signal set_req_state            : std_logic;
108
        signal set_req_ip                       : std_logic;
109
        signal set_mac_addr             : std_logic;
110
        signal set_mac_addr_invalid : std_logic;
111
        signal set_send_req             : std_logic;
112
        signal clear_send_req   : std_logic;
113
 
114
 
115
        -- rx control signals
116
        signal next_rx_state    : rx_state_type;
117
        signal set_rx_state             : std_logic;
118
        signal rx_event                         : rx_event_type;
119
        signal rx_count_mode    : count_mode_type;
120
        signal set_arp_oper             : std_logic;
121
        signal arp_oper_set_val : arp_oper_type;
122
        signal dataval                  : std_logic_vector (7 downto 0);
123
        signal set_arp_entry_request            : std_logic;
124
 
125
        signal set_mac5                         : std_logic;
126
        signal set_mac4                         : std_logic;
127
        signal set_mac3                         : std_logic;
128
        signal set_mac2                         : std_logic;
129
        signal set_mac1                         : std_logic;
130
        signal set_mac0                         : std_logic;
131
 
132
        signal set_ip3                  : std_logic;
133
        signal set_ip2                  : std_logic;
134
        signal set_ip1                  : std_logic;
135
        signal set_ip0                  : std_logic;
136
 
137
        -- tx control signals
138
        signal next_tx_state    : tx_state_type;
139
        signal set_tx_state             : std_logic;
140
        signal tx_count_mode            : count_mode_type;
141
        signal clear_reply_req  : std_logic;
142
        signal set_chn_reqd             : set_clr_type;
143
        signal kill_data_out_valid      : std_logic;
144
 
145
 
146
        -- function to determine whether the rx pkt is an arp pkt and whether we want to process it
147
        -- Returns 1 if we should discard
148
        -- The following will make us ignore the frame (all values hexadecimal):
149
        -- PDU type /= 0806
150
        -- Protocol Type /= 0800
151
        -- Hardware Type /= 1
152
        -- Hardware Length /= 6
153
        -- Protocol Length /= 4
154
        -- Operation /= 1 or 2
155
        -- Target IP /= our IP (i.er. message is not meant for us)
156
        --
157
        function not_our_arp(data : STD_LOGIC_VECTOR; count : unsigned; our_ip : std_logic_vector) return std_logic is
158
        begin
159
                if
160
                        (count = 12 and data /= x"08") or                                               -- PDU type 0806 : ARP
161
                        (count = 13 and data /= x"06") or
162
                        (count = 14 and data /= x"00") or                                               -- HW type 1 : eth
163
                        (count = 15 and data /= x"01") or
164
                        (count = 16 and data /= x"08") or                                               -- Protocol 0800 : IP
165
                        (count = 17 and data /= x"00") or
166
                        (count = 18 and data /= x"06") or                                               -- HW Length 6
167
                        (count = 19 and data /= x"04") or                                               -- protocol length 4
168
                        (count = 20 and data /= x"00") or                                               -- operation 1 or 2 (req or reply)
169
                        (count = 21 and data /= x"01" and data /= x"02") or
170
                        (count = 38 and data /= our_ip(31 downto 24)) or        -- target IP is ours
171
                        (count = 39 and data /= our_ip(23 downto 16)) or
172
                        (count = 40 and data /= our_ip(15 downto 8)) or
173
                        (count = 41 and data /= our_ip(7 downto 0))
174
                then
175
                        return '1';
176
                else
177
                        return '0';
178
                end if;
179
        end function not_our_arp;
180
 
181
begin
182
        req_combinatorial : process (
183
                -- input signals
184
                arp_req_req,
185
                -- state variables
186
                req_state, req_ip_addr, mac_addr_found, mac_addr_valid_reg, send_request_needed, arp_entry,
187
                -- control signals
188
                next_req_state, set_req_state, set_req_ip, set_mac_addr,set_mac_addr_invalid,set_send_req, clear_send_req)
189
        begin
190
                -- set output followers
191
                arp_req_rslt.got_err <= '0';     -- errors not returned in this version
192
                -- zero time response to lookup request if already in cache
193
                if arp_req_req.lookup_req = '1' and arp_req_req.ip = arp_entry.ip and arp_entry.is_valid = '1' then
194
                        arp_req_rslt.got_mac <= '1';
195
                        arp_req_rslt.mac <= arp_entry.mac;
196
                else
197
                        arp_req_rslt.got_mac <= mac_addr_valid_reg;
198
                        arp_req_rslt.mac <= mac_addr_found;
199
                end if;
200
 
201
                -- set signal defaults
202
                next_req_state <= IDLE;
203
                set_req_state <= '0';
204
                set_req_ip <= '0';
205
                set_mac_addr <= '0';
206
                set_mac_addr_invalid <= '0';
207
                set_send_req <= '0';
208
                clear_send_req <= '0';
209
 
210
                -- REQ FSM
211
                case req_state is
212
                        when IDLE =>
213
                                if arp_req_req.lookup_req = '1' then
214
                                        -- check if we already have the info in cache
215
                                        if arp_req_req.ip = arp_entry.ip and arp_entry.is_valid = '1' then
216
                                                -- already have this IP
217
                                                set_mac_addr <= '1';
218
                                        else
219
                                                next_req_state <= LOOKUP;
220
                                                set_req_state <= '1';
221
                                                set_req_ip <= '1';
222
                                                set_mac_addr_invalid <= '1';
223
                                        end if;
224
                                end if;
225
 
226
                        when LOOKUP =>
227
                                if arp_entry.ip = req_ip_addr and arp_entry.is_valid = '1' then
228
                                        -- already have this IP
229
                                        next_req_state <= IDLE;
230
                                        set_req_state <= '1';
231
                                        set_mac_addr <= '1';
232
                                else
233
                                        -- need to request mac for this IP
234
                                        next_req_state <= REQUEST;
235
                                        set_req_state <= '1';
236
                                        set_send_req <= '1';
237
                                end if;
238
 
239
                        when REQUEST =>
240
                                        clear_send_req <= '1';
241
                                        next_req_state <= WAIT_REPLY;
242
                                        set_req_state <= '1';
243
 
244
                        when WAIT_REPLY =>
245
                                if arp_entry.is_valid = '1' then
246
                                        -- have reply, go back to LOOKUP state to see if it is the right one
247
                                        next_req_state <= LOOKUP;
248
                                        set_req_state <= '1';
249
                                end if;
250
                                -- TODO: add timeout here                               
251
 
252
                end case;
253
        end process;
254
 
255
        req_sequential : process (data_in_clk,reset)
256
        begin
257
                if rising_edge(data_in_clk) then
258
                        if reset = '1' then
259
                                -- reset state variables
260
                                req_state <= IDLE;
261
                                req_ip_addr <= (others => '0');
262
                                mac_addr_found <= (others => '0');
263
                                mac_addr_valid_reg <= '0';
264
                                send_request_needed <= '0';
265
                        else
266
                                -- Next req_state processing
267
                                if set_req_state = '1' then
268
                                        req_state <= next_req_state;
269
                                else
270
                                        req_state <= req_state;
271
                                end if;
272
 
273
                                -- Latch the requested IP address
274
                                if set_req_ip = '1' then
275
                                        req_ip_addr <= arp_req_req.ip;
276
                                else
277
                                        req_ip_addr <= req_ip_addr;
278
                                end if;
279
 
280
                                -- send request to TX&RX FSMs to send an ARP request
281
                                if set_send_req = '1' then
282
                                        send_request_needed <= '1';
283
                                elsif clear_send_req = '1' then
284
                                        send_request_needed <= '0';
285
                                else
286
                                        send_request_needed <= send_request_needed;
287
                                end if;
288
 
289
                                -- Set the found mac address
290
                                if set_mac_addr = '1' then
291
                                        mac_addr_found <= arp_entry.mac;
292
                                        mac_addr_valid_reg <= '1';
293
                                elsif set_mac_addr_invalid = '1' then
294
                                        mac_addr_found <= (others => '0');
295
                                        mac_addr_valid_reg <= '0';
296
                                else
297
                                        mac_addr_found <= mac_addr_found;
298
                                        mac_addr_valid_reg <= mac_addr_valid_reg;
299
                                end if;
300
 
301
                        end if;
302
                end if;
303
        end process;
304
 
305
 
306
        rx_combinatorial : process (
307
                -- input signals
308
                data_in, data_in_valid, data_in_last, our_ip_address,
309
                -- state variables
310
                rx_state, rx_count, arp_operation, arp_req_count, arp_err_data,
311
                -- control signals
312
                next_rx_state, set_rx_state, rx_event, rx_count_mode, set_arp_oper, arp_oper_set_val,
313
                dataval,set_mac5,set_mac4,set_mac3,set_mac2,set_mac1,set_mac0,set_ip3,set_ip2,set_ip1,set_ip0, set_err_data,
314
                set_arp_entry_request)
315
        begin
316
                -- set output followers
317
                req_count <= STD_LOGIC_VECTOR(arp_req_count);
318
 
319
                -- set signal defaults
320
                next_rx_state <= IDLE;
321
                set_rx_state <= '0';
322
                rx_event <= NO_EVENT;
323
                rx_count_mode <= HOLD;
324
                set_arp_oper <= '0';
325
                arp_oper_set_val <= NOP;
326
                dataval <= (others => '0');
327
                set_mac5 <= '0';
328
                set_mac4 <= '0';
329
                set_mac3 <= '0';
330
                set_mac2 <= '0';
331
                set_mac1 <= '0';
332
                set_mac0 <= '0';
333
                set_ip3 <= '0';
334
                set_ip2 <= '0';
335
                set_ip1 <= '0';
336
                set_ip0 <= '0';
337
                set_arp_entry_request <= '0';
338
                set_err_data <= '0';
339
 
340
                -- determine event (if any)
341
                if data_in_valid = '1' then
342
                        rx_event <= DATA;
343
                end if;
344
 
345
                -- RX FSM
346
                case rx_state is
347
                        when IDLE =>
348
                                rx_count_mode <= RST;
349
                                case rx_event is
350
                                        when NO_EVENT => -- (nothing to do)
351
                                        when DATA =>
352
                                                next_rx_state <= PARSE;
353
                                                set_rx_state <= '1';
354
                                                rx_count_mode <= INCR;
355
                                end case;
356
 
357
                        when PARSE =>
358
                                case rx_event is
359
                                        when NO_EVENT => -- (nothing to do)
360
                                        when DATA =>
361
                                                rx_count_mode <= INCR;
362
                                                -- handle early frame termination
363
                                                if data_in_last = '1' then
364
                                                        next_rx_state <= IDLE;
365
                                                        set_rx_state <= '1';
366
                                                else
367
                                                        -- check for end of frame. Also, detect and discard if not our frame
368
                                                        if rx_count = 42 then
369
                                                                next_rx_state <= PROCESS_ARP;
370
                                                                set_rx_state <= '1';
371
                                                        elsif not_our_arp(data_in,rx_count,our_ip_address) = '1' then
372
                                                                dataval <= data_in;
373
                                                                set_err_data <= '1';
374
                                                                next_rx_state <= WAIT_END;
375
                                                                set_rx_state <= '1';
376
                                                        elsif rx_count = 21 then
377
                                                                -- capture ARP operation
378
                                                                case data_in is
379
                                                                        when x"01" =>
380
                                                                                arp_oper_set_val <= REQUEST;
381
                                                                                set_arp_oper <= '1';
382
                                                                        when x"02" =>
383
                                                                                arp_oper_set_val <= REPLY;
384
                                                                                set_arp_oper <= '1';
385
                                                                        when others =>  -- ignore other values
386
                                                                end case;
387
                                                        -- capture source mac addr
388
                                                        elsif rx_count = 22 then
389
                                                                set_mac5 <= '1';
390
                                                                dataval <= data_in;
391
                                                        elsif rx_count = 23 then
392
                                                                set_mac4 <= '1';
393
                                                                dataval <= data_in;
394
                                                        elsif rx_count = 24 then
395
                                                                set_mac3 <= '1';
396
                                                                dataval <= data_in;
397
                                                        elsif rx_count = 25 then
398
                                                                set_mac2 <= '1';
399
                                                                dataval <= data_in;
400
                                                        elsif rx_count = 26 then
401
                                                                set_mac1 <= '1';
402
                                                                dataval <= data_in;
403
                                                        elsif rx_count = 27 then
404
                                                                set_mac0 <= '1';
405
                                                                dataval <= data_in;
406
                                                        -- capture source ip addr
407
                                                        elsif rx_count = 28 then
408
                                                                set_ip3 <= '1';
409
                                                                dataval <= data_in;
410
                                                        elsif rx_count = 29 then
411
                                                                set_ip2 <= '1';
412
                                                                dataval <= data_in;
413
                                                        elsif rx_count = 30 then
414
                                                                set_ip1 <= '1';
415
                                                                dataval <= data_in;
416
                                                        elsif rx_count = 31 then
417
                                                                set_ip0 <= '1';
418
                                                                dataval <= data_in;
419
                                                        end if;
420
                                                end if;
421
                                end case;
422
 
423
                        when PROCESS_ARP =>
424
                                next_rx_state <= WAIT_END;
425
                                set_rx_state <= '1';
426
                                case arp_operation is
427
                                        when NOP => -- (nothing to do)
428
                                        when REQUEST =>
429
                                                        set_arp_entry_request <= '1';
430
                                                        arp_oper_set_val <= NOP;
431
                                                        set_arp_oper <= '1';
432
                                        when REPLY =>
433
                                                        set_arp_entry_request <= '1';
434
                                                        arp_oper_set_val <= NOP;
435
                                                        set_arp_oper <= '1';
436
                                end case;
437
 
438
                        when WAIT_END =>
439
                                case rx_event is
440
                                        when NO_EVENT => -- (nothing to do)
441
                                        when DATA =>
442
                                                if data_in_last = '1' then
443
                                                        next_rx_state <= IDLE;
444
                                                        set_rx_state <= '1';
445
                                                end if;
446
                                end case;
447
 
448
                end case;
449
 
450
        end process;
451
 
452
        rx_sequential : process (data_in_clk)
453
        begin
454
                if rising_edge(data_in_clk) then
455
                        if reset = '1' then
456
                                -- reset state variables
457
                                rx_state <= IDLE;
458
                                rx_count <= x"00";
459
                                arp_operation <= NOP;
460
                                arp_req_count <= x"00";
461
                                -- reset arp entry store
462
                                arp_entry.ip <= x"00000000";
463
                                arp_entry.mac <= x"000000000000";
464
                                arp_entry.is_valid <= '0';
465
                                arp_entry.reply_required <= '0';
466
                                arp_err_data <= (others => '0');
467
                        else
468
                                -- Next rx_state processing
469
                                if set_rx_state = '1' then
470
                                        rx_state <= next_rx_state;
471
                                else
472
                                        rx_state <= rx_state;
473
                                end if;
474
 
475
                                -- rx_count processing
476
                                case rx_count_mode is
477
                                        when RST =>
478
                                                rx_count <= x"00";
479
                                        when INCR =>
480
                                                rx_count <= rx_count + 1;
481
                                        when HOLD =>
482
                                                rx_count <= rx_count;
483
                                end case;
484
 
485
                                -- err data
486
                                if set_err_data = '1' then
487
                                        arp_err_data <= data_in;
488
                                else
489
                                        arp_err_data <= arp_err_data;
490
                                end if;
491
 
492
                                -- arp operation processing
493
                                if set_arp_oper = '1' then
494
                                        arp_operation <= arp_oper_set_val;
495
                                else
496
                                        arp_operation <= arp_operation;
497
                                end if;
498
 
499
                                -- source mac capture
500
                                if (set_mac5 = '1') then new_arp_entry.mac(47 downto 40) <= dataval; end if;
501
                                if (set_mac4 = '1') then new_arp_entry.mac(39 downto 32) <= dataval; end if;
502
                                if (set_mac3 = '1') then new_arp_entry.mac(31 downto 24) <= dataval; end if;
503
                                if (set_mac2 = '1') then new_arp_entry.mac(23 downto 16) <= dataval; end if;
504
                                if (set_mac1 = '1') then new_arp_entry.mac(15 downto 8) <= dataval; end if;
505
                                if (set_mac0 = '1') then new_arp_entry.mac(7 downto 0) <= dataval; end if;
506
 
507
                                -- source ip capture
508
                                if (set_ip3 = '1') then new_arp_entry.ip(31 downto 24) <= dataval; end if;
509
                                if (set_ip2 = '1') then new_arp_entry.ip(23 downto 16) <= dataval; end if;
510
                                if (set_ip1 = '1') then new_arp_entry.ip(15 downto 8) <= dataval; end if;
511
                                if (set_ip0 = '1') then new_arp_entry.ip(7 downto 0) <= dataval; end if;
512
 
513
                                -- set arp entry request
514
                                if set_arp_entry_request = '1' then
515
                                        -- copy info from new entry to arp_entry and set reply required
516
                                        arp_entry.mac <= new_arp_entry.mac;
517
                                        arp_entry.ip <= new_arp_entry.ip;
518
                                        arp_entry.is_valid <= '1';
519
                                        if arp_operation = REQUEST then
520
                                                arp_entry.reply_required <= '1';
521
                                        else
522
                                                arp_entry.reply_required <= '0';
523
                                        end if;
524
                                        -- count another ARP pkt received
525
                                        arp_req_count <= arp_req_count + 1;
526
                                elsif clear_reply_req = '1' then
527
                                        -- note: clear_reply_req is set by tx logic, but handled in the clk domain of the rx
528
                                        -- maintain arp entry state, but reset the reply required flag
529
                                        arp_entry.mac <= arp_entry.mac;
530
                                        arp_entry.ip <= arp_entry.ip;
531
                                        arp_entry.is_valid <= arp_entry.is_valid;
532
                                        arp_entry.reply_required <= '0';
533
                                        arp_req_count <= arp_req_count;
534
                                elsif send_request_needed = '1' then
535
                                        -- set up the arp entry to take the request to be transmitted out by the TX FSM
536
                                        arp_entry.ip <= req_ip_addr;
537
                                        arp_entry.mac <= (others => '0');
538
                                        arp_entry.is_valid <= '0';
539
                                        arp_entry.reply_required <= '0';
540
                                else
541
                                        arp_entry <= arp_entry;
542
                                        arp_req_count <= arp_req_count;
543
                                end if;
544
 
545
                        end if;
546
                end if;
547
        end process;
548
 
549
        tx_combinatorial : process (
550
                -- input signals
551
                data_out_ready, send_request_needed, mac_tx_granted, our_mac_address, our_ip_address,
552
                -- state variables
553
                tx_state, tx_count, tx_mac_chn_reqd, arp_entry,
554
                -- control signals
555
                next_rx_state, set_rx_state, tx_count_mode, kill_data_out_valid,
556
                set_chn_reqd, clear_reply_req)
557
        begin
558
                -- set output followers
559 4 pjf
                mac_tx_req <= tx_mac_chn_reqd;
560 2 pjf
 
561 4 pjf
                -- set initial values for combinatorial outputs
562
                data_out_first <= '0';
563
 
564 2 pjf
                case tx_state is
565
                        when SEND   =>
566
                                if data_out_ready = '1' and kill_data_out_valid = '0' then
567
                                        data_out_valid <= '1';
568
                                else
569
                                        data_out_valid <= '0';
570
                                end if;
571
                        when OTHERS =>  data_out_valid <= '0';
572
                end case;
573
 
574
                -- set signal defaults
575
                next_tx_state <= IDLE;
576
                set_tx_state <= '0';
577
                tx_count_mode <= HOLD;
578
                data_out <= x"00";
579
                data_out_last <= '0';
580
                clear_reply_req <= '0';
581
                set_chn_reqd <= HOLD;
582
                kill_data_out_valid <= '0';
583
 
584
                -- TX FSM
585
                case tx_state is
586
                        when IDLE =>
587
                                tx_count_mode <= RST;
588
                                if arp_entry.reply_required = '1' then
589
                                        set_chn_reqd <= SET;
590
                                        next_tx_state <= WAIT_MAC;
591
                                        set_tx_state <= '1';
592
                                elsif send_request_needed = '1' then
593
                                        set_chn_reqd <= SET;
594
                                        next_tx_state <= WAIT_MAC;
595
                                        set_tx_state <= '1';
596
                                else
597
                                        set_chn_reqd <= CLR;
598
                                end if;
599
 
600
                        when WAIT_MAC =>
601
                                tx_count_mode <= RST;
602
                                if mac_tx_granted = '1' then
603
                                        next_tx_state <= SEND;
604
                                        set_tx_state <= '1';
605
                                end if;
606
                                -- TODO - should handle timeout here
607
 
608
                        when SEND =>
609
                                if data_out_ready = '1' then
610
                                                tx_count_mode <= INCR;
611
                                end if;
612
                                case tx_count is
613 4 pjf
                                        when x"00"  =>
614
                                                data_out_first <= data_out_ready;
615
                                                data_out <= x"ff";                                                              -- dst = broadcast
616
 
617 2 pjf
                                        when x"01"  => data_out <= x"ff";
618
                                        when x"02"  => data_out <= x"ff";
619
                                        when x"03"  => data_out <= x"ff";
620
                                        when x"04"  => data_out <= x"ff";
621
                                        when x"05"  => data_out <= x"ff";
622
                                        when x"06"  => data_out <= our_mac_address (47 downto 40);      -- src = our mac
623
                                        when x"07"  => data_out <= our_mac_address (39 downto 32);
624
                                        when x"08"  => data_out <= our_mac_address (31 downto 24);
625
                                        when x"09"  => data_out <= our_mac_address (23 downto 16);
626
                                        when x"0a"  => data_out <= our_mac_address (15 downto 8);
627
                                        when x"0b"  => data_out <= our_mac_address (7 downto 0);
628
                                        when x"0c"  => data_out <= x"08";                                                                       -- pkt type = 0806 : ARP
629
                                        when x"0d"  => data_out <= x"06";
630
                                        when x"0e"  => data_out <= x"00";                                                                       -- HW type = 0001 : eth
631
                                        when x"0f"  => data_out <= x"01";
632
                                        when x"10"  => data_out <= x"08";                                                                       -- protocol = 0800 : ip
633
                                        when x"11"  => data_out <= x"00";
634
                                        when x"12"  => data_out <= x"06";                                                                       -- HW size = 06
635
                                        when x"13"  => data_out <= x"04";                                                                       -- prot size = 04
636
 
637
                                        when x"14"  =>  data_out <= x"00";                                                                      -- opcode =             
638
                                        when x"15"  =>
639
                                                if arp_entry.is_valid = '1' then
640
                                                        data_out <= x"02";                                                                                                                                                      -- 02 : REPLY if arp_entry valid
641
                                                else
642
                                                        data_out <= x"01";                                                                                                                                                      -- 01 : REQ if arp_entry invalid
643
                                                end if;
644
 
645
                                        when x"16" => data_out <= our_mac_address (47 downto 40);       -- sender mac
646
                                        when x"17" => data_out <= our_mac_address (39 downto 32);
647
                                        when x"18" => data_out <= our_mac_address (31 downto 24);
648
                                        when x"19" => data_out <= our_mac_address (23 downto 16);
649
                                        when x"1a" => data_out <= our_mac_address (15 downto 8);
650
                                        when x"1b" => data_out <= our_mac_address (7 downto 0);
651
                                        when x"1c" => data_out <= our_ip_address (31 downto 24);        -- sender ip
652
                                        when x"1d" => data_out <= our_ip_address (23 downto 16);
653
                                        when x"1e" => data_out <= our_ip_address (15 downto 8);
654
                                        when x"1f" => data_out <= our_ip_address (7 downto 0);
655
                                        when x"20" => data_out <= arp_entry.mac (47 downto 40);         -- target mac
656
                                        when x"21" => data_out <= arp_entry.mac (39 downto 32);
657
                                        when x"22" => data_out <= arp_entry.mac (31 downto 24);
658
                                        when x"23" => data_out <= arp_entry.mac (23 downto 16);
659
                                        when x"24" => data_out <= arp_entry.mac (15 downto 8);
660
                                        when x"25" => data_out <= arp_entry.mac (7 downto 0);
661
                                        when x"26" => data_out <= arp_entry.ip  (31 downto 24);         -- target ip
662
                                        when x"27" => data_out <= arp_entry.ip   (23 downto 16);
663
                                        when x"28" => data_out <= arp_entry.ip   (15 downto 8);
664
 
665
                                        when x"29" =>
666
                                                data_out <= arp_entry.ip(7 downto 0);
667
                                                data_out_last <= '1';
668
 
669
                                        when x"2a" =>
670
                                                clear_reply_req <= '1';                 -- reset the reply request (done in the rx clk process domain)
671
                                                kill_data_out_valid <= '1';     -- data is no longer valid
672
                                                next_tx_state <= IDLE;
673
                                                set_tx_state <= '1';
674
 
675
                                        when others =>
676
                                                next_tx_state <= IDLE;
677
                                                set_tx_state <= '1';
678
                                end case;
679
                end case;
680
        end process;
681
 
682
        tx_sequential : process (data_out_clk,reset)
683
        begin
684
                if rising_edge(data_out_clk) then
685
                        if reset = '1' then
686
                                -- reset state variables
687
                                tx_state <= IDLE;
688
                                tx_mac_chn_reqd <= '0';
689
                        else
690
                                -- Next rx_state processing
691
                                if set_tx_state = '1' then
692
                                        tx_state <= next_tx_state;
693
                                else
694
                                        tx_state <= tx_state;
695
                                end if;
696
 
697
                                -- tx_count processing
698
                                case tx_count_mode is
699
                                        when RST =>
700
                                                tx_count <= x"00";
701
                                        when INCR =>
702
                                                tx_count <= tx_count + 1;
703
                                        when HOLD =>
704
                                                tx_count <= tx_count;
705
                                end case;
706
 
707
                                -- control access request to mac tx chn
708
                                case set_chn_reqd is
709
                                        when SET => tx_mac_chn_reqd <= '1';
710
                                        when CLR => tx_mac_chn_reqd <= '0';
711
                                        when HOLD => tx_mac_chn_reqd <= tx_mac_chn_reqd;
712
                                end case;
713
 
714
                        end if;
715
                end if;
716
        end process;
717
 
718
 
719
end Behavioral;
720
 

powered by: WebSVN 2.1.0

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