Project: How Do You Build an AXI4-Lite Peripheral?

Give the peripheral a register map, write a slave that accepts AXI4-Lite writes and reads into those registers, test it against a reference model with random back-pressure, and synthesize it for the target device to check timing. This one multiplies: software writes A and B and reads their product, which is enough to show every part of the interface. It was simulated in Icarus Verilog, linted with Verilator, and synthesized in Vivado for a Zynq-7020, where the first version missed 100 MHz and needed one register to fix it.

How this was checked

The RTL was simulated in Icarus Verilog 12 against a testbench with a reference model (305 transactions, no errors), linted with Verilator 5.036 with no warnings, and synthesized out of context in Vivado 2025.2 for the xc7z020clg400-1 at 100 MHz. It has not been run on a board; the steps for connecting it to the Zynq processing system follow AMD's documentation.

What you'll learn

  • Writing a register map
  • An AXI4-Lite slave that accepts address and data in either order
  • Byte write strobes
  • A testbench with random back-pressure and a protocol check
  • Reading a synthesis timing report, and fixing a failing path

Step 1: the register map

OffsetNameAccessMeaning
0x00ARead/writeFirst operand
0x04BRead/writeSecond operand
0x08PRODUCTRead onlyA x B, low 32 bits
0x0CIDRead only0x534C_0001, to check the peripheral is there
The peripheral in a Zynq systemThe Cortex-A9 in the processing system reaches the peripheral through the general-purpose AXI port and an AXI interconnect; inside, the slave logic writes registers A and B and reads PRODUCT and ID.Cortex-A9M_AXI_GP0AXIinterconnectAXI slaveA, BPRODUCTaxil_mult
Software sees four registers at the peripheral's base address.

Step 2: the RTL

The write side captures the address and the data in separate registers as each handshake happens, in either order, and acts once it holds both. Writes to PRODUCT or ID get SLVERR. The read side accepts an address, then holds RVALID and the data until RREADY.

