Verilog Coding

Can you develop HDL code for a 3-state FSM (IDLE, READ, WRITE), with transitions based on the "op" input signal and a 4-clock-cycle return to IDLE?

Design Verification Engineer

Meta

Apple

Prysmian Group

General Motors

Xilinx

Samsung

Did you come across this question in an interview?

Answers

Expert Answer

Anonymous

5Exceptional
module fsm (
  input wire clk,         // Clock signal
  input wire reset_n,     // Active low reset
  input wire op,          // Operation signal: 0 for READ, 1 for WRITE
  output reg [1:0] state  // Current state of the FSM
);

  // Define states using parameters
  localparam [1:0] IDLE  = 2'b00,
                   READ  = 2'b01,
                   WRITE = 2'b10;

  reg [1:0] next_state;   // Next state logic
  reg [2:0] cycle_count;  // Counter for clock cycles (3-bit to count up to 4)

  // State transition logic (sequential logic)
  always @(posedge clk or negedge reset_n) begin
    if (!reset_n) begin
      state <= IDLE;           // Reset to IDLE state
      cycle_count <= 3'b000;   // Reset cycle counter
    end
    else begin
      state <= next_state;     // Update current state to next state
      if (state == READ || state == WRITE) begin
        cycle_count <= cycle_count + 1'b1;  // Increment counter in READ/WRITE
      end
      else begin
        cycle_count <= 3'b000;  // Reset counter in IDLE state
      end
    end
  end

  // Next state logic (combinational logic)
  always @(*) begin
    case (state)
      IDLE: begin
        if (op == 1'b0)
          next_state = READ;  // Go to READ state if op is 0
        else
          next_state = WRITE; // Go to WRITE state if op is 1
      end

      READ: begin
        if (cycle_count == 3'd3)
          next_state = IDLE;  // After 4 clock cycles, go back to IDLE
        else
          next_state = READ;  // Remain in READ state until 4 cycles
      end

      WRITE: begin
        if (cycle_count == 3'd3)
          next_state = IDLE;  // After 4 clock cycles, go back to IDLE
        else
          next_state = WRITE; // Remain in WRITE state until 4 cycles
      end

      default: next_state = IDLE;  // Default case to handle any invalid state
    endcase
  end

endmodule

  • Can you develop HDL code for a 3-state FSM (IDLE, READ, WRITE), with transitions based on the "op" input signal and a 4-clock-cycle return to IDLE?
  • How would you code an HDL FSM with IDLE, READ, and WRITE states, transitioning based on "op" signal and reverting to IDLE after 4 cycles?
  • Could you script an HDL FSM with three states (IDLE, READ, WRITE) that moves based on "op" input and cycles back to IDLE after 4 ticks?
  • What approach would you take to write HDL for a FSM with IDLE, READ, and WRITE states, transitioning on "op" input and resetting after 4 cycles?
  • Can you construct HDL code for a FSM with states IDLE, READ, WRITE, transitioning on "op" and reverting to IDLE every 4 clock cycles?
  • How do you envision coding a HDL FSM with IDLE, READ, WRITE states, changing states on "op" input and returning to IDLE after 4 clock cycles?
  • In what manner would you draft HDL code for a FSM encompassing IDLE, READ, and WRITE, with state changes based on "op" and a 4-cycle reset to IDLE?
  • Can you formulate HDL for a FSM with states IDLE, READ, and WRITE, with transitions governed by "op" and a consistent return to IDLE every 4 cycles?
  • Could you demonstrate writing HDL for a FSM having IDLE, READ, and WRITE states, with transitions triggered by "op" and a 4-clock-cycle reset to IDLE?
  • Write HDL code for a FSM that has 3 states: IDLE, READ, and WRITE. The FSM should transition from IDLE to READ or WRITE based on an input signal called "op", which is 0 for READ and 1 for WRITE. Once in READ or WRITE state, the FSM should transition back to IDLE state after a fixed number of clock cycles (let's say 4 clock cycles).
Try Our AI Interviewer

Prepare for success with realistic, role-specific interview simulations.

Try AI Interview Now

Interview question asked to Design Verification Engineers interviewing at Samsung, Huawei, Autodesk and others: Can you develop HDL code for a 3-state FSM (IDLE, READ, WRITE), with transitions based on the "op" input signal and a 4-clock-cycle return to IDLE?.