Advertisement
tariq786

ethernet_test_top

Jul 15th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module ethernet_test_top(
  2.     input wire clk_100_pin,
  3.    
  4.     output wire PhyResetOut_pin,
  5.     input wire MII_TX_CLK_pin, // 25 MHz clock for 100 Mbps - not used here
  6.     output reg [7:0] GMII_TXD_pin,
  7.     output reg GMII_TX_EN_pin,
  8.     output reg GMII_TX_ER_pin,
  9.     output wire GMII_TX_CLK_pin,
  10.     input wire [7:0] GMII_RXD_pin,
  11.     input wire GMII_RX_DV_pin,
  12.     input wire GMII_RX_ER_pin,
  13.     input wire GMII_RX_CLK_pin,
  14.     output wire MDC_pin,
  15.     inout wire MDIO_pin,
  16.    
  17.     output wire [7:0] leds,
  18. //  input wire [7:0] sw,
  19.     input wire [5:0] btn,
  20.    
  21.     output wire rs232_tx
  22.    );
  23.  
  24.     // System clock
  25.     wire clk_100;
  26.     IBUFG ibufg_100 (
  27.         .I(clk_100_pin),
  28.         .O(clk_100));
  29.        
  30.     // 125 MHz for PHY. 90 degree shifted clock drives PHY's GMII_TX_CLK.
  31.     wire clk_125, clk_125_GTX_CLK, clk_125_GTX_CLK_n, pll_locked;
  32.     wire pll_rst;
  33.     clk_125_tx clk_125_tx(
  34.         .CLK_IN1(clk_100),
  35.         .CLK_OUT1(clk_125), // 0 deg
  36.         .CLK_OUT2(clk_125_GTX_CLK), // 90 deg
  37.         .CLK_OUT3(),
  38.         .RESET(pll_rst),
  39.         .LOCKED(pll_locked));
  40.  
  41.     // PLL reset logic
  42.     // Based on http://forums.xilinx.com/t5/Spartan-Family-FPGAs/RESET-SIGNALS/m-p/133182#M10198
  43.     reg [25:0] pll_status_counter = 0;
  44.     always @(posedge clk_100)
  45.         if (pll_locked)
  46.             pll_status_counter <= 0;
  47.         else
  48.             pll_status_counter <= pll_status_counter + 1'b1;
  49.            
  50.     assign pll_rst = (pll_status_counter > (2**26 - 26'd20)); // Reset for 20 cycles
  51.        
  52.     // The USRP2 has a 125 MHz oscillator connected to clk_to_mac. While the
  53.     // 88E1111 generates a 125 MHz reference (125CLK), this isn't connected.
  54.     // We generate this clock from the Atlys' 100 MHz oscillator using the DCM.
  55.     wire clk_to_mac;
  56.     assign clk_to_mac = clk_125;
  57.    
  58.     // USRP2 runs the GEMAC's FIFOs at 100 MHz, though this is buffered through a DCM.
  59.     wire dsp_clk;
  60.     assign dsp_clk = clk_125;
  61.    
  62.     // USRP2 runs its CPU and the Wishbone bus at 50 MHz system clock, possibly due to
  63.     // speed limitations in the Spartan-3. Let's try running it at full speed.
  64.     wire wb_clk;
  65.     assign wb_clk = clk_125;
  66.  
  67.  
  68.     // Hold the FSMs in reset until the PLL has locked
  69.     wire dsp_rst;
  70.    
  71.     reset reset (
  72.         .clk(clk_100),
  73.         .pll_lock(pll_locked),
  74.         .rst_1(),
  75.        
  76.         .clk_2(dsp_clk),
  77.         .rst_2(dsp_rst),
  78.    
  79.         .ext_reset(sw_reconfig));
  80.  
  81.     //  Drive the GTX_CLK output from a DDR register
  82.     wire GMII_GTX_CLK_int;
  83.    
  84.     ODDR2 ODDR_gmii (
  85.         .Q(GMII_TX_CLK_pin),      // Data output (connect directly to top-level port)
  86.       .C0(clk_125_GTX_CLK),    // 0 degree clock input
  87.       .C1(~clk_125_GTX_CLK),    // 180 degree clock input
  88.       .CE(1'b1),    // Clock enable input
  89.       .D0(1'b0),    // Posedge data input
  90.       .D1(1'b1),    // Negedge data input
  91.       .R(1'b0),      // Synchronous reset input
  92.       .S(1'b0)       // Synchronous preset input
  93.       );
  94.  
  95.     // Register MAC outputs
  96.     wire GMII_TX_EN, GMII_TX_ER;
  97.     wire [7:0] GMII_TXD;
  98.    
  99.     always @(posedge GMII_GTX_CLK_int)
  100.     begin
  101.         GMII_TX_EN_pin <= GMII_TX_EN;
  102.         GMII_TX_ER_pin <= GMII_TX_ER;
  103.         GMII_TXD_pin <= GMII_TXD;
  104.     end
  105.    
  106.     // LEDs for debugging
  107.     reg [7:0] ledreg;
  108.     assign leds = ledreg;
  109.    
  110.     always @(posedge dsp_clk) begin
  111.         ledreg <= {4'd0, gemac_debug};
  112.     end
  113.  
  114.     // Sync a pushbutton to FSM clock to initiate packet
  115.     wire sw_reconfig;
  116.     wire sw_send_packet;
  117.     edge_detect edge_detect_s1 (.async_sig(btn[0]), .clk(dsp_clk), .rise(), .fall(sw_send_packet));
  118.     edge_detect edge_detect_s2 (.async_sig(btn[1]), .clk(dsp_clk), .rise(), .fall(sw_reconfig));
  119.  
  120.    
  121.     // The top module of the USRP2 MAC core
  122.     localparam dw = 32; // WB data bus width
  123.     localparam aw = 8; // WB address bus width
  124.     wire wb_rst;
  125.    wire rd2_dst_rdy, wr2_dst_rdy;
  126.     wire wr2_src_rdy, rd2_src_rdy;
  127.    wire [3:0]    wr2_flags;
  128.     wire [3:0]   rd2_flags;
  129.    wire [31:0]  rd2_data;
  130.     wire [31:0]  wr2_data;
  131.    wire [dw-1:0] wb_dat_o;
  132.     wire [dw-1:0] wb_dat_i;
  133.     wire [aw-1:0] wb_adr;
  134.     wire wb_ack;
  135.     wire wb_stb, wb_cyc, wb_we;
  136.     wire [79:0] debug_mac;
  137.    
  138.     simple_gemac_wrapper #(
  139.         .RXFIFOSIZE(9), .TXFIFOSIZE(6)
  140.         ) simple_gemac_wrapper (
  141.        
  142.         .clk125(clk_to_mac),
  143.         .reset(wb_rst),
  144.      
  145.         // PHY pins
  146.       .GMII_GTX_CLK(GMII_GTX_CLK_int), .GMII_TX_EN(GMII_TX_EN),
  147.       .GMII_TX_ER(GMII_TX_ER), .GMII_TXD(GMII_TXD),
  148.       .GMII_RX_CLK(GMII_RX_CLK_pin), .GMII_RX_DV(GMII_RX_DV_pin),  
  149.       .GMII_RX_ER(GMII_RX_ER_pin), .GMII_RXD(GMII_RXD_pin),
  150.         .mdio(MDIO_pin), .mdc(MDC_pin),
  151.        
  152.         // I/O buses
  153.         .sys_clk(dsp_clk),
  154.       .rx_f36_data({rd2_flags,rd2_data}), .rx_f36_src_rdy(rd2_src_rdy), .rx_f36_dst_rdy(rd2_dst_rdy),
  155.       .tx_f36_data({wr2_flags,wr2_data}), .tx_f36_src_rdy(wr2_src_rdy), .tx_f36_dst_rdy(wr2_dst_rdy),
  156.        
  157.         // Wishbone signals
  158.       .wb_clk(wb_clk), .wb_rst(wb_rst), .wb_stb(wb_stb), .wb_cyc(wb_cyc), .wb_ack(wb_ack),
  159.       .wb_we(wb_we), .wb_adr(wb_adr), .wb_dat_i(wb_dat_o), .wb_dat_o(wb_dat_i),
  160.      
  161.       .debug(debug_mac));
  162.    
  163.     // After the PLL has locked, configure the MAC and PHY using a state machine
  164.     wire gemac_ready;
  165.     wire [3:0] gemac_debug;
  166.     gemac_configure gemac_configure (
  167.         .clk(wb_clk),
  168.        
  169.         // Wishbone signals
  170.         .wb_rst(wb_rst),
  171.         .wb_stb(wb_stb),
  172.         .wb_cyc(wb_cyc),
  173.         .wb_ack(wb_ack),
  174.         .wb_we(wb_we),
  175.         .wb_adr(wb_adr[7:0]),
  176.         .wb_dat_i(wb_dat_i),
  177.         .wb_dat_o(wb_dat_o),
  178.        
  179.         .phy_reset(PhyResetOut_pin), // Connect to PHY's reset pin
  180.         .reset(dsp_rst),
  181.         .debug(gemac_debug),
  182.         .ready(gemac_ready)); // Signal to rest of the system that negotiation is complete
  183.  
  184.  
  185.     // Send out Ethernet packets
  186.     packet_sender packet_sender (
  187.         .clk(dsp_clk),
  188.         .reset(~gemac_ready),
  189.         .wr_flags_o(wr2_flags),
  190.         .wr_data_o(wr2_data),
  191.         .wr_dst_rdy_i(wr2_dst_rdy),
  192.         .wr_src_rdy_o(wr2_src_rdy),
  193.        
  194. //      .packet_size_i(sw[7:0]),
  195. //      .start(sw_send_packet)
  196.         .start(1'b1)   
  197.        
  198.         );
  199.        
  200.    
  201.     // Receive Ethernet packets
  202.     wire [7:0] udp_data_out;
  203.     wire udp_data_out_en;
  204.    
  205.     packet_receiver packet_receiver (
  206.         .clk(dsp_clk),
  207.         .reset(~gemac_ready),
  208.        
  209.         .rd_flags_i(rd2_flags),
  210.         .rd_data_i(rd2_data),
  211.        
  212.         .rd_src_rdy_i(rd2_src_rdy),
  213.         .rd_dst_rdy_o(rd2_dst_rdy),
  214.        
  215.         .data_out_en(udp_data_out_en),
  216.         .data_out(udp_data_out)
  217.    
  218.     );
  219.    
  220.    
  221.    
  222.     // UART and a packet-sized FIFO
  223.  
  224.     // Baud rate generator - 150e6 / 976 / 16 ~ 9600 bps
  225.     reg [10:0] baud_count = 0;
  226.     reg en_16_x_baud;
  227.    
  228.     always @(posedge dsp_clk)
  229.         if (baud_count == 814) begin
  230.             baud_count <= 1'b0;
  231.             en_16_x_baud <= 1'b1;
  232.         end else begin
  233.             baud_count <= baud_count + 1'b1;
  234.             en_16_x_baud <= 1'b0;
  235.         end
  236.  
  237.  
  238.     reg [7:0] uart_byte;
  239.     reg uart_wr_en;
  240.    
  241.     always @(posedge dsp_clk) begin
  242.         uart_byte <= udp_data_out;
  243.         uart_wr_en <= udp_data_out_en;
  244.     end
  245.    
  246.     // No buffer here - will overrun with packets > 16 bytes without more flow control or a FIFO
  247.     uart_tx6 transmit (
  248.         .data_in(uart_byte),
  249.         .buffer_write(uart_wr_en),
  250.         .buffer_reset(1'b0),
  251.         .en_16_x_baud(en_16_x_baud),
  252.         .serial_out(rs232_tx),
  253.         .buffer_data_present(),
  254.         .buffer_full(),
  255.         .buffer_half_full(),
  256.         .clk(dsp_clk));
  257.  
  258. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement