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

Subversion Repositories udp_ip_stack

[/] [udp_ip_stack/] [trunk/] [contrib/] [from_tim/] [udp_ip_stack/] [tags/] [v1.3/] [rtl/] [vhdl/] [arp.vhd] - Blame information for rev 35

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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