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

Subversion Repositories cheap_ethernet

[/] [cheap_ethernet/] [trunk/] [Ethernet_test/] [ethernet_test.v] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 graver
`timescale 1ns / 1ps
2
`include "const.vh"
3
//////////////////////////////////////////////////////////////////////////////////
4
// Company: 
5
// Engineer: Ivan Krutov (grvfrv@gmail.com)
6
// 
7
// Create Date:    10:36:10 08/28/2012 
8
// Design Name:    Cheap Ethernet test project
9
// Module Name:    ethernet_test
10
// Project Name:   Cheap Ethernet
11
// Target Devices: Spartan 3E
12
// Tool versions:  ISE 14.1
13
// Description:    Base functional of ARP, ICMP, UDP server, UDP client.
14
//
15
// Dependencies:   EthernetRX.v, EthernetTX.v, TENBASET_RxD.v, TENBASET_TxD.v,
16
//                      const.vh
17
// Revision: 
18
// Revision 0.9 beta
19
// Additional Comments: 
20
//
21
//////////////////////////////////////////////////////////////////////////////////
22
 
23
module ethernet_test(clk_in,
24
        Ethernet_RDp, Ethernet_RDm, Ethernet_TDp, Ethernet_TDm, Ethernet_LED_Link, Ethernet_LED_Act,
25
        PushButton, LED_Test);
26
 
27
input clk_in;                                                           // 50 MHz
28
input Ethernet_RDp, Ethernet_RDm;       // Ethernel input
29
output Ethernet_TDp, Ethernet_TDm;      // Ethernel output
30
output Ethernet_LED_Link;                               // Ethernet link - Not realized! (VCC)
31
output Ethernet_LED_Act;                                // Etheret activity (low - active)
32
input PushButton;                                                       // Pushbutton for send data test
33
output LED_Test;
34
 
35
// Server config
36
wire [31:0] HostIP       = 32'hC0A8012C; // 192.168.1.44
37
wire [47:0] HostMAC      = 48'h001234567890;
38
wire [15:0] HostPort     = 16'd1024;
39
 
40
// Client config
41
wire [31:0] RemoteIP             = 32'hC0A80102; // 192.168.1.2
42
reg [47:0] RemoteMAC             = 48'h0;
43
wire [15:0] RemotePort   = 16'd1024;
44
 
45
wire clk20, clk50;
46
wire Ethernet_RD;
47
 
48
reg SendStart = 0;
49
wire SendDataReq;
50
reg [7:0] SendData = 0;
51
reg [9:0] SendDataLen = 0;
52
wire [9:0] SendDataPos;
53
wire SendingPacket;
54
reg [2:0] SendPacketType = 0;
55
 
56
reg [31:0] SendToIP   = 32'h0;
57
reg [47:0] SendToMAC  = 48'h0;
58
reg [15:0] SendToPort = 16'h0;
59
 
60
wire RcvStart, RcvStop;
61
wire [7:0] RcvData;
62
wire [9:0] RcvDataLen;
63
wire [9:0] RcvDataPos;
64
wire RcvDataAvailable;
65
wire RcvCRCValid;
66
wire ReceivingPacket;
67
wire [2:0] RcvPacketType;
68
wire [31:0] RcvFromIP;
69
wire [47:0] RcvFromMAC;
70
wire [15:0] RcvFromPort;
71
wire [15:0] RcvICMPId;
72
wire [15:0] RcvICMPSeq;
73
wire [15:0] RcvICMPCRC;
74
 
75
wire Ethernet_LED_Link = 1;
76
reg Ethernet_LED_Act = 0;
77
reg LED_Test = 0;
78
reg last_btn_st = 0;
79
 
80
reg PacketReceived = 0;
81
reg PacketProcessed = 0;
82
reg [7:0] Cmd = 0;
83
reg [7:0] Arg = 0;
84
reg SendReq = 0;
85
 
86
reg [2:0] SavedPacketType = 0;
87
reg [9:0] SavedDataLen = 0;
88
reg [15:0] SavedPort = 0;
89
reg SavedEcho = 0;
90
reg Echo = 0;
91
 
