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 10

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

powered by: WebSVN 2.1.0

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