Tuesday, June 12, 2012

Testbench environment in SV for Round-robin arbiter for 4 masters


Here Testing Environment is created for this given arbiter DUT. DUT code is created such a way that logic inference take minimum area. Environment is created using SV and simulation is done using Modelsim SE PLUS 6.4.

//DUT top module
module arbiter(input clk, rst,input ack, input[3:0] req,
              output reg [3:0] grant,output [3:0] token_ring);
           
reg [1:0]qd=0;
reg [1:0]c=0;
reg [3:0]q=4'b0001;
wire[3:0]en;
wire [3:0]in0;
wire [3:0]out1;
wire [3:0]out0;
wire [3:0]out2;
wire [3:0]out3;

//4 Priority encoder instantiation
priority_logic1 uut0(.in(in0),.en(en[0]),.out(out0));
priority_logic1 uut1(.in(in0),.en(en[1]),.out(out1));
priority_logic1 uut2(.in(in0),.en(en[2]),.out(out2));
priority_logic1 uut3(.in(in0),.en(en[3]),.out(out3));

//Shift_1 modue instant
shift_1 uutt0(req,in0,c);

assign en=q;
assign token_ring=q;
always @(out1 or out2 or out3 or out0)//(posedge arb.clk)
begin
 if(rst)  //active low reset
   begin
        grant[0]<=out0[0]|out1[3]|out2[2]|out3[1];
        grant[1]<=out0[1]|out1[0]|out2[3]|out3[2];
        grant[2]<=out0[2]|out1[1]|out2[0]|out3[3];
        grant[3]<=out0[3]|out1[2]|out2[1]|out3[0];
    end
  else
    begin
        grant<=4'b0000;
    end
//$strobe($time," :DUT       : request = %b, ack = %b , grant = %b, reset = %b",req,ack,grant,rst);  
end

//To implement ring counter functionality
always @(posedge ack)
begin
 if (rst)
  if(q<8)
    begin
  q<=2*q;
  c<=c+1;  //count till c=3
 end
  else
 begin
  c<=0;
  q<=4'b0001;
 end
  else
 begin
  c<=0;
     q<=4'b0001;
    end
 end

endmodule

//Priority encoder module
module priority_logic1(
    input [3:0]in,
    input en,
    output [3:0]out
    );
assign out=en?(in[0]?4'b0001:(in[1]?4'b0010:(in[2]?4'b0100:(in[3]?4'b1000:4'b0000)))):4'b0000;
endmodule

//Shift_1 module to implement round-robin priority functionality
module shift_1(
   input [3:0]datain,
   output [3:0]dataout,
   input [1:0]sel);

wire [3:0]d0;
wire [3:0]d1;
assign dataout=d1;

assign d0=sel[0]?{datain[0],datain[3:1]}:datain;
assign d1=sel[1]?{d0[1:0],d0[3:2]}:d0;
endmodule


Reference model code :


//reference model
module reference_model(
    input clk,
    input rst,
    input [3:0] req,
   // output  ack,
    input ack,
    output reg [3:0] gnt
    );
 
reg [3:0] ringout;

//ack_generator ack1(.clk(clk),.rst(rst),.ack(ack));

always@(posedge ack or negedge rst)
//always@(posedge clk)
begin
if(~rst) //active low reset, ring count=4'b0001
ringout <= 4'b0001;
else if(ack)  //ring counter will be changed only when acknowledge will assert
ringout <= {ringout[2:0],ringout[3]};
else
ringout <= ringout;  //elsering counter will not change state
end

//always@(posedge ack)
always@(req or ringout)  //grant will be changed based on ring counter state or request change
if ( ringout[0])         //if ringout=4'b0001 then 1>2>3>4
begin
casex (req)
4'bxxx1: gnt=4'b0001;
4'bxx10: gnt=4'b0010;
4'bx100: gnt=4'b0100;
4'b1000: gnt=4'b1000;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant = %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);

end
else if(ringout[1])      //if ringout=4'b0010 then 2>3>4>1
begin
casex (req)
4'bxx1x: gnt=4'b0010;
4'bx10x: gnt=4'b0100;
4'b100x: gnt=4'b1000;
4'b0001: gnt=4'b0001;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant = %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);

end
else if( ringout[2])    //if ringout=4'b0100 then 3>4>1>2
begin
casex (req)
4'bx1xx: gnt=4'b0100;
4'b10xx: gnt=4'b1000;
4'b00x1: gnt=4'b0001;
4'b0010: gnt=4'b0010;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant= %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);

end
else
begin
casex (req)             //if ringout=4'b0100 then 4>1>2>3
4'b1xxx: gnt=4'b1000;
4'b0xx1: gnt=4'b0001;
4'b0x10: gnt=4'b0010;
4'b0100: gnt=4'b0100;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant = %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);

