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

Subversion Repositories pit

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

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 19 rehayes
  //  Async Reset, 16 bit Bus, 16 bit Granularity,Wait States
181
  pit_top #(.SINGLE_CYCLE(1'b0))
182
          pit_1(
183 16 rehayes
          // wishbone interface
184
          .wb_clk_i(mstr_test_clk),
185
          .wb_rst_i(1'b0),
186
          .arst_i(rstn),
187
          .wb_adr_i(adr[2:0]),
188
          .wb_dat_i(dat_o),
189
          .wb_dat_o(dat0_i),
190
          .wb_we_i(we),
191
          .wb_stb_i(stb0),
192
          .wb_cyc_i(cyc),
193
          .wb_sel_i( 2'b11 ),
194
          .wb_ack_o(ack_1),
195
          .pit_irq_o(inta_1),
196 3 rehayes
 
197 16 rehayes
          .pit_o(pit_1_out),
198
          .ext_sync_i(1'b0),
199
          .cnt_sync_o(count_en_1),
200
          .cnt_flag_o(count_flag_1)
201
  );
202 3 rehayes
 
203 16 rehayes
  // hookup wishbone_PIT_slave core - Parameters take all default values
204
  //  Sync Reset, 16 bit Bus, 16 bit Granularity
205
  pit_top #(.ARST_LVL(1'b1))
206
          pit_2(
207
          // wishbone interface
208
          .wb_clk_i(mstr_test_clk),
209
          .wb_rst_i(sync_reset),
210
          .arst_i(1'b0),
211
          .wb_adr_i(adr[2:0]),
212
          .wb_dat_i(dat_o),
213
          .wb_dat_o(dat1_i),
214
          .wb_we_i(we),
215
          .wb_stb_i(stb1),
216
          .wb_cyc_i(cyc),
217
          .wb_sel_i( 2'b11 ),
218
          .wb_ack_o(ack_2),
219
          .pit_irq_o(inta_2),
220 3 rehayes
 
221 16 rehayes
          .pit_o(pit_2_out),
222
          .ext_sync_i(count_en_1),
223
          .cnt_sync_o(count_en_2),
224
          .cnt_flag_o(count_flag_2)
225
  );
226 3 rehayes
 
227 16 rehayes
  // hookup wishbone_PIT_slave core
228
  //  16 bit Bus, 16 bit Granularity
229
  pit_top #(.NO_PRESCALE(1'b1))
230
          pit_3(
231
          // wishbone interface
232
          .wb_clk_i(mstr_test_clk),
233
          .wb_rst_i(sync_reset),
234
          .arst_i(1'b1),
235
          .wb_adr_i(adr[2:0]),
236
          .wb_dat_i(dat_o),
237
          .wb_dat_o(dat2_i),
238
          .wb_we_i(we),
239
          .wb_stb_i(stb2),
240
          .wb_cyc_i(cyc),
241
          .wb_sel_i( 2'b11 ),
242
          .wb_ack_o(ack_3),
243
          .pit_irq_o(inta_3),
244 3 rehayes
 
245 16 rehayes
          .pit_o(pit_3_out),
246
          .ext_sync_i(count_en_1),
247
          .cnt_sync_o(count_en_3),
248
          .cnt_flag_o(count_flag_3)
249
  );
250 3 rehayes
 
251 16 rehayes
  // hookup wishbone_PIT_slave core
252
  //  8 bit Bus, 8 bit Granularity
253
  pit_top #(.DWIDTH(8))
254
          pit_4(
255
          // wishbone interface
256
          .wb_clk_i(mstr_test_clk),
257
          .wb_rst_i(sync_reset),
258
          .arst_i(1'b1),
259
          .wb_adr_i(adr[2:0]),
260
          .wb_dat_i(dat_o[7:0]),
261
          .wb_dat_o(dat3_i[7:0]),
262
          .wb_we_i(we),
263
          .wb_stb_i(stb3),
264
          .wb_cyc_i(cyc),
265
          .wb_sel_i( 2'b11 ),
266
          .wb_ack_o(ack_4),
267
          .pit_irq_o(inta_4),
268 3 rehayes
 
269 16 rehayes
          .pit_o(pit_4_out),
270
          .ext_sync_i(count_en_1),
271
          .cnt_sync_o(count_en_4),
272
          .cnt_flag_o(count_flag_4)
273
  );
274
 
275 3 rehayes
// Test Program
276
initial
277
  begin
278
      $display("\nstatus: %t Testbench started", $time);
279
 
280
      // reset system
281
      rstn = 1'b1; // negate reset
282
      repeat(1) @(posedge mstr_test_clk);
283
      sync_reset = 1'b1;  // Make the sync reset 1 clock cycle long
284
      #2;          // move the async reset away from the clock edge
285
      rstn = 1'b0; // assert async reset
286
      #5;          // Keep the async reset pulse with less than a clock cycle
287
      rstn = 1'b1; // negate async reset
288
      repeat(1) @(posedge mstr_test_clk);
289
      sync_reset = 1'b0;
290
 
291
      $display("\nstatus: %t done reset", $time);
292
      test_num = test_num + 1;
293
 
294
      repeat(2) @(posedge mstr_test_clk);
295
 
296
      //
297
      // program core
298
      //
299
 
300
      reg_test_16;
301
 
302
      reg_test_8;
303
 
304
      u0.wb_write(1, SLAVE_0_CNTRL,   PIT_CNTRL_SLAVE); // Enable Slave Mode
305
      u0.wb_write(1, SLAVE_1_CNTRL,   PIT_CNTRL_SLAVE); // Enable Slave Mode
306
      u0.wb_write(1, SLAVE_2_CNTRL_1, 16'h0080); // Enable Slave Mode
307
      u0.wb_write(1, SLAVE_0_MOD,     16'h000a); // load Modulo
308
      u0.wb_write(1, SLAVE_1_MOD,     16'h0010); // load Modulo
309
      u0.wb_write(1, SLAVE_2_MOD_0,   16'h0010); // load Modulo
310
 
311
      // Set Master Mode PS=0, Modulo=16
312
      test_num = test_num + 1;
313
      $display("TEST #%d Starts at vector=%d, ms_test", test_num, vector);
314
 
315
      u0.wb_write(1, PIT_MOD,   16'h0010); // load prescaler hi-byte
316
      u0.wb_write(1, PIT_CNTRL, PIT_CNTRL_ENA); // Enable to start counting
317
      $display("status: %t programmed registers", $time);
318
 
319
      wait_flag_set;  // Wait for Counter to tomeout
320
      u0.wb_write(1, PIT_CNTRL, PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
321
 
322
      wait_flag_set;  // Wait for Counter to tomeout
323
      u0.wb_write(1, PIT_CNTRL, PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
324
 
325
      repeat(10) @(posedge mstr_test_clk);
326
      u0.wb_write(1, PIT_CNTRL, 16'b0); //
327
 
328
      repeat(10) @(posedge mstr_test_clk);
329
 
330
      mstr_psx_modx(2,4);
331
 
332
      mstr_psx_modx(4,0);
333
 
334
      repeat(100) @(posedge mstr_test_clk);
335 16 rehayes
 
336
      wrap_up;
337
 
338
  end  // Main Test Flow
339 3 rehayes
 
340
// Poll for flag set
341
task wait_flag_set;
342
  begin
343
    u0.wb_read(1, PIT_CNTRL, q);
344
    while(~|(q & PIT_CNTRL_FLAG))
345
      u0.wb_read(1, PIT_CNTRL, q); // poll it until it is set
346
    $display("PIT Flag set detected at vector =%d", vector);
347
  end
348
endtask
349
 
350
// check register bits - reset, read/write
351
task reg_test_16;
352
  begin
353
      test_num = test_num + 1;
354
      $display("TEST #%d Starts at vector=%d, reg_test_16", test_num, vector);
355
      u0.wb_cmp(0, PIT_CNTRL, 16'h4000);   // verify reset
356
      u0.wb_cmp(0, PIT_MOD,   16'h0000);   // verify reset
357
      u0.wb_cmp(0, PIT_COUNT, 16'h0001);   // verify reset
358
 
359
      u0.wb_write(1, PIT_CNTRL, 16'hfffe); // load prescaler lo-byte
360
      u0.wb_cmp(  0, PIT_CNTRL, 16'hCf02); // verify write data
361
      u0.wb_write(1, PIT_CNTRL, 16'h0000); // load prescaler lo-byte
362
      u0.wb_cmp(  0, PIT_CNTRL, 16'h4000); // verify write data
363
 
364
      u0.wb_write(1, PIT_MOD, 16'h5555); // load prescaler lo-byte
365
      u0.wb_cmp(  0, PIT_MOD, 16'h5555); // verify write data
366
      u0.wb_write(1, PIT_MOD, 16'haaaa); // load prescaler lo-byte
367
      u0.wb_cmp(  0, PIT_MOD, 16'haaaa); // verify write data
368
 
369
      u0.wb_write(0, PIT_COUNT, 16'hfffe);
370
      u0.wb_cmp(  0, PIT_COUNT, 16'h0001); // verify register not writable
371
  end
372
endtask
373
 
374
task reg_test_8;
375
  begin
376
      test_num = test_num + 1;
377
      $display("TEST #%d Starts at vector=%d, reg_test_8", test_num, vector);
378
      u0.wb_cmp(0, SLAVE_2_CNTRL_0, 16'h0000);   // verify reset
379
      u0.wb_cmp(0, SLAVE_2_CNTRL_1, 16'h0040);   // verify reset
380
      u0.wb_cmp(0, SLAVE_2_MOD_0,   16'h0000);   // verify reset
381
      u0.wb_cmp(0, SLAVE_2_MOD_1,   16'h0000);   // verify reset
382
      u0.wb_cmp(0, SLAVE_2_COUNT_0, 16'h0001);   // verify reset
383
      u0.wb_cmp(0, SLAVE_2_COUNT_1, 16'h0000);   // verify reset
384
 
385
      u0.wb_write(1, SLAVE_2_CNTRL_0, 16'hfffe); // load prescaler lo-byte
386
      u0.wb_cmp(  0, SLAVE_2_CNTRL_0, 16'h0002); // verify write data
387
      u0.wb_write(1, SLAVE_2_CNTRL_0, 16'h0000); // load prescaler lo-byte
388
      u0.wb_cmp(  0, SLAVE_2_CNTRL_0, 16'h0000); // verify write data
389
      u0.wb_cmp(  0, SLAVE_2_CNTRL_1, 16'h0040); // verify write data
390
 
391
      u0.wb_write(1, SLAVE_2_MOD_0, 16'hff55); // load prescaler lo-byte
392
      u0.wb_cmp(  0, SLAVE_2_MOD_0, 16'h0055); // verify write data
393
      u0.wb_write(1, SLAVE_2_MOD_0, 16'hffaa); // load prescaler lo-byte
394
      u0.wb_cmp(  0, SLAVE_2_MOD_0, 16'h00aa); // verify write data
395
      u0.wb_write(1, SLAVE_2_MOD_1, 16'hff66); // load prescaler lo-byte
396
      u0.wb_cmp(  0, SLAVE_2_MOD_1, 16'h0066); // verify write data
397
      u0.wb_write(1, SLAVE_2_MOD_1, 16'hff99); // load prescaler lo-byte
398
      u0.wb_cmp(  0, SLAVE_2_MOD_1, 16'h0099); // verify write data
399
      u0.wb_write(1, SLAVE_2_MOD_1, 16'hff00); // load prescaler lo-byte
400
 
401
      u0.wb_write(0, SLAVE_2_COUNT_0, 16'hfffe);
402
      u0.wb_cmp(  0, SLAVE_2_COUNT_0, 16'h0001); // verify register not writable
403
      u0.wb_write(0, SLAVE_2_COUNT_1, 16'hfffe);
404
      u0.wb_cmp(  0, SLAVE_2_COUNT_1, 16'h0000); // verify register not writable
405
  end
406
endtask
407
 
408
task mstr_psx_modx;
409
  input [ 3:0] ps_val;
410
  input [15:0] mod_val;
411
  reg   [15:0] cntrl_val;
412
  begin
413
      test_num = test_num + 1;
414
      $display("TEST #%d Starts at vector=%d, mstr_psx_modx Pre=%h, Mod=%h",
415
                test_num, vector, ps_val, mod_val);
416
      // program internal registers
417
 
418
      cntrl_val = {1'b0, 3'b0, ps_val, 8'b0} | PIT_CNTRL_IRQEN;
419
      u0.wb_write(1, PIT_MOD,   mod_val); // load modulo
420
      u0.wb_write(1, PIT_CNTRL, ( cntrl_val | PIT_CNTRL_ENA)); // Enable to start counting
421
 
422
      wait_flag_set;  // Wait for Counter to timeout
423
      u0.wb_write(1, PIT_CNTRL, cntrl_val | PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
424
 
425
      wait_flag_set;  // Wait for Counter to timeout
426
      u0.wb_write(1, PIT_CNTRL, cntrl_val | PIT_CNTRL_FLAG | PIT_CNTRL_ENA); //
427
 
428
      repeat(10) @(posedge mstr_test_clk);
429
 
430
      u0.wb_write(1, PIT_CNTRL, 16'b0); //
431
 
432
   end
433
endtask
434
 
435 16 rehayes
task wrap_up;
436
  begin
437
    test_num = test_num + 1;
438
    repeat(10) @(posedge mstr_test_clk);
439
    $display("\nSimulation Finished!! - vector =%d", vector);
440
    if (error_count == 0)
441
      $display("Simulation Passed");
442
    else
443
      $display("Simulation Failed  --- Errors =%d", error_count);
444 3 rehayes
 
445 16 rehayes
    $finish;
446
  end
447
endtask
448
 
449
 
450 3 rehayes
endmodule  // tst_bench_top
451
 

powered by: WebSVN 2.1.0

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