aboutsummaryrefslogtreecommitdiffstats
path: root/eth/demo/verilog/demo.v
blob: 649903c9a259b5e10e08b605ec8623f805f1b2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
module demo(
  Reset_n,
  Clk_100M,
  Clk_125M, // GMII only

  RS232_TXD,
  RS232_RXD,

  USB_TXD,
  USB_RXD,

  //--- 10/100/1000BASE-T Ethernet PHY (MII/GMII)
  PHY_RESET_n,

  PHY_RXC,
  PHY_RXD,
  PHY_RXDV,
  PHY_RXER,

  PHY_GTX_CLK, // GMII only
  PHY_TXC,
  PHY_TXD,
  PHY_TXEN,
  PHY_TXER,

  PHY_COL,
  PHY_CRS,

  PHY_MDC,
  PHY_MDIO,

  // Misc. I/Os
  LED,
  Button
);

  input        Reset_n;
  input        Clk_100M;
  input        Clk_125M; // GMII

  output       RS232_TXD;
  input        RS232_RXD;

  output       USB_TXD;
  input        USB_RXD;

  //--- 10/100/1000BASE-T Ethernet PHY (MII/GMII)
  output       PHY_RESET_n;

  input        PHY_RXC;
  input [7:0]  PHY_RXD;
  input        PHY_RXDV;
  input        PHY_RXER;

  output       PHY_GTX_CLK; // GMII only
  input        PHY_TXC;
  output [7:0] PHY_TXD;
  output       PHY_TXEN;
  output       PHY_TXER;

  input        PHY_COL;
  input        PHY_CRS;

  output       PHY_MDC;
  inout        PHY_MDIO;

  // Misc. I/Os
  output [1:4] LED;

  input [1:4]  Button;

  //-------------------------------------------------------------------------
  // Local declarations
  //-------------------------------------------------------------------------

  // Rename to "standard" core clock name
  wire Clk = Clk_100M;

  reg [27:0] Counter;
  always @( negedge Reset_n or posedge Clk )
    if ( ~Reset_n )
      Counter <= 0;
    else
      Counter <= Counter + 1;

  assign LED[1:4] = Counter[27:24];

  //-------------------------------------------------------------------------
  // Instantiation of sub-modules
  //-------------------------------------------------------------------------

  //--- UART ----------------------------------------------------------------

  wire       UART_RXD;
  wire       UART_TXD;
  wire       UART_RxValid;
  wire [7:0] UART_RxData;
  wire       UART_TxReady;
  wire       UART_TxValid;
  wire [7:0] UART_TxData;

  demo_uart demo_uart(
    .Reset_n( Reset_n ),
    .Clk    ( Clk     ),

    // Interface to UART PHY
    .RXD_i( UART_RXD ),
    .TXD_o( UART_TXD ),

    // Clk is divided by (Prescaler+1) to generate 16x the bitrate
`ifdef EHDL_SIMULATION
    .Prescaler_i( 16'd3 ), // Corresponds to VERY FAST - for simulation only!
`else                      
    .Prescaler_i( 16'd650 ), // Corresponds to 9600 baud (assuming 100 MHz clock)
`endif
                      
    // Pulsed when RxData is valid
    .RxValid_o( UART_RxValid ),
    .RxData_o ( UART_RxData  ),

    // Asserted when ready for a new Tx byte
    .TxReady_o( UART_TxReady ),

    // Pulsed when TxData is valid
    .TxValid_i( UART_TxValid ),
    .TxData_i ( UART_TxData  )
  );

  // Transmit & receive in parallel on either RS232 or USB/RS232 interface
//  assign UART_RXD = RS232_RXD & USB_RXD; // RS232 signals are high when inactive
  assign     UART_RXD = RS232_RXD;

  assign RS232_TXD = UART_TXD;
  assign USB_TXD   = UART_TXD;

  //--- UART-to-Wishbone Master ---------------------------------------------

  wire        WB_STB_ETH;
  wire        WB_STB_PDM;
  wire        WB_STB_PG;
  wire        WB_CYC;
  wire [14:0] WB_ADR;
  wire        WB_WE;
  wire [15:0] WB_DAT_Wr;
  wire [15:0] WB_DAT_Rd;
  wire        WB_ACK;

  demo_wishbone_master demo_wishbone_master(
    .Reset_n( Reset_n ),
    .Clk    ( Clk     ),

    //--- UART interface

    // Pulsed when RxData_i is valid
    .RxValid_i( UART_RxValid ),
    .RxData_i ( UART_RxData  ),

    // Asserted when ready for a new Tx byte
    .TxReady_i( UART_TxReady ),

    // Pulsed when TxData_o is valid
    .TxValid_o( UART_TxValid ),
    .TxData_o ( UART_TxData  ),

    //--- Wishbone interface
    .STB_ETH_O( WB_STB_ETH ),
    .STB_PDM_O( WB_STB_PDM ),
    .STB_PG_O ( WB_STB_PG  ),
    .CYC_O    ( WB_CYC     ),
    .ADR_O    ( WB_ADR     ),
    .WE_O     ( WB_WE      ), 
    .DAT_O    ( WB_DAT_Wr  ),
    .DAT_I    ( WB_DAT_Rd  ),
    .ACK_I    ( WB_ACK     )
  );

  //--- Wishbone clients ----------------------------------------------------

  //--- Packet Descriptor Memory --------------------------------------------

  wire [15:0] WB_DAT_Rd_PDM;
  wire        WB_ACK_PDM;

  wire        PDM_Rd;
  wire [13:0] PDM_Addr;
  wire [31:0] PDM_RdData;

  demo_packet_descriptor_memory demo_packet_descriptor_memory(
    .Reset_n( Reset_n ),
    .Clk    ( Clk     ),

    //--- Wishbone interface
    .STB_I( WB_STB_PDM    ),
    .CYC_I( WB_CYC        ),
    .ADR_I( WB_ADR        ),
    .WE_I ( WB_WE         ), 
    .DAT_I( WB_DAT_Wr     ),
    .DAT_O( WB_DAT_Rd_PDM ),
    .ACK_O( WB_ACK_PDM    ),

    //--- Packet Generator interface
    // RdData_o is always valid exactly one clock after Addr_i changes
    // and Rd_i is asserted
    .Rd_i    ( PDM_Rd     ),
    .Addr_i  ( PDM_Addr   ),
    .RdData_o( PDM_RdData )
  );

  //--- Packet Generator ----------------------------------------------------

  wire [15:0] WB_DAT_Rd_PG;
  wire        WB_ACK_PG;

  wire        Rx_mac_ra;
  wire        Rx_mac_rd;
  wire [31:0] Rx_mac_data;
  wire [1:0]  Rx_mac_BE;
  wire        Rx_mac_pa;
  wire        Rx_mac_sop;
  wire        Rx_mac_err;
  wire        Rx_mac_eop;

  wire        Tx_mac_wa;
  wire        Tx_mac_wr;
  wire [31:0] Tx_mac_data;
  wire [1:0]  Tx_mac_BE;
  wire        Tx_mac_sop;
  wire        Tx_mac_eop;

  demo_packet_generator demo_packet_generator(
    .Reset_n( Reset_n ),
    .Clk    ( Clk     ),

    //--- Wishbone interface
    .STB_I( WB_STB_PG    ),
    .CYC_I( WB_CYC       ),
    .ADR_I( WB_ADR[1:0]  ),
    .WE_I ( WB_WE        ),
    .DAT_I( WB_DAT_Wr    ),
    .DAT_O( WB_DAT_Rd_PG ),
    .ACK_O( WB_ACK_PG    ),

    //--- Packet Descriptor Memory interface
    // RdData_i is always valid exactly one clock after Addr_o changes
    // and Rd_o is asserted
    .Rd_o    ( PDM_Rd     ),
    .Addr_o  ( PDM_Addr   ),
    .RdData_i( PDM_RdData ),

    //--- User (packet) interface
    .Rx_mac_ra  ( Rx_mac_ra   ),
    .Rx_mac_rd  ( Rx_mac_rd   ),
    .Rx_mac_data( Rx_mac_data ),
    .Rx_mac_BE  ( Rx_mac_BE   ),
    .Rx_mac_pa  ( Rx_mac_pa   ),
    .Rx_mac_sop ( Rx_mac_sop  ),
    .Rx_mac_err ( Rx_mac_err  ),
    .Rx_mac_eop ( Rx_mac_eop  ),

    .Tx_mac_wa  ( Tx_mac_wa   ),
    .Tx_mac_wr  ( Tx_mac_wr   ),
    .Tx_mac_data( Tx_mac_data ),
    .Tx_mac_BE  ( Tx_mac_BE   ),
    .Tx_mac_sop ( Tx_mac_sop  ),
    .Tx_mac_eop ( Tx_mac_eop  )
  );

  //--- Simple Wishbone client ----------------------------------------------

  reg [15:0] Reg1;
  reg [15:0] Reg2;

  reg        WB_ACK_Reg;
  reg [15:0] WB_DAT_Reg;

  always @( negedge Reset_n or posedge Clk )
    if ( ~Reset_n )
      begin
        WB_ACK_Reg <= 0;
        WB_DAT_Reg <= 'b0;

        Reg1 <= 16'h1234;
        Reg2 <= 16'hABCD;
      end
    else
      begin
        WB_ACK_Reg <= 0;
        if ( WB_CYC & ~( WB_STB_ETH | WB_STB_PG | WB_STB_PDM ) )
          begin
            WB_ACK_Reg <= 1;
            if ( WB_WE )
              begin
                if ( WB_ADR[0] )
                  Reg2 <= WB_DAT_Wr;
                else
                  Reg1 <= WB_DAT_Wr;
              end
            else
              begin
                if ( WB_ADR[0] )
                  WB_DAT_Reg <= Reg2;
                else
                  WB_DAT_Reg <= Reg1;
              end
          end
      end

  //--- DUT - Ethernet Core -------------------------------------------------

  wire [15:0] WB_DAT_Rd_ETH;
  wire        WB_ACK_ETH;

  wire [2:0] Speed;

  MAC_top dut(
    // System signals
    .Clk_125M( Clk_125M ),
    .Clk_user( Clk      ),
    .Speed   ( Speed    ),

    // Wishbone compliant core host interface
    .RST_I( ~Reset_n ),
    .CLK_I( Clk     ),
    .STB_I( WB_STB_ETH    ),
    .CYC_I( WB_CYC        ),
    .ADR_I( WB_ADR[6:0]   ),
    .WE_I ( WB_WE         ),
    .DAT_I( WB_DAT_Wr     ),
    .DAT_O( WB_DAT_Rd_ETH ),
    .ACK_O( WB_ACK_ETH    ),

    // User (packet) interface
    .Rx_mac_ra  ( Rx_mac_ra   ),
    .Rx_mac_rd  ( Rx_mac_rd   ),
    .Rx_mac_data( Rx_mac_data ),
    .Rx_mac_BE  ( Rx_mac_BE   ),
    .Rx_mac_pa  ( Rx_mac_pa   ),
    .Rx_mac_sop ( Rx_mac_sop  ),
    .Rx_mac_err ( Rx_mac_err  ),
    .Rx_mac_eop ( Rx_mac_eop  ),

    .Tx_mac_wa  ( Tx_mac_wa   ),
    .Tx_mac_wr  ( Tx_mac_wr   ),
    .Tx_mac_data( Tx_mac_data ),
    .Tx_mac_BE  ( Tx_mac_BE   ),
    .Tx_mac_sop ( Tx_mac_sop  ),
    .Tx_mac_eop ( Tx_mac_eop  ),

    // PHY interface (GMII/MII)
    .Gtx_clk( PHY_GTX_CLK  ), // Used only in GMII mode
    .Rx_clk ( PHY_RXC      ),
    .Tx_clk ( PHY_TXC      ),  // Used only in MII mode
    .Tx_er  ( PHY_TXER     ),
    .Tx_en  ( PHY_TXEN     ),
    .Txd    ( PHY_TXD      ),
    .Rx_er  ( PHY_RXER     ),
    .Rx_dv  ( PHY_RXDV     ),
    .Rxd    ( PHY_RXD      ),
    .Crs    ( PHY_CRS      ),
    .Col    ( PHY_COL      ),

    // MDIO interface (to PHY)
    .Mdio( PHY_MDIO ),
    .Mdc ( PHY_MDC  )
  );

  //--- Combination of Wishbone read data and acknowledge -------------------

  assign WB_DAT_Rd = ({16{WB_ACK_Reg}} & WB_DAT_Reg   ) |
                     ({16{WB_ACK_PDM}} & WB_DAT_Rd_PDM) |
                     ({16{WB_ACK_PG }} & WB_DAT_Rd_PG ) |
                     ({16{WB_ACK_ETH}} & WB_DAT_Rd_ETH);

  assign WB_ACK = WB_ACK_Reg | WB_ACK_PDM | WB_ACK_PG | WB_ACK_ETH;

  assign PHY_RESET_n = Reset_n;                 

endmodule