/****************************************************************************
    +-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+
    |F|u|n|c|t|i|o|n|a|l| |V|e|r|i|f|i|c|a|t|i|o|n| |o|f| |H|a|r|d|w|a|r|e|
    +-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+

    FILE            apb_slave_driver.sv

    DESCRIPTION     drives slave response

 ****************************************************************************/

`ifndef APB_SLAVE_DRIVER_SV
`define APB_SLAVE_DRIVER_SV

/**
 * Class: apb_slave_driver
 */
class apb_slave_driver extends uvm_driver #(apb_transaction, apb_transaction);
    
    // apb virtual interface
    virtual apb_if vif;
    
    // configuration
    apb_slave_config cfg;
    
    // UVM factory registration
    `uvm_component_utils_begin(apb_slave_driver)
        `uvm_field_object(cfg, UVM_DEFAULT | UVM_REFERENCE)
    `uvm_component_utils_end       

    // new - constructor
	function new(string name = "apb_slave_driver", uvm_component parent = null);
		super.new(name, parent);
	endfunction : new
    
    // UVM build_phase
    function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        // get configuration object from db
        if(!uvm_config_db#(apb_slave_config)::get(this, "", "apb_slave_config", cfg))
            `uvm_fatal("NOCONFIG",{"Config object must be set for: ",get_full_name(),".cfg"})
    endfunction: build_phase
    
    // UVM connect_phase
    function void connect_phase(uvm_phase phase);
        super.connect_phase(phase);
        // get interface from db
        if(!uvm_config_db#(virtual apb_if)::get(this, "", "apb_if", vif))
            `uvm_fatal("NOVIF",{"virtual interface must be set for: ",get_full_name(),".vif"})    
    endfunction : connect_phase
    
    // additional class methods
    extern virtual task run_phase(uvm_phase phase);
    extern virtual task get_and_drive();
    extern virtual task reset();
    extern virtual task drive_tr (apb_transaction tr);    

endclass : apb_slave_driver

// UVM run_phase
task apb_slave_driver::run_phase(uvm_phase phase);
    reset(); // init.
    forever begin
        fork
            get_and_drive(); // thread killed at reset
            @(negedge vif.presetn); // reset is active low
        join_any
        disable fork;
        
        reset();
    end
endtask : run_phase

// reset signals
task apb_slave_driver::reset();
    `uvm_info(get_type_name(), "Reset observed", UVM_MEDIUM)
    vif.prdata  <= {WDATA_WIDTH {1'bZ}};
    vif.pready  <= 1'b0;
    vif.pslverr <= 1'b0;
     @(posedge vif.presetn); // reset dropped
endtask : reset

// sequencer/driver handshake
task apb_slave_driver::get_and_drive();
    forever begin
        seq_item_port.get_next_item(req);
        drive_tr(req);
        seq_item_port.item_done();
    end
endtask : get_and_drive

// drive transaction
task apb_slave_driver::drive_tr (apb_transaction tr);
    
    // wait for the master to initiate the transaction
    @(posedge vif.pclk iff (vif.penable && cfg.check_psel_index(vif.psel)));

    tr.dir = apb_direction_enum'(vif.pwrite);
    
    // delay
    if (tr.delay > 0) begin
        repeat(tr.delay) @(posedge vif.pclk);
    end    
    // respond
    vif.pslverr <= tr.error;
    vif.pready <= 1'b1;
    if (tr.dir == APB_READ)
        vif.prdata <= tr.rdata;
    @(posedge vif.pclk);
    vif.prdata <= {WDATA_WIDTH {1'bZ}};
    vif.pready <= 1'b0;

    `uvm_info(get_type_name(), $sformatf("APB Finished Driving tr \n%s", tr.sprint()), UVM_HIGH)
endtask : drive_tr

`endif