92
wire [16:0] SendICMPCRC1 = {RcvICMPCRC[15:8] + 9'h08, RcvICMPCRC[7:0]};
93
wire [15:0] SendICMPCRC2 = SendICMPCRC1[15:0] + SendICMPCRC1[16];
94
 
95
wire [7:0] FIFOOut;
96
wire FIFOFull;
97
wire FIFOEmpty;
98
 
99
 
100
DCMMAIN DCMMAIN1 (
101
    .CLKIN_IN(clk_in),  // 50 MHz
102
    .RST_IN(1'b0),
103
    .CLKDV_OUT(),
104
    .CLKFX_OUT(clk20),
105
    .CLKIN_IBUFG_OUT(),
106
    .CLK0_OUT(clk50),
107
         .LOCKED_OUT());
108
 
109
   IBUFDS #(
110
      .IBUF_DELAY_VALUE("0"),    // Specify the amount of added input delay for
111
                                 //    the buffer: "0"-"12" (Spartan-3E)
112
      .IFD_DELAY_VALUE("0"),  // Specify the amount of added delay for input
113
                                 //    register: "AUTO", "0"-"6" (Spartan-3E)
114
      .IOSTANDARD("DIFF_HSTL_III_18")     // Specify the input I/O standard
115
   ) IBUFDS_EthRD (
116
      .O(Ethernet_RD),  // Buffer output
117
      .I(Ethernet_RDp),  // Diff_p buffer input (connect directly to top-level port)
118
      .IB(Ethernet_RDm) // Diff_n buffer input (connect directly to top-level port)
119
   );
120
 
121
FIFORX FIFO_RD (
122
  .rst(RcvStart), // input rst
123
  .wr_clk(clk50), // input wr_clk
124
  .rd_clk(clk20), // input rd_clk
125
  .din(RcvData), // input [7 : 0] din
126
  .wr_en(RcvDataAvailable), // input wr_en
127
  .rd_en(SendDataReq /*&& SendingPacket*/), // input rd_en
128
  .dout(FIFOOut), // output [7 : 0] dout
129
  .full(FIFOFull), // output full
130
  .empty(FIFOEmpty) // output empty
131
);
132
 
133
EthernetTX EthTX1 (clk20,
134
        HostIP, SendToIP, HostMAC, SendToMAC, HostPort, SendToPort,
135
   SendStart, SendDataReq, SendData, SendDataLen, SendDataPos, SendingPacket,
136
        SendPacketType, RcvICMPId, RcvICMPSeq, SendICMPCRC2,
137
        Ethernet_TDp, Ethernet_TDm);
138
 
139
EthernetRX EthRX1 (clk50,
140
        Ethernet_RD,
141
        HostIP, HostMAC, HostPort,
142
        RcvStart, RcvStop, RcvData, RcvDataLen, RcvDataPos, RcvDataAvailable, RcvCRCValid, ReceivingPacket,
143
        RcvPacketType, RcvICMPId, RcvICMPSeq, RcvICMPCRC,
144
        RcvFromIP, RcvFromMAC, RcvFromPort);
145
 
146
 
147
// Ethernet LEDs
148
reg [19:0] EthLEDCnt = 0;
149
always @(posedge clk20) begin
150
        EthLEDCnt <= EthLEDCnt + 1;
151
        Ethernet_LED_Act <= Ethernet_LED_Act && !(ReceivingPacket | SendingPacket);
152
        if (Ethernet_LED_Act)
153
                EthLEDCnt <= 0;
154
        if (&EthLEDCnt)
155
                Ethernet_LED_Act <= 1;
156
end
157
 
158
// Transmit packets
159
always @(posedge clk20) begin
160
        SendStart = 0;
161
        if (!SendingPacket && (PacketReceived || SendReq) && !PacketProcessed)
162
                begin
163
                Echo <= 0;
164
                PacketProcessed <= 1;
165
                SendToIP <= RcvFromIP;
166
                SendToMAC <= RcvFromMAC;
167
 
168
                if (RcvPacketType ==    `ARPReply)                      // Remote MAC resolved
169
                        begin
170
                        RemoteMAC = RcvFromMAC;
171
                        SendToPort = SavedPort;
172
                        SendStart = 1;
173
                        SendPacketType = SavedPacketType;
174
                        SendDataLen = SavedDataLen;
175
                        Echo <= SavedEcho;
176
                        end
177
                else if ((RcvPacketType == `UDP) || SendReq)    // Send UDP packet
178
                        begin
