Module 10: Capstone Projects · Project 4 — Synchronous SRAM Controller

Project 4, Part 2: An FSM Built From Wait States

The previous article derived controller wait cycles from SRAM datasheet constraints. This article designs the memory controller finite state machine (FSM) and address decoding logic. A dedicated wait-state counter coordinates read access delays and write pulse durations without state explosion.

Memory Controller State Machine Architecture

The controller implements five operational states: MEM_IDLE, MEM_READ_WAIT, MEM_READ_DONE, MEM_WRITE_ASSERT, and MEM_WRITE_DONE:

typedef enum logic [2:0] {
    MEM_IDLE, MEM_READ_WAIT, MEM_READ_DONE,
    MEM_WRITE_ASSERT, MEM_WRITE_DONE
} mem_state_t;

mem_state_t state, next_state;
logic [3:0] wait_count;

localparam READ_WAIT_CYCLES  = 2;   // Satisfies t_AA and t_OE access times
localparam WRITE_WAIT_CYCLES = 4;   // Satisfies t_WP minimum write pulse width

// Block 1: State register update
always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) state <= MEM_IDLE;
    else        state <= next_state;
end

// Block 2: Combinational next-state and control output decode
always_comb begin
    next_state = state;
    ce_n       = 1'b1;
    oe_n       = 1'b1;
    we_n       = 1'b1;
    ready      = 1'b0;

    unique case (state)
        MEM_IDLE: begin
            if (read_req)       next_state = MEM_READ_WAIT;
            else if (write_req) next_state = MEM_WRITE_ASSERT;
        end

        MEM_READ_WAIT: begin
            ce_n = 1'b0;
            oe_n = 1'b0;   // Assert active-low read strobes
            if (wait_count == READ_WAIT_CYCLES - 1)
                next_state = MEM_READ_DONE;
        end

        MEM_READ_DONE: begin
            ce_n  = 1'b0;
            oe_n  = 1'b0;
            ready = 1'b1;   // Data bus is valid; latch data this cycle
            next_state = MEM_IDLE;
        end

        MEM_WRITE_ASSERT: begin
            ce_n = 1'b0;
            we_n = 1'b0;   // Assert active-low write strobes
            if (wait_count == WRITE_WAIT_CYCLES - 1)
                next_state = MEM_WRITE_DONE;
        end

        MEM_WRITE_DONE: begin
            ce_n  = 1'b0;
            we_n  = 1'b1;   // Release WE_n before releasing CE_n
            ready = 1'b1;
            next_state = MEM_IDLE;
        end

        default: next_state = MEM_IDLE;
    endcase
end

// Dedicated wait-state hardware counter
always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
        wait_count <= 0;
    end else if (state == MEM_READ_WAIT || state == MEM_WRITE_ASSERT) begin
        wait_count <= wait_count + 1'b1;
    end else begin
        wait_count <= 0;
    end
end

In MEM_WRITE_DONE, we_n deasserts to 1 while ce_n remains low. This sequence enforces the write hold specification before ending the transaction.

Counter-Based Delay vs State Explosion

Hardware designers avoid defining explicit individual states for every clock cycle delay. If a slow memory requires 20 wait cycles, defining 20 sequential states inflates code size and introduces errors.

One state plus a counter versus twenty near-identical wait states
Figure 1: A counter scales; a chain of states does not

A dedicated hardware counter scales efficiently. Adjusting a parameter accommodates different memory access times without modifying state transitions.

Memory-Mapped Address Decoding

Memory controllers decode upper address bits to identify requests directed to their memory range:

logic addr_in_range;
assign addr_in_range = (addr >= BASE_ADDR) && (addr < BASE_ADDR + MEM_DEPTH);

assign read_req_gated  = read_req  && addr_in_range;
assign write_req_gated = write_req && addr_in_range;

The controller evaluates requests only when the address falls within its designated boundaries. Addresses outside the range remain ignored, permitting other bus peripherals to respond.

Practice Exercise

Trace the signal transitions during a read operation where READ_WAIT_CYCLES = 2. Verify the exact cycle when the controller asserts ready.

Solution

The transaction executes across four clock cycles:

  • Cycle 0: The controller resides in MEM_IDLE. Signal read_req asserts; the FSM selects MEM_READ_WAIT.
  • Cycle 1: State is MEM_READ_WAIT. Strobes ce_n and oe_n assert low. Counter wait_count is 0.
  • Cycle 2: State remains MEM_READ_WAIT. Counter wait_count increments to 1 (READ_WAIT_CYCLES - 1). The FSM selects MEM_READ_DONE.
  • Cycle 3: State enters MEM_READ_DONE. Signal ready asserts high to indicate valid data.

The controller delays the ready signal by two full wait cycles, satisfying the memory access time.

The SRAM controller uses an FSM combined with a dedicated wait counter to manage memory transactions. This architecture scales to arbitrary timing requirements without state explosion. The next article combines the controller with a timing-accurate memory model and verifies read/write integrity.

← Project 4, Part 1: What an SRAM Datasheet Demands Project 4, Part 3: A Testbench That Enforces the Timing →