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 49

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 46 dinesha
  $display("-------------------------------------- ");
273
  $display(" Case-1: Single Write/Read Case        ");
274
  $display("-------------------------------------- ");
275
 
276
  burst_write(32'h4_0000,8'h4);
277 30 dinesha
 #1000;
278 43 dinesha
  burst_read();
279 30 dinesha
 
280 46 dinesha
  // Repeat one more time to analysis the
281
  // SDRAM state change for same col/row address
282
  $display("-------------------------------------- ");
283
  $display(" Case-2: Repeat same transfer once again ");
284
  $display("----------------------------------------");
285
  burst_write(32'h4_0000,8'h4);
286 43 dinesha
  burst_read();
287 46 dinesha
  burst_write(32'h0040_0000,8'h5);
288
  burst_read();
289
  $display("----------------------------------------");
290
  $display(" Case-3 Create a Page Cross Over        ");
291
  $display("----------------------------------------");
292
  burst_write(32'h4_0FFC,8'h8);
293
  burst_write(32'h0040_0FF8,8'hF);
294 43 dinesha
  burst_read();
295
  burst_read();
296 49 dinesha
 
297 46 dinesha
  $display("----------------------------------------");
298
  $display(" Case:4 4 Write & 4 Read                ");
299
  $display("----------------------------------------");
300
  burst_write(32'h4_0000,8'h4);
301
  burst_write(32'h5_0000,8'h5);
302
  burst_write(32'h6_0000,8'h6);
303
  burst_write(32'h7_0000,8'h7);
304 43 dinesha
  burst_read();
305
  burst_read();
306 46 dinesha
  burst_read();
307
  burst_read();
308 43 dinesha
 
309 46 dinesha
  $display("---------------------------------------");
310
  $display(" Case:5 16 Write & 16 Read With Different Bank and Row ");
311
  $display("---------------------------------------");
312
  //----------------------------------------
313
  // Address Decodeing:
314
  //  with cfg_col bit configured as: 00
315
  //    <12 Bit Row> <2 Bit Bank> <8 Bit Column> <2'b00>
316
  //
317
  burst_write({12'h000,2'b00,8'h00,2'b00},8'h4);   // Row: 0 Bank : 0
318
  burst_write({12'h000,2'b01,8'h00,2'b00},8'h5);   // Row: 0 Bank : 1
319
  burst_write({12'h000,2'b10,8'h00,2'b00},8'h6);   // Row: 0 Bank : 2
320
  burst_write({12'h000,2'b11,8'h00,2'b00},8'h7);   // Row: 0 Bank : 3
321
  burst_write({12'h001,2'b00,8'h00,2'b00},8'h4);   // Row: 1 Bank : 0
322
  burst_write({12'h001,2'b01,8'h00,2'b00},8'h5);   // Row: 1 Bank : 1
323
  burst_write({12'h001,2'b10,8'h00,2'b00},8'h6);   // Row: 1 Bank : 2
324
  burst_write({12'h001,2'b11,8'h00,2'b00},8'h7);   // Row: 1 Bank : 3
325
  burst_read();
326
  burst_read();
327
  burst_read();
328
  burst_read();
329
  burst_read();
330
  burst_read();
331
  burst_read();
332
  burst_read();
333 43 dinesha
 
334 46 dinesha
  burst_write({12'h002,2'b00,8'h00,2'b00},8'h4);   // Row: 2 Bank : 0
335
  burst_write({12'h002,2'b01,8'h00,2'b00},8'h5);   // Row: 2 Bank : 1
336
  burst_write({12'h002,2'b10,8'h00,2'b00},8'h6);   // Row: 2 Bank : 2
337
  burst_write({12'h002,2'b11,8'h00,2'b00},8'h7);   // Row: 2 Bank : 3
338
  burst_write({12'h003,2'b00,8'h00,2'b00},8'h4);   // Row: 3 Bank : 0
339
  burst_write({12'h003,2'b01,8'h00,2'b00},8'h5);   // Row: 3 Bank : 1
340
  burst_write({12'h003,2'b10,8'h00,2'b00},8'h6);   // Row: 3 Bank : 2
341
  burst_write({12'h003,2'b11,8'h00,2'b00},8'h7);   // Row: 3 Bank : 3
342 43 dinesha
 
343 46 dinesha
  burst_read();
344
  burst_read();
345
  burst_read();
346
  burst_read();
347
  burst_read();
348
  burst_read();
349
  burst_read();
350
  burst_read();
351
 
352
  $display("---------------------------------------------------");
353
  $display(" Case: 6 Random 2 write and 2 read random");
354
  $display("---------------------------------------------------");
355 30 dinesha
  for(k=0; k < 20; k++) begin
356 43 dinesha
     StartAddr = $random & 32'h003FFFFF;
357
     burst_write(StartAddr,($random & 8'h3f)+1);
358
 #100;
359
 
360
     StartAddr = $random & 32'h003FFFFF;
361
     burst_write(StartAddr,($random & 8'h3f)+1);
362
 #100;
363
     burst_read();
364
 #100;
365
     burst_read();
366
 #100;
367 30 dinesha
  end
368
 
369
 
370
  #10000;
371
 
372
        $display("###############################");
373
    if(ErrCnt == 0)
374
        $display("STATUS: SDRAM Write/Read TEST PASSED");
375
    else
376
        $display("ERROR:  SDRAM Write/Read TEST FAILED");
377
        $display("###############################");
378
 
379
    $finish;
380
end
381
 
382 43 dinesha
 
383 30 dinesha
task burst_write;
384
input [31:0] Address;
385 43 dinesha
input [7:0]  bl;
386 30 dinesha
int i;
387
begin
388 43 dinesha
  afifo.push_back(Address);
389
  bfifo.push_back(bl);
390
 
391 30 dinesha
   @ (negedge sdram_clk);
392
   app_req        = 1;
393
   app_wr_en_n    = 0;
394
   app_req_wr_n   = 1'b0;
395 43 dinesha
   app_req_addr   = Address[31:2];
396
   app_req_len    = bl;
397
   $display("Write Address: %x, Burst Size: %d",Address,bl);
398 30 dinesha
 
399
   // wait for app_req_ack == 1
400
   do begin
401
       @ (posedge sdram_clk);
402
   end while(app_req_ack == 1'b0);
403
   @ (negedge sdram_clk);
404
   app_req           = 0;
405
 
406 43 dinesha
   for(i=0; i < bl; i++) begin
407
      app_wr_data        = $random & 32'hFFFFFFFF;
408
      dfifo.push_back(app_wr_data);
409 30 dinesha
 
410
      do begin
411
          @ (posedge sdram_clk);
412
      end while(app_wr_next_req == 1'b0);
413
          @ (negedge sdram_clk);
414
 
415
       $display("Status: Burst-No: %d  Write Address: %x  WriteData: %x ",i,Address,app_wr_data);
416
   end
417
   app_req           = 0;
418
   app_wr_en_n       = 4'hF;
419 43 dinesha
 
420
 
421 30 dinesha
end
422
endtask
423
 
424
task burst_read;
425 43 dinesha
reg [31:0] Address;
426
reg [7:0]  bl;
427 30 dinesha
 
428
int i,j;
429 43 dinesha
reg [31:0]   exp_data;
430 30 dinesha
begin
431 43 dinesha
 
432
   Address = afifo.pop_front();
433
   bl      = bfifo.pop_front();
434 30 dinesha
 
435 43 dinesha
   app_req        = 1;
436
   app_wr_en_n    = 0;
437
   app_req_wr_n   = 1;
438
   app_req_addr   = Address[29:2];
439
   app_req_len    = bl;
440
 
441 30 dinesha
      // wait for app_req_ack == 1
442
      do begin
443
          @ (posedge sdram_clk);
444
      end while(app_req_ack == 1'b0);
445
      @ (negedge sdram_clk);
446
      app_req           = 0;
447
 
448 43 dinesha
      for(j=0; j < bl; j++) begin
449 30 dinesha
         wait(app_rd_valid == 1);
450 43 dinesha
         exp_data        = dfifo.pop_front(); // Exptected Read Data
451
         if(app_rd_data !== exp_data) begin
452
             $display("READ ERROR: Burst-No: %d Addr: %x Rxp: %x Exd: %x",j,Address+(j*2),app_rd_data,exp_data);
453 30 dinesha
             ErrCnt = ErrCnt+1;
454
         end else begin
455
             $display("READ STATUS: Burst-No: %d Addr: %x Rxd: %x",j,Address+(j*2),app_rd_data);
456
         end
457
         @ (posedge sdram_clk);
458
         @ (negedge sdram_clk);
459
      end
460
end
461
endtask
462
 
463
 
464
endmodule

powered by: WebSVN 2.1.0

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