Part of Curriculum: Protocol RTL Design (Module 10: UART & SPI Serial Controllers)
View Full Roadmap →
Level 6 Easy 10 pts ✓ Solved

SPI Mode Selector (CPOL/CPHA)

Protocols & Interfaces · combinational, protocol

Decode SPI's 4 clocking modes from CPOL and CPHA into the two facts a controller actually needs: what the clock idles at, and which edge samples data.

Write a synthesizable Verilog module named top_module matching the ports below exactly.
📐 Circuit Schematic Diagram
cpolcphaidle_clocksample_edgeSPI MODE SELECTORPROTOCOL INTERFACE
⚡ Timing & Synthesizability Constraints

Timing Contract: Strict synchronous timing aligned to posedge clk or zero-delay combinational propagation.

RTL Constraints: Synthesizable Verilog-2001. No # delays in synthesizable RTL.

Concept: CPOL sets the clock's idle level directly (idle_clock=cpol). Which edge samples data is a clean XOR of the two: sample_edge = cpol ^ cpha (0=rising, 1=falling) — a fact worth knowing cold rather than re-deriving from the mode table every time. Swapping that XOR for an AND is a very plausible boolean slip: it happens to agree with the correct answer for modes 0 and... no, it actually gets 3 of the 4 modes wrong, because AND and XOR only ever agree when both inputs are 0.
NameDirWidthDescription
cpolinput1Clock polarity (0=idle low, 1=idle high)
cphainput1Clock phase (0=sample on first edge, 1=sample on second)
idle_clockoutput1Clock's idle level
sample_edgeoutput10=sample on rising edge, 1=sample on falling edge
Expected Waveform (Golden Timing Contract)
Verilog-2001 / SV Saved
Font: |
// Click Run Tests (or press Ctrl+Enter) to compile your RTL and simulate against hidden testbenches.
Simulation Timing Diagram (VCD Waveform)
100%
No simulation waveform yet. Run tests to simulate and generate VCD traces.
No compilation run yet. Syntax warnings and elaboration messages will appear here.
No simulation output yet. Raw stdout from vvp will appear here.

RTL Problems / Level 6 · Protocols & Interfaces

What You Will Learn

By solving SPI Mode Selector (CPOL/CPHA), you will acquire core industry skills:

  • Synthesizable hardware modeling in standard Verilog-2001 / SystemVerilog.
  • Correct handling of interface port contracts, edge-triggered clocks, and active-low/high resets.
  • Simulation waveform interpretation and debugging using timing diagrams (VCD traces).
  • Architectural avoidance of timing violations, glitching hazards, and synthesis warnings.

Problem Statement & Requirements

Decode SPI's 4 clocking modes from CPOL and CPHA into the two facts a controller actually needs: what the clock idles at, and which edge samples data.

Hardware design teams at leading semiconductor firms evaluate these exact port contracts, race-condition assertions, and corner cases during technical design reviews and ASIC engineer interviews.

Concept: CPOL sets the clock's idle level directly (idle_clock=cpol). Which edge samples data is a clean XOR of the two: sample_edge = cpol ^ cpha (0=rising, 1=falling) — a fact worth knowing cold rather than re-deriving from the mode table every time. Swapping that XOR for an AND is a very plausible boolean slip: it happens to agree with the correct answer for modes 0 and... no, it actually gets 3 of the 4 modes wrong, because AND and XOR only ever agree when both inputs are 0.

Port List & Interface Timing

The hidden testbench instantiates top_module. Module port names and bit-widths must match the interface contract exactly:

NameDirWidthDescription
cpolinput1Clock polarity (0=idle low, 1=idle high)
cphainput1Clock phase (0=sample on first edge, 1=sample on second)
idle_clockoutput1Clock's idle level
sample_edgeoutput10=sample on rising edge, 1=sample on falling edge

Timing Constraints & Synthesizable RTL Rules

⚡ Synthesizable Implementation Guidelines

Timing Contract: Strict synchronous timing aligned to posedge clk or zero-delay combinational propagation.

Synthesis Constraints: Synthesizable Verilog-2001. No # delays in synthesizable RTL.

Starter RTL Code

module top_module(
  input  cpol,
  input  cpha,
  output idle_clock,
  output sample_edge
);

  // Your code here — idle_clock = cpol; sample_edge = cpol ^ cpha.

endmodule

Related RTL Problems in Level 6

  • Manchester Encoder — Combine a data bit with the clock to produce a Manchester-encoded line signal — the self-clocking encoding used in classic Ethernet and RFID.
  • Manchester Decoder — Recover the original data bit from a Manchester-encoded line signal — the exact inverse of the Manchester Encoder problem.
  • Quadrature Rotary Encoder Decoder — Decode the two-phase A/B signal from a real rotary knob or mouse wheel into a direction and position count — sample B on every rising edge of A.

Frequently Asked Questions

What does SPI Mode Selector (CPOL/CPHA) require?

Decode SPI's 4 clocking modes from CPOL and CPHA into the two facts a controller actually needs: what the clock idles at, and which edge samples data. Implement it as Verilog module top_module matching the listed ports.

Is this design combinational or sequential?

Tags: combinational, protocol. If a clock signal is present in the port table, implement it as sequential logic; otherwise, use continuous assignments or combinational blocks.

How is my solution graded?

A hidden SystemVerilog testbench simulates your module in the cloud judge. You pass when all directed corner cases match the golden model and the VCD timing waveform matches the contract.

How does this challenge help with semiconductor interviews?

Technical interviewers at NVIDIA, Qualcomm, Apple, and Intel routinely ask candidates to code, debug, and explain the timing waveforms of SPI Mode Selector (CPOL/CPHA) to assess real-world synthesizability and race-condition awareness.