end
endmodule


Arbiter Top module code:

`timescale 1ns / 1ps
//top module
module top();

//Clock generation 50MHz
bit clk;
always #20 clk = ~clk;

//DUT interface instantioation
arb_inf arb_dut_inf(clk);

//Reference model instantiation
arb_refm arb_refm_inf(clk);

//Environment instantioation
testcase TC(arb_dut_inf,arb_refm_inf);

//DUT instantiation
arbiter DUT (.clk(clk),.rst(arb_dut_inf.rst),.ack(arb_dut_inf.ack),.req(arb_dut_inf.req),.grant(arb_dut_inf.gnt),.token_ring(arb_dut_inf.token));

//Reference model interface instantiation
reference_model RM(.clk(clk),.ack(arb_refm_inf.ack),.rst(arb_refm_inf.rst),.gnt(arb_refm_inf.gnt),.req(arb_refm_inf.req));              


initial
begin

  //applying reset
  arb_dut_inf.rst<=0;
  arb_refm_inf.rst<=0;
  $display($time,": before reset");

  @(posedge arb_dut_inf.arb_bfm_cb);

  //removing reset after
  arb_dut_inf.rst<=1;
  arb_refm_inf.rst<=1;
  $display($time,": after reset");
end
endmodule


DUT interface code :

//DUT interfaces
interface arb_inf (input bit clk);

  logic [3:0] req;
  bit rst;
  logic [3:0] gnt;
  bit ack;
  logic [3:0] token;

  //clocking block for DUT input interface
  clocking arb_bfm_cb @(posedge clk);
  output req;
  output ack;
  endclocking : arb_bfm_cb

  //clocking block for DUT output interface
  clocking arb_mon_cb @(posedge clk);
  input gnt;
  input ack;
  input req;
  input token;
  endclocking : arb_mon_cb

 modport MON (clocking arb_mon_cb); //direction for DUT output interface
 modport BFM (clocking arb_bfm_cb); //direction for DUT input interface

endinterface  : arb_inf


Reference model Interface code :


//Reference model interface
interface arb_refm (input bit clk);

 logic [3:0] req;
 logic [3:0] gnt;
 bit ack;
 bit rst;
 logic [3:0] token;

//clocking block for ref. module input interface
 clocking arb_bfm_refm @(posedge clk);
  output req;
  output ack;
 endclocking : arb_bfm_refm

//clocking block for ref. module output interface
 clocking arb_sb_refm @(posedge clk);
  input req;
  input ack;
  input gnt;
  input token;
 endclocking : arb_sb_refm

 modport BFM (clocking arb_bfm_refm); //define direction for Ref. model input interface
 modport SCOREBOARD (clocking arb_sb_refm); //define direction for Ref. model output interface

 endinterface : arb_refm


Arbitar Transaction class :



`ifndef G_TRANS
`define G_TRANS

//Transaction class
class Transaction;

  randc logic [3:0] req;  //Randomizing req
        logic [3:0] gnt;
        logic [3:0] token;
        bit ack;
     
  //Transaction class new() method
  function new();
      //$display("Constructor of Transaction ");
  endfunction : new

  //display() method
  function void display ();
      $display ("Transaction : request = %b,grant = %b,ack = %b",req,gnt,ack);
  endfunction : display

  //For deep copy
  function Transaction copy();
   Transaction cpy =new();
   cpy.req =this.req;
   cpy.gnt =this.gnt;
   cpy.ack =this.ack;
   cpy.token =this.token;
   copy = cpy;
  endfunction

endclass
`endif

Arbitar Generator class :


`include "arb_transaction"
`ifndef G_GEN
`define G_GEN

//Generator class
class Generator;

  Transaction tr;

  int max_trans_cnt; //define variable for maximum number transactions
  int trans_cnt = 0; //incrementing till all testcases applied


  mailbox #(Transaction) gen2bfm;
 
  function new(mailbox #(Transaction) gen2bfm, int max_trans_cnt);
    $display("Constructor of Generator ");
    this.gen2bfm       = gen2bfm;
    this.max_trans_cnt = max_trans_cnt;
    tr= new();
  endfunction


Arbitar BFM class :



`include "arb_transaction"
`ifndef G_BFM
`define G_BFM

