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

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [tb/] [tb.v] - Blame information for rev 17

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

Line No. Rev Author Line
1 2 csantifort
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
//  Top Level testbench                                         //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  Instantiates the system, ddr3 memory model and tb_uart      //
10
//                                                              //
11
//  Author(s):                                                  //
12
//      - Conor Santifort, csantifort.amber@gmail.com           //
13
//                                                              //
14
//////////////////////////////////////////////////////////////////
15
//                                                              //
16
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
17
//                                                              //
18
// This source file may be used and distributed without         //
19
// restriction provided that this copyright statement is not    //
20
// removed from the file and that any derivative work contains  //
21
// the original copyright notice and the associated disclaimer. //
22
//                                                              //
23
// This source file is free software; you can redistribute it   //
24
// and/or modify it under the terms of the GNU Lesser General   //
25
// Public License as published by the Free Software Foundation; //
26
// either version 2.1 of the License, or (at your option) any   //
27
// later version.                                               //
28
//                                                              //
29
// This source is distributed in the hope that it will be       //
30
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
31
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
32
// PURPOSE.  See the GNU Lesser General Public License for more //
33
// details.                                                     //
34
//                                                              //
35
// You should have received a copy of the GNU Lesser General    //
36
// Public License along with this source; if not, download it   //
37
// from http://www.opencores.org/lgpl.shtml                     //
38
//                                                              //
39
//////////////////////////////////////////////////////////////////
40
 
41
`timescale  1 ps / 1 ps
42
 
43
`include "system_config_defines.v"
44
`include "global_defines.v"
45
 
46
 
47
module tb();
48
 
49
`include "debug_functions.v"
50
 
51
reg                     sysrst;
52
`ifdef XILINX_VIRTEX6_FPGA
53
reg                     clk_533mhz;
54
`endif
55
reg                     clk_200mhz;
56
reg                     clk_25mhz;
57 15 csantifort
reg [31:0]              clk_count = 'd0;
58 2 csantifort
 
59
integer                 log_file;
60
 
61
`ifdef AMBER_LOAD_MAIN_MEM
62
integer                 main_mem_file;
63
reg     [31:0]          main_mem_file_address;
64
reg     [31:0]          main_mem_file_data;
65
reg     [127:0]         main_mem_file_data_128;
66
integer                 main_mem_line_count;
67
reg     [22:0]          mm_ddr3_addr;
68
`endif
69
 
70
integer                 boot_mem_file;
71
reg     [31:0]          boot_mem_file_address;
72
reg     [31:0]          boot_mem_file_data;
73
integer                 boot_mem_line_count;
74
integer                 fgets_return;
75
reg     [120*8-1:0]     line;
76
reg     [120*8-1:0]     aligned_line;
77
 
78
wire [12:0]             ddr3_addr;
79
wire [2:0]              ddr3_ba;
80
wire                    ddr3_ck_p;
81
wire                    ddr3_ck_n;
82
wire [15:0]             ddr3_dq;
83
wire [1:0]              ddr3_dqs_p;
84
wire [1:0]              ddr3_dqs_n;
85
wire [1:0]              ddr3_dm;
86
wire                    ddr3_ras_n;
87
wire                    ddr3_cas_n;
88
wire                    ddr3_we_n;
89
wire                    ddr3_cke;
90
wire                    ddr3_odt;
91
wire                    ddr3_reset_n;
92
 
93
 
94
`ifdef XILINX_SPARTAN6_FPGA
95
wire                    mcb3_rzq;
96
wire                    mcb3_zio;
97
`endif
98
 
99
tri1                    md_pad_io;
100
 
101
wire                    uart0_cts;
102
wire                    uart0_rx;
103
wire                    uart0_rts;
104
wire                    uart0_tx;
105
 
106
 
