How is it possible to do a write as well as read using a apb slave?

I have tried a apb slave, can there be a apb master or a apb slave right? is it possible for a same bus to act as master and slave at different instance, so should i need to write a apb_master dut separately and apb_slave dut separately? . please clarify me as i am new to building interfaces. can anyone give me a perfect example to connect how transfers happen between a apb master and slave?

module apb_slave
#(
addrWidth = 8,
dataWidth = 32
)
(
input clk,
input rst_n,
input [addrWidth-1:0] paddr,
input pwrite,
input psel,
input penable,
input [dataWidth-1:0] pwdata,
output logic [dataWidth-1:0] prdata
);

logic [dataWidth-1:0] mem [256];

logic [1:0] apb_st;
const logic [1:0] SETUP = 0;
const logic [1:0] W_ENABLE = 1;
const logic [1:0] R_ENABLE = 2;

// SETUP → ENABLE
always @(negedge rst_n or posedge clk) begin
if (rst_n == 0) begin
apb_st <= 0;
prdata <= 0;
end

else begin
case (apb_st)
SETUP : begin
// clear the prdata
prdata <= 0;

    // Move to ENABLE when the psel is asserted
    if (psel && !penable) begin
      if (pwrite) begin
        apb_st <= W_ENABLE;
      end

      else begin
        apb_st <= R_ENABLE;
      end
    end
  end

  W_ENABLE : begin
    // write pwdata to memory
    if (psel && penable && pwrite) begin
      mem[paddr] <= pwdata;
    end

    // return to SETUP
    apb_st <= SETUP;
  end

  R_ENABLE : begin
    // read prdata from memory
    if (psel && penable && !pwrite) begin
      prdata <= mem[paddr];
    end

    // return to SETUP
    apb_st <= SETUP;
  end
endcase

end
end
initial
begin
$dumpfile(“test.vcd”);
$dumpvars;
end

endmodule

In reply to Arun_Rajha:

It has been since 2004 that I last looked at that interface, thus I hardly remember anything about it. However, at that time I published the book Using PSL/Sugar for Formal and Dynamic Verification 2nd Edition where in Chapter 8 I addressed the design and verification of the AHB. Specifically, the chapter demonstrates how an AMBA™ AHB bus specification1 and an IDT 71V433 Synchronous pipelined SRAM2 are used to design and verify a memory slave controller in Verilog and PSL. Simulation with a PSL aware simulator was used for verification. The testbench does not have an automatic verifier, which is included in a traditional self-checking methodology. Instead, verification relies on ABV methodology.

http://systemverilog.us/vf/PSL_AHB_Ch8.zip
This link provides the PDF for that chapter and all the Verilog code used in the process.
Reading the PDF and the code may help you better understand the interface and the requirements.

BTW, I DO NOT recommend the use of PSL; use SystemVerilog instead.

Ben Cohen
http://www.systemverilog.us/ ben@systemverilog.us
For training, consulting, services: contact Home - My cvcblr


  1. SVA Alternative for Complex Assertions
    Verification Horizons - March 2018 Issue | Verification Academy
  2. SVA: Package for dynamic and range delays and repeats | Verification Academy
  3. SVA in a UVM Class-based Environment
    SVA in a UVM Class-based Environment | Verification Horizons | Verification Academy