Saturday, 18 June 2016

Task inside Interface

interface simple_bus (input logic clk); // Define the interface
  logic req, gnt;
  logic [7:0] addr, data;
  logic [1:0] mode;
  logic start, rdy;

modport slave(input req, addr, mode, start, clk,
               output gnt, rdy,
               ref data,
               import slaveRead,slaveWrite);
             
//import into module that uses the modport
modport master(input gnt, rdy, clk,
               output req, addr, mode, start,
               ref data,
               import masterRead,masterWrite);
             
//import into module that uses the modport
task masterRead(input logic[7:0] raddr=8'h0); // masterRead method
  $display("Inside Master_Read");
endtask
             
task slaveRead;
  $display("Inside Slave_Read");
endtask
           
task masterWrite(input logic [7:0] waddr=8'h0);
  $display("Inside Master_Write");
endtask
             
task slaveWrite;
  $display("Inside Slave_Write");
endtask
             
endinterface: simple_bus
             
             
module mod (interface i)               ;
  always@(posedge i.clk) begin
     i.slaveRead;
     i.masterRead;
     i.slaveWrite;
     i.masterWrite;
   end              
endmodule
               
module top;
  logic clk = 0;
   simple_bus sb_intf(clk); // Instantiate the interface
   mod m (sb_intf.master) ;

   always #5 clk=!clk;
                 
   initial begin
      #50 $finish;                  
   end                                  

endmodule
Process Control in System Verilog


module test;

  task automatic do_n_way( int N );
  process job[] = new [N];
  foreach(job[j]) begin
    fork
      automatic int k = j;
      begin
        job[k] = process::self();      
        #(5+k);
      end
    join_none
  end

  foreach(job[j]) // wait for all processes to start
    wait( job[j] != null);

  $display("All JOB Started !!",$time);

    job[1].suspend();
 
    job[1].resume();  

    job[1].await();
 
    foreach(job[j]) begin
      if(job[j].status != process::FINISHED )
        job[j].kill();  
    end  
  endtask

  initial begin
    do_n_way(3);
  end

endmodule

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/////////////////////Fork JoinExample///////////////////////////
//////////////////////////////////////////////////////////////////////////
module test;

  task automatic do_n_way( int N );
  process job[] = new [N];
  foreach(job[j]) begin
    fork
      automatic int k = j;
      begin
        job[k] = process::self();      
        #(5+k);
      end
    join_none
  end

  foreach(job[j]) // wait for all processes to start
    wait( job[j] != null);

  $display("All JOB Done !!",$time);

    job[1].suspend();
    #15
    job[1].resume();  

    job[0].await();
    //job[1].await();
 
    foreach(job[j]) begin
      if(job[j].status != process::FINISHED )begin
        $display("JOb[%0d] killed with status::%0d @%t",j,job[j].status,$time);
        job[j].kill();  
      end
    end  
 
    foreach(job[j]) begin
      $display("JOb[%0d] status ::%0d  @%t",j,job[j].status,$time);
    end
 
  endtask

  initial begin
   fork  //Try using without fork
    do_n_way(3);
    do_n_way(4);
   join
  end

endmodule


//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////AWAIT Example///////////////////////////
//////////////////////////////////////////////////////////////////////////
module test;

  task automatic do_n_way( int N );
  process job[] = new [N];
  foreach(job[j]) begin
    fork
      automatic int k = j;
      begin
        job[k] = process::self();      
        #(5+k);
      end
    join_none
  end

  foreach(job[j]) // wait for all processes to start
    wait( job[j] != null);

  $display("All JOB Done !!",$time);

    job[1].suspend();
    #15
    job[1].resume();  

    job[0].await();
    job[1].await();
 
    foreach(job[j]) begin
      if(job[j].status != process::FINISHED )begin
        $display("JOb[%0d] killed with status :: ",j,job[j].status);
        job[j].kill();  
      end
    end  
 
    foreach(job[j]) begin
      $display("JOb[%0d] status :: ",j,job[j].status);
    end
 
  endtask

  initial begin
    do_n_way(3);
  end


endmodule