/
bit_diff_tb.sv
2097 lines (1670 loc) · 74.2 KB
/
bit_diff_tb.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Greg Stitt
// University of Florida
//
// This file contains a collection of testbenches that graduate evolve a simple
// testbench into a more complex constrained-random verfication (CRV) testbench
// that would be used to test more complex modules.
`timescale 1 ns / 10 ps
// Module: bit_diff_tb_basic
// Description: Follows the simple template seen so far. Although simple, this
// template has significant limitations that become more apparent when doing
// more complex tests that are required for more complex modules.
//
// Specificially, a testbench often has 4 primary parts:
// -Generation of test sequences that stimulate the DUT
// -A driver that converts that test sequences into DUT pin values
// -A monitor that checks the DUT outputs when new results or other behaviors
// -A scoreboard that compares any results from the monitor with correct values
// based on the a reference model that is applied to the same test sequences.
//
// The primary limitation of this simple testbench is that it does all these
// parts in the same region of code. This makes it hard to modify one part
// without affecting the others. It also makes it hard for other people to
// understand the purpose of the code. Finally, it doesn't scale well to
// complicated tests, and multiple types of tests.
//
// So, we'll first understand this basic testbench and then gradually transform
// it into something where the different parts are isolated from each other.
module bit_diff_tb_basic;
localparam NUM_TESTS = 1000;
localparam WIDTH = 16;
logic clk, rst, go, done;
logic [WIDTH-1:0] data;
logic signed [$clog2(2*WIDTH+1)-1:0] result;
// Testbench variables
int passed, failed, reference;
// Instantiate the DUT
bit_diff #(.WIDTH(WIDTH)) DUT (.*);
// Reference model for getting the correct result.
function int model(int data, int width);
automatic int diff = 0;
for (int i=0; i < width; i++) begin
diff = data[0] ? diff+1 : diff-1;
data = data >> 1;
end
return diff;
endfunction
// Generate the clock.
initial begin : generate_clock
clk = 1'b0;
while(1) #5 clk = ~clk;
end
// Do everything else.
initial begin
$timeformat(-9, 0, " ns");
passed = 0;
failed = 0;
// Reset the design.
rst <= 1'b1;
go <= 1'b0;
data <= '0;
for (int i=0; i < 5; i++) @(posedge clk);
@(negedge clk);
rst <= 1'b0;
// Perform NUM_TESTS number of randome tests.
for (int i=0; i < NUM_TESTS; i++) begin
data <= $random;
go <= 1'b1;
@(posedge clk);
go <= 1'b0;
// Works for registered outputs, but not safe for glitches that may
// occur from combinational logic outputs.
// Test bit_diff_fsmd_2p for an example of where this fails.
//@(posedge done);
// Instead, wait until done is cleared on an edge, and then asserted
// on an edge.
@(posedge clk iff (done == 1'b0));
//$display("Done is 0 (time %0t).", $time);
@(posedge clk iff (done == 1'b1));
//$display("Done is 1 (time %0t).", $time);
// Similar strategy, but less concise
/*while(1) begin
@(posedge clk);
if (done) break;
end */
// Compare the output with the reference model.
reference = model(data, WIDTH);
if (result == reference) begin
$display("Test passed (time %0t) for input = %h", $time, data);
passed ++;
end
else begin
$display("Test failed (time %0t): result = %0d instead of %0d for input = %h.", $time, result, reference, data);
failed ++;
end
end
$display("Tests completed: %0d passed, %0d failed", passed, failed);
disable generate_clock;
end
// Check to make sure done cleared within a cycle. Go is anded with done
// because go should have no effect when the circuit is already active.
assert property (@(posedge clk) disable iff (rst) go && done |=> !done);
// Check to make sure done is only cleared when go is asserted (i.e. done is
// left asserted indefinitely).
assert property (@(posedge clk) disable iff (rst) $fell(done) |-> $past(go,1));
endmodule // bit_diff_tb_basic
// For the next testbench, we are going to start to decouple the different
// reponsibilities into separate code. First, we are going to separate the
// generator, which is responsible for generating input sequences. We generally
// want to do this with constrained-random verification, which benefits from
// having a class that acts as a basic transaction object that represents an
// abstract version of the input.
//
// One common source of confusion is how the generator and driver are different.
// In all the simple testbenches, they really weren't different. Every
// transaction object was just the set of input signals. As we get more abstract
// the transaction object starts to become much simpler.
//
// For the bit difference example, the simplest transaction we could have is
// the value of the data we want to test. The generator will generate
// sequences to test (without any consideration of the timing or interface
// protocol), and then the driver will convert each transaction into the
// corresponding set of inputs of the DUT pins.
class bit_diff_item1 #(WIDTH);
rand bit [WIDTH-1:0] data;
endclass
// This initial generator simply uses CRV to create input sequences, while
// communicating with the driver.
//
// Like modules, classes can also have parameters. In this case, the generator
// has a num_tests parameter which control the number of tests generated.
class generator1 #(int NUM_TESTS, int WIDTH);
// Mailboxes are simply queues that allow different tasks to pass data
// between them.
mailbox driver_mailbox;
// Custom events can be used to synchronize tasks. Here we have events
// that will represent the completion of the driver and generator.
event driver_done_event;
event generator_done_event;
// Our generator uses a run method/task that produces the specified number
// of random inputs.
task run();
// Create a new transaction object.
bit_diff_item1 #(WIDTH) item = new;
// Perform num_tests tests.
for (int i=0; i < NUM_TESTS; i++) begin
// Use the CRV randomize functionality to generate a random inpu.
if (!item.randomize()) $display("Randomize failed");
// Print a message so we know what is going on at what time.
$display("Time %0t [Generator]: Generating input h%h for test %0d.", $time, item.data, i);
// Send the random input to the driver.
driver_mailbox.put(item);
// Wait on the custom event for the driver to finish driving the
// generated test before starting a new test.
@(driver_done_event);
end
// Report completion and trigger the generator done event for anything
// waiting on the generator to finish.
$display("Time %0t [Generator]: Generator done.", $time);
-> generator_done_event;
endtask
endclass // generator
// Our initial driver will also be very simple. It will wait until it gets
// an input to drive, assert the appropriate signals and wait for the test
// to complete, then signal the generator to provide another test.
class driver1 #(WIDTH);
logic clk, rst, go, done;
logic [WIDTH-1:0] data;
logic signed [$clog2(2*WIDTH+1)-1:0] result;
// Again, we have the same mailbox and event used in the generator. These
// actually aren't the same yet, they are just handles to some mailbox that
// hasn't been assigned yet. When we create the mailbox somewhere
// else, we will initialize these handles to use the same mailbox
// instance. The event is handled similarly.
mailbox driver_mailbox;
event driver_done_event;
task run();
$display("Time %0t [Driver]: Driver starting.", $time);
forever begin
bit_diff_item1 #(WIDTH) item;
// Wait for the generator to send an input test transaction to drive.
driver_mailbox.get(item);
// Print a message when we get the transaction.
$display("Time %0t [Driver]: Driving h%h test.", $time, item.data);
// Drive the test by putting the data on the correpsonding input and
// then asserting go
data <= item.data;
go <= 1'b1;
@(posedge clk);
go <= 1'b0;
// Wait for done to be cleared and then asserted on a rising edge.
//@(posedge done);
@(posedge clk iff (done == 1'b0));
@(posedge clk iff (done == 1'b1));
// Trigger the driver done event so the generator knows to produce
// another test.
-> driver_done_event;
end
endtask
endclass
// Module: bit_diff_tb1
// Description: In this testbench, we separate the generator and driver from
// the main testbench module.
module bit_diff_tb1;
localparam NUM_TESTS = 1000;
localparam WIDTH = 16;
logic clk, rst, go, done;
logic [WIDTH-1:0] data;
logic signed [$clog2(2*WIDTH+1)-1:0] result;
int passed, failed, reference;
// We have to first dynamically allocate the mailbox for it to exist.
// The mailbox variable is just a handle, which is initially uninitialized.
mailbox driver_mailbox = new;
// Create the events. These are static objects and don't need to be
// dynamically allocated with new.
event driver_done_event;
event generator_done_event;
// Create an instance of the generator and driver. Like the mailbox, any
// class instance must be dynamically allocated with new. The variable is
// just a handle.
generator1 #(.NUM_TESTS(NUM_TESTS), .WIDTH(WIDTH)) gen = new;
driver1 #(.WIDTH(WIDTH)) drv = new;
bit_diff #(.WIDTH(WIDTH)) DUT (.*);
// Connect the variables in the driver to the variables in this testbench.
// This apparently can't be done with continuous assignment.
// NOTE: There is a much better way of doing this that we will see in the
// next testbench.
always @(drv.go) go = drv.go;
always @(drv.data) data = drv.data;
always @(clk) drv.clk = clk;
always @(done) drv.done = done;
initial begin : generate_clock
clk = 1'b0;
while(1) #5 clk = ~clk;
end
initial begin
$timeformat(-9, 0, " ns");
// Initialize the generator and driver mailboxes and events.
gen.driver_mailbox = driver_mailbox;
drv.driver_mailbox = driver_mailbox;
gen.driver_done_event = driver_done_event;
drv.driver_done_event = driver_done_event;
gen.generator_done_event = generator_done_event;
// Initialize the circuit. This could potentially be done in the driver.
rst <= 1'b1;
go <= 1'b0;
for (int i=0; i < 5; i++) @(posedge clk);
@(negedge clk);
rst <= 1'b0;
@(posedge clk);
// Fork off two parallel tasts for the generator and the driver.
fork
gen.run();
drv.run();
join_any
// join_any blocks until any the tasks in the fork complete. We could also
// potentially do a join, but that wouldn't work in this case since the
// driver runs forever.
end
function int model(int data, int width);
automatic int diff = 0;
for (int i=0; i < width; i++) begin
diff = data[0] ? diff+1 : diff-1;
data = data >> 1;
end
return diff;
endfunction
// Here we have started to separate the monitor and scoreboard from the
// main testbench by moving it into a separate test block.
// If we knew that the done signal couldn't have glitches, we could have
// replaced the first 3 lines with "always @(posedge done) begin".
always begin
// Wait for completion.
@(posedge clk iff (done == 1'b0));
@(posedge clk iff (done == 1'b1));
// Compare with the reference model.
reference = model(data, WIDTH);
if (result == reference) begin
$display("Test passed (time %0t) for input = h%h", $time, data);
passed ++;
end
else begin
$display("Test failed (time %0t): result = %0d instead of %0d for input = %h.", $time, result, reference, data);
failed ++;
end
end
// Cleanup: wait until the generator is done, then print a final summary
// message and disable the clock generation to end the simulation.
// Note that this only works because the generator waits on the driver,
// which waits for completion of each test it drives. In general, it is
// better to explicitly wait until all tests have completed.
initial begin
@(generator_done_event);
$display("Tests completed: %0d passed, %0d failed", passed, failed);
disable generate_clock;
end
assert property (@(posedge clk) disable iff (rst) go && done |=> !done);
assert property (@(posedge clk) disable iff (rst) $fell(done) |-> $past(go,1));
endmodule
// One huge weakness of the previous testbench is that we manually had to
// connect the testbench variables to the driver's variables. For this simple
// example, it wasn't a huge overhead, but a complex design could have 100s of
// signals. Clearly we wouldn't want to manually connect those.
//
// Instead, we can encapsulate all the port signals as part of an interface
// as shown below. We can now pass around this one interface instead of all
// the individual signals. Nearly every real testbench will use an interface
// like this.
interface bit_diff_if #(parameter int WIDTH) (input logic clk);
logic rst, go, done;
logic [WIDTH-1:0] data;
logic signed [$clog2(2*WIDTH+1)-1:0] result;
endinterface
// Driver 2 is a simplified version of driver 1 that uses the new interface
// instead of separate signals. We don't need a new generator yet since it
// doesn't use any of the testbench signals, which illustrates one advantage
// of this decoupling based on separate responsibilities.
class driver2 #(WIDTH);
// We don't want the actual interface here, just a pointer to it, which
// we accomplish with the virtual keyword.
virtual bit_diff_if #(.WIDTH(WIDTH)) vif;
mailbox driver_mailbox;
event driver_done_event;
task run();
$display("Time %0t [Driver]: Driver starting.", $time);
forever begin
bit_diff_item1 #(WIDTH) item;
// Wait for the generator to send an input test to drive.
driver_mailbox.get(item);
$display("Time %0t [Driver]: Driving h%h test.", $time, item.data);
// We can now access all the variable through the interface.
vif.data <= item.data;
vif.go <= 1'b1;
@(posedge vif.clk);
vif.go <= 1'b0;
@(posedge vif.clk iff (vif.done == 1'b0));
@(posedge vif.clk iff (vif.done == 1'b1));
-> driver_done_event;
end
endtask
endclass
// Module: bit_diff_tb2
// Description: This testbench simplifies the previous one by using the
// new interface.
module bit_diff_tb2;
localparam NUM_TESTS = 1000;
localparam WIDTH = 16;
// We only need the clk signal now since everything else will be
// part of the interface.
logic clk;
int passed, failed, reference;
// We have to first dynamically allocate the mailbox for it to exist.
// The mailbox variable is just a handle, which is initially untinitialized.
mailbox driver_mailbox = new;
// Create the events. These are static objects and don't need to be
// dynamically allocated with new.
event driver_done_event;
event generator_done_event;
// Create an instance of the generator and driver. Like the mailbox, any
// class instance must be dynamically allocated with new. The variable is
// just a handle.
generator1 #(.NUM_TESTS(NUM_TESTS), .WIDTH(WIDTH)) gen = new;
driver2 #(.WIDTH(WIDTH)) drv = new;
// Create the interface.
bit_diff_if #(.WIDTH(WIDTH)) _if (.clk(clk));
// Instantiate the DUT. We unfortunately can't use .* anymore because
// of the interface. However, we could modify the DUT module to use
// the interface, in which case we only need to connect the interface
// here. The tutorials will illustrate synthesizable interfaces in another
// section.
bit_diff #(.WIDTH(WIDTH)) DUT (.clk(clk), .rst(_if.rst), .go(_if.go),
.done(_if.done), .data(_if.data), .result(_if.result));
initial begin : generate_clock
clk = 1'b0;
while(1) #5 clk = ~clk;
end
initial begin
$timeformat(-9, 0, " ns");
gen.driver_mailbox = driver_mailbox;
drv.driver_mailbox = driver_mailbox;
gen.driver_done_event = driver_done_event;
drv.driver_done_event = driver_done_event;
gen.generator_done_event = generator_done_event;
drv.vif = _if;
_if.rst <= 1'b1;
_if.go <= 1'b0;
for (int i=0; i < 5; i++) @(posedge clk);
@(negedge clk);
_if.rst <= 1'b0;
@(posedge clk);
fork
gen.run();
drv.run();
join_any
end
function int model(int data, int width);
automatic int diff = 0;
for (int i=0; i < width; i++) begin
diff = data[0] ? diff+1 : diff-1;
data = data >> 1;
end
return diff;
endfunction
// Same as previous testbench, except now it uses the interface.
always begin
// Wait for completion.
@(posedge clk iff (_if.done == 1'b0));
@(posedge clk iff (_if.done == 1'b1));
// Compare with the reference model.
reference = model(_if.data, WIDTH);
if (_if.result == reference) begin
$display("Test passed (time %0t) for input = h%h", $time, _if.data);
passed ++;
end
else begin
$display("Test failed (time %0t): result = %0d instead of %0d for input = h%h.", $time, _if.result, reference, _if.data);
failed ++;
end
end
initial begin
@(generator_done_event);
$display("Tests completed: %0d passed, %0d failed", passed, failed);
disable generate_clock;
end
assert property (@(posedge clk) disable iff (_if.rst) _if.go && _if.done |=> !_if.done);
assert property (@(posedge clk) disable iff (_if.rst) $fell(_if.done) |-> $past(_if.go,1));
endmodule // bit_diff_tb2
// Next, we modify the testbench further by decoupling the monitor and
// scoreboard from the main testbench code.
//
// To separate the monitor and scoreboard, we need to extend our transaction
// object to include the result, which the monitor sends to the scoreboard.
//
// Notice that the result is not rand because it is provided as a DUT output.
class bit_diff_item2 #(WIDTH);
rand bit [WIDTH-1:0] data;
bit signed [$clog2(WIDTH*2+1)-1:0] result;
endclass
// The monitor simply monitors the outputs of the DUT and looks for any
// situation we want to verify. Since we only have one output for this example,
// the monitor simply waits until completion and then passes the result output
// to the scoreboard.
class monitor1 #(parameter int WIDTH);
// Like the driver and generator, we encapsulate all the DUT signals in an
// interface.
virtual bit_diff_if #(.WIDTH(WIDTH)) vif;
// Mailbox handle for sending results to the scoreboard for verification.
mailbox scoreboard_mailbox;
task run();
$display("Time %0t [Monitor]: Monitor starting.", $time);
forever begin
// Create the transaction object.
bit_diff_item2 #(.WIDTH(WIDTH)) item = new;
// Wait for completion.
@(posedge vif.clk iff (vif.done == 1'b0));
@(posedge vif.clk iff (vif.done == 1'b1));
// Save the input data and result from the interface.
item.data = vif.data;
item.result = vif.result;
$display("Time %0t [Monitor]: Monitor detected result=%0d for data=h%h.", $time, vif.result, vif.data);
// Send the input data and result to the scoreboard and go back to
// monitoring.
scoreboard_mailbox.put(item);
end
endtask
endclass
// The scoreboard waits for a transaction from the monitor, which specifies
// the input being tested and the corresponding result. The scoreboard then
// compares the result with the reference model, updates statistics, and
// prints logging information.
class scoreboard1 #(parameter int WIDTH);
// Mailbox handle to receive data from the monitor.
mailbox scoreboard_mailbox;
int passed, failed, reference;
// We move the reference model into the scoreboard here, since it isn't
// needed anywhere else.
function int model(int data, int width);
automatic int diff = 0;
for (int i=0; i < width; i++) begin
diff = data[0] ? diff+1 : diff-1;
data = data >> 1;
end
return diff;
endfunction
task run();
passed = 0;
failed = 0;
forever begin
bit_diff_item2 #(.WIDTH(WIDTH)) item;
// Wait until the monitor sends a result to verify.
scoreboard_mailbox.get(item);
// Verify the result.
reference = model(item.data, WIDTH);
if (item.result == reference) begin
$display("Time %0t [Scoreboard] Test passed for input = h%h", $time, item.data);
passed ++;
end
else begin
$display("Time %0t [Scoreboard] Test failed: result = %0d instead of %0d for input = h%h.", $time, item.result, reference, item.data);
failed ++;
end
end
endtask
// Method to report the status of the simulation at any point.
function void report_status();
$display("Test status: %0d passed, %0d failed", passed, failed);
endfunction
endclass
// Module: bit_diff_tb3
// Description: Modified version of the previous testbench to decouple the
// monitor and scoreboard. Notice that the main testbench module keeps getting
// simpler because the different reponsibilities are moved elsewhere
module bit_diff_tb3;
localparam NUM_TESTS = 1000;
localparam WIDTH = 16;
logic clk;
mailbox driver_mailbox = new;
mailbox scoreboard_mailbox = new;
event driver_done_event;
event generator_done_event;
generator1 #(.NUM_TESTS(NUM_TESTS), .WIDTH(WIDTH)) gen = new;
driver2 #(.WIDTH(WIDTH)) drv = new;
monitor1 #(.WIDTH(WIDTH)) monitor = new;
scoreboard1 #(.WIDTH(WIDTH)) scoreboard = new;
bit_diff_if #(.WIDTH(WIDTH)) _if (.clk(clk));
bit_diff #(.WIDTH(WIDTH)) DUT (.clk(clk), .rst(_if.rst), .go(_if.go),
.done(_if.done), .data(_if.data), .result(_if.result));
initial begin : generate_clock
clk = 1'b0;
while(1) #5 clk = ~clk;
end
initial begin
$timeformat(-9, 0, " ns");
// Initialize the generator and driver.
gen.driver_mailbox = driver_mailbox;
drv.driver_mailbox = driver_mailbox;
gen.driver_done_event = driver_done_event;
drv.driver_done_event = driver_done_event;
gen.generator_done_event = generator_done_event;
drv.vif = _if;
// Initialize the monitor and scoreboard.
monitor.vif = _if;
monitor.scoreboard_mailbox = scoreboard_mailbox;
scoreboard.scoreboard_mailbox = scoreboard_mailbox;
// Initialize the circuit.
_if.rst <= 1'b1;
_if.go <= 1'b0;
for (int i=0; i < 5; i++) @(posedge clk);
@(negedge clk);
_if.rst <= 1'b0;
@(posedge clk);
// Fork off threads for the other main components.
fork
gen.run();
drv.run();
monitor.run();
scoreboard.run();
join_any
end
initial begin
@(generator_done_event);
scoreboard.report_status();
disable generate_clock;
end
assert property (@(posedge clk) disable iff (_if.rst) _if.go && _if.done |=> !_if.done);
assert property (@(posedge clk) disable iff (_if.rst) $fell(_if.done) |-> $past(_if.go,1));
endmodule // bit_diff_tb3
// One common testbench strategy is to encapsulate the generator, driver,
// monitor, and scoreboard inside an "environment," which further simplifies
// the main testbench.
//
// Here we create an initial environment which does this. It basically replaces
// the code from the main testbench that declares, instantiates, and connects
// everything together.
class env #(int NUM_TESTS, int WIDTH);
generator1 #(.NUM_TESTS(NUM_TESTS), .WIDTH(WIDTH)) gen;
driver2 #(.WIDTH(WIDTH)) drv;
monitor1 #(.WIDTH(WIDTH)) monitor;
scoreboard1 #(.WIDTH(WIDTH)) scoreboard;
virtual bit_diff_if #(.WIDTH(WIDTH)) vif;
mailbox scoreboard_mailbox;
mailbox driver_mailbox;
event driver_done_event;
event generator_done_event;
// We give the environment a new method to create new instances of everything
// encapsulated by the environment.
function new();
gen = new;
drv = new;
monitor = new;
scoreboard = new;
scoreboard_mailbox = new;
driver_mailbox = new;
endfunction // new
// The environment's run task connects everything together and forks off
// the individual threads. The connections could have also been made in the
// new() method, but we need the fork code here.
virtual task run();
drv.vif = vif;
monitor.vif = vif;
gen.driver_mailbox = driver_mailbox;
drv.driver_mailbox = driver_mailbox;
gen.driver_done_event = driver_done_event;
drv.driver_done_event = driver_done_event;
monitor.scoreboard_mailbox = scoreboard_mailbox;
scoreboard.scoreboard_mailbox = scoreboard_mailbox;
fork
gen.run();
drv.run();
monitor.run();
scoreboard.run();
join_any
// When any of the tasks complete, report the status of the tests. While
// it might be confusing to wait for any of the tasks to complete,
// instead of waiting for all tasks, the generator is the only task that
// actually ever exits, which it does after all the tests have completed.
scoreboard.report_status();
endtask // run
endclass
// Module: bit_diff_tb4
// Description: This testbench simplifies the previous one by using the newly
// created environment. As before, this main testbench gets even simpler with
// the environment.
module bit_diff_tb4;
localparam NUM_TESTS = 1000;
localparam WIDTH = 16;
logic clk;
// Instead of separately creating the generator, driver, monitor, and
// scoreboard, we just create the environment.
env #(.NUM_TESTS(NUM_TESTS), .WIDTH(WIDTH)) _env = new;
bit_diff_if #(.WIDTH(WIDTH)) _if (.clk(clk));
bit_diff #(.WIDTH(WIDTH)) DUT (.clk(clk), .rst(_if.rst), .go(_if.go),
.done(_if.done), .data(_if.data),
.result(_if.result));
initial begin : generate_clock
clk = 1'b0;
while(1) #5 clk = ~clk;
end
initial begin
$timeformat(-9, 0, " ns");
// Connect the interface to the environment.
_env.vif = _if;
// Initialize the circuit.
_if.rst <= 1'b1;
_if.go <= 1'b0;
for (int i=0; i < 5; i++) @(posedge clk);
@(negedge clk);
_if.rst <= 1'b0;
@(posedge clk);
// Run the environment.
_env.run();
disable generate_clock;
end
assert property (@(posedge clk) disable iff (_if.rst) _if.go && _if.done |=> !_if.done);
assert property (@(posedge clk) disable iff (_if.rst) $fell(_if.done) |-> $past(_if.go,1));
endmodule // bit_diff_tb4
// Up to this point, the actual coverage is pretty weak. The generator simply
// generates a test and then waits until that test is done. This does not test
// modifying the data input while a test is ongoing. It also doesn't test
// toggling go while a test is ongoing.
//
// We now start to improve the testbench by adding these capabilities to the
// generator. The generator is now responsible for generating values for the
// data input and the go input, and it no longer waits for the driver to
// finish the previous test.
//
// To support this new functionality, we need to expand our transaction object
// with a go bit.
class bit_diff_item3 #(WIDTH);
rand bit [WIDTH-1:0] data;
rand bit go;
bit signed [$clog2(WIDTH*2+1)-1:0] result;
// A uniform distribution of go values probably isn't what we want, so
// we'll make sure go is 0 90% of the time.
constraint c_go_dist { go dist{0 :/ 90, 1:/ 10 }; }
endclass
// In the new generator, we make several changes. First, we use the new
// transaction object to randomly produce go values. Second, we remove the for
// loop. In this new version, the testbench won't wait for the generator to
// finish. It will wait for the scoreboard to finish. Not only is this a more
// intuitive way of waiting for completion, but it is also necessary.
// Previously, the generator produced one data input per execution of the DUT.
// Now, the generator produces input values as frequently as it can. Many of
// those values will occur when the DUT is already active. So, if we simply
// counted the number of inputs tested, the number of actual executions of the
// DUT would be much smaller than before.
class generator2 #(int WIDTH);
mailbox driver_mailbox;
event driver_done_event;
task run();
bit_diff_item3 #(.WIDTH(WIDTH)) item;
forever begin
item = new;
if (!item.randomize()) $display("Randomize failed");
// This display is commented out because this loop can potentially
// execute every cycle, which could make the log overwhelming.
//$display("Time %0t [Generator]: Generating input h%h, go=%0b.", $time, item.data, item.go);
driver_mailbox.put(item);
// The generator still waits for the driver to finish driving the
// current test, but we'll see that happens every cycle now, instead
// of the driver waiting for completion of the DUT.
@(driver_done_event);
end
endtask
endclass // generator2
// The new driver is more complicated because it no longer just has to wait
// for a message from the generator. It still drives tests from the generator,
// but since some of those tests will not actually produce an output from the
// DUT, the driver must keep track of which inputs will affect the output.
// For example, data inputs that don't coincide with assertions of go should
// be ignored by the scoreboard. Similarly, assertions of go while the DUT
// is already active should also be ignored by the scoreboard.
//
// We could potentially move this code to the monitor, in which case the monitor
// would have to check the DUT inputs and outputs.
class driver3 #(int WIDTH);
virtual bit_diff_if #(.WIDTH(WIDTH)) vif;
mailbox driver_mailbox;
// Since only the driver knows when an input can cause the DUT to start a
// new execution, we create a new scoreboard mailbox that the driver will
// use to send data inputs that will produce an output from the DUT.
mailbox scoreboard_data_mailbox;
event driver_done_event;
task run();
// To know whether or not generated inputs will create a DUT output,
// we need to know whether or not the DUT is currently active.
logic is_first_test = 1'b1;
logic is_active = 1'b0;
$display("Time %0t [Driver]: Driver starting.", $time);
forever begin
bit_diff_item3 #(.WIDTH(WIDTH)) item;
// If the circuit is reset at any point, reset the driver state.
while (vif.rst) begin
@(posedge vif.clk);
is_first_test = 1'b1;
is_active = 1'b0;
end
// Wait for the generator to send an input to drive. Unlike before,
// the generator now delivers inputs every cycle, and includes the
// go signal in order to test assertions of go while the DUT is already
// active.
driver_mailbox.get(item);
//$display("Time %0t [Driver]: Driving data=h%h, go=%0b.", $time, item.data, item.go);
// For this driver, we drive both the data and go inputs directly
// from the generator.
vif.data = item.data;
vif.go = item.go;
// Wait until the next clock edge where the inputs will be seen.
// This is needed here because signals haven't changed yet on the
// current clock cycle. So, if done is about to change, we won't see
// it. That would cause the following code to mistake the DUT as
// being active, which could prevent sending the test to the
// scoreboard.
@(posedge vif.clk);
// If done is asserted, or if this is the first_test,
// then the DUT should be inactive and ready for another test.
if (vif.done || is_first_test)
is_active = 1'b0;
// If the DUT isn't already active, and we get a go signal, we are
// starting a test, so inform the scoreboard. The scoreboard will
// then wait to get the result from the monitor. This strategy allows
// the testbench to test assertions of go that don't correspond to
// the start of a test because the DUT is already active. The DUT
// should ignore these assertions.
if (!is_active && vif.go) begin
$display("Time %0t [Driver]: Sending start of test for data=h%h.", $time, item.data);
scoreboard_data_mailbox.put(item);
is_active = 1'b1;
is_first_test = 1'b0;
end
// Tell the generator the driver has driven the last test, which
// happens every cycle except during reset.
-> driver_done_event;
end
endtask
endclass
// The monitor becomes a little simpler now because the driver informs the
// scoreboard of the corresponding input for the next output. So, the monitor
// solely sends the output to the scoreboard.
class monitor2 #(int WIDTH);
virtual bit_diff_if #(.WIDTH(WIDTH)) vif;
// We rename the scoreboard mailbox since driver now uses a separate
// mailbox for sending data inputs.
mailbox scoreboard_result_mailbox;
task run();
$display("Time %0t [Monitor]: Monitor starting.", $time);
forever begin
bit_diff_item3 #(.WIDTH(WIDTH)) item = new;
@(posedge vif.clk iff (vif.done == 1'b0));
@(posedge vif.clk iff (vif.done == 1'b1));
// In this version, we only care about the result, because the driver
// already informed the scoreboard of the next n value to check.
item.result = vif.result;
$display("Time %0t [Monitor]: Monitor detected result=%0d.", $time, vif.result);
scoreboard_result_mailbox.put(item);
end
endtask
endclass