Skip to main content

A Python-based HDL design prototyping framework for circuit simulation and verification

Project description

HDLproto

HDLproto is a lightweight, pre-RTL simulator that runs on pure Python. With zero external dependencies, it lets you quickly validate signal timing and control logic during the early design stages (from spec to architecture).

  • Intuitive API: express HDL design rules directly using @always_ff / @always_comb and .r / .w
  • Event-driven safety: detect rule violations, multiple drivers, and non-convergent combinational logic via exceptions
  • Easy to adopt: runs with Python only — great for learning, teaching, and rapid prototyping

Requirements

  • Python 3.10+

Installation

From PyPI:

pip install hdlproto

Development install

git clone https://github.com/shntn/hdlproto.git
cd hdlproto
pip install -e .

Tip (running examples from the repo without installing): PYTHONPATH=. python example/ex_module.py

Quick Start (minimal example)

Save the script below as quickstart.py and run it.

from hdlproto import *

class Counter(Module):
    def __init__(self, en, out):
        self.en = Input(en)
        self.out = Output(out)
        self.cnt = Reg(init=0, width=4)
        self.cnt_next = Wire(init=0, width=4)
        super().__init__()

    @always_ff
    def seq(self, reset):
        if reset:
            self.cnt.r = 0
        elif self.en.w:
            self.cnt.r = self.cnt_next.w

    @always_comb
    def comb(self):
        self.cnt_next.w = (self.cnt.r + 1) % 16
        self.out.w = self.cnt.r

class TbCounter(TestBench):
    def __init__(self):
        self.en = Wire(init=1)
        self.out = Wire(init=0, width=4)
        self.dut = Counter(self.en, self.out)
        super().__init__()

    @testcase
    def run(self, simulator):
        for i in range(6):
            if i == 3:  # stop increment mid-way
                self.en.w = 0
            simulator.clock()
            print(f"cycle={i}, out={self.out.w}")

if __name__ == "__main__":
    sim = Simulator(SimConfig(), TbCounter())
    sim.reset()
    sim.testcase("run")

What happens:

  • Within one clock, the simulator evaluates @always_ff (register updates) → @always_comb (wire/outputs)
  • Dropping en to 0 at i == 3 stops the counter as expected

Design Rules (important)

  • @always_ff: only write to Reg via .r (writing Wire/Input/Output here is invalid)
  • @always_comb: only drive Wire/Output via .w (writing Reg here is invalid)
  • Convergence loop: @always_comb re-evaluates until signals stabilize; non-convergence raises an exception

Key Exceptions

  • SignalInvalidAccess: phase violation (e.g., writing Reg in always_comb, writing Wire in always_ff)
  • SignalWriteConflict: multiple always_* blocks drive the same signal
  • SignalUnstableError: combinational logic failed to stabilize within the allowed iterations (possible feedback loop)

Included Examples

  • example/ex_module.py: a slightly richer starter example
  • example/ex_sap1.py: a SAP-1 implementation, great for pre-RTL exploration
  • example/ex_exception.py: small scripts that intentionally raise the main exceptions

Lisence

This project is licensed under the MIT License, see the LICENSE.txt file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hdlproto-0.1.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hdlproto-0.1.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file hdlproto-0.1.0.tar.gz.

File metadata

  • Download URL: hdlproto-0.1.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0rc1

File hashes

Hashes for hdlproto-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0a3d42d13a899a0e6c74f08a470163e12df2a9b02e21354d0c210647fd103fb1
MD5 dd8dc8e39924e2e49fa9bccc746a674a
BLAKE2b-256 c71c278f50e6a5b5e04d7f843cc3d2504249aaf31f01e28d6690bcd201d1a986

See more details on using hashes here.

File details

Details for the file hdlproto-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hdlproto-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0rc1

File hashes

Hashes for hdlproto-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71d640b1130f979a8705f2ad710bd995e684f2eff58be2f04729e169a0ef29e5
MD5 24e4c105c4d977ebbed0e994118c1723
BLAKE2b-256 9be2cfcf5a1518c4b34a13c4607733efa603b19de92be8c22c17773c500a95d8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page