uvm通信-analysis_port使用示例(完整uvm测试平台)

资料来源

(1) 《The UVM Primer》第15章

1.top.sv

1 module top;
2    import uvm_pkg::*;
3 `include "uvm_macros.svh"
4    import dice_pkg::*;
5    initial run_test("dice_test");
6 endmodule : top

2.dice_pkg.sv

package dice_pkg;
   import uvm_pkg::*;
`include "uvm_macros.svh"
       
`include "coverage.svh"
`include "histogram.svh"
`include "average.svh"
`include "dice_roller.svh"
`include "dice_test.svh"
   
endpackage : dice_pkg

3.dice_test.svh

class dice_test extends uvm_test;
   `uvm_component_utils(dice_test);

   dice_roller dice_roller_h;
   coverage coverage_h;
   histogram histogram_h;
   average average_h;

   function void connect_phase(uvm_phase phase);
      dice_roller_h.roll_ap.connect(coverage_h.analysis_export);
      dice_roller_h.roll_ap.connect(histogram_h.analysis_export);      
      dice_roller_h.roll_ap.connect(average_h.analysis_export);
   endfunction : connect_phase
   
      
   function new(string name, uvm_component parent);
      super.new(name,parent);
   endfunction : new


   function void build_phase(uvm_phase phase);
      
      
      dice_roller_h = new("dice_roller_h", this);
      coverage_h = new("coverage_h", this);
      histogram_h = new("histogram_h",this);
      average_h     = new("average_h",this);

   endfunction : build_phase


endclass : dice_test

4.dice_roller.svh

 1 class dice_roller extends uvm_component;
 2    `uvm_component_utils(dice_roller);
 3 
 4    uvm_analysis_port #(int) roll_ap;
 5 
 6    function void build_phase (uvm_phase phase);
 7       
 8       roll_ap = new("roll_ap",this);
 9       
10    endfunction : build_phase
11    
12    rand byte die1;
13    rand byte die2;
14    
15    constraint d6 { die1 >= 1; die1 <= 6; 
16                    die2 >= 1; die2 <= 6; }
17 
18 
19    task run_phase(uvm_phase phase);
20       int the_roll;
21       phase.raise_objection(this);
22       void'(randomize());
23       repeat (20) begin
24          void'(randomize());
25          the_roll = die1 + die2;
26          roll_ap.write(the_roll);
27       end
28       phase.drop_objection(this);
29    endtask : run_phase
30    
31 
32 
33    function new(string name, uvm_component parent);
34       super.new(name,parent);
35    endfunction : new
36    
37 endclass : dice_roller

5.coverage.svh

 1 class coverage extends uvm_subscriber#(int);
 2    `uvm_component_utils(coverage);
 3    int the_roll;
 4 
 5    covergroup dice_cg;
 6       coverpoint the_roll{
 7          bins  twod6[] = {2,3,4,5,6,7,8,9,10,11,12};}
 8    endgroup
 9 
10 
11    function new(string name, uvm_component parent = null);
12       super.new(name,parent);
13       dice_cg = new();
14    endfunction : new
15    
16 
17    function void write (int t);
18       the_roll = t;
19       dice_cg.sample();
20    endfunction : write
21 
22    function void report_phase(uvm_phase phase);
23       
24      $display("\nCOVERAGE: %2.0f%% ", dice_cg.get_coverage());
25    endfunction : report_phase
26 endclass : coverage

6.histogram.svh

 1 class histogram extends uvm_subscriber #(int);
 2    `uvm_component_utils(histogram);
 3 
 4    int rolls[int];
 5    
 6 
 7    function new(string name, uvm_component parent);
 8       super.new(name,parent);
 9       for (int ii = 2; ii <= 12; ii++) rolls[ii] = 0;
10    endfunction : new
11    
12    function void write(int t);
13       rolls[t]++;
14    endfunction : write
15 
16 
17    function void report_phase(uvm_phase phase);
18       string bar;
19       string message;
20       
21       message = "\n";
22       foreach (rolls[ii]) begin
23          string roll_msg;
24          bar = "";
25          repeat (rolls[ii]) bar = {bar,"#"};
26          roll_msg = $sformatf( "%2d: %s\n", ii, bar);
27          message = {message,roll_msg};
28       end
29       $display(message);
30    endfunction : report_phase
31 endclass : histogram

7.average.svh

 1 class average extends uvm_subscriber #(int);
 2    `uvm_component_utils(average);
 3 
 4    real dice_total;
 5    real count;
 6 
 7    function new(string name, uvm_component parent = null);
 8       super.new(name,parent);
 9       dice_total = 0.0;
10       count = 0.0;
11    endfunction : new
12 
13    function void write(int t);
14       dice_total = dice_total + t;
15       count++;
16    endfunction : write
17 
18    function void report_phase(uvm_phase phase);
19       
20       $display ("DICE AVERAGE: %2.1f",dice_total/count);
21    endfunction : report_phase
22    
23 endclass : average

 

posted on 2022-04-13 20:34  知北游。。  阅读(554)  评论(0编辑  收藏  举报

导航