Interface Protocols
SPI Mode-0 Host (8-Bit Frames)
SPI host/controller operating in CPOL=0 CPHA=0 with 8-bit shift frames.
About this block
Simulate it in the Verilog IDE
SPI Mode-0 Host (8-Bit Frames) is a Verilog reference block in the Interface Protocols catalog. UART, SPI, I2C, and other serial or peripheral protocol interfaces. Marked Hard: a larger reference. Simulate the packaged testbench before you change the RTL.
A lightweight, parameterizable SPI host controller implementing Mode 0 (CPOL=0, CPHA=0). This IP core facilitates 8-bit data transfers between a master and slave device, generating the necessary SCK signal from a system clock.
This IP provides a robust, synchronous SPI host interface suitable for basic peripheral communication. It features a parameterizable clock divider for flexible speed control, a clear state machine architecture, and includes a self-checking testbench for immediate verification. The design is optimized for simplicity and reliability in low-to-medium throughput applications.
- Choose Open in IDE. RTL Studio loads the packaged workspace for SPI Mode-0 Host (8-Bit Frames).
- Read the Verilog module in the editor. The catalog line for this block: SPI host/controller operating in CPOL=0 CPHA=0 with 8-bit shift frames.
- Run the packaged testbench with the in-browser Verilator simulator, then open the VCD in the waveform viewer.
- Run Yosys in the same IDE for a generic netlist. Sky130 liberty mapping is a separate Pro library option.
RTL Studio is the Verilog simulator and IDE around this block: Slang lint, Verilator, VCD waveforms, and Yosys, in the browser. Simulation guide · Yosys guide.
Features
- SPI Mode 0 Support: Implements standard SPI Mode 0 (Idle Low, Sample on Rising Edge).
- 8-Bit Data Width: Transfers 8-bit data words MSB-first.
- Parameterizable Clock: Configurable
CLK_DIVparameter to adjust SCK frequency relative toclk. - Synchronous Design: Uses a synchronous state machine for reliable timing and easy integration.
- Verified: Includes a comprehensive self-checking testbench with waveform dump support.
Ports
| Port Name | Direction | Width | Description |
|---|---|---|---|
clk | Input | 1 | System clock input |
rst_n | Input | 1 | Active-low asynchronous reset |
start | Input | 1 | Start transfer pulse (single cycle) |
tx_data | Input | 8 | Data to transmit |
done | Output | 1 | Transfer complete flag (high during DONE state) |
rx_data | Output | 8 | Received data |
sck | Output | 1 | SPI clock output |
mosi | Output | 1 | Master Out Slave In |
miso | Input | 1 | Master In Slave Out |
Architecture
The design is composed of three main blocks:
- Clock Divider: Generates the SPI bit clock (
sck) from the system clock (clk) based on theCLK_DIVparameter. - State Machine: Controls the transfer sequence through three states:
IDLE,SHIFT, andDONE_S. - Shift Register Logic: Handles data loading, shifting out on
mosi, and sampling frommiso.
### Data Flow
- Data is loaded into the shift register when
startis asserted. - Bits are shifted out MSB-first on
mosi. - Bits are sampled from
misoon the rising edge ofsck. - The
donesignal asserts when the transfer is complete.
Verification
The included testbench (tb_simple_spi_host.v) verifies functional correctness.
### Test Cases
| Test Case | TX Data | MISO Data | Expected RX | Result |
|---|---|---|---|---|
| TC1 | 0x55 | 0x55 | 0x55 | PASS |
| TC2 | 0xAA | 0xAA | 0xAA | PASS |
| TC3 | 0x00 | 0x00 | 0x00 | PASS |
| TC4 | 0xFF | 0xFF | 0xFF | PASS |
| TC5 | 0xAB | 0xAB | 0xAB | PASS |
| TC6 | 0x00 | 0xFF | 0xFF | PASS |
| TC7 | 0xFF | 0x00 | 0x00 | PASS |
### Running Simulation
- Run the simulation in your preferred EDA tool.
- Check the log output for
INFO: PASS. - Inspect
sim/dump.vcdusing the waveform viewer for timing analysis.
### Expected Log Output
Integration
- Chip Select: This IP does not handle chip select (CS); you must manage CS externally to ensure the slave is selected during the transfer.
- Timing Constraints: Ensure setup and hold times for the external
misosignal relative tosckare met by the slave device. - Reset Synchronization: The
rst_nis asynchronous. Ensure your system reset logic synchronizesrst_nif crossing clock domains. - Start Signal: The
startsignal should be a single-cycle pulse. If held high, the controller may initiate multiple transfers depending on implementation details; it is recommended to de-assertstartimmediately after assertion.
More in Interface Protocols
- 1-Bit GPIO Pin Driver (Combinational) — L1 Upgrade — L2 Upgrade
- 32-Bit MMIO GPIO (DI Read, Byte-Strobed DO) — L2 Upgrade
- 8-Bit GPIO Output Register
- 8-Bit GPIO Set/Clear Register
- 8-Bit GPIO with Direction and Rising-Edge IRQ
- I2C Arbitration Lost Detector
- I2C Bus Idle Timeout
- I2C Clock Stretch Detector
- I2C NACK Sticky Flag Detector
- I2C Start/Stop Detector
- JTAG TAP Controller
- Lightweight I²C Master Controller (Standard Mode)
- Lightweight I²C Slave Controller (Standard Mode)
- SPI Master Lite
- SPI Mode-3 Slave (CPOL=1, CPHA=1) 8-Bit
- SPI Slave Controller (Mode 0)