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 48

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
 
144
          .app_wr_data        (app_wr_data        ),
145
          .app_wr_en_n        (app_wr_en_n        ),
146
          .app_rd_data        (app_rd_data        ),
147
          .app_last_rd        (app_last_rd        ),
148 46 dinesha
          .app_last_wr        (app_last_wr        ),
149 30 dinesha
          .app_rd_valid       (app_rd_valid       ),
150
          .app_wr_next_req    (app_wr_next_req    ),
151
          .app_req_dma_last   (app_req            ),
152
 
153
/* Interface to SDRAMs */
154
          .sdr_cs_n           (sdr_cs_n           ),
155
          .sdr_cke            (sdr_cke            ),
156
          .sdr_ras_n          (sdr_ras_n          ),
157
          .sdr_cas_n          (sdr_cas_n          ),
158
          .sdr_we_n           (sdr_we_n           ),
159
          .sdr_dqm            (sdr_dqm            ),
160
          .sdr_ba             (sdr_ba             ),
161
          .sdr_addr           (sdr_addr           ),
162
          .pad_sdr_din        (Dq                 ),
163
          .sdr_dout           (sdr_dout           ),
164
          .sdr_den_n          (sdr_den_n          ),
165
 
166
    /* Parameters */
167
          .sdr_init_done      (sdr_init_done      ),