axil_mult.v
`timescale 1ns/1ps
// A custom AXI4-Lite peripheral: two operand registers and their product.
//   0x00 A        read/write
//   0x04 B        read/write
//   0x08 PRODUCT  read only, A * B (low 32 bits)
//   0x0C ID       read only, 0x534C_0001
// A write to a read-only register is refused with SLVERR. The address and
// write-data channels may arrive in either order or together; one write and
// one read are handled at a time, as AXI4-Lite allows.
module axil_mult (
  input  wire        aclk,
  input  wire        aresetn,
  // write address
  /* verilator lint_off UNUSEDSIGNAL */
  input  wire [3:0]  s_awaddr,       // bits 1:0 are the byte within a word, unused
  /* verilator lint_on UNUSEDSIGNAL */
  input  wire        s_awvalid,
  output reg         s_awready,
  // write data
  input  wire [31:0] s_wdata,
  input  wire [3:0]  s_wstrb,
  input  wire        s_wvalid,
  output reg         s_wready,
  // write response
  output reg  [1:0]  s_bresp,
  output reg         s_bvalid,
  input  wire        s_bready,
  // read address
  /* verilator lint_off UNUSEDSIGNAL */
  input  wire [3:0]  s_araddr,
  /* verilator lint_on UNUSEDSIGNAL */
  input  wire        s_arvalid,
  output reg         s_arready,
  // read data
  output reg  [31:0] s_rdata,
  output reg  [1:0]  s_rresp,
  output reg         s_rvalid,
  input  wire        s_rready
);
  localparam OKAY = 2'b00, SLVERR = 2'b10;
  localparam [31:0] ID = 32'h534C_0001;
  reg [31:0] a, b;
  // The product is registered: a 32 x 32 multiply feeding the read mux
  // directly missed a 100 MHz clock by 0.064 ns on a Zynq-7020. It is ready
  // one clock after A or B changes, long before a read can follow a write.
  reg [31:0] product;
  always @(posedge aclk) product <= a * b;

  // ---- writes: capture address and data separately, act when both are in
  reg [3:2]  aw_addr;
  reg        aw_have, w_have;
  reg [31:0] w_data;
  reg [3:0]  w_strb;
  function [31:0] merge(input [31:0] old, input [31:0] data, input [3:0] strb);
    integer i;
    begin
      for (i = 0; i < 4; i = i + 1) merge[i*8 +: 8] = strb[i] ? data[i*8 +: 8] : old[i*8 +: 8];
    end
  endfunction
  always @(posedge aclk) begin
    if (!aresetn) begin
      a <= 0; b <= 0;
      aw_have <= 0; w_have <= 0;
      s_awready <= 0; s_wready <= 0; s_bvalid <= 0; s_bresp <= OKAY;
      aw_addr <= 2'd0; w_data <= 0; w_strb <= 0;
    end else begin
      // Accept each channel for one clock when it is offered and not yet held.
      s_awready <= !aw_have && !s_awready && s_awvalid && !s_bvalid;
      s_wready  <= !w_have && !s_wready && s_wvalid && !s_bvalid;
      if (s_awvalid && s_awready) begin aw_addr <= s_awaddr[3:2]; aw_have <= 1; end
      if (s_wvalid && s_wready)   begin w_data <= s_wdata; w_strb <= s_wstrb; w_have <= 1; end
      if (aw_have && w_have && !s_bvalid) begin
        case (aw_addr)
          2'd0: begin a <= merge(a, w_data, w_strb); s_bresp <= OKAY; end
          2'd1: begin b <= merge(b, w_data, w_strb); s_bresp <= OKAY; end
          default: s_bresp <= SLVERR;            // PRODUCT and ID are read only
        endcase
        s_bvalid <= 1; aw_have <= 0; w_have <= 0;
      end
      if (s_bvalid && s_bready) s_bvalid <= 0;
    end
  end

  // ---- reads: one clock to accept the address, then hold RVALID until RREADY
  always @(posedge aclk) begin
    if (!aresetn) begin
      s_arready <= 0; s_rvalid <= 0; s_rdata <= 0; s_rresp <= OKAY;
    end else begin
      s_arready <= !s_arready && s_arvalid && !s_rvalid;
      if (s_arvalid && s_arready) begin
        case (s_araddr[3:2])
          2'd0: s_rdata <= a;
          2'd1: s_rdata <= b;
          2'd2: s_rdata <= product;
          default: s_rdata <= ID;
        endcase
        s_rresp <= OKAY; s_rvalid <= 1;
      end else if (s_rvalid && s_rready) s_rvalid <= 0;
    end
  end
endmodule

Step 3: the testbench

The testbench drives the master side with tasks that change signals only on the falling clock edge, and checks every handshake at the falling edge before the rising edge that completes it. It sends address and data together, address first, and data first; waits a random 0 to 3 clocks before taking each response; and checks two protocol rules on every clock: BVALID and RVALID, once raised, stay up with stable data until taken. A model of A and B predicts every read. Three directed checks come first: 6 x 7, a write to PRODUCT, and the ID.

Icarus Verilog output
directed: 6 x 7 read back as 42
directed: write to PRODUCT answered SLVERR
directed: ID = 534c0001
random: 149 writes (74 refused with SLVERR), 156 reads, errors = 0

Step 4: synthesis and a timing fix

Synthesized for a Zynq-7020 with a 100 MHz clock, the first version had the 32 x 32 multiplier feeding the read multiplexer directly, and failed timing by 0.064 ns: a worst negative slack (WNS) of −0.064 ns, so the longest path took 10.064 ns. Registering the product, so it is computed one clock after A or B changes, split that path. Software can't read PRODUCT within a clock of writing B anyway, because a read takes several clocks on the bus.

VersionWNS at 100 MHzLongest pathSlice LUTsFlip-flopsDSP slices
Product straight to the read mux−0.064 ns (fails)10.064 ns671423
Product registered+2.932 ns (passes)7.068 ns671593

The 32 x 32 multiply maps onto 3 of the device's 220 DSP slices. The 17 extra flip-flops are the product register. These are out-of-context synthesis estimates; a full implementation in a block design adds routing delay, so keep some slack in hand.

Step 5: put it on a Zynq

  • In Vivado, use Tools, Create and Package New IP, and package axil_mult.v as an AXI4-Lite peripheral, mapping its s_ ports to an AXI interface.
  • Create a block design, add the Zynq processing system and run block automation for your board.
  • Add your IP and run connection automation: Vivado adds an AXI interconnect from M_AXI_GP0 and assigns a base address in the address editor, within 0x4000_0000 to 0x7FFF_FFFF.
  • Generate the bitstream, export the hardware, and in Vitis write to base + 0x0 and base + 0x4 and read base + 0x8.

Common mistakes

  • Assuming AW arrives before W. Test all three orders.
  • Ignoring WSTRB, so byte writes clobber whole registers.
  • Dropping BVALID before BREADY.
  • Checking timing only after building the whole block design.
  • Forgetting that offsets are byte addresses: register 2 is at 0x8.

Check your understanding

  • Q1. Why does the slave keep the address and data in separate registers? Answer: so it can accept them in either order, as AXI requires.
  • Q2. What did registering the product cost? Answer: 17 flip-flops and one clock of latency, invisible to software.
  • Q3. Which response does a write to 0x0C get? Answer: SLVERR, because ID is read only.

Related

Sources

Last verified

Simulated in Icarus Verilog 12, linted with Verilator 5.036 and synthesized in Vivado 2025.2 on 17 November 2026; the outputs and reports above are from those runs. The files are in the site's repository under scripts/soc.