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

Subversion Repositories pit

[/] [pit/] [trunk/] [bench/] [verilog/] [tst_bench_top.v] - Blame information for rev 16

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

Line No. Rev Author Line
1 3 rehayes
////////////////////////////////////////////////////////////////////////////////
2
//
3
//  WISHBONE revB.2 compliant Programable Interval Timer - Test Bench
4
//
5
//  Author: Bob Hayes
6
//          rehayes@opencores.org
7
//
8
//  Downloaded from: http://www.opencores.org/projects/pit.....
9
//
10
////////////////////////////////////////////////////////////////////////////////
11
// Copyright (c) 2009, Robert Hayes
12
//
13
// All rights reserved.
14
//
15
// Redistribution and use in source and binary forms, with or without
16
// modification, are permitted provided that the following conditions are met:
17
//     * Redistributions of source code must retain the above copyright
18
//       notice, this list of conditions and the following disclaimer.
19
//     * Redistributions in binary form must reproduce the above copyright
20
//       notice, this list of conditions and the following disclaimer in the
21
//       documentation and/or other materials provided with the distribution.
22
//     * Neither the name of the <organization> nor the
23
//       names of its contributors may be used to endorse or promote products
24
//       derived from this software without specific prior written permission.
25
//
26
// THIS SOFTWARE IS PROVIDED BY Robert Hayes ''AS IS'' AND ANY
27
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
// DISCLAIMED. IN NO EVENT SHALL Robert Hayes BE LIABLE FOR ANY
30
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
////////////////////////////////////////////////////////////////////////////////
37
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
38
 
39
 
40
`include "timescale.v"
41
 
42
module tst_bench_top();
43
 
44 16 rehayes
  parameter STOP_ON_ERROR = 1'b0;
45
  parameter MAX_VECTOR = 1_000;
46
  //
47
  // wires && regs
48
  //
49
  reg        mstr_test_clk;
50
  reg [19:0] vector;
51
  reg [ 7:0] test_num;
52
  reg        rstn;
53
  reg        sync_reset;
54 3 rehayes
 
55 16 rehayes
  wire [31:0] adr;
56
  wire [15:0] dat_i, dat_o, dat0_i, dat1_i, dat2_i, dat3_i;
57
  wire we;
58
  wire stb;
59
  wire cyc;
60
  wire ack, ack_1, ack_2, ack_3, ack_4;
61
  wire inta_1, inta_2, inta_3, inta_4;
62
  wire count_en_1;
63
  wire count_flag_1;
64 3 rehayes
 
65 16 rehayes
  reg [15:0] q, qq;
66
  reg [15:0] error_count;
67 3 rehayes
 
68 16 rehayes
  wire scl, scl0_o, scl0_oen, scl1_o, scl1_oen;
69
  wire sda, sda0_o, sda0_oen, sda1_o, sda1_oen;
70 3 rehayes
 
71 16 rehayes
  // Name Address Locations
72
  parameter PIT_CNTRL = 5'b0_0000;
73
  parameter PIT_MOD   = 5'b0_0001;
74
  parameter PIT_COUNT = 5'b0_0010;
75 3 rehayes
 
76 16 rehayes
  parameter RD      = 1'b1;
77
  parameter WR      = 1'b0;
78
  parameter SADR    = 7'b0010_000;
79 3 rehayes
 
80 16 rehayes
  parameter CTR_EN  = 8'b1000_0000;  // core enable bit
81
  parameter CTR_IEN = 8'b0100_0000;  // core interrupt enable bit
82 3 rehayes
 
83 16 rehayes
  parameter PIT_CNTRL_SLAVE  = 16'h8000;  // PIT Slave mode
84
  parameter PIT_CNTRL_FLAG   = 16'h0004;  // PIT Rollover Flag
85
  parameter PIT_CNTRL_IRQEN  = 16'h0002;  // PIT Interupt Enable
86
  parameter PIT_CNTRL_ENA    = 16'h0001;  // PIT Enable
87 3 rehayes
 
88 16 rehayes
  parameter SLAVE_0_CNTRL = 5'b0_1000;
89
  parameter SLAVE_0_MOD   = 5'b0_1001;
90
  parameter SLAVE_0_COUNT = 5'b0_1010;
91 3 rehayes
 
92 16 rehayes
  parameter SLAVE_1_CNTRL = 5'b1_0000;
93
  parameter SLAVE_1_MOD   = 5'b1_0001;
94
  parameter SLAVE_1_COUNT = 5'b1_0010;
95 3 rehayes
 
96 16 rehayes
  parameter SLAVE_2_CNTRL_0 = 5'b1_1000;
97
  parameter SLAVE_2_CNTRL_1 = 5'b1_1001;
98
  parameter SLAVE_2_MOD_0   = 5'b1_1010;
99
  parameter SLAVE_2_MOD_1   = 5'b1_1011;
100
  parameter SLAVE_2_COUNT_0 = 5'b1_1100;
101
  parameter SLAVE_2_COUNT_1 = 5'b1_1101;
102 3 rehayes
 
103 16 rehayes
  // initial values and testbench setup
104
  initial
105
    begin
106
      mstr_test_clk = 0;
107
      vector = 0;
108
      test_num = 0;
109
      error_count = 0;
110 3 rehayes
 
111 16 rehayes
      `ifdef WAVES
