Wednesday, May 21, 2014

SOP(star of packet)_EOP(end of packet) various possible scenarios veirfication handling

//sop_eop_dut which passes input signals to output ports only. No operation is being performed on incoming packet

module sop_eop_dut(
input [7:0] data_in,
input clk,sop_in,eop_in,
output reg [7:0] data_out,
output reg sop_out,eop_out);
always@(posedge clk) begin
  data_out = data_in;
  sop_out  = sop_in;
  eop_out  = eop_in;
end
endmodule


//testbench having logic to tackle various sop_eop scenarios like.1) only sop 2) sop eop in the same clock_cycle 3) only eop. The message which is being sent through packet is in ASCII format.

module tb;
 
reg clk;
reg [7:0] data_in;
reg sop_in,eop_in;
wire [7:0] data;
wire sop,eop;
static int pkt_count = 0;
static int out_pkt_count = 0;
event mid_pkt;
reg sop_flag;
bit [7:0] q_data[$];
bit [7:0] out_q_data[$];
//---------------------------clock generator------------------------------//
initial clk = 0;
always #5 clk =~clk;
//------------------------------------------------------------------------//
//----------------------------DUT instantiation---------------------------//
sop_eop_dut IDUT(
.clk(clk),
.sop_in(sop_in),
.eop_in(eop_in),
.data_in(data_in),
.sop_out(sop),
.eop_out(eop),
.data_out(data));
//------------------------------------------------------------------------//
//--------------stimulus to DUT inputs sop_in,eop_in,data_in--------------//
initial
begin
   data_in = 0;
   sop_in = 0;
   eop_in = 0;
   sop_flag = 0;
@(posedge clk);    //packet 1 : sop -eop packet
begin
   data_in = 32;
   sop_in = 1;
   eop_in = 1;
end              
@(posedge clk);    //empty cycle stimulas
begin
    data_in = 0;
    sop_in = 0;
    eop_in = 0;
    end   
@(posedge clk);    //packet 2 : sop of the packet sent
begin
    data_in = 72;
    sop_in = 1;
    eop_in = 0;
  end
@(posedge clk);    //packet 2 : middle of the packet sent
begin
    data_in = 73;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 2 : eop of the packet sent
begin
    data_in = 32;
    sop_in = 0;
    eop_in = 1;
  end   
@(posedge clk);    //empty cycle stimulas 
begin
    data_in = 0;
    sop_in = 0;
    eop_in = 0;
  end  
@(posedge clk);    //packet 3 : sop of the packet sent
begin
    data_in = 71;
    sop_in = 1;
    eop_in = 0;
  end
@(posedge clk);    //packet 3 : middle of the packet sent
begin
    data_in = 79;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 3 : middle of the packet sent
begin
    data_in = 79;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 3 : middle of the packet sent
begin
    data_in = 68;
    sop_in = 0;
    eop_in = 1;
  end
@(posedge clk);    //packet 4 : sop of the packet sent
begin
    data_in = 77;
    sop_in = 1;
    eop_in = 0;
  end
@(posedge clk);    //packet 4 : middle of the packet sent
begin
    data_in = 79;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 4 : middle of the packet sent
begin
    data_in = 82;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 4 : middle of the packet sent
begin
    data_in = 78;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 4 : middle of the packet sent
begin
    data_in = 73;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 4 : middle of the packet sent
begin
    data_in = 78;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 4 :eop of the packet sent
begin
    data_in = 71;
    sop_in = 0;
    eop_in = 1;
  end
@(posedge clk);    //empty cycle stimulas 
begin
    data_in = 0;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //empty cycle stimulas 
begin
    data_in = 0;
    sop_in = 0;
    eop_in = 0;
  end  
@(posedge clk);    //packet 5 : sop of the packet sent
begin
    data_in = 68;
    sop_in = 1;
    eop_in = 0;
  end
@(posedge clk);    //packet 5 : middle of the packet sent
begin
    data_in = 73;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 5 : middle of the packet sent
begin
    data_in = 86;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 5 : middle of the packet sent
begin
    data_in = 89;
    sop_in = 0;
    eop_in = 0;
  end
@(posedge clk);    //packet 5 : eop of the packet sent
begin
    data_in = 65;
    sop_in = 0;
    eop_in = 1;
  end
@(posedge clk);    //empty cycle stimulas 
begin
    data_in = 0;
    sop_in = 0;
    eop_in = 0;
  end  
@(posedge clk);    //empty cycle stimulas 
begin
    data_in = 0;
    sop_in = 0;
    eop_in = 0;
  end       
 $stop;   
end

//-------------To store DUT output packet in "out_q_data" queue---------//
function void store_output(input bit [7:0] data_queue [$]);
 out_pkt_count = out_pkt_count +1;
 $display("time : %tns , Store_output function : Packet_count is : %d , Packet data is : %p ",$time(),out_pkt_count,data_queue);
 out_q_data = {};
endfunction
always @(posedge clk) begin
 if(sop == 1)begin
  out_q_data.push_front(data);
  if(eop != 1) begin
   #10;
   ->mid_pkt;
  end
  else if(eop == 1)
   store_output(out_q_data);
 end
end
always @(posedge clk) begin
  if(mid_pkt.triggered)
    if(sop != 1 && eop != 1) begin
     out_q_data.push_front(data);
     #10; 
     ->mid_pkt;
    end
end
always @(posedge eop) begin
  if(sop !=1) begin
   out_q_data.push_front(data);
   store_output(out_q_data);
  end