179
                        if (SendReq)
180
                                begin
181
                                SendToIP <= RemoteIP;
182
                                SendToMAC <= RemoteMAC;
183
                                end
184
                        else if (Cmd == `CmdDataEcho)
185
                                Echo <= 1;
186
                        SendToPort = RemotePort;
187
                        SendStart = 1;
188
                        SendPacketType = `UDP;
189
                        SendDataLen = SendReq ? 18 : RcvDataLen;
190
                        end
191
                else if (RcvPacketType == `ARPReq)                              // Send ARP reply
192
                        begin
193
                        SendStart = 1;
194
                        SendPacketType = `ARPReply;
195
                        SendDataLen = 18;
196
                        end
197
                else if (RcvPacketType == `ICMPReq)                             // Send ICMP reply
198
                        begin
199
                        Echo <= 1;
200
                        SendStart = 1;
201
                        SendPacketType = `ICMPReply;
202
                        SendDataLen = RcvDataLen;
203
                        end
204
 
205
                if (~|RemoteMAC && SendStart && SendReq)                // If remote MAC unknown send ARP request
206
                        begin                                                                                                   // TODO: clear RemoteMAC after 2 minutes inactivity
207
                        SavedPacketType <= SendPacketType;
208
                        SavedDataLen <= SendDataLen;
209
                        SavedPort <= SendToPort;
210
                        SavedEcho <= Echo;
211
                        SendToIP <= RemoteIP;
212
                        SendStart = 1;
213
                        SendPacketType = `ARPReq;
214
                        SendDataLen = 18;
215
                        end
216
                end
217
 
218
        if (!PacketReceived && !SendReq)
219
                PacketProcessed <= 0;
220
 
221
        case (SendDataPos)
222
                0: SendData <= Cmd;
223
                1: SendData <= Arg;
224
                default: SendData <= 0;
225
        endcase
226
 
227
        if (Echo /*RcvPacketType == `ICMPReq*/) // Cmd: Data echo
228
                SendData <= FIFOOut;
229
end
230
 
231
 
232
reg [25:0] cnt = 0;
233
reg AutoSend = 1;
234
 
235
// Receive packets
236
always @(posedge clk50) begin
237
        if (~&cnt)
238
                cnt <= cnt + 1;
239
 
240
        if (PacketProcessed)
241
                begin
242
                PacketReceived <= 0;
243
                SendReq <= 0;
244
                end
245
 
246
        if (RcvDataAvailable)
247
                if (RcvPacketType == `UDP)
248
                        case (RcvDataPos)
249
                                0: Cmd <= RcvData;
250
                                1: Arg <= RcvData;
251
                        endcase
252
 
253
        if (RcvStop && RcvCRCValid)
254
                begin
255
                PacketReceived <= 1;
256
                if (RcvPacketType == `UDP)
257
                        begin
258
                        if (Cmd == `CmdLEDCtrl)                 // Received Cmd: Leds control
259
                                begin
260
                                LED_Test <= Arg[0];
261
                                Cmd <= `CmdDone;                                // Send reply: Cmd: Done
262
                                end
263
                        else if (Cmd == `CmdStatus)     // Received Cmd: Get Status
264
                                begin
265
                                Cmd <= `CmdStatus;                      // Send reply: Cmd: Status
266
                                Arg <= {5'd0, PushButton, AutoSend, LED_Test};  // Status
267
                                end
268
                        else if (Cmd == `CmdSetConfig)// Received Cmd: Set Config
269
                                begin
270
                                Cmd <= `CmdDone;                                // Send reply: Cmd: Status
271
                                AutoSend <= Arg[0];
272
                                end
273
                        end
274
                end
275
 
276
        if (!SendingPacket && !ReceivingPacket && !PacketProcessed)
277
                begin
278
                last_btn_st <= PushButton;
279
                if (last_btn_st != PushButton || (&cnt && AutoSend))
280
                        begin
281
                        cnt <= 0;
282
                        Cmd <= `CmdSwChanged;   // Cmd: Pushbuttons state changed
283
                        if (&cnt)
284
                                Cmd <= `CmdStatus;
285
                        Arg <= {8{PushButton}};
286
                        SendReq <= 1;
287
                        end
288
                end
289
end
290
 
291
endmodule

powered by: WebSVN 2.1.0

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