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

Subversion Repositories wbddr3

[/] [wbddr3/] [trunk/] [bench/] [cpp/] [ddrsdram_tb.cpp] - Blame information for rev 4

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

Line No. Rev Author Line
1 4 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    ddrsdram_tb.cpp
4
//
5
// Project:     A wishbone controlled DDR3 SDRAM memory controller.
6
//
7
// Purpose:     To determine whether or not the wbddrsdram Verilog module works.
8
//              Run this program with no arguments.  If the last line output
9
//      is "SUCCESS", you will know it works.
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
////////////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// You should have received a copy of the GNU General Public License along
29
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
30
// target there if the PDF file isn't present.)  If not, see
31
// <http://www.gnu.org/licenses/> for a copy.
32
//
33
// License:     GPL, v3, as defined and found on www.gnu.org,
34
//              http://www.gnu.org/licenses/gpl.html
35
//
36
//
37
////////////////////////////////////////////////////////////////////////////////
38
//
39
//
40
#include <stdio.h>
41
 
42
#include "verilated.h"
43
#include "Vwbddrsdram.h"
44
#include "ddrsdramsim.h"
45
 
46
const int       BOMBCOUNT = 2048,
47
                SDRAMMASK = 0x3ffffff,
48
                LGMEMSIZE = 28;
49
 