168 44 dinesha
          .cfg_req_depth      (2'h3               ),            //how many req. buffer should hold
169 30 dinesha
          .cfg_sdr_en         (1'b1               ),
170
          .cfg_sdr_mode_reg   (12'h033            ),
171
          .cfg_sdr_tras_d     (4'h4               ),
172
          .cfg_sdr_trp_d      (4'h2               ),
173
          .cfg_sdr_trcd_d     (4'h2               ),
174
          .cfg_sdr_cas        (3'h3               ),
175
          .cfg_sdr_trcar_d    (4'h7               ),
176
          .cfg_sdr_twr_d      (4'h1               ),
177 45 dinesha
          .cfg_sdr_rfsh       (12'h100            ), // reduced from 12'hC35
178 30 dinesha
          .cfg_sdr_rfmax      (3'h6               )
179
 
180
);
181
 
182
 
183
`ifdef SDR_32BIT
184
  assign Dq[7:0]    = (sdr_den_n[0] == 1'b0) ? sdr_dout[7:0]   : 8'hZZ;
185
  assign Dq[15:8]   = (sdr_den_n[1] == 1'b0) ? sdr_dout[15:8]  : 8'hZZ;
186
  assign Dq[23:16]  = (sdr_den_n[2] == 1'b0) ? sdr_dout[23:16] : 8'hZZ;
187
  assign Dq[31:24]  = (sdr_den_n[3] == 1'b0) ? sdr_dout[31:24] : 8'hZZ;
188
mt48lc2m32b2 #(.data_bits(32)) u_sdram32 (
189
          .Dq                 (Dq                 ) ,
190
          .Addr               (sdr_addr           ),
191
          .Ba                 (sdr_ba             ),
192
          .Clk                (sdram_clk_d        ),
193
          .Cke                (sdr_cke            ),
194
          .Cs_n               (sdr_cs_n           ),
195
          .Ras_n              (sdr_ras_n          ),
196
          .Cas_n              (sdr_cas_n          ),
197
          .We_n               (sdr_we_n           ),
198
          .Dqm                (sdr_dqm            )
199
     );
200
 
201
`elsif SDR_16BIT
202
 
203
assign Dq[7:0]  = (sdr_den_n[0] == 1'b0) ? sdr_dout[7:0]  : 8'hZZ;
204
assign Dq[15:8] = (sdr_den_n[1] == 1'b0) ? sdr_dout[15:8] : 8'hZZ;
205
 
206
   IS42VM16400K u_sdram16 (
207
          .dq                 (Dq                 ),
208
          .addr               (sdr_addr           ),
209
          .ba                 (sdr_ba             ),
210
          .clk                (sdram_clk_d        ),
211
          .cke                (sdr_cke            ),
212
          .csb                (sdr_cs_n           ),
213
          .rasb               (sdr_ras_n          ),
214
          .casb               (sdr_cas_n          ),
215
          .web                (sdr_we_n           ),
216
          .dqm                (sdr_dqm            )
217
    );
218
`else
219
 
220
assign Dq[7:0]  = (sdr_den_n[0] == 1'b0) ? sdr_dout[7:0]  : 8'hZZ;
221
 
222
mt48lc8m8a2 #(.data_bits(8)) u_sdram8 (
223
          .Dq                 (Dq                 ) ,
224
          .Addr               (sdr_addr           ),
225
          .Ba                 (sdr_ba             ),
226
          .Clk                (sdram_clk_d        ),
227
          .Cke                (sdr_cke            ),
228
          .Cs_n               (sdr_cs_n           ),
229
          .Ras_n              (sdr_ras_n          ),
230
          .Cas_n              (sdr_cas_n          ),
231
          .We_n               (sdr_we_n           ),
232
          .Dqm                (sdr_dqm            )
233
     );
234
`endif
235
 
236
//--------------------
237 43 dinesha
// data/address/burst length FIFO
238 30 dinesha
//--------------------
239 43 dinesha
int dfifo[$]; // data fifo
240
int afifo[$]; // address  fifo
241
int bfifo[$]; // Burst Length fifo
242 30 dinesha
 
243
reg [31:0] read_data;
244
reg [31:0] ErrCnt;
245
int k;
246
reg [31:0] StartAddr;
247
/////////////////////////////////////////////////////////////////////////
248
// Test Case
249
/////////////////////////////////////////////////////////////////////////
250
 
251
initial begin //{
252
  ErrCnt          = 0;
253
   app_req_addr  = 0;
254
   app_wr_data    = 0;
255
   app_wr_en_n    = 4'hF;
256
   app_req_wr_n   = 0;
257
   app_req        = 0;
258
   app_req_len    = 0;
259
 
260
  RESETN    = 1'h1;
261
 
262
 #100
263
  // Applying reset
264
  RESETN    = 1'h0;
265
  #10000;
266
  // Releasing reset
267
  RESETN    = 1'h1;
268
  #1000;
269
  wait(u_dut.sdr_init_done == 1);
270
 
271
  #1000;
272 48 dinesha
/********************
273 46 dinesha
  $display("-------------------------------------- ");
274
  $display(" Case-1: Single Write/Read Case        ");
275
  $display("-------------------------------------- ");
276
 
277
  burst_write(32'h4_0000,8'h4);
278 30 dinesha
 #1000;
279 43 dinesha
  burst_read();
280 30 dinesha
 
281 46 dinesha
  // Repeat one more time to analysis the
282
  // SDRAM state change for same col/row address
283
  $display("-------------------------------------- ");
284
  $display(" Case-2: Repeat same transfer once again ");
285
  $display("----------------------------------------");
286
  burst_write(32'h4_0000,8'h4);
287 43 dinesha
  burst_read();
288 46 dinesha
  burst_write(32'h0040_0000,8'h5);
289
  burst_read();
290 48 dinesha
***************/
291 46 dinesha
  $display("----------------------------------------");
292
  $display(" Case-3 Create a Page Cross Over        ");
293
  $display("----------------------------------------");
294
  burst_write(32'h4_0FFC,8'h8);
295
  burst_write(32'h0040_0FF8,8'hF);
296 43 dinesha
  burst_read();
297
  burst_read();
298 48 dinesha
/*****************
299 46 dinesha
  $display("----------------------------------------");
300
  $display(" Case:4 4 Write & 4 Read                ");
301
  $display("----------------------------------------");
302
  burst_write(32'h4_0000,8'h4);
303
  burst_write(32'h5_0000,8'h5);
304
  burst_write(32'h6_0000,8'h6);
305
  burst_write(32'h7_0000,8'h7);
306 43 dinesha
  burst_read();
307
  burst_read();
308 46 dinesha
  burst_read();
309
  burst_read();
310 43 dinesha
 
311 46 dinesha
  $display("---------------------------------------");
312
  $display(" Case:5 16 Write & 16 Read With Different Bank and Row ");
313
  $display("---------------------------------------");
314
  //----------------------------------------
315
  // Address Decodeing:
316
  //  with cfg_col bit configured as: 00
317
  //    <12 Bit Row> <2 Bit Bank> <8 Bit Column> <2'b00>
318
  //
319
  burst_write({12'h000,2'b00,8'h00,2'b00},8'h4);   // Row: 0 Bank : 0
320
  burst_write({12'h000,2'b01,8'h00,2'b00},8'h5);   // Row: 0 Bank : 1
321
  burst_write({12'h000,2'b10,8'h00,2'b00},8'h6);   // Row: 0 Bank : 2
322
  burst_write({12'h000,2'b11,8'h00,2'b00},8'h7);   // Row: 0 Bank : 3
323
  burst_write({12'h001,2'b00,8'h00,2'b00},8'h4);   // Row: 1 Bank : 0
324
  burst_write({12'h001,2'b01,8'h00,2'b00},8'h5);   // Row: 1 Bank : 1
325
  burst_write({12'h001,2'b10,8'h00,2'b00},8'h6);   // Row: 1 Bank : 2
326
  burst_write({12'h001,2'b11,8'h00,2'b00},8'h7);   // Row: 1 Bank : 3
327
  burst_read();
328
  burst_read();
329
  burst_read();
330
  burst_read();
331
  burst_read();
332
  burst_read();
333
  burst_read();
334
  burst_read();
335 43 dinesha
 
336 46 dinesha
  burst_write({12'h002,2'b00,8'h00,2'b00},8'h4);   // Row: 2 Bank : 0
337
  burst_write({12'h002,2'b01,8'h00,2'b00},8'h5);   // Row: 2 Bank : 1
338
  burst_write({12'h002,2'b10,8'h00,2'b00},8'h6);   // Row: 2 Bank : 2
339
  burst_write({12'h002,2'b11,8'h00,2'b00},8'h7);   // Row: 2 Bank : 3
340
  burst_write({12'h003,2'b00,8'h00,2'b00},8'h4);   // Row: 3 Bank : 0
341
  burst_write({12'h003,2'b01,8'h00,2'b00},8'h5);   // Row: 3 Bank : 1
342
  burst_write({12'h003,2'b10,8'h00,2'b00},8'h6);   // Row: 3 Bank : 2
343
  burst_write({12'h003,2'b11,8'h00,2'b00},8'h7);   // Row: 3 Bank : 3
344 43 dinesha
 
345 46 dinesha
  burst_read();
346
  burst_read();
347
  burst_read();
348
  burst_read();
349
  burst_read();
350
  burst_read();
351
  burst_read();
352
  burst_read();
353
 
354
  $display("---------------------------------------------------");
355
  $display(" Case: 6 Random 2 write and 2 read random");
356
  $display("---------------------------------------------------");
357 30 dinesha
  for(k=0; k < 20; k++) begin
358 43 dinesha
     StartAddr = $random & 32'h003FFFFF;
359
     burst_write(StartAddr,($random & 8'h3f)+1);
360
 #100;
361
 
362
     StartAddr = $random & 32'h003FFFFF;
363
     burst_write(StartAddr,($random & 8'h3f)+1);
364
 #100;
365
     burst_read();
366
 #100;
367
     burst_read();
368
 #100;
369 30 dinesha
  end
370
 
371 48 dinesha
*********************/
372 30 dinesha
 
373
  #10000;
374
 
375
        $display("###############################");
376
    if(ErrCnt == 0)
377
        $display("STATUS: SDRAM Write/Read TEST PASSED");
378
    else
379
        $display("ERROR:  SDRAM Write/Read TEST FAILED");
380
        $display("###############################");
381
 
382
    $finish;
383
end
384
 
385 43 dinesha
 
386 30 dinesha
task burst_write;
387
input [31:0] Address;
388 43 dinesha
input [7:0]  bl;
389 30 dinesha
int i;
390
begin
391 43 dinesha
  afifo.push_back(Address);
392
  bfifo.push_back(bl);
393
 
394 30 dinesha
   @ (negedge sdram_clk);
395
   app_req        = 1;
396
   app_wr_en_n    = 0;
397
   app_req_wr_n   = 1'b0;
398 43 dinesha
   app_req_addr   = Address[31:2];
399
   app_req_len    = bl;
400
   $display("Write Address: %x, Burst Size: %d",Address,bl);
401 30 dinesha
 
402
   // wait for app_req_ack == 1
403
   do begin
404
       @ (posedge sdram_clk);
405
   end while(app_req_ack == 1'b0);
406
   @ (negedge sdram_clk);
407
   app_req           = 0;
408
 
409 43 dinesha
   for(i=0; i < bl; i++) begin
410
      app_wr_data        = $random & 32'hFFFFFFFF;
411
      dfifo.push_back(app_wr_data);
412 30 dinesha
 
413
      do begin
414
          @ (posedge sdram_clk);
415
      end while(app_wr_next_req == 1'b0);
416
          @ (negedge sdram_clk);
417
 
418
       $display("Status: Burst-No: %d  Write Address: %x  WriteData: %x ",i,Address,app_wr_data);
419
   end
420
   app_req           = 0;
421
   app_wr_en_n       = 4'hF;
422 43 dinesha
 
423
 
424 30 dinesha
end
425
endtask
426
 
427
task burst_read;
428 43 dinesha
reg [31:0] Address;
429
reg [7:0]  bl;
430 30 dinesha
 
431
int i,j;
432 43 dinesha
reg [31:0]   exp_data;
433 30 dinesha
begin
434 43 dinesha
 
435
   Address = afifo.pop_front();
436
   bl      = bfifo.pop_front();
437 30 dinesha
 
438 43 dinesha
   app_req        = 1;
439
   app_wr_en_n    = 0;
440
   app_req_wr_n   = 1;
441
   app_req_addr   = Address[29:2];
442
   app_req_len    = bl;
443
 
444 30 dinesha
      // wait for app_req_ack == 1
445
      do begin
446
          @ (posedge sdram_clk);
447
      end while(app_req_ack == 1'b0);
448
      @ (negedge sdram_clk);
449
      app_req           = 0;
450
 
451 43 dinesha
      for(j=0; j < bl; j++) begin
452 30 dinesha
         wait(app_rd_valid == 1);
453 43 dinesha
         exp_data        = dfifo.pop_front(); // Exptected Read Data
454
         if(app_rd_data !== exp_data) begin
455
             $display("READ ERROR: Burst-No: %d Addr: %x Rxp: %x Exd: %x",j,Address+(j*2),app_rd_data,exp_data);
456 30 dinesha
             ErrCnt = ErrCnt+1;
457
         end else begin
458
             $display("READ STATUS: Burst-No: %d Addr: %x Rxd: %x",j,Address+(j*2),app_rd_data);
459
         end
460
         @ (posedge sdram_clk);
461
         @ (negedge sdram_clk);
462
      end
463
end
464
endtask
465
 
466
 
467
endmodule

powered by: WebSVN 2.1.0

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