aboutsummaryrefslogtreecommitdiffstats
path: root/eth/demo/verilog/tb_demo.v
blob: c5a8a3f416f5a03e2ab8c92b64b45cbfbe142ab2 (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
`timescale 1ns / 1ns

module tb_demo;

  //-------------------- Instantiate Xilinx glbl module ----------------------
  // - this is needed to get ModelSim to work because e.g. I/O buffer models
  //   refer directly to glbl.GTS and similar signals

  wire GSR;
  wire GTS;
  xlnx_glbl glbl( .GSR( GSR ), .GTS( GTS ) );

  reg  VLOG_ExitSignal = 0;
  reg  Done = 0;
  reg  Error = 0;

  //-------------------------------------------------------------------------

  reg        Reset_n;
  reg        Clk_100M;
  reg        Clk_125M;

  wire       RS232_TXD;
  wire       RS232_RXD;

  wire       USB_TXD;
  wire       USB_RXD;

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

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

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

  wire       PHY_COL = 0;
  wire       PHY_CRS = 0;

  wire       PHY_MDC;
  wire       PHY_MDIO;

  wire [1:4] LED;

  reg [1:4]  Button = 4'b0000;

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

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

  //--- DUT

  demo demo(
    .Reset_n ( Reset_n  ),
    .Clk_100M( Clk_100M ),
    .Clk_125M( Clk_125M ),

    .RS232_TXD( RS232_TXD ),
    .RS232_RXD( RS232_RXD ),

    .USB_TXD( USB_TXD ),
    .USB_RXD( USB_RXD ),

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

    .PHY_RXC ( PHY_RXC  ),
    .PHY_RXD ( PHY_RXD  ),
    .PHY_RXDV( PHY_RXDV ),
    .PHY_RXER( PHY_RXER ),

    .PHY_GTX_CLK( PHY_GTX_CLK ), // GMII only
    .PHY_TXC    ( PHY_TXC  ),
    .PHY_TXD    ( PHY_TXD  ),
    .PHY_TXEN   ( PHY_TXEN ),
    .PHY_TXER   ( PHY_TXER ),

    .PHY_COL( PHY_COL ),
    .PHY_CRS( PHY_CRS ),

    .PHY_MDC ( PHY_MDC  ),
    .PHY_MDIO( PHY_MDIO ),

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

  //-------------------------------------------------------------------------
  // MII/GMII Ethernet PHY model

  reg [2:0]  Speed = 3'b000;

  Phy_sim U_Phy_sim(
    .Gtx_clk( PHY_GTX_CLK ),
    .Rx_clk ( PHY_RXC  ),
    .Tx_clk ( PHY_TXC  ),
    .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  ),
    .Speed  ( Speed    ),
    .Done   ( Done     )
  );

  //-------------------------------------------------------------------------
  // Generate all clocks & reset
  //-------------------------------------------------------------------------

  // Core master clock (100 MHz)
  initial 
    begin
      #10;
      while ( !Done )
        begin
          #5 Clk_100M = 0;
          #5 Clk_100M = 1;
        end
    end

  // GMII master clock (125 MHz)
  initial 
    begin
      #10;
      while ( !Done )
        begin
          #4 Clk_125M = 0;
          #4 Clk_125M = 1;
        end
    end

  initial
    begin
      Reset_n = 0;

      #103;
      Reset_n = 1;
    end

  //--- Emulate UART Transmitter --------------------------------------------

  parameter    PRESCALER_16X = 3;
  integer      Prescaler;
  integer      TxLen = 0;
  reg [2:0]    TxState;
  integer      TxBit;
  reg [1023:0] TxMsg;
  reg          TXD;
  reg          TxDone;

  always @( negedge Reset_n or posedge Clk_100M )
    if ( ~Reset_n )
      begin
        Prescaler <= 0;
        TxState   = 0;
        TXD       = 1;
        TxBit     = 0;
        TxDone    <= 0;
      end
    else
      begin
        TxDone <= 0;

        if ( Prescaler == ((PRESCALER_16X + 1)*16 -1) )
          Prescaler <= 0;
        else
          Prescaler <= Prescaler + 1;

        if ( Prescaler==0 )
          begin
            casez ( TxState )
              0: // IDLE
                begin
                  if ( TxLen != 0 )
                    begin // Send start bit!
                      TxBit = (TxLen-1)*8;
                      TxLen = TxLen - 1;
                      TXD = 0;
                      TxState = 1;
                    end
                end

              1: // Send next data bit
                begin
                  // Send next data bit
                  TXD = TxMsg[ TxBit ];
                  TxBit = TxBit + 1;
                  if ( (TxBit % 8)==0 )
                    // Next send two stop bits
                    TxState = 2;
                end

              2: // First of two stop bits
                begin
                  TXD = 1;
                  TxState = 3;
                end

              3: // Second of two stop bits
                begin
                  TXD = 1;
                  TxState = 0;
                  if ( TxLen == 0 )
                    // Done with transmission!
                    TxDone <= 1;
                end
            endcase
          end
      end

  assign RS232_RXD = TXD;
  assign USB_RXD = 1;

  //--- Send commands to the DUT --------------------------------------------

  initial
    begin
      #10;
      while ( ~Reset_n ) #10;

      // Wait a couple of clock edges before continuing to allow
      // internal logic to get out of reset
      repeat ( 5 )
        @( posedge Clk_100M );

      // Wait for the "READY" message to complete transmission
      #60000;

      // Select 100 Mbps
      Speed = 3'b010;
      TxMsg = "W 0022 0002 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8000 8003 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8001 0011 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8002 1234 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8003 5678 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8004 9ABC ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8005 DEF0 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8006 C5C0 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "W 8007 BABE ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      TxMsg = "R 8006 ";
      TxLen = 7;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      // Enable PG!
      TxMsg = "W 1000 0001 ";
      TxLen = 12;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      // Read back that PG has been enabled!
      TxMsg = "R 1000 ";
      TxLen = 7;
      while ( ~TxDone )
        @( posedge Clk_100M );

      #50000;

      #50000;

      Done = 1; #10;

      $stop;
    end

  //--- Directly accesses a register on the internal Wishbone bus, bypassing the UART interface

  task WrReg;
    input [15:0] Reg;
    input [15:0] Data;

    begin
    end
  endtask

endmodule