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

Subversion Repositories sdr_ctrl

[/] [sdr_ctrl/] [trunk/] [verif/] [tb/] [tb_core.sv] - Blame information for rev 46

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 dinesha
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////                                                              ////
4
////  This file is part of the SDRAM Controller project           ////
5
////  http://www.opencores.org/cores/sdr_ctrl/                    ////
6
////                                                              ////
7
////  Description                                                 ////
8
////  SDRAM CTRL definitions.                                     ////
9
////                                                              ////
10
////  To Do:                                                      ////
11
////    nothing                                                   ////
12 43 dinesha
//   Version  :0.1 - Test Bench automation is improvised with     ////
13
//             seperate data,address,burst length fifo.           ////
14
//             Now user can create different write and            ////
15
//             read sequence                                      ////
16 30 dinesha
////                                                              ////
17
////  Author(s):                                                  ////
18
////      - Dinesh Annayya, dinesha@opencores.org                 ////
19
////                                                              ////
20
//////////////////////////////////////////////////////////////////////
21
////                                                              ////
22
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
23
////                                                              ////
24
//// This source file may be used and distributed without         ////
25
//// restriction provided that this copyright statement is not    ////
26
//// removed from the file and that any derivative work contains  ////
27
//// the original copyright notice and the associated disclaimer. ////
28
////                                                              ////
29
//// This source file is free software; you can redistribute it   ////
30
//// and/or modify it under the terms of the GNU Lesser General   ////
31
//// Public License as published by the Free Software Foundation; ////
32
//// either version 2.1 of the License, or (at your option) any   ////
33
//// later version.                                               ////
34
////                                                              ////
35
//// This source is distributed in the hope that it will be       ////
36
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
37
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
38
//// PURPOSE.  See the GNU Lesser General Public License for more ////
39
//// details.                                                     ////
40
////                                                              ////
41
//// You should have received a copy of the GNU Lesser General    ////
42
//// Public License along with this source; if not, download it   ////
43
//// from http://www.opencores.org/lgpl.shtml                     ////
44
////                                                              ////
45
//////////////////////////////////////////////////////////////////////
46
 
47
// This testbench stand-alone verify the sdram core
48
 
49
`timescale 1ns/1ps
50
 
51
module tb_core;
52
 
53
parameter P_SYS  = 10;     //    100MHz
54
 
55
// General
56
reg            RESETN;
57
reg            sdram_clk;
58
 
59
initial sdram_clk = 0;
60
 
61
always #(P_SYS/2) sdram_clk = !sdram_clk;
62
 
63
parameter      dw              = 32;  // data width
64
parameter      tw              = 8;   // tag id width
65
parameter      bl              = 5;   // burst_lenght_width
66
 
67
//-------------------------------------------
68
// Application Interface bus
69
//-------------------------------------------
70
reg                   app_req            ; // Application Request
71
reg  [8:0]            app_req_len        ; // Burst Request length
72
wire                  app_req_ack        ; // Application Request Ack
73
reg [29:0]            app_req_addr       ; // Application Address
74
reg                   app_req_wr_n       ; // 1 -> Read, 0 -> Write
75
reg [dw-1:0]          app_wr_data        ; // Write Data
76
reg [dw/8-1:0]        app_wr_en_n        ; // Write Enable, Active Low
77
wire                  app_rd_valid       ; // Read Valid
78
wire                  app_last_rd        ; // Last Read Valid
79 46 dinesha
wire                  app_last_wr        ; // Last Write Valid
80 30 dinesha
wire [dw-1:0]         app_rd_data        ; // Read Data
81
 
82
//--------------------------------------------
83
// SDRAM I/F
84
//--------------------------------------------
85
 
86
`ifdef SDR_32BIT
87
   wire [31:0]           Dq                 ; // SDRAM Read/Write Data Bus
88
   wire [31:0]           sdr_dout           ; // SDRAM Data Out
89
   wire [31:0]           pad_sdr_din        ; // SDRAM Data Input
90
   wire [3:0]            sdr_den_n          ; // SDRAM Data Enable
91
   wire [3:0]            sdr_dqm            ; // SDRAM DATA Mask
92
`elsif SDR_16BIT
93
   wire [15:0]           Dq                 ; // SDRAM Read/Write Data Bus
94
   wire [15:0]           sdr_dout           ; // SDRAM Data Out
95
   wire [15:0]           pad_sdr_din        ; // SDRAM Data Input
96
   wire [1:0]            sdr_den_n          ; // SDRAM Data Enable
97
   wire [1:0]            sdr_dqm            ; // SDRAM DATA Mask
98
`else
99
   wire [7:0]           Dq                 ; // SDRAM Read/Write Data Bus
