RTL Design / Module 6: FSMs & Control
Lesson 6.7 · Lab

Lab: protocol FSM (handshake-driven control)

Wire everything from Module 6 into one block. You build a valid-ready transfer controller that accepts bytes from a source interface, buffers them, and forwards to a sink — all driven by a 3-state Moore FSM with safe defaults and a self-checking testbench.

Lab goal

Produce a synthesizable proto_xfer_ctrl module plus testbench you can paste into the EcrioniX Verilog simulator and run immediately. This is the Module 6 checkpoint.

1

Block diagram — what you're building

Protocol transfer controller block diagram

Accept from source when buffer empty; forward to sink when sink is ready.

StateMoore outputsTransition
IDLEs_ready=1, m_valid=0s_valid → HOLD (latch data)
HOLDs_ready=0, m_valid=1m_ready → IDLE (xfer done)
2'b11 etc.safe low outputsdefault → IDLE
IDLEs_valid&s_readyHOLDm_valid&m_readyIDLE
2

Interface spec

  • Source (slave): s_valid, s_data[7:0] in; s_ready out
  • Sink (master): m_valid, m_data[7:0] out; m_ready in
  • Transfer rules: source xfer = s_valid && s_ready; sink xfer = m_valid && m_ready
  • Style: 3-block Moore FSM, default: next = IDLE
3

Step 1 — state diagram

Lab FSM state diagram

Two-state core FSM — simplest protocol controller. HOLD until sink accepts.

4

Step 2 — next-state + output decode

fsm_snippet.v
localparam IDLE = 2'd0;
localparam HOLD = 2'd1;

reg [1:0] state, next;

// Block 1: state + data register
always @(posedge clk or negedge rst_n) begin
  if (!rst_n) begin
    state    <= IDLE;
    data_reg <= 8'h00;
  end else begin
    state <= next;
    if (state == IDLE && s_valid && s_ready)
      data_reg <= s_data;
  end
end

// Block 2: next-state
always @(*) begin
  next = IDLE;
  case (state)
    IDLE: if (s_valid) next = HOLD;
    HOLD: if (m_ready) next = IDLE;
    default: next = IDLE;
  endcase
end

// Block 3: Moore outputs
assign s_ready = (state == IDLE);
assign m_valid = (state == HOLD);
assign m_data  = data_reg;
Trap

s_ready must NOT depend on s_valid. It depends only on state == IDLE. Same for m_valid — only from state, not m_ready.

5

Full DUT — paste-ready module

proto_xfer_ctrl.v
// proto_xfer_ctrl — Module 6.7 lab DUT
// Valid-ready byte pipe: source → buffer → sink
module proto_xfer_ctrl (
  input  wire       clk,
  input  wire       rst_n,
  // Source (we are sink to source)
  input  wire       s_valid,
  input  wire [7:0] s_data,
  output wire       s_ready,
  // Sink (we are source to sink)
  output wire       m_valid,
  output wire [7:0] m_data,
  input  wire       m_ready
);
  localparam IDLE = 2'd0;
  localparam HOLD = 2'd1;

  reg [1:0] state, next;
  reg [7:0] data_reg;

  always @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      state    <= IDLE;
      data_reg <= 8'h00;
    end else begin
      state <= next;
      if (state == IDLE && s_valid && s_ready)
        data_reg <= s_data;
    end
  end

  always @(*) begin
    next = IDLE;
    case (state)
      IDLE: if (s_valid) next = HOLD;
      HOLD: if (m_ready) next = IDLE;
      default: next = IDLE;
    endcase
  end

  assign s_ready = (state == IDLE);
  assign m_valid = (state == HOLD);
  assign m_data  = data_reg;
endmodule
6

Self-checking testbench

proto_xfer_ctrl_tb.v
module proto_xfer_ctrl_tb;
  reg clk, rst_n;
  reg s_valid, m_ready;
  reg [7:0] s_data;
  wire s_ready, m_valid;
  wire [7:0] m_data;

  proto_xfer_ctrl dut (
    .clk(clk), .rst_n(rst_n),
    .s_valid(s_valid), .s_data(s_data), .s_ready(s_ready),
    .m_valid(m_valid), .m_data(m_data), .m_ready(m_ready)
  );

  initial clk = 0;
  always #5 clk = ~clk;

  integer errors, bytes_rcvd;
  reg [7:0] expect;

  initial begin
    errors = 0; bytes_rcvd = 0;
    rst_n = 0; s_valid = 0; s_data = 0; m_ready = 1;
    #20 rst_n = 1;

    // Test 1: single byte
    @(posedge clk); s_valid = 1; s_data = 8'hA5;
    @(posedge clk); s_valid = 0;
    wait(m_valid); expect = 8'hA5;
    if (m_data !== expect) begin
      $display("FAIL: got %h expected %h", m_data, expect);
      errors = errors + 1;
    end else $display("PASS: single byte 0xA5");
    @(posedge clk);

    // Test 2: back-pressure (m_ready low)
    m_ready = 0;
    @(posedge clk); s_valid = 1; s_data = 8'h3C;
    @(posedge clk); s_valid = 0;
    repeat(3) @(posedge clk);
    if (!m_valid) begin $display("FAIL: m_valid should be high"); errors++; end
    m_ready = 1;
    @(posedge clk);
    if (m_data !== 8'h3C) begin $display("FAIL: back-pressure byte"); errors++; end
    else $display("PASS: back-pressure byte 0x3C");

    // Test 3: two bytes back-to-back
    m_ready = 1;
    @(posedge clk); s_valid = 1; s_data = 8'h11;
    @(posedge clk); s_valid = 1; s_data = 8'h22;
    wait(m_valid && m_ready); @(posedge clk);
    wait(m_valid && m_ready);
    if (m_data !== 8'h22) begin $display("FAIL: second byte"); errors++; end
    else $display("PASS: back-to-back");

    if (errors == 0) $display("=== ALL TESTS PASSED ===");
    else $display("=== %0d TESTS FAILED ===", errors);
    $finish;
  end
endmodule
▶ Run lab in Verilog simulator
7

Extension challenges

📦

2-byte FIFO

Add a second buffer slot so s_ready=1 in HOLD if one slot free — throughput boost.

🔢

Beat counter

Add byte_count output that increments on each successful sink transfer.

⚠️

Illegal state TB

Force state=2'b11 and verify recovery to IDLE within 1 cycle.

8

Module 6 checkpoint

  • Explain Moore vs Mealy and pick the right style for an output
  • Write a 2- or 3-block FSM with named states and safe default
  • Choose binary vs one-hot encoding with justification
  • Recover from illegal states in simulation
  • Implement fixed-priority and round-robin arbiters
  • Apply valid-ready rules without combo loops
  • Build and verify a handshake-driven protocol controller

Module 7 is next: Datapath & Memory Systems — skid buffers, FIFOs, and tying control FSMs to real datapaths.

🎯 What to remember

  • Protocol FSMs are Moore machines with handshake signals as Moore outputs.
  • ready from state; valid from state — never cross-combine.
  • Draw diagram → 3-block RTL → self-checking TB is the Module 6 workflow.
  • Module 6 complete — you can design, encode, protect, arbitrate, and handshake.
Advertisement