//BFM class
class Bfm;

  Transaction tr;

  mailbox #(Transaction) bfm_mailbox; //declare generator to BFM mailbox

  virtual arb_inf.BFM  arb_in;  //declare  DUT input interface
  virtual arb_refm.BFM ref_in;  //declare ref. model input interface


  function new (mailbox #(Transaction) bfm_mailbox, virtual arb_inf.BFM arb_in,virtual arb_refm.BFM ref_in);
    $display("Constructor of bfm ");
    this.bfm_mailbox = bfm_mailbox;
    this.ref_in = ref_in;
    this.arb_in = arb_in;
  endfunction : new

//run() task
  task run();
   forever  
       begin
       tr=new();
       bfm_mailbox.get(tr);
       dut_input(tr);
       end
   endtask: run
 
 //passing values to DUT and refm
 task dut_input(Transaction tr);

 //sending req
  @(posedge arb_in.arb_bfm_cb);
    arb_in.arb_bfm_cb.req[3:0]<=tr.req[3:0];
    ref_in.arb_bfm_refm.req[3:0]<= tr.req[3:0];
     
  @(arb_in.arb_bfm_cb.req[3:0]);
    $display($time," :BFM : bfm sent data to DUT  : request = %b ack=%b",arb_in.arb_bfm_cb.req[3:0],arb_in.arb_bfm_cb.ack);
    $display($time," :BFM : bfm sent data to Refm : request = %b ack=%b",ref_in.arb_bfm_refm.req[3:0], ref_in.arb_bfm_refm.ack);

  //ack==1 after two clock cycle
     @(posedge arb_in.arb_bfm_cb);
     @(posedge arb_in.arb_bfm_cb);  
 
       arb_in.arb_bfm_cb.ack<=1;
       ref_in.arb_bfm_refm.ack<=1;
     
      @(arb_in.arb_bfm_cb.ack);
      $display($time," :BFM : bfm sent data to DUT  : request = %b ack=%b",arb_in.arb_bfm_cb.req[3:0],arb_in.arb_bfm_cb.ack);
      $display($time," :BFM : bfm sent data to Refm : request = %b ack=%b",ref_in.arb_bfm_refm.req[3:0], ref_in.arb_bfm_refm.ack);

  //ack will remain high for one clock cycle            
       @(posedge arb_in.arb_bfm_cb);
         arb_in.arb_bfm_cb.ack<=0;
         ref_in.arb_bfm_refm.ack<=0;
                   
   endtask

endclass :Bfm
`endif

Arbitar Monitor class :



`include "arb_transaction"

`ifndef G_MONITOR
`define G_MONITOR

//Monitor class
class Monitor;

Transaction tr;

virtual arb_inf.MON arb_dut_inf;  //declare DUT to monitor interface
mailbox #(Transaction) mon2sb;    //define Monitor to Scoreboard mailbox

//new() method for Monitor classs
function new (virtual arb_inf.MON arb_dut_inf, mailbox #(Transaction) mon2sb);
$display("Constructor of Monitor ");
this.arb_dut_inf=arb_dut_inf;
this.mon2sb = mon2sb;
endfunction

//main() method of Monitor class
task main();
forever
begin
tr=new();

@(posedge  arb_dut_inf.arb_mon_cb);
@(posedge  arb_dut_inf.arb_mon_cb.ack);             //block consecutive statements untill ack will be asserted
tr.ack = arb_dut_inf.arb_mon_cb.ack;                //taking ack from DUT through interface
tr.req[3:0] = arb_dut_inf.arb_mon_cb.req[3:0];      //taking req from DUT through interface
tr.gnt[3:0] = arb_dut_inf.arb_mon_cb.gnt[3:0];      //taking gnt from DUT through interface
tr.token[3:0] = arb_dut_inf.arb_mon_cb.token[3:0];  //taking token from DUT through interface

mon2sb.put(tr);  //putting req,token,ack,gnt into mailbox
//if(tr.ack==1)
$display($time," :Monitor : Data sent to the Scoreboard :req = %b, ack = %b, gnt = %b, token = %b",tr.req[3:0],tr.ack,tr.gnt[3:0],tr.token[3:0]);
//tr.display();
end
endtask

endclass

`endif


Arbitar Scoreboard class : 


`include "arb_transaction"

`ifndef G_ScoreBoard
`define G_ScoreBoard

//Scoreboard class
class Scoreboard;

//Declaring tr1,tr2 Transaction class objects
Transaction tr1;
Transaction tr2;

int max_transaction; //define variable for maximum number of transaction
event ended; 
int match;   //define variable to count total numbes of successful testcases

mailbox #(Transaction) mon_sb;  //declare monitor to scoreboard mailbox
virtual arb_refm.SCOREBOARD refm_out; //declare ref. medel to scoreboard interface

//new() function 
function new (mailbox #(Transaction) mon_sb,virtual arb_refm.SCOREBOARD refm_out, int max_transaction);
$display("Constructor of Scoreboard ");
this.max_transaction = max_transaction;
this.mon_sb = mon_sb;
this.refm_out = refm_out;
endfunction

//Scoreboard class run() function
task run();
tr1=new();
tr2=new();  

forever
  begin 
//@(posedge refm_out.arb_sb_refm); 
 @(posedge refm_out.arb_sb_refm.ack);
  tr1.ack=refm_out.arb_sb_refm.ack;  //checking ack from ref. model
  
  if(tr1.ack==1)
    begin
   tr1.req = refm_out.arb_sb_refm.req;
   tr1.gnt= refm_out.arb_sb_refm.gnt;
    mon_sb.get(tr2);                 //taking req,ack,gnt from monitor to scoreboard mailbox
   
 $display($time," :Scoreboard : From Refm: req_refm =%b, ack_refm =%b, gnt_refm=%b",tr1.req[3:0],tr1.ack,tr1.gnt[3:0]);
 $display($time," :Scoreboard : From DUT : req      =%b, ack      =%b, gnt     =%b",tr2.req[3:0],tr2.ack,tr2.gnt[3:0]);
  
  if(tr1.req!=tr2.req) //comparing req(tr1.req) coming from ref. medel with req(tr2.req) coming from scoreboard.
  begin
  $display ($time," :ERROR : request form dut and refm doesn't match");
  $display("");
  end
  
  else if(tr1.ack!=tr2.ack) //comparing ack(tr1.ack) coming from ref. medel with req(tr2.ack) coming from scoreboard.
  begin
  $display ($time," :ERROR : ack form dut and refm doesn't match");
  $display("");
  end
  
  else if(tr1.gnt!=tr2.gnt)//comparing grant(tr1.gnt) coming from ref. medel with grant(tr2.gnt) coming from scoreboard.
  begin
  $display ($time," :ERROR : grant form dut and refm doesn't match");
  $display("");
  end
  
  else   //if req,gnt,ack is matching then testcase tested DUT functionality successfully
  begin
  $display ($time," :Congratulations : Testcase successfully tested");
  $display("");
  match++;   //counting total number of successful testcases
  end
  end

  if(--max_transaction<1)  //if all testcases have been applied to DUT then end event will be triggered to stop simulation 
    begin
  $display ($time," :Number of Succesfull testcases are : %d",match);
  ->ended;    //end event is defined in Environment class
    end
  end
  
  endtask 
  
  endclass 
`endif
  

Arbitar Testcase program :

//Testcase
program testcase(arb_inf arb_in,arb_refm ref_in);
`include "arb_environment"

Environment env_obj;
initial
begin
$display(" ******************* Start of testcase ****************");

//creating environment class instant(object),passing DUT and ref. medel interfaces
env_obj=new(arb_in,ref_in);

//calling run() procedure of environment class
env_obj.run();

$display(" ******************* End of testcase ****************");
end

endprogram 


Arbitar Environment class :


`include "arb_transaction"
`include "arb_generator"
`include "arb_scoreboard"
`include "arb_bfm"
`include "arb_monitor"

`ifndef G_ENV
`define G_ENV

class Environment ;

//declare DUT and Ref. model interfaces
virtual arb_inf   arb_dut_inf;
virtual arb_refm  arb_reference;



mailbox #(Transaction) mon2sb;  //declare monitor_to_scoreboard mailbox
mailbox #(Transaction) gen2bfm; //declare generator_to_bfm mailbox

//declare class objects
 Generator gen_obj;
 Bfm bfm_obj;
 Scoreboard sb_obj;
 Monitor mon_obj;

function new(virtual arb_inf  arb_dut_inf,
             virtual arb_refm arb_reference);
             
  $display("Constructor of Environment");
  
  this.arb_dut_inf   =  arb_dut_inf;
  this.arb_reference =  arb_reference;
  
  mon2sb=new()  ;
  gen2bfm=new() ;
  
  gen_obj=new(gen2bfm,16);
  bfm_obj=new(gen2bfm,arb_dut_inf,arb_reference);
  mon_obj=new(arb_dut_inf,mon2sb);
  sb_obj =new(mon2sb,arb_reference,16);

endfunction : new

//define Environment before_test() method
virtual task before_test();
fork
sb_obj.run();
bfm_obj.run();
mon_obj.main();
join_none
endtask

//define Environment start_test() method
virtual task start_test();
fork
gen_obj.run();
join_none
endtask

//define Environment after_test() method
virtual task after_test();
fork
wait(sb_obj.ended.triggered);  //wait till all the testcases have been applied
$display("end event triggered");
join

$display("End of simulation");
endtask

//define Environment class run() method
task run();
before_test();
start_test();
after_test();
endtask 

endclass
`endif