100
   wire [7:0]           sdr_dout           ; // SDRAM Data Out
101
   wire [7:0]           pad_sdr_din        ; // SDRAM Data Input
102
   wire [0:0]           sdr_den_n          ; // SDRAM Data Enable
103
   wire [0:0]           sdr_dqm            ; // SDRAM DATA Mask
104
`endif
105
 
106
wire [1:0]            sdr_ba             ; // SDRAM Bank Select
107
wire [11:0]           sdr_addr           ; // SDRAM ADRESS
108
wire                  sdr_init_done      ; // SDRAM Init Done
109
 
110
// to fix the sdram interface timing issue
111
wire #(2.0) sdram_clk_d = sdram_clk;
112
wire #(1.0) pad_clk     = sdram_clk_d;
113
 
114
`ifdef SDR_32BIT
115
 
116
   sdrc_core #(.SDR_DW(32),.SDR_BW(4)) u_dut(
117
`elsif SDR_16BIT
118
   sdrc_core #(.SDR_DW(16),.SDR_BW(2)) u_dut(
119
`else  // 8 BIT SDRAM
120
   sdrc_core #(.SDR_DW(8),.SDR_BW(1)) u_dut(
121
`endif
122
      // System
123
          .clk                (sdram_clk          ),
124
          .reset_n            (RESETN             ),
125
          .pad_clk            (pad_clk            ),
126
`ifdef SDR_32BIT
127
          .sdr_width          (2'b00              ), // 32 BIT SDRAM
128
`elsif SDR_16BIT
129
          .sdr_width          (2'b01              ), // 16 BIT SDRAM
130
`else
131
          .sdr_width          (2'b10              ), // 8 BIT SDRAM
132
`endif
133
          .cfg_colbits        (2'b00              ), // 8 Bit Column Address
134
 
135
 
136
/* Request from app */
137
          .app_req            (app_req            ),    // Transfer Request
138
          .app_req_addr       (app_req_addr       ),    // SDRAM Address
139
          .app_req_len        (app_req_len        ),    // Burst Length (in 16 bit words)
140
          .app_req_wrap       (1'b0               ),    // Wrap mode request (xfr_len = 4)
141
          .app_req_wr_n       (app_req_wr_n       ),    // 0 => Write request, 1 => read req
142
          .app_req_ack        (app_req_ack        ),    // Request has been accepted
143
          .sdr_core_busy_n    (                   ),    // OK to arbitrate next request
144
 
145
          .app_wr_data        (app_wr_data        ),
146
          .app_wr_en_n        (app_wr_en_n        ),
147
          .app_rd_data        (app_rd_data        ),
148
          .app_last_rd        (app_last_rd        ),
149 46 dinesha
          .app_last_wr        (app_last_wr        ),
150 30 dinesha
          .app_rd_valid       (app_rd_valid       ),
151
          .app_wr_next_req    (app_wr_next_req    ),
152
          .app_req_dma_last   (app_req            ),
153
 
154
/* Interface to SDRAMs */
155
          .sdr_cs_n           (sdr_cs_n           ),
156
          .sdr_cke            (sdr_cke            ),
157
          .sdr_ras_n          (sdr_ras_n          ),
158
          .sdr_cas_n          (sdr_cas_n          ),
159
          .sdr_we_n           (sdr_we_n           ),
160
          .sdr_dqm            (sdr_dqm            ),
161
          .sdr_ba             (sdr_ba             ),
162
          .sdr_addr           (sdr_addr           ),
163
          .pad_sdr_din        (Dq                 ),
164
          .sdr_dout           (sdr_dout           ),
165
          .sdr_den_n          (sdr_den_n          ),
166
 
167
    /* Parameters */
168
          .sdr_init_done      (sdr_init_done      ),
169 44 dinesha
          .cfg_req_depth      (2'h3               ),            //how many req. buffer should hold
170 30 dinesha
          .cfg_sdr_en         (1'b1               ),
171
          .cfg_sdr_mode_reg   (12'h033            ),
172
          .cfg_sdr_tras_d     (4'h4               ),
173
          .cfg_sdr_trp_d      (4'h2               ),
174
          .cfg_sdr_trcd_d     (4'h2               ),
175
          .cfg_sdr_cas        (3'h3               ),
176
          .cfg_sdr_trcar_d    (4'h7               ),
177
          .cfg_sdr_twr_d      (4'h1               ),
178 45 dinesha
          .cfg_sdr_rfsh       (12'h100            ), // reduced from 12'hC35
179 30 dinesha
          .cfg_sdr_rfmax      (3'h6               )
180
 
181
);
182
 
183
 
184
`ifdef SDR_32BIT
185
  assign Dq[7:0]    = (sdr_den_n[0] == 1'b0) ? sdr_dout[7:0]   : 8'hZZ;
186
  assign Dq[15:8]   = (sdr_den_n[1] == 1'b0) ? sdr_dout[15:8]  : 8'hZZ;
187
  assign Dq[23:16]  = (sdr_den_n[2] == 1'b0) ? sdr_dout[23:16] : 8'hZZ;
188
  assign Dq[31:24]  = (sdr_den_n[3] == 1'b0) ? sdr_dout[31:24] : 8'hZZ;
189
mt48lc2m32b2 #(.data_bits(32)) u_sdram32 (
190
          .Dq                 (Dq                 ) ,
191
          .Addr               (sdr_addr           ),
192
          .Ba                 (sdr_ba             ),
193
          .Clk                (sdram_clk_d        ),
194
          .Cke                (sdr_cke            ),
195
          .Cs_n               (sdr_cs_n           ),
196
          .Ras_n              (sdr_ras_n          ),
197
          .Cas_n              (sdr_cas_n          ),
198
          .We_n               (sdr_we_n           ),
199
          .Dqm                (sdr_dqm            )
200
     );
201
 
202
`elsif SDR_16BIT
203
 
204
assign Dq[7:0]  = (sdr_den_n[0] == 1'b0) ? sdr_dout[7:0]  : 8'hZZ;
205
assign Dq[15:8] = (sdr_den_n[1] == 1'b0) ? sdr_dout[15:8] : 8'hZZ;
206
 
207
   IS42VM16400K u_sdram16 (
208
          .dq                 (Dq                 ),
209
          .addr               (sdr_addr           ),
210
          .ba                 (sdr_ba             ),
211
          .clk                (sdram_clk_d        ),
212
          .cke                (sdr_cke            ),
213
          .csb                (sdr_cs_n           ),
214
          .rasb               (sdr_ras_n          ),
215
          .casb               (sdr_cas_n          ),
216
          .web                (sdr_we_n           ),
217
          .dqm                (sdr_dqm            )
218
    );
219
`else
220
 
221
assign Dq[7:0]  = (sdr_den_n[0] == 1'b0) ? sdr_dout[7:0]  : 8'hZZ;
222
 
223
mt48lc8m8a2 #(.data_bits(8)) u_sdram8 (
224
          .Dq                 (Dq                 ) ,
225
          .Addr               (sdr_addr           ),
226
          .Ba                 (sdr_ba             ),
227
          .Clk                (sdram_clk_d        ),
228
          .Cke                (sdr_cke            ),
229
          .Cs_n               (sdr_cs_n           ),
230
          .Ras_n              (sdr_ras_n          ),
231
          .Cas_n              (sdr_cas_n          ),
232
          .We_n               (sdr_we_n           ),
233
          .Dqm                (sdr_dqm            )
234
     );
235
`endif
236
 
237
//--------------------
238 43 dinesha
// data/address/burst length FIFO
239 30 dinesha
//--------------------
240 43 dinesha
int dfifo[$]; // data fifo
241
int afifo[$]; // address  fifo
242
int bfifo[$]; // Burst Length fifo
243 30 dinesha
 
244
reg [31:0] read_data;
245
reg [31:0] ErrCnt;
246
int k;
247
reg [31:0] StartAddr;
248
/////////////////////////////////////////////////////////////////////////
249
// Test Case
250
/////////////////////////////////////////////////////////////////////////
251
 
252
initial begin //{
253
  ErrCnt          = 0;
254
   app_req_addr  = 0;
255
   app_wr_data    = 0;
256
   app_wr_en_n    = 4'hF;
257
   app_req_wr_n   = 0;
258
   app_req        = 0;
259
   app_req_len    = 0;
260
 
261
  RESETN    = 1'h1;
262
 
263
 #100
264
  // Applying reset
265
  RESETN    = 1'h0;
266
  #10000;
267
  // Releasing reset
268
  RESETN    = 1'h1;
269
  #1000;
270
  wait(u_dut.sdr_init_done == 1);
271
 
272
  #1000;
273
 
274 46 dinesha
  $display("-------------------------------------- ");
275
  $display(" Case-1: Single Write/Read Case        ");
276
  $display("-------------------------------------- ");
277
 
278
  burst_write(32'h4_0000,8'h4);
279 30 dinesha
 #1000;
280 43 dinesha
  burst_read();
281 30 dinesha
 
282 46 dinesha
  // Repeat one more time to analysis the
283
  // SDRAM state change for same col/row address
284
  $display("-------------------------------------- ");
285
  $display(" Case-2: Repeat same transfer once again ");
286
  $display("----------------------------------------");
287
  burst_write(32'h4_0000,8'h4);
288 43 dinesha
  burst_read();
289 46 dinesha
  burst_write(32'h0040_0000,8'h5);
290
  burst_read();
291 30 dinesha
 
292 46 dinesha
  $display("----------------------------------------");
293
  $display(" Case-3 Create a Page Cross Over        ");
294
  $display("----------------------------------------");
295
  burst_write(32'h4_0FFC,8'h8);
296
  burst_write(32'h0040_0FF8,8'hF);
297 43 dinesha
  burst_read();
298
  burst_read();
299 46 dinesha
 
300
  $display("----------------------------------------");
301
  $display(" Case:4 4 Write & 4 Read                ");
302
  $display("----------------------------------------");
303
  burst_write(32'h4_0000,8'h4);
304
  burst_write(32'h5_0000,8'h5);
305
  burst_write(32'h6_0000,8'h6);
306
  burst_write(32'h7_0000,8'h7);
307 43 dinesha
  burst_read();
308
  burst_read();
309 46 dinesha
  burst_read();
310
  burst_read();
311 43 dinesha
 
312 46 dinesha
  $display("---------------------------------------");
313
  $display(" Case:5 16 Write & 16 Read With Different Bank and Row ");
314
  $display("---------------------------------------");
315
  //----------------------------------------
316
  // Address Decodeing:
317
  //  with cfg_col bit configured as: 00
318
  //    <12 Bit Row> <2 Bit Bank> <8 Bit Column> <2'b00>
319
  //
320
  burst_write({12'h000,2'b00,8'h00,2'b00},8'h4);   // Row: 0 Bank : 0
321
  burst_write({12'h000,2'b01,8'h00,2'b00},8'h5);   // Row: 0 Bank : 1
322
  burst_write({12'h000,2'b10,8'h00,2'b00},8'h6);   // Row: 0 Bank : 2
323
  burst_write({12'h000,2'b11,8'h00,2'b00},8'h7);   // Row: 0 Bank : 3
324
  burst_write({12'h001,2'b00,8'h00,2'b00},8'h4);   // Row: 1 Bank : 0
325
  burst_write({12'h001,2'b01,8'h00,2'b00},8'h5);   // Row: 1 Bank : 1
326
  burst_write({12'h001,2'b10,8'h00,2'b00},8'h6);   // Row: 1 Bank : 2
327
  burst_write({12'h001,2'b11,8'h00,2'b00},8'h7);   // Row: 1 Bank : 3
328
  burst_read();
329
  burst_read();
330
  burst_read();
331
  burst_read();
332
  burst_read();
333
  burst_read();
334
  burst_read();
335
  burst_read();
336 43 dinesha
 
337 46 dinesha
  burst_write({12'h002,2'b00,8'h00,2'b00},8'h4);   // Row: 2 Bank : 0
338
  burst_write({12'h002,2'b01,8'h00,2'b00},8'h5);   // Row: 2 Bank : 1
339
  burst_write({12'h002,2'b10,8'h00,2'b00},8'h6);   // Row: 2 Bank : 2
340
  burst_write({12'h002,2'b11,8'h00,2'b00},8'h7);   // Row: 2 Bank : 3
341
  burst_write({12'h003,2'b00,8'h00,2'b00},8'h4);   // Row: 3 Bank : 0
342
  burst_write({12'h003,2'b01,8'h00,2'b00},8'h5);   // Row: 3 Bank : 1
343
  burst_write({12'h003,2'b10,8'h00,2'b00},8'h6);   // Row: 3 Bank : 2
344
  burst_write({12'h003,2'b11,8'h00,2'b00},8'h7);   // Row: 3 Bank : 3
345 43 dinesha
 
346 46 dinesha
  burst_read();
347
  burst_read();
348
  burst_read();
349
  burst_read();
350
  burst_read();
351
  burst_read();
352
  burst_read();
353
  burst_read();
354
 
355
  $display("---------------------------------------------------");
356
  $display(" Case: 6 Random 2 write and 2 read random");
357
  $display("---------------------------------------------------");
358 30 dinesha
  for(k=0; k < 20; k++) begin
359 43 dinesha
     StartAddr = $random & 32'h003FFFFF;
360
     burst_write(StartAddr,($random & 8'h3f)+1);
361
 #100;
362
 
363
     StartAddr = $random & 32'h003FFFFF;
364
     burst_write(StartAddr,($random & 8'h3f)+1);
365
 #100;
366
     burst_read();
367
 #100;
368
     burst_read();
369
 #100;
370 30 dinesha
  end
371
 
372
 
373 43 dinesha
 
374 30 dinesha
  #10000;
375
 
376
        $display("###############################");
377
    if(ErrCnt == 0)
378
        $display("STATUS: SDRAM Write/Read TEST PASSED");
379
    else
380
        $display("ERROR:  SDRAM Write/Read TEST FAILED");
381
        $display("###############################");
382
 
383
    $finish;
384
end
385
 
386 43 dinesha
 
387 30 dinesha
task burst_write;
388
input [31:0] Address;
389 43 dinesha
input [7:0]  bl;
390 30 dinesha
int i;
391
begin
392 43 dinesha
  afifo.push_back(Address);
393
  bfifo.push_back(bl);
394
 
395 30 dinesha
   @ (negedge sdram_clk);
396
   app_req        = 1;
397
   app_wr_en_n    = 0;
398
   app_req_wr_n   = 1'b0;
399 43 dinesha
   app_req_addr   = Address[31:2];
400
   app_req_len    = bl;
401
   $display("Write Address: %x, Burst Size: %d",Address,bl);
402 30 dinesha
 
403
   // wait for app_req_ack == 1
404
   do begin
405
       @ (posedge sdram_clk);
406
   end while(app_req_ack == 1'b0);
407
   @ (negedge sdram_clk);
408
   app_req           = 0;
409
 
410 43 dinesha
   for(i=0; i < bl; i++) begin
411
      app_wr_data        = $random & 32'hFFFFFFFF;
412
      dfifo.push_back(app_wr_data);
413 30 dinesha
 
414
      do begin
415
          @ (posedge sdram_clk);
416
      end while(app_wr_next_req == 1'b0);
417
          @ (negedge sdram_clk);
418
 
419
       $display("Status: Burst-No: %d  Write Address: %x  WriteData: %x ",i,Address,app_wr_data);
420
   end
421
   app_req           = 0;
422
   app_wr_en_n       = 4'hF;
423 43 dinesha
 
424
 
425 30 dinesha
end
426
endtask
427
 
428
task burst_read;
429 43 dinesha
reg [31:0] Address;
430
reg [7:0]  bl;
431 30 dinesha
 
432
int i,j;
433 43 dinesha
reg [31:0]   exp_data;
434 30 dinesha
begin
435 43 dinesha
 
436
   Address = afifo.pop_front();
437
   bl      = bfifo.pop_front();
438 30 dinesha
 
439 43 dinesha
   app_req        = 1;
440
   app_wr_en_n    = 0;
441
   app_req_wr_n   = 1;
442
   app_req_addr   = Address[29:2];
443
   app_req_len    = bl;
444
 
445 30 dinesha
      // wait for app_req_ack == 1
446
      do begin
447
          @ (posedge sdram_clk);
448
      end while(app_req_ack == 1'b0);
449
      @ (negedge sdram_clk);
450
      app_req           = 0;
451
 
452 43 dinesha
      for(j=0; j < bl; j++) begin
453 30 dinesha
         wait(app_rd_valid == 1);
454 43 dinesha
         exp_data        = dfifo.pop_front(); // Exptected Read Data
455
         if(app_rd_data !== exp_data) begin
456
             $display("READ ERROR: Burst-No: %d Addr: %x Rxp: %x Exd: %x",j,Address+(j*2),app_rd_data,exp_data);
457 30 dinesha
             ErrCnt = ErrCnt+1;
458
         end else begin
459
             $display("READ STATUS: Burst-No: %d Addr: %x Rxd: %x",j,Address+(j*2),app_rd_data);
460
         end
461
         @ (posedge sdram_clk);
462
         @ (negedge sdram_clk);
463
      end
464
end
465
endtask
466
 
467
 
468
endmodule

powered by: WebSVN 2.1.0

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