107
// ======================================
108
// Instantiate FPGA
109
// ======================================
110
system u_system (
111
    // Clocks and resets
112
    .brd_rst            ( sysrst            ),
113
    .brd_clk_p          ( clk_200mhz        ),
114
    .brd_clk_n          ( ~clk_200mhz       ),
115
 
116
    `ifdef XILINX_VIRTEX6_FPGA
117
    .sys_clk_p          ( clk_533mhz        ),
118
    .sys_clk_n          ( ~clk_533mhz       ),
119
    `endif
120
 
121
    // UART 0 signals
122
    .o_uart0_cts        ( uart0_cts         ),
123
    .o_uart0_rx         ( uart0_rx          ),
124
    .i_uart0_rts        ( uart0_rts         ),
125
    .i_uart0_tx         ( uart0_tx          ),
126
 
127
    // DDR3 signals
128
    .ddr3_dq            ( ddr3_dq           ),
129
    .ddr3_addr          ( ddr3_addr         ),
130
    .ddr3_ba            ( ddr3_ba           ),
131
    .ddr3_ras_n         ( ddr3_ras_n        ),
132
    .ddr3_cas_n         ( ddr3_cas_n        ),
133
    .ddr3_we_n          ( ddr3_we_n         ),
134
    .ddr3_odt           ( ddr3_odt          ),
135
    .ddr3_reset_n       ( ddr3_reset_n      ),
136
    .ddr3_cke           ( ddr3_cke          ),
137
    .ddr3_dm            ( ddr3_dm           ),
138
    .ddr3_dqs_p         ( ddr3_dqs_p        ),
139
    .ddr3_dqs_n         ( ddr3_dqs_n        ),
140
    .ddr3_ck_p          ( ddr3_ck_p         ),
141
    .ddr3_ck_n          ( ddr3_ck_n         ),
142
    `ifdef XILINX_VIRTEX6_FPGA
143
    .ddr3_cs_n          ( ddr3_cs_n         ),
144
    `endif
145
    `ifdef XILINX_SPARTAN6_FPGA
146
    .mcb3_rzq           ( mcb3_rzq          ),
147
    .mcb3_zio           ( mcb3_zio          ),
148
    `endif
149
 
150
    // Ethernet MII signals
151
    .mtx_clk_pad_i      ( clk_25mhz         ),
152
    .mtxd_pad_o         (                   ),
153
    .mtxen_pad_o        (                   ),
154
    .mtxerr_pad_o       (                   ),
155
    .mrx_clk_pad_i      ( clk_25mhz         ),
156
    .mrxd_pad_i         ( 4'd0              ),
157
    .mrxdv_pad_i        ( 1'd0              ),
158
    .mrxerr_pad_i       ( 1'd0              ),
159
    .mcoll_pad_i        ( 1'd0              ),
160
    .mcrs_pad_i         ( 1'd0              ),  // Assert Carrier Sense from PHY
161
    .phy_reset_n        (                   ),
162
 
163
    // Ethernet MD signals
164
    .md_pad_io          ( md_pad_io         ),
165
    .mdc_pad_o          (                   )
166
 
167
);
168
 
169
 
170
// ======================================
171
// Instantiate DDR3 Memory Model
172
// ======================================
173
`ifdef XILINX_FPGA
174
    ddr3_model_c3 #(
175
          .DEBUG      ( 0                   )   // Set to 1 to enable debug messages
176
          )
177
    u_ddr3_model (
178
          .ck         ( ddr3_ck_p           ),
179
          .ck_n       ( ddr3_ck_n           ),
180
          .cke        ( ddr3_cke            ),
181
          `ifdef XILINX_VIRTEX6_FPGA
182
          .cs_n       ( ddr3_cs_n           ),
183
          `else
184
          .cs_n       ( 1'b0                ),
185
          `endif
186
          .ras_n      ( ddr3_ras_n          ),
187
          .cas_n      ( ddr3_cas_n          ),
188
          .we_n       ( ddr3_we_n           ),
189
          .dm_tdqs    ( ddr3_dm             ),
190
          .ba         ( ddr3_ba             ),
191
          .addr       ( {1'd0, ddr3_addr}   ),
192 11 csantifort
          .dq         ( ddr3_dq             ),
193
          .dqs        ( ddr3_dqs_p          ),
194
          .dqs_n      ( ddr3_dqs_n          ),
195 2 csantifort
          .tdqs_n     (                     ),
196
          .odt        ( ddr3_odt            ),
197
          .rst_n      ( ddr3_reset_n        )
198
          );
199
`endif
200
 
201
 
202
// ======================================
203
// Instantiate Testbench UART
204
// ======================================
205
tb_uart u_tb_uart (
206
    .i_uart_cts_n   ( uart0_cts ),          // Clear To Send
207
    .i_uart_rxd     ( uart0_rx  ),
208
    .o_uart_rts_n   ( uart0_rts ),          // Request to Send
209
    .o_uart_txd     ( uart0_tx  )
210
 
211
);
212
 
213
 
214
// ======================================
215
// Global module for xilinx hardware simulations
216
// ======================================
217
`ifdef XILINX_FPGA
218
    `define GLBL
219
    glbl glbl();
220
`endif
221
 
222
 
223
// ======================================
224
// Clock and Reset
225
// ======================================
226
 
227
// 200 MHz clock
228
initial
229
    begin
230
    clk_200mhz = 1'd0;
231
    // Time unit is pico-seconds
232
    forever #2500 clk_200mhz = ~clk_200mhz;
233
    end
234
 
235
 
236
`ifdef XILINX_VIRTEX6_FPGA
237
// 400 MHz clock
238
initial
239
    begin
240
    clk_533mhz = 1'd0;
241
    // Time unit is pico-seconds
242
    forever #938 clk_533mhz = ~clk_533mhz;
243
    end
244
`endif
245
 
246
 
247
// 25 MHz clock
248
initial
249
    begin
250
    clk_25mhz = 1'd0;
251
    forever #20000 clk_25mhz = ~clk_25mhz;
252
    end
253
 
254
initial
255
    begin
256
    sysrst = 1'd1;
257 15 csantifort
    #40000
258 2 csantifort
    sysrst = 1'd0;
259
    end
260
 
261
 
262
// ======================================
263
// Counter of system clock ticks        
264
// ======================================
265
always @ ( posedge `U_SYSTEM.sys_clk )
266 15 csantifort
    clk_count <= clk_count + 1'd1;
267 2 csantifort
 
268
 
269
 
270
// ======================================
271
// Test Name
272
// ======================================
273
initial
274
    begin
275
    $display("Test %s, log file %s",`AMBER_TEST_NAME, `AMBER_LOG_FILE);
276
    log_file = $fopen(`AMBER_LOG_FILE, "a");
277
    end
278
 
279
 
280
 
281
// ======================================
282
// Initialize Boot Memory
283
// ======================================
284
`ifndef XILINX_FPGA
285
    initial
286
        begin
287
        $display("Load boot memory from %s", `BOOT_MEM_FILE);
288
        boot_mem_line_count   = 0;
289
        boot_mem_file         = $fopen(`BOOT_MEM_FILE,    "r");
290
        if (boot_mem_file == 0)
291
            begin
292
            `TB_ERROR_MESSAGE
293
            $display("ERROR: Can't open input file %s", `BOOT_MEM_FILE);
294
            end
295
 
296
        if (boot_mem_file != 0)
297
            begin
298
            fgets_return = 1;
299
            while (fgets_return != 0)
300
                begin
301
                fgets_return        = $fgets(line, boot_mem_file);
302
                boot_mem_line_count = boot_mem_line_count + 1;
303
                aligned_line        = align_line(line);
304
 
305
                // if the line does not start with a comment
306
                if (aligned_line[120*8-1:118*8] != 16'h2f2f)
307
                    begin
308
                    // check that line doesnt start with a '@' or a blank
309
                    if (aligned_line[120*8-1:119*8] != 8'h40 && aligned_line[120*8-1:119*8] != 8'h00)
310
                        begin
311
                        $display("Format ERROR in input file %s, line %1d. Line must start with a @, not %08x",
312
                                 `BOOT_MEM_FILE, boot_mem_line_count, aligned_line[118*8-1:117*8]);
313
                        `TB_ERROR_MESSAGE
314
                        end
315
 
316
                    if (aligned_line[120*8-1:119*8] != 8'h00)
317
                        begin
318
                        boot_mem_file_address  =   hex_chars_to_32bits (aligned_line[119*8-1:111*8]);
319
                        boot_mem_file_data     =   hex_chars_to_32bits (aligned_line[110*8-1:102*8]);
320
 
321
                        tb.u_system.u_boot_mem.u_mem.mem [boot_mem_file_address[12:2]] = boot_mem_file_data;
322
 
323
                        `ifdef AMBER_LOAD_MEM_DEBUG
324
                            $display ("Load Boot Mem: PAddr: 0x%08x, Data 0x%08x",
325
                                        boot_mem_file_address, boot_mem_file_data);
326
                        `endif
327
                        end
328
                    end
329
                end
330
 
331
            $display("Read in %1d lines", boot_mem_line_count);
332
            end
333
        end
334
 
335
`endif
336
 
337
 
338
// ======================================
339
// Initialize Main Memory
340
// ======================================
341
`ifdef AMBER_LOAD_MAIN_MEM
342
    initial
343
        begin
344
        $display("Load main memory from %s", `MAIN_MEM_FILE);
345
        `ifdef XILINX_FPGA
346
        // Wait for DDR3 initialization to complete
347
        $display("Wait for DDR3 initialization to complete before loading main memory");
348
        #70000000
349
        $display("Done waiting at %d ticks", `U_TB.clk_count);
350
        `endif
351
        main_mem_file   = $fopen(`MAIN_MEM_FILE, "r");
352
 
353
        // Read RAM File
354
        main_mem_line_count   = 0;
355
 
356
        if (main_mem_file == 0)
357
            begin
358
            $display("ERROR: Can't open input file %s", `MAIN_MEM_FILE);
359
            `TB_ERROR_MESSAGE
360
            end
361
 
362
 
363
        if (main_mem_file != 0)
364
            begin
365
            fgets_return = 1;
366
            while (fgets_return != 0)
367
                begin
368
                fgets_return        = $fgets(line, main_mem_file);
369
                main_mem_line_count = main_mem_line_count + 1;
370
                aligned_line        = align_line(line);
371
 
372
                // if the line does not start with a comment
373
                if (aligned_line[120*8-1:118*8] != 16'h2f2f)
374
                    begin
375
                    // check that line doesnt start with a '@' or a blank
376
                    if (aligned_line[120*8-1:119*8] != 8'h40 && aligned_line[120*8-1:119*8] != 8'h00)
377
                        begin
378
                        $display("Format ERROR in input file %s, line %1d. Line must start with a @, not %08x",
379
                                 `MAIN_MEM_FILE, main_mem_line_count, aligned_line[118*8-1:117*8]);
380
                        `TB_ERROR_MESSAGE
381
                        end
382
 
383
                    if (aligned_line[120*8-1:119*8] != 8'h00)
384
                        begin
385
                        main_mem_file_address =   hex_chars_to_32bits (aligned_line[119*8-1:111*8]);
386
                        main_mem_file_data    =   hex_chars_to_32bits (aligned_line[110*8-1:102*8]);
387
 
388
                        `ifdef XILINX_FPGA
389
                            mm_ddr3_addr = {main_mem_file_address[13:11], main_mem_file_address[26:14], main_mem_file_address[10:4]};
390
 
391 11 csantifort
                            main_mem_file_data_128 = tb.u_ddr3_model.memory [mm_ddr3_addr];
392
                            tb.u_ddr3_model.memory [mm_ddr3_addr] =
393 2 csantifort
                                    insert_32_into_128 ( main_mem_file_address[3:2],
394
                                                         main_mem_file_data_128,
395
                                                         main_mem_file_data );
396
 
397
                            `ifdef AMBER_LOAD_MEM_DEBUG
398 11 csantifort
                                main_mem_file_data_128 = tb.u_ddr3_model.memory [mm_ddr3_addr];
399 2 csantifort
                                $display ("Load DDR3: PAddr: 0x%08x, DDR3 Addr 0x%08h, Data 0x%032x",
400
                                          main_mem_file_address, mm_ddr3_addr, main_mem_file_data_128);
401
                            `endif
402
 
403
                        `else
404
                            // Fast simulation model of main memory
405
 
406
                            // U_RAM - Can either point to simple or Xilinx DDR3 model. 
407
                            // Set in hierarchy_defines.v
408
 
409
                            main_mem_file_data_128 = `U_RAM [main_mem_file_address[31:4]];
410
                            `U_RAM [main_mem_file_address[31:4]] =
411
                                insert_32_into_128 ( main_mem_file_address[3:2],
412
                                                     main_mem_file_data_128,
413
                                                     main_mem_file_data );
414
 
415
                            `ifdef AMBER_LOAD_MEM_DEBUG
416
                                $display ("Load RAM: PAddr: 0x%08x, Data 0x%08x",
417
                                           main_mem_file_address, main_mem_file_data);
418
                            `endif
419
 
420
                        `endif
421
 
422
                        end
423
                    end
424
                end
425
 
426
            $display("Read in %1d lines", main_mem_line_count);
427
            end
428
        end
429
`endif
430
 
431
 
432
dumpvcd u_dumpvcd();
433
 
434
// ======================================
435
// Terminate Test  
436
// ======================================
437 15 csantifort
`ifdef AMBER_A25_CORE
438
    `include "a25_localparams.v"
439
    `include "a25_functions.v"
440
`else
441
    `include "a23_localparams.v"
442
    `include "a23_functions.v"
443
`endif
444 2 csantifort
 
445
reg testfail;
446
wire        test_status_set;
447
wire [31:0] test_status_reg;
448
 
449
initial
450
    testfail = 1'd0;
451
 
452
assign test_status_set = `U_TEST_MODULE.test_status_set;
453
assign test_status_reg = `U_TEST_MODULE.test_status_reg;
454
 
455
always @*
456
        begin
457
        if ( test_status_set || testfail )
458
            begin
459
            if ( test_status_reg == 32'd17 && !testfail )
460
                begin
461
                display_registers;
462
                $display("++++++++++++++++++++");
463 15 csantifort
                $write("Passed %s %0d ticks\n", `AMBER_TEST_NAME, `U_TB.clk_count);
464 2 csantifort
                $display("++++++++++++++++++++");
465 15 csantifort
                $fwrite(`U_TB.log_file,"Passed %s %0d ticks\n", `AMBER_TEST_NAME, `U_TB.clk_count);
466 2 csantifort
                $finish;
467
                end
468
            else
469
                begin
470
                display_registers;
471
                if ( testfail )
472
                    begin
473
                    $display("++++++++++++++++++++");
474 15 csantifort
                    $write("Failed %s\n", `AMBER_TEST_NAME);
475 2 csantifort
                    $display("++++++++++++++++++++");
476 15 csantifort
                    $fwrite(`U_TB.log_file,"Failed %s\n", `AMBER_TEST_NAME);
477 2 csantifort
                    $finish;
478
                    end
479
                else
480
                    begin
481
                    $display("++++++++++++++++++++");
482
                    if (test_status_reg >= 32'h8000)
483
                        $write("Failed %s - with error 0x%08x\n", `AMBER_TEST_NAME, test_status_reg);
484
                    else
485
                        $write("Failed %s - with error %1d\n", `AMBER_TEST_NAME, test_status_reg);
486
                    $display("++++++++++++++++++++");
487
                    if (test_status_reg >= 32'h8000)
488
                        $fwrite(`U_TB.log_file,"Failed %s - with error 0x%08h\n", `AMBER_TEST_NAME, test_status_reg);
489
                    else
490
                        $fwrite(`U_TB.log_file,"Failed %s - with error %1d\n", `AMBER_TEST_NAME, test_status_reg);
491
                    $finish;
492
                    end
493
                end
494
            end
495
        end
496
 
497
 
498
// ======================================
499 15 csantifort
// Timeout
500
// ======================================
501
always @ ( posedge `U_SYSTEM.sys_clk )
502
    if ( `AMBER_TIMEOUT != 0 )
503
        if (`U_TB.clk_count >= `AMBER_TIMEOUT)
504
            begin
505
            `TB_ERROR_MESSAGE
506
            $display("Timeout Error");
507
            end
508
 
509
// ======================================
510 2 csantifort
// Tasks
511
// ======================================
512
task display_registers;
513
begin
514
    $display("");
515
    $display("----------------------------------------------------------------------------");
516
    $display("Amber Core");
517
 
518
    case (`U_EXECUTE.status_bits_mode)
519
        FIRQ:    $display("         User       > FIRQ         IRQ          SVC");
520
        IRQ:     $display("         User         FIRQ       > IRQ          SVC");
521
        SVC:     $display("         User         FIRQ         IRQ        > SVC");
522
        default: $display("       > User         FIRQ         IRQ          SVC");
523
    endcase
524
 
525
    $display("r0       0x%08x", `U_REGISTER_BANK.r0);
526
    $display("r1       0x%08x", `U_REGISTER_BANK.r1);
527
    $display("r2       0x%08x", `U_REGISTER_BANK.r2);
528
    $display("r3       0x%08x", `U_REGISTER_BANK.r3);
529
    $display("r4       0x%08x", `U_REGISTER_BANK.r4);
530
    $display("r5       0x%08x", `U_REGISTER_BANK.r5);
531
    $display("r6       0x%08x", `U_REGISTER_BANK.r6);
532
    $display("r7       0x%08x", `U_REGISTER_BANK.r7);
533
    $display("r8       0x%08x   0x%08x ", `U_REGISTER_BANK.r8,  `U_REGISTER_BANK.r8_firq);
534
    $display("r9       0x%08x   0x%08x ", `U_REGISTER_BANK.r9,  `U_REGISTER_BANK.r9_firq);
535
    $display("r10      0x%08x   0x%08x ", `U_REGISTER_BANK.r10, `U_REGISTER_BANK.r10_firq);
536
    $display("r11      0x%08x   0x%08x ", `U_REGISTER_BANK.r11, `U_REGISTER_BANK.r11_firq);
537
    $display("r12      0x%08x   0x%08x ", `U_REGISTER_BANK.r12, `U_REGISTER_BANK.r12_firq);
538
 
539
    $display("r13      0x%08x   0x%08x   0x%08x   0x%08x",
540
                                               `U_REGISTER_BANK.r13,
541
                                               `U_REGISTER_BANK.r13_firq,
542
                                               `U_REGISTER_BANK.r13_irq,
543
                                               `U_REGISTER_BANK.r13_svc);
544
    $display("r14 (lr) 0x%08x   0x%08x   0x%08x   0x%08x",
545
                                               `U_REGISTER_BANK.r14,
546
                                               `U_REGISTER_BANK.r14_firq,
547
                                               `U_REGISTER_BANK.r14_irq,
548
                                               `U_REGISTER_BANK.r14_svc);
549
 
550
 
551
    $display("r15 (pc) 0x%08x", {6'd0,`U_REGISTER_BANK.r15,2'd0});
552
    $display("");
553
    $display("Status Bits: N=%d, Z=%d, C=%d, V=%d, IRQ Mask %d, FIRQ Mask %d, Mode = %s",
554
       `U_EXECUTE.status_bits_flags[3],
555
       `U_EXECUTE.status_bits_flags[2],
556
       `U_EXECUTE.status_bits_flags[1],
557
       `U_EXECUTE.status_bits_flags[0],
558
       `U_EXECUTE.status_bits_irq_mask,
559
       `U_EXECUTE.status_bits_firq_mask,
560
       mode_name (`U_EXECUTE.status_bits_mode) );
561
    $display("----------------------------------------------------------------------------");
562
    $display("");
563
 
564
end
565
endtask
566
 
567
 
568
// ======================================
569
// Functions
570
// ======================================
571
function [127:0] insert_32_into_128;
572
input [1:0]   pos;
573
input [127:0] word128;
574
input [31:0]  word32;
575
begin
576
     case (pos)
577
         2'd0: insert_32_into_128 = {word128[127:32], word32};
578
         2'd1: insert_32_into_128 = {word128[127:64], word32, word128[31:0]};
579
         2'd2: insert_32_into_128 = {word128[127:96], word32, word128[63:0]};
580
         2'd3: insert_32_into_128 = {word32, word128[95:0]};
581
     endcase
582
end
583
endfunction
584
 
585
 
586 15 csantifort
endmodule
587 2 csantifort
 

powered by: WebSVN 2.1.0

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