112
           $shm_open("waves");
113
           $shm_probe("AS",tst_bench_top,"AS");
114
           $display("\nINFO: Signal dump enabled ...\n\n");
115
        `endif
116 3 rehayes
 
117 16 rehayes
        `ifdef WAVES_V
118
           $dumpfile ("pit_wave_dump.lxt");
119
           $dumpvars (0, tst_bench_top);
120
           $dumpon;
121
           $display("\nINFO: VCD Signal dump enabled ...\n\n");
122
        `endif
123 3 rehayes
 
124 16 rehayes
    end
125 3 rehayes
 
126 16 rehayes
  // generate clock
127
  always #20 mstr_test_clk = ~mstr_test_clk;
128 3 rehayes
 
129 16 rehayes
  // Keep a count of how many clocks we've simulated
130
  always @(posedge mstr_test_clk)
131
    begin
132
      vector <= vector + 1;
133
      if (vector > MAX_VECTOR)
134
        begin
135
          error_count <= error_count + 1;
136
          $display("\n ------ !!!!! Simulation Timeout at vector=%d\n -------", vector);
137
          wrap_up;
138
        end
139
    end
140 3 rehayes
 
141 16 rehayes
  // Add up errors tha come from WISHBONE read compares
142
  always @u0.cmp_error_detect
143
    begin
144
      error_count <= error_count + 1;
145
    end
146 3 rehayes
 
147 16 rehayes
  // hookup wishbone master model
148
  wb_master_model #(.dwidth(16), .awidth(32))
149
          u0 (
150
          .clk(mstr_test_clk),
151
          .rst(rstn),
152
          .adr(adr),
153
          .din(dat_i),
154
          .dout(dat_o),
155
          .cyc(cyc),
156
          .stb(stb),
157
          .we(we),
158
          .sel(),
159
          .ack(ack),
160
          .err(1'b0),
161
          .rty(1'b0)
162
  );
163 3 rehayes
 
164
 
165 16 rehayes
  // Address decoding for different PIT module instances
166
  wire stb0 = stb && ~adr[4] && ~adr[3];
167
  wire stb1 = stb && ~adr[4] &&  adr[3];
168
  wire stb2 = stb &&  adr[4] && ~adr[3];
169
  wire stb3 = stb &&  adr[4] &&  adr[3];
170 3 rehayes
 
171 16 rehayes
  // Create the Read Data Bus
172
  assign dat_i = ({16{stb0}} & dat0_i) |
173
                 ({16{stb1}} & dat1_i) |
174
                 ({16{stb2}} & dat2_i) |
175
                 ({16{stb3}} & {8'b0, dat3_i[7:0]});
176 8 rehayes
 
177 16 rehayes
  assign ack = ack_1 || ack_2 || ack_3 || ack_4;
178 3 rehayes
 
179 16 rehayes
  // hookup wishbone_PIT_master core - Parameters take all default values
180
  //  Async Reset, 16 bit Bus, 16 bit Granularity
181
  pit_top pit_1(
182
          // wishbone interface
183
          .wb_clk_i(mstr_test_clk),
184
          .wb_rst_i(1'b0),
185
          .arst_i(rstn),
186
          .wb_adr_i(adr[2:0]),
187
          .wb_dat_i(dat_o),
188
          .wb_dat_o(dat0_i),
189
          .wb_we_i(we),
190
          .wb_stb_i(stb0),
191
          .wb_cyc_i(cyc),
192
          .wb_sel_i( 2'b11 ),
193
          .wb_ack_o(ack_1),
194
          .pit_irq_o(inta_1),
195 3 rehayes
 
196 16 rehayes
          .pit_o(pit_1_out),
197
          .ext_sync_i(1'b0),
198
          .cnt_sync_o(count_en_1),
199
          .cnt_flag_o(count_flag_1)
200
  );
201 3 rehayes
 
202 16 rehayes
  // hookup wishbone_PIT_slave core - Parameters take all default values
203
  //  Sync Reset, 16 bit Bus, 16 bit Granularity
204
  pit_top #(.ARST_LVL(1'b1))
205
          pit_2(
206
          // wishbone interface
207
          .wb_clk_i(mstr_test_clk),
208
          .wb_rst_i(sync_reset),
209
          .arst_i(1'b0),
210
          .wb_adr_i(adr[2:0]),
211
          .wb_dat_i(dat_o),
212
          .wb_dat_o(dat1_i),
213
          .wb_we_i(we),
214
          .wb_stb_i(stb1),
215
          .wb_cyc_i(cyc),
216
          .wb_sel_i( 2'b11 ),
217
          .wb_ack_o(ack_2),
218
          .pit_irq_o(inta_2),
219 3 rehayes
 
220 16 rehayes
          .pit_o(pit_2_out),
221
          .ext_sync_i(count_en_1),
222
          .cnt_sync_o(count_en_2),
223
          .cnt_flag_o(count_flag_2)
224
  );
225 3 rehayes
 
226 16 rehayes
  // hookup wishbone_PIT_slave core
227
  //  16 bit Bus, 16 bit Granularity
228
  pit_top #(.NO_PRESCALE(1'b1))
229
          pit_3(
230
          // wishbone interface
231
          .wb_clk_i(mstr_test_clk),
232
          .wb_rst_i(sync_reset),
233
          .arst_i(1'b1),
234
          .wb_adr_i(adr[2:0]),
235
          .wb_dat_i(dat_o),
236
          .wb_dat_o(dat2_i),
237
          .wb_we_i(we),
238
          .wb_stb_i(stb2),
239
          .wb_cyc_i(cyc),
240
          .wb_sel_i( 2'b11 ),
241
          .wb_ack_o(ack_3),
242
          .pit_irq_o(inta_3),
243 3 rehayes
 
244 16 rehayes
          .pit_o(pit_3_out),
245
          .ext_sync_i(count_en_1),
246
          .cnt_sync_o(count_en_3),
247
          .cnt_flag_o(count_flag_3)
248
  );
249 3 rehayes
 
250 16 rehayes
  // hookup wishbone_PIT_slave core
251
  //  8 bit Bus, 8 bit Granularity
252
  pit_top #(.DWIDTH(8))
253
          pit_4(
254
          // wishbone interface
255
          .wb_clk_i(mstr_test_clk),
256
          .wb_rst_i(sync_reset),
257
          .arst_i(1'b1),
258
          .wb_adr_i(adr[2:0]),
259
          .wb_dat_i(dat_o[7:0]),
260
          .wb_dat_o(dat3_i[7:0]),
261
          .wb_we_i(we),
262
          .wb_stb_i(stb3),
263
          .wb_cyc_i(cyc),
264
          .wb_sel_i( 2'b11 ),
265
          .wb_ack_o(ack_4),
266
          .pit_irq_o(inta_4),
267 3 rehayes
 
268 16 rehayes
          .pit_o(pit_4_out),
269
          .ext_sync_i(count_en_1),
270
          .cnt_sync_o(count_en_4),
271
          .cnt_flag_o(count_flag_4)
272
  );
273
 
274 3 rehayes
// Test Program
275
initial
276
  begin
277
      $display("\nstatus: %t Testbench started", $time);
278
 
279
      // reset system
280
      rstn = 1'b1; // negate reset
281
      repeat(1) @(posedge mstr_test_clk);
282
      sync_reset = 1'b1;  // Make the sync reset 1 clock cycle long
283
      #2;          // move the async reset away from the clock edge
284
      rstn = 1'b0; // assert async reset
285
      #5;          // Keep the async reset pulse with less than a clock cycle
286
      rstn = 1'b1; // negate async reset
287
      repeat(1) @(posedge mstr_test_clk);
288
      sync_reset = 1'b0;
289
 
290
      $display("\nstatus: %t done reset", $time);
291
      test_num = test_num + 1;
292
 
293
      repeat(2) @(posedge mstr_test_clk);
294
 
295
      //
296
      // program core
297
      //
298
 
299
      reg_test_16;
300
 
301
      reg_test_8;
302
 
303
      u0.wb_write(1, SLAVE_0_CNTRL,   PIT_CNTRL_SLAVE); // Enable Slave Mode
304
      u0.wb_write(1, SLAVE_1_CNTRL,   PIT_CNTRL_SLAVE); // Enable Slave Mode
305
      u0.wb_write(1, SLAVE_2_CNTRL_1, 16'h0080); // Enable Slave Mode
306
      u0.wb_write(1, SLAVE_0_MOD,     16'h000a); // load Modulo
307
      u0.wb_write(1, SLAVE_1_MOD,     16'h0010); // load Modulo
308
      u0.wb_write(1, SLAVE_2_MOD_0,   16'h0010); // load Modulo
309
 
310
      // Set Master Mode PS=0, Modulo=16
311
      test_num = test_num + 1;
312
      $display("TEST #%d Starts at vector=%d, ms_test", test_num, vector);
313
 
314
      u0.wb_write(1, PIT_MOD,   16'h0010); // load prescaler hi-byte
315
      u0.wb_write(1, PIT_CNTRL, PIT_CNTRL_ENA); // Enable to start counting
316
      $display("status: %t programmed registers", $time);
317
 
318
      wait_flag_set;  // Wait for Counter to tomeout
319
      u0.wb_write(1, PIT_CNTRL, PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
320
 
321
      wait_flag_set;  // Wait for Counter to tomeout
322
      u0.wb_write(1, PIT_CNTRL, PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
323
 
324
      repeat(10) @(posedge mstr_test_clk);
325
      u0.wb_write(1, PIT_CNTRL, 16'b0); //
326
 
327
      repeat(10) @(posedge mstr_test_clk);
328
 
329
      mstr_psx_modx(2,4);
330
 
331
      mstr_psx_modx(4,0);
332
 
333
      repeat(100) @(posedge mstr_test_clk);
334 16 rehayes
 
335
      wrap_up;
336
 
337
  end  // Main Test Flow
338 3 rehayes
 
339
// Poll for flag set
340
task wait_flag_set;
341
  begin
342
    u0.wb_read(1, PIT_CNTRL, q);
343
    while(~|(q & PIT_CNTRL_FLAG))
344
      u0.wb_read(1, PIT_CNTRL, q); // poll it until it is set
345
    $display("PIT Flag set detected at vector =%d", vector);
346
  end
347
endtask
348
 
349
// check register bits - reset, read/write
350
task reg_test_16;
351
  begin
352
      test_num = test_num + 1;
353
      $display("TEST #%d Starts at vector=%d, reg_test_16", test_num, vector);
354
      u0.wb_cmp(0, PIT_CNTRL, 16'h4000);   // verify reset
355
      u0.wb_cmp(0, PIT_MOD,   16'h0000);   // verify reset
356
      u0.wb_cmp(0, PIT_COUNT, 16'h0001);   // verify reset
357
 
358
      u0.wb_write(1, PIT_CNTRL, 16'hfffe); // load prescaler lo-byte
359
      u0.wb_cmp(  0, PIT_CNTRL, 16'hCf02); // verify write data
360
      u0.wb_write(1, PIT_CNTRL, 16'h0000); // load prescaler lo-byte
361
      u0.wb_cmp(  0, PIT_CNTRL, 16'h4000); // verify write data
362
 
363
      u0.wb_write(1, PIT_MOD, 16'h5555); // load prescaler lo-byte
364
      u0.wb_cmp(  0, PIT_MOD, 16'h5555); // verify write data
365
      u0.wb_write(1, PIT_MOD, 16'haaaa); // load prescaler lo-byte
366
      u0.wb_cmp(  0, PIT_MOD, 16'haaaa); // verify write data
367
 
368
      u0.wb_write(0, PIT_COUNT, 16'hfffe);
369
      u0.wb_cmp(  0, PIT_COUNT, 16'h0001); // verify register not writable
370
  end
371
endtask
372
 
373
task reg_test_8;
374
  begin
375
      test_num = test_num + 1;
376
      $display("TEST #%d Starts at vector=%d, reg_test_8", test_num, vector);
377
      u0.wb_cmp(0, SLAVE_2_CNTRL_0, 16'h0000);   // verify reset
378
      u0.wb_cmp(0, SLAVE_2_CNTRL_1, 16'h0040);   // verify reset
379
      u0.wb_cmp(0, SLAVE_2_MOD_0,   16'h0000);   // verify reset
380
      u0.wb_cmp(0, SLAVE_2_MOD_1,   16'h0000);   // verify reset
381
      u0.wb_cmp(0, SLAVE_2_COUNT_0, 16'h0001);   // verify reset
382
      u0.wb_cmp(0, SLAVE_2_COUNT_1, 16'h0000);   // verify reset
383
 
384
      u0.wb_write(1, SLAVE_2_CNTRL_0, 16'hfffe); // load prescaler lo-byte
385
      u0.wb_cmp(  0, SLAVE_2_CNTRL_0, 16'h0002); // verify write data
386
      u0.wb_write(1, SLAVE_2_CNTRL_0, 16'h0000); // load prescaler lo-byte
387
      u0.wb_cmp(  0, SLAVE_2_CNTRL_0, 16'h0000); // verify write data
388
      u0.wb_cmp(  0, SLAVE_2_CNTRL_1, 16'h0040); // verify write data
389
 
390
      u0.wb_write(1, SLAVE_2_MOD_0, 16'hff55); // load prescaler lo-byte
391
      u0.wb_cmp(  0, SLAVE_2_MOD_0, 16'h0055); // verify write data
392
      u0.wb_write(1, SLAVE_2_MOD_0, 16'hffaa); // load prescaler lo-byte
393
      u0.wb_cmp(  0, SLAVE_2_MOD_0, 16'h00aa); // verify write data
394
      u0.wb_write(1, SLAVE_2_MOD_1, 16'hff66); // load prescaler lo-byte
395
      u0.wb_cmp(  0, SLAVE_2_MOD_1, 16'h0066); // verify write data
396
      u0.wb_write(1, SLAVE_2_MOD_1, 16'hff99); // load prescaler lo-byte
397
      u0.wb_cmp(  0, SLAVE_2_MOD_1, 16'h0099); // verify write data
398
      u0.wb_write(1, SLAVE_2_MOD_1, 16'hff00); // load prescaler lo-byte
399
 
400
      u0.wb_write(0, SLAVE_2_COUNT_0, 16'hfffe);
401
      u0.wb_cmp(  0, SLAVE_2_COUNT_0, 16'h0001); // verify register not writable
402
      u0.wb_write(0, SLAVE_2_COUNT_1, 16'hfffe);
403
      u0.wb_cmp(  0, SLAVE_2_COUNT_1, 16'h0000); // verify register not writable
404
  end
405
endtask
406
 
407
task mstr_psx_modx;
408
  input [ 3:0] ps_val;
409
  input [15:0] mod_val;
410
  reg   [15:0] cntrl_val;
411
  begin
412
      test_num = test_num + 1;
413
      $display("TEST #%d Starts at vector=%d, mstr_psx_modx Pre=%h, Mod=%h",
414
                test_num, vector, ps_val, mod_val);
415
      // program internal registers
416
 
417
      cntrl_val = {1'b0, 3'b0, ps_val, 8'b0} | PIT_CNTRL_IRQEN;
418
      u0.wb_write(1, PIT_MOD,   mod_val); // load modulo
419
      u0.wb_write(1, PIT_CNTRL, ( cntrl_val | PIT_CNTRL_ENA)); // Enable to start counting
420
 
421
      wait_flag_set;  // Wait for Counter to timeout
422
      u0.wb_write(1, PIT_CNTRL, cntrl_val | PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
423
 
424
      wait_flag_set;  // Wait for Counter to timeout
425
      u0.wb_write(1, PIT_CNTRL, cntrl_val | PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
426
 
427
      repeat(10) @(posedge mstr_test_clk);
428
 
429
      u0.wb_write(1, PIT_CNTRL, 16'b0); //
430
 
431
   end
432
endtask
433
 
434 16 rehayes
task wrap_up;
435
  begin
436
    test_num = test_num + 1;
437
    repeat(10) @(posedge mstr_test_clk);
438
    $display("\nSimulation Finished!! - vector =%d", vector);
439
    if (error_count == 0)
440
      $display("Simulation Passed");
441
    else
442
      $display("Simulation Failed  --- Errors =%d", error_count);
443 3 rehayes
 
444 16 rehayes
    $finish;
445
  end
446
endtask
447
 
448
 
449 3 rehayes
endmodule  // tst_bench_top
450
 

powered by: WebSVN 2.1.0

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