NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

PyHGL: A Python-based Hardware Generation Language Framework

Jintao Sun, Zeke Wang, Tao Lu, Wenzhi ChenarXiv 2023 · DOI

Introduction

Traditional Hardware Description Languages (HDLs) like Verilog and VHDL remain the industry standard for hardware design, yet they are notoriously verbose and ill-suited for the rapid prototyping demands of modern engineering. While existing Hardware Generation Languages (HGLs) such as Chisel (Scala-based) or MyHDL (Python-based) have attempted to bridge this gap, they often suffer from a "semantic gap" between design and simulation, limited expressiveness in asynchronous domains, and a failure to adequately handle the "unknown" (X) state critical for hardware verification.

PyHGL is a new open-source framework that unifies hardware generation, simulation, and verification into a single, "Pythonic" environment. By extending Python’s syntax and introducing an optimized three-state (0, 1, X) simulator, PyHGL allows engineers to write hardware code that is significantly more concise than Verilog while maintaining the simulation accuracy required for complex circuits.

The PyHGL Framework

PyHGL stands out by being a syntactic superset of Python. Rather than acting as a simple library, it utilizes Python’s official parser generator (Pegen) to introduce hardware-specific operators, such as <== for non-blocking assignments, while remaining fully compatible with standard Python code.

Key technical features include:

  • Vectorized Operations: Similar to NumPy, PyHGL supports operations on entire signal arrays without explicit loops, drastically reducing boilerplate code.
  • Automatic Port Deduction: The framework infers module ports during the elaboration phase, removing the need for redundant and error-prone declarations.
  • Unified Environment: Designers can move from configuration to RTL generation and waveform simulation within a single Python script.

Optimized Three-State Simulation

A major contribution of PyHGL is its event-driven simulator that handles 0, 1, and X states. High-level HGLs often ignore the X state to maintain performance, but this risks missing uninitialized registers or bus contentions. PyHGL solves this with a "smart" simulation algorithm.

The PyHGL gate simulation model utilizes X_count and X_changed flags to optimize three-state logic propagation.

As shown in the gate logic model above, the simulator tracks unknown inputs via a counter (X_count). Because most gates in a real design operate on binary values (0 or 1) most of the time, the simulator only triggers expensive three-state logic calculations when an unknown state is actually detected. Otherwise, it defaults to a faster binary simulation, matching the speed of industry-standard tools like Icarus Verilog.

Experimental Results

The authors evaluated PyHGL using real-world benchmarks, including an AES Cipher, the SRV32 RISC-V processor, and the Chisel Tutorial Suite.

Code Density and Productivity

One of the most striking results is the improvement in productivity. For an AES implementation, PyHGL required only 275 lines of code compared to 1,690 in Verilog. On average, PyHGL code is 6.1× denser than traditional RTL and approximately 1.7× denser than Chisel.

Comparison of Lines of Code (LoC) across different hardware description and generation languages for an AES implementation.

Performance and Quality

Beyond conciseness, PyHGL delivers high-quality hardware and high-speed simulation:

  • Hardware Efficiency: The PyHGL-generated AES implementation used fewer FPGA resources (LUTs and FFs) than hand-written Verilog, proving that high-level abstraction does not necessitate efficiency loss.
  • Simulation Speed: The optimized X-propagation algorithm provides a 2.9× speedup over standard three-state simulators. For wide-bit-length operations (e.g., a 4096-bit divider), PyHGL’s simulator was 15× faster than Icarus Verilog, as the gate computation time begins to outweigh the Python interpreter's overhead.

Conclusion

PyHGL successfully demonstrates that hardware design can benefit from the elegance of Python without sacrificing the rigor of three-state simulation or the efficiency of the final RTL. While the CPython interpreter can introduce overhead for very small circuits, the framework's ability to handle complex, wide-bit-length designs with high code density makes it a powerful tool for modern hardware engineers. Future work will focus on deeper integration with formal verification tools and expanding the library of Pythonic hardware primitives.

Summary updated

BibTeX
@misc{sun2023pyhgl,
  author = {Jintao Sun and Zeke Wang and Tao Lu and Wenzhi Chen},
  title = {{PyHGL: A Python-based Hardware Generation Language Framework}},
  year = {2023},
  eprint = {2309.04859},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2309.04859},
}
Figure