end
//----------------------------------------------------------------//
//------ model to store output packet in "q_data" queue based on sop_in,eop_in DUT input--------//
function void store_packet(input bit [7:0] data_queue [$]);
 pkt_count = pkt_count +1;
 $display("time : %tns , Store_packet function : Packet_count is : %d , Packet data is : %p ",$time(),pkt_count,data_queue);
 q_data = {};
endfunction
always @(posedge clk) begin
if(sop_in == 1 && eop_in == 1) begin
  q_data.push_front(data_in);
  sop_flag = 0;
  $display("time : %tns , sop - eop packet : data : %d , sop_flag : %d",$time(),data_in,sop_flag);
  store_packet(q_data);
end
else if(sop_in == 1 && eop_in != 1) begin
  q_data.push_front(data_in);
  sop_flag = 1;
  $display("time : %tns , sop of the packet : data : %d , sop_flag : %d",$time(),data_in,sop_flag);
end
else if(sop_in != 1 && eop_in != 1) begin
  if(sop_flag == 1) begin
   q_data.push_front(data_in);
   $display("time : %tns , middle of the packet : data : %d , sop_flag : %d",$time(),data_in,sop_flag);
   end
  else   
   $display("time : %tns , empty cycle : data : %d , sop_flag : %d",$time(),data_in,sop_flag);
end
else if (sop_in != 1 && eop_in == 1) begin
 if(sop_flag == 1) begin
   q_data.push_front(data_in);
   sop_flag = 0;
   $display("time : %tns , eop of the packet : data : %d , sop_flag : %d",$time(),data_in,sop_flag);
   store_packet(q_data);
  end
end
else begin
 sop_flag = 0;
 $display("time : %tns , else cond : No Condition Matched:  sop : %d , eop : %d  , data : %d , sop_flag: %d,",$time(),sop_in,eop_in,data_in,sop_flag);
 end
end
//-------------------------------------------------------------------------------------------------------------------//
endmodule



//Simulation log. Questa transcript

# Compile of sop_eop_dut.sv was successful.
# Compile of tb.sv was successful.
# 2 compiles, 0 failed with no errors.
vsim -voptargs=+acc work.tb
# vsim -voptargs=+acc work.tb
# Loading sv_std.std
# Loading work.tb(fast)
# Loading work.sop_eop_dut(fast)
do sop_eop.do
run -all
# time :   5ns , empty cycle : data :   0 , sop_flag : 0
# time :  15ns , sop - eop packet : data :  32 , sop_flag : 0
# time :  15ns , Store_packet function : Packet_count is :           1 , Packet data is : '{32}
# time :  25ns , Store_output function : Packet_count is :           1 , Packet data is : '{32}
# time :  25ns , empty cycle : data :   0 , sop_flag : 0
# time :  35ns , sop of the packet : data :  72 , sop_flag : 1
# time :  45ns , middle of the packet : data :  73 , sop_flag : 1
# time :  55ns , eop of the packet : data :  32 , sop_flag : 0
# time :  55ns , Store_packet function : Packet_count is :           2 , Packet data is : '{32, 73, 72}
# time :  55ns , Store_output function : Packet_count is :           2 , Packet data is : '{32, 73, 72}
# time :  65ns , empty cycle : data :   0 , sop_flag : 0
# time :  75ns , sop of the packet : data :  71 , sop_flag : 1
# time :  85ns , middle of the packet : data :  79 , sop_flag : 1
# time :  95ns , middle of the packet : data :  79 , sop_flag : 1
# time : 105ns , eop of the packet : data :  68 , sop_flag : 0
# time : 105ns , Store_packet function : Packet_count is :           3 , Packet data is : '{68, 79, 79, 71}
# time : 105ns , Store_output function : Packet_count is :           3 , Packet data is : '{68, 79, 79, 71}
# time : 115ns , sop of the packet : data :  77 , sop_flag : 1
# time : 125ns , middle of the packet : data :  79 , sop_flag : 1
# time : 135ns , middle of the packet : data :  82 , sop_flag : 1
# time : 145ns , middle of the packet : data :  78 , sop_flag : 1
# time : 155ns , middle of the packet : data :  73 , sop_flag : 1
# time : 165ns , middle of the packet : data :  78 , sop_flag : 1
# time : 175ns , eop of the packet : data :  71 , sop_flag : 0
# time : 175ns , Store_packet function : Packet_count is :4,Packet data is: '{71, 78, 73, 78, 82, 79, 77}
# time : 175ns , Store_output function : Packet_count is :4,Packet data is: '{71, 78, 73, 78, 82, 79, 77}
# time : 185ns , empty cycle : data :   0 , sop_flag : 0
# time : 195ns , empty cycle : data :   0 , sop_flag : 0
# time : 205ns , sop of the packet : data :  68 , sop_flag : 1
# time : 215ns , middle of the packet : data :  73 , sop_flag : 1
# time : 225ns , middle of the packet : data :  86 , sop_flag : 1
# time : 235ns , middle of the packet : data :  89 , sop_flag : 1
# time : 245ns , eop of the packet : data :  65 , sop_flag : 0
# time : 245ns , Store_packet function : Packet_count is :    5 , Packet data is : '{65, 89, 86, 73, 68}
# time : 245ns , Store_output function : Packet_count is :    5 , Packet data is : '{65, 89, 86, 73, 68}
# time : 255ns , empty cycle : data :   0 , sop_flag : 0
# Break in Module tb at /scratch/pnq_scratch/aghona/x120/tb.sv line 195

//waveform snapshot