50
class   DDRSDRAM_TB {
51
        long            m_tickcount;
52
        Vwbddrsdram     *m_core;
53
        DDRSDRAMSIM     *m_sdram;
54
        bool            m_bomb;
55
public:
56
 
57
        DDRSDRAM_TB(void) {
58
                m_core = new Vwbddrsdram;
59
                m_sdram= new DDRSDRAMSIM(LGMEMSIZE);
60
        }
61
 
62
        unsigned &operator[](const int index) { return (*m_sdram)[index]; }
63
        void    set(unsigned addr, unsigned v) {
64
                (*m_sdram)[addr] = v;
65
        }
66
 
67
        void    tick(void) {
68
                m_core->i_clk = 1;
69
 
70
                m_core->i_ddr_data = (*m_sdram)(
71
                        m_core->o_ddr_reset_n,
72
                        m_core->o_ddr_cke,
73
                        m_core->o_ddr_cs_n,
74
                        m_core->o_ddr_ras_n,
75
                        m_core->o_ddr_cas_n,
76
                        m_core->o_ddr_we_n,
77
                        m_core->o_ddr_dqs,
78
                        m_core->o_ddr_dm,
79
                        m_core->o_ddr_odt,
80
                        m_core->o_ddr_bus_oe,
81
                        m_core->o_ddr_addr,
82
                        m_core->o_ddr_ba,
83
                        m_core->o_ddr_data);
84
 
85
                printf("%08lx-WB: %s/%s %s%s%s %s@0x%08x[%08x/%08x] -- ",
86
                        m_tickcount,
87
                        (m_core->i_wb_cyc)?"CYC":"   ",
88
                        (m_core->i_wb_stb)?"STB":"   ",
89
                        (m_core->o_wb_stall)?"STALL":"     ",
90
                        (m_core->o_wb_ack)?"ACK":"   ",
91
                        (m_core->o_cmd_accepted)?"BUS":"   ",
92
                        (m_core->i_wb_we)?"W":"R",
93
                        (m_core->i_wb_addr),
94
                        (m_core->i_wb_data),
95
                        (m_core->o_wb_data));
96
 
97
                printf("%s%s %d%d%d%d %s%s%s%s B[%d]@%04x %08x %08x",
98
                        (m_core->o_ddr_reset_n)?" ":"R",
99
                        (m_core->o_ddr_cke)?"CK":"  ",
100
                        (m_core->o_ddr_cs_n),
101
                        (m_core->o_ddr_ras_n),
102
                        (m_core->o_ddr_cas_n),
103
                        (m_core->o_ddr_we_n),
104
                        //
105
                        (m_core->o_ddr_dqs)?"D":" ",
106
                        (m_core->o_ddr_dm)?"M":" ",
107
                        (m_core->o_ddr_odt)?"O":" ",
108
                        (m_core->o_ddr_bus_oe)?"E":" ",
109
                        //
110
                        (m_core->o_ddr_ba),
111
                        (m_core->o_ddr_addr),
112
                        (m_core->i_ddr_data),
113
                        (m_core->o_ddr_data));
114
 
115
                // Reset logic
116
                printf(" RST(%06x%s[%d] - %08x->%08x)",
117
                        m_core->v__DOT__reset_timer,
118
                        (m_core->v__DOT__reset_ztimer)?"Z":" ",
119
                        (m_core->v__DOT__reset_address),
120
                        (m_core->v__DOT__reset_instruction),
121
                        (m_core->v__DOT__reset_cmd));
122
 
123
                extern int gbl_state, gbl_counts;
124
                printf(" %d:%08x ", gbl_state, gbl_counts);
125
 
126
                if (m_core->v__DOT__reset_override)
127
                        printf(" OVERRIDE");
128
                printf("\n");
129
 
130
                m_core->eval();
131
                m_core->i_clk = 0;
132
                m_core->eval();
133
 
134
                m_tickcount++;
135
 
136
                /*
137
                if ((m_core->o_wb_ack)&&(!m_core->i_wb_cyc)) {
138
                        printf("SETTING ERR TO TRUE!!!!!  ACK w/ no CYC\n");
139
                        // m_bomb = true;
140
                }
141
                */
142
        }
143
 
144
        void reset(void) {
145
                m_core->i_reset  = 1;
146
                m_core->i_wb_cyc = 0;
147
                m_core->i_wb_stb = 0;
148
                tick();
149
                m_core->i_reset  = 0;
150
        }
151
 
152
        void wb_tick(void) {
153
                m_core->i_wb_cyc   = 0;
154
                m_core->i_wb_stb = 0;
155
                tick();
156
        }
157
 
158
        unsigned wb_read(unsigned a) {
159
                int             errcount = 0;
160
                unsigned        result;
161
 
162
                printf("WB-READ(%08x)\n", a);
163
 
164
                m_core->i_wb_cyc = 1;
165
                m_core->i_wb_stb = 1;
166
                m_core->i_wb_we  = 0;
167
                m_core->i_wb_addr= a & SDRAMMASK;
168
 
169
                if (m_core->o_wb_stall) {
170
                        while((errcount++ < BOMBCOUNT)&&(m_core->o_wb_stall))
171
                                tick();
172
                } else
173
                        tick();
174
 
175
                m_core->i_wb_stb = 0;
176
 
177
                while((errcount++ <  BOMBCOUNT)&&(!m_core->o_wb_ack))
178
                        tick();
179
 
180
 
181
                result = m_core->o_wb_data;
182
 
183
                // Release the bus?
184
                m_core->i_wb_cyc = 0;
185
                m_core->i_wb_stb = 0;
186
 
187
                if(errcount >= BOMBCOUNT) {
188
                        printf("SETTING ERR TO TRUE!!!!!\n");
189
                        m_bomb = true;
190
                } else if (!m_core->o_wb_ack) {
191
                        printf("SETTING ERR TO TRUE--NO ACK, NO TIMEOUT\n");
192
                        m_bomb = true;
193
                }
194
                tick();
195
 
196
                return result;
197
        }
198
 
199
        void    wb_read(unsigned a, int len, unsigned *buf) {
200
                int             errcount = 0;
201
                int             THISBOMBCOUNT = BOMBCOUNT * len;
202
                int             cnt, rdidx, inc;
203
 
204
                printf("WB-READ(%08x, %d)\n", a, len);
205
 
206
                while((errcount++ < BOMBCOUNT)&&(m_core->o_wb_stall))
207
                        wb_tick();
208
 
209
                if (errcount >= BOMBCOUNT) {
210
                        m_bomb = true;
211
                        return;
212
                }
213
 
214
                errcount = 0;
215
 
216
                m_core->i_wb_cyc = 1;
217
                m_core->i_wb_stb = 1;
218
                m_core->i_wb_we  = 0;
219
                m_core->i_wb_addr= a & SDRAMMASK;
220
 
221
                rdidx =0; cnt = 0;
222
                inc = 1;
223
 
224
                do {
225
                        int     s;
226
                        s = (m_core->o_wb_stall==0)?0:1;
227
                        tick();
228
                        if (!s)
229
                                m_core->i_wb_addr += inc;
230
                        cnt += (s==0)?1:0;
231
                        if (m_core->o_wb_ack)
232
                                buf[rdidx++] = m_core->o_wb_data;
233
                } while((cnt < len)&&(errcount++ < THISBOMBCOUNT));
234
 
235
                m_core->i_wb_stb = 0;
236
 
237
                while((rdidx < len)&&(errcount++ < THISBOMBCOUNT)) {
238
                        tick();
239
                        if (m_core->o_wb_ack)
240
                                buf[rdidx++] = m_core->o_wb_data;
241
                }
242
 
243
                // Release the bus?
244
                m_core->i_wb_cyc = 0;
245
 
246
                if(errcount >= THISBOMBCOUNT) {
247
                        printf("SETTING ERR TO TRUE!!!!! (errcount=%08x, THISBOMBCOUNT=%08x)\n", errcount, THISBOMBCOUNT);
248
                        m_bomb = true;
249
                } else if (!m_core->o_wb_ack) {
250
                        printf("SETTING ERR TO TRUE--NO ACK, NO TIMEOUT\n");
251
                        m_bomb = true;
252
                }
253
                tick();
254
        }
255
 
256
        void    wb_write(unsigned a, unsigned int v) {
257
                int errcount = 0;
258
 
259
                printf("WB-WRITE(%08x) = %08x\n", a, v);
260
                m_core->i_wb_cyc = 1;
261
                m_core->i_wb_stb = 1;
262
                m_core->i_wb_we  = 1;
263
                m_core->i_wb_addr= a & SDRAMMASK;
264
                m_core->i_wb_data= v;
265
 
266
                if (m_core->o_wb_stall)
267
                        while((errcount++ < BOMBCOUNT)&&(m_core->o_wb_stall))
268
                                tick();
269
                tick();
270
 
271
                m_core->i_wb_stb = 0;
272
 
273
                while((errcount++ <  BOMBCOUNT)&&(!m_core->o_wb_ack))
274
                        tick();
275
 
276
                // Release the bus?
277
                m_core->i_wb_cyc = 0;
278
                m_core->i_wb_stb = 0;
279
 
280
                if(errcount >= BOMBCOUNT) {
281
                        printf("SETTING ERR TO TRUE!!!!!\n");
282
                        m_bomb = true;
283
                } tick();
284
        }
285
 
286
        void    wb_write(unsigned a, unsigned int ln, unsigned int *buf) {
287
                unsigned errcount = 0, nacks = 0;
288
 
289
                m_core->i_wb_cyc = 1;
290
                m_core->i_wb_stb = 1;
291
                for(unsigned stbcnt=0; stbcnt<ln; stbcnt++) {
292
                        m_core->i_wb_we  = 1;
293
                        m_core->i_wb_addr= (a+stbcnt) & SDRAMMASK;
294
                        m_core->i_wb_data= buf[stbcnt];
295
                        errcount = 0;
296
 
297
                        while((errcount++ < BOMBCOUNT)&&(m_core->o_wb_stall)) {
298
                                tick(); if (m_core->o_wb_ack) nacks++;
299
                        }
300
                        // Tick, now that we're not stalled.  This is the tick
301
                        // that gets accepted.
302
                        tick(); if (m_core->o_wb_ack) nacks++;
303
                }
304
 
305
                m_core->i_wb_stb = 0;
306
 
307
                errcount = 0;
308
                while((nacks < ln)&&(errcount++ < BOMBCOUNT)) {
309
                        tick();
310
                        if (m_core->o_wb_ack) {
311
                                nacks++;
312
                                errcount = 0;
313
                        }
314
                }
315
 
316
                // Release the bus
317
                m_core->i_wb_cyc = 0;
318
                m_core->i_wb_stb = 0;
319
 
320
                if(errcount >= BOMBCOUNT) {
321
                        printf("SETTING ERR TO TRUE!!!!!\n");
322
                        m_bomb = true;
323
                } tick();
324
        }
325
 
326
        bool    bombed(void) const { return m_bomb; }
327
 
328
};
329
 
330
void    uload(unsigned len, unsigned *buf) {
331
        FILE    *fp = fopen("/dev/urandom", "r");
332
 
333
        if ((NULL == fp)||(len != fread(buf, sizeof(unsigned), len, fp))) {
334
                for(int i=0; i<(int)len; i++)
335
                        buf[i] = rand();
336
        } if (NULL == fp)
337
                fclose(fp);
338
}
339
 
340
int main(int  argc, char **argv) {
341
        Verilated::commandArgs(argc, argv);
342
        DDRSDRAM_TB     *tb = new DDRSDRAM_TB;
343
        unsigned        *rdbuf, *mbuf;
344
        int     nw = 3, nr = 13;
345
        unsigned        mlen = (1<<(LGMEMSIZE-2));
346
 
347
        printf("Giving the core 140k cycles to start up\n");
348
        // Before testing, let's give the unit time enough to warm up
349
        tb->reset();
350
        for(int i=0; i<140850; i++)
351
                tb->wb_tick();
352
 
353
        printf("Getting some memory ...\n");
354
        rdbuf = new unsigned[mlen];
355
        mbuf  = new unsigned[mlen]; // Match buffer
356
        printf("Charging my memory with random values\n");
357
        uload(mlen, rdbuf);
358
 
359
        // First test: singular reads through the memory, followed by
360
        // singular  writes
361
        printf("Starting the single-read test\n");
362
        for(int i=0; i<(int)mlen; i++) {
363
                tb->wb_write(i, rdbuf[i]);
364
                tb->wb_tick();
365
                if ((*tb)[i] != rdbuf[i]) {
366
                        printf("WRITE[%06x] = %08x (Expecting %08x) FAILED\n",
367
                                i, (*tb)[i], rdbuf[i]);
368
                        goto test_failure;
369
                } if (tb->bombed())
370
                        goto test_failure;
371
 
372
        } for(int i=0; i<(int)mlen; i++) {
373
                unsigned        v;
374
                if (rdbuf[i] != (v=tb->wb_read(i))) {
375
                        printf("READ[%06x] = %08x (Expecting %08x)\n",
376
                                i, v, rdbuf[i]);
377
                        goto test_failure;
378
                } if (tb->bombed())
379
                        goto test_failure;
380
                tb->wb_tick();
381
        }
382
 
383
        // Second test: Vector writes going through all memory, followed a
384
        // massive vector read
385
        uload(mlen, rdbuf); // Get some new values
386
        tb->wb_write(0, mlen, rdbuf);
387
        if (tb->bombed())
388
                goto test_failure;
389
        for(int i=0; i<(int)mlen; i++) {
390
                unsigned        v;
391
                if (rdbuf[i] != (v=(*tb)[i])) {
392
                        printf("V-WRITE[%06x] = %08x (Expecting %08x)\n",
393
                                i, v, rdbuf[i]);
394
                        goto test_failure;
395
                }
396
        }
397
 
398
        tb->wb_read( 0, mlen, mbuf);
399
        if (tb->bombed())
400
                goto test_failure;
401
        for(int i=0; i<(int)mlen; i++) {
402
                if (rdbuf[i] != mbuf[i]) {
403
                        printf("V-READ[%06x] = %08x (Expecting %08x)\n",
404
                                i, mbuf[i], rdbuf[i]);
405
                        goto test_failure;
406
                }
407
        }
408
 
409
        // Third test: Vector writes going through all memory, an prime number
410
        // of values at a time, followed by reads via a different prime number
411
        for(int i=0; i<(int)mlen; i+=nw) {
412
                int     ln = ((int)mlen-i>nw)?nw:mlen-i;
413
                tb->wb_write(i, nw, &rdbuf[i]);
414
                for(int j=0; j<ln; j++) {
415
                        if ((*tb)[i+j] != rdbuf[i+j]) {
416
                                printf("P-WRITE[%06x] = %08x (Expecting %08x) FAILED\n",
417
                                        i, (*tb)[i], rdbuf[i]);
418
                                goto test_failure;
419
                        }
420
                } if (tb->bombed())
421
                        goto test_failure;
422
        } for(int i=0; i<(int)mlen; i+=nr) {
423
                int     ln = ((int)mlen-i>nr)?nr:mlen-i;
424
                tb->wb_write(i, nr, &mbuf[i]);
425
                for(int j=0; j<ln; j++) {
426
                        if (mbuf[i+j] != rdbuf[i+j]) {
427
                                printf("P-READ[%06x] = %08x (Expecting %08x) FAILED\n",
428
                                        i, mbuf[i], rdbuf[i]);
429
                                goto test_failure;
430
                        }
431
                } if (tb->bombed())
432
                        goto test_failure;
433
        }
434
 
435
 
436
        printf("SUCCESS!!\n");
437
        exit(0);
438
test_failure:
439
        printf("FAIL-HERE\n");
440
        for(int i=0; i<64; i++)
441
                tb->tick();
442
        printf("TEST FAILED\n");
443
        exit(-1);
444
}

powered by: WebSVN 2.1.0

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