This repository contains a fully parameterized and pipelined SystemVerilog implementation of a Bitonic Sorting Network.
Unlike a purely combinational approach, this V2.0 architecture utilizes synchronous pipeline registers between sorting stages to achieve higher throughput, shorter critical paths, and better timing closure. Built with dynamic generate blocks and $clog2 logic, the RTL automatically scales to instantiate the required hardware for any power-of-two inputs.
- Full Parameterization: The network dynamically scales using
NUM_INPUTSandDATA_WIDTHtop-level parameters. - Synchronous Pipelining:
always_ffregisters isolate every comparison stage, allowing the network to process a new set of data every single clock cycle after the initial latency. - Verified Scalability: The default repository simulation demonstrates a 4-input, 32-bit configuration operating with a verified 3-clock-cycle latency.
design.sv: The main SystemVerilog design file. It consists of:sorting_network: The top-level parameterized module that uses nestedgenerateloops to automatically wire Compare-and-Swap (CAS) units and pipeline registers based on the requestedNUM_INPUTS.cas_unit: The foundational purely combinational Compare-and-Swap logic block.
testbench.sv: A clock-driven testbench that applies test vectors to the network, accounts for pipeline latency, and verifies the sorted 32-bit outputs.waveform.pdf&result.pdf: Exported simulation waveforms and logs verifying the concurrent sorting operation and the exact 3-cycle propagation delay fromintoout.
- Data-Independent Control Flow: The sequence of comparisons is exactly the same regardless of the input data. There are no conditional branches, making this algorithm perfectly suited for pipelined ASICs and FPGAs.
- Massive Parallelism: Multiple comparators operate simultaneously within the same clock cycle across different pairs of data paths.
- RTL Scalability: While a synthesized bitstream has a fixed hardware size, this SystemVerilog RTL can generate a sorting network for any 2^n inputs without rewriting the underlying logic.
The fundamental combinational building block of the network.
- Inputs: Two parameterized N-bit binary numbers (
AandB). - Outputs: Two N-bit binary lines labeled
highandlow. - Logic: Uses continuous assignment to evaluate
A > B. Based on the boolean result, a multiplexer routes the larger value tohighand the smaller value tolow.
This module handles the structural generation and synchronous data movement.
- Automatic Scaling: Calculates the required number of stages using
(M * (M + 1)) / 2whereM = $clog2(NUM_INPUTS). - Pipeline Registers: Instantiates a 2D array of flip-flops (
pipeline_regs) to hold data between combinational stages. - Generate Blocks: Uses recursive
forloops to instantiate and wire the correct ascending/descending CAS units into thecomb_wiresarrays for each specific stage of the bitonic sequence.
- Latency (Depth): Measured in clock cycles. A 4-input network has a 3-cycle latency. An 8-input network has a 6-cycle latency.
- Throughput: 1 fully sorted array per clock cycle (post-latency).
- Area (Size): Scales dynamically. The architecture dictates the exact number of comparators and D-flip-flops synthesized based on the
NUM_INPUTSparameter.
Pipelined sorting networks are highly efficient hardware accelerators primarily applied in:
- Network Routers: For high-speed packet scheduling and QoS prioritization.
- Digital Signal Processing (DSP): For non-linear median filtering.
- Database Accelerators: For rapidly sorting keys in memory arrays.
You can run and verify this design using standard SystemVerilog simulators such as ModelSim, Xilinx Vivado, Icarus Verilog, or EDA Playground.
- Create a new project in your simulator.
- Compile both
design.svandtestbench.sv. - Ensure your simulator is configured to support SystemVerilog-2012 (for
always_ff,always_comb, and multi-dimensional arrays). - Run the simulation. The testbench will generate the clock signal and drive the pipeline automatically.