展示 HN:SHDL – 一种基于逻辑门构建的极简硬件描述语言
Show HN: SHDL – A minimal hardware description language built from logic gates

原始链接: https://github.com/rafa-rrayes/SHDL

## SHDL:逻辑门电路仿真 SHDL是一种轻量级的硬件描述语言和Python驱动(PySHDL),专为仿真完全由逻辑门构建的数字电路而设计。它提供了一种直观的语法来定义电路,以及一个无缝的Python API来进行交互,从而实现轻松的测试和分析。 主要特性包括:用于快速和可移植仿真的C后端、命令行编译工具,以及对可重用电路组件和常量的支持。用户在`.shdl`文件中定义电路,将其编译为C,然后通过Python使用诸如`poke`(设置输入)、`peek`(读取输出)和`step`(推进仿真)等方法与其交互。 SHDL仍在开发中,欢迎用户通过GitHub issues、讨论、pull requests或直接邮件提供反馈。它需要Python 3.10+和一个GCC或兼容的C编译器。该项目旨在易于使用,并为实验、教育和个人项目提供价值。

## SHDL:一种极简主义硬件描述语言 rafa_rrayes 创建了 SHDL(简单硬件描述语言),这是一种新语言,旨在*理解*数字逻辑最基本层面的原理。与 Verilog 或 VHDL 不同,SHDL 故意避免抽象——没有算术运算符或隐式位宽。相反,电路由逻辑门(与、异或、非等)和导线显式构建,并进行分层组合。 配套的 PySHDL Python 接口允许用户模拟电路、输入值并观察输出。SHDL 编译为 C 以提高速度,但优先考虑语言本身的透明性和简洁性。 该项目旨在成为数字逻辑的学习工具、HDL 实验平台以及可视化硬件构建过程的工具。创建者正在寻求关于语言设计、可用性以及“反抽象”方法对 HDL 的价值的反馈。提供的示例包括全加器、16 位寄存器和 16 位进位加器的实现。 项目 GitHub 地址:[https://github.com/rafa-rrayes/SHDL](https://github.com/rafa-rrayes/SHDL)
相关文章

原文

PyPI Downloads

A lightweight hardware description language and Python driver for digital circuit simulation using exclusively logic gates! SHDL provides an intuitive syntax for defining digital circuits and a clean Python API for interacting with them (PySHDL).

  • 🚀 Simple Syntax - Easy-to-learn hardware description language
  • 🐍 Python Integration - Seamless Python API for circuit simulation
  • C Backend - Compiles to optimized C code for fast simulation and portability
  • 🔧 Command Line Tools - Built-in compiler and utilities
  • 📦 Component Reuse - Import and compose reusable circuit components
  • 🔢 Constants Support - Use named constants for parameterizable designs

We recommend using uv for using PySHDL. If you don't have it installed, you can install it via pip:

1. Define a Circuit (SHDL)

Create a file fullAdder.shdl:

component FullAdder(A, B, Cin) -> (Sum, Cout) {

    x1: XOR; a1: AND;
    x2: XOR; a2: AND;
    o1: OR;

    connect {
        A -> x1.A; B -> x1.B;
        A -> a1.A; B -> a1.B;

        x1.O -> x2.A; Cin -> x2.B;
        x1.O -> a2.A; Cin -> a2.B;
        a1.O -> o1.A; a2.O -> o1.B;

        x2.O -> Sum; o1.O -> Cout;
    }
}
from SHDL import Circuit

# Load and compile the circuit
with Circuit("fullAdder.shdl") as c:
    # Set input values
    c.poke("A", 1)
    c.poke("B", 1)
    c.poke("Cin", 1)
    # Run simulation
    c.step(10)
    # Read output
    result = c.peek("Sum")
print(f"Result: {result}")  # Output: Result: 60

3. Compile from Command Line

# Compile SHDL to C
shdlc adder.shdl -o adder.c

# Compile and build executable
shdlc adder.shdl --optimize 3
shdlc [options] <input.shdl>

Options:
  -o, --output FILE       Output C file (default: <input>.c)
  -I, --include DIR       Add directory to component search path
  -c, --compile-only      Generate C code only, do not compile to binary
  -O, --optimize LEVEL    GCC optimization level 0-3 (default: 3)
Circuit(shdl_file, search_paths=None)

Create a new circuit instance from a SHDL file.

Methods:

  • poke(port_name, value) - Set an input port value
  • peek(port_name) - Read an output port value
  • step(cycles) - Advance simulation by N cycles
  • reset() - Reset circuit to initial state

See the examples/ directory for more complete examples:

  • interacting.py - Basic circuit interaction
  • SHDL_components/ - Reusable component library

For comprehensive documentation, visit our Documentation Site.

GitHub repository: rafa-rrayes/SHDL

  • Python >= 3.10
  • GCC or compatible C compiler (for circuit compilation)

SHDL is still early-stage, and real-world feedback is incredibly valuable. If you try the library—whether for a small experiment, a class assignment, or a personal project—I would love to hear how it went.

Please consider sharing: • What worked well • What felt confusing or missing • Any bugs you hit • Feature ideas • Example circuits you built

You can give feedback in any of the following ways: • Open an Issue: 👉 https://github.com/rafa-rrayes/SHDL/issues • Start a Discussion: 👉 https://github.com/rafa-rrayes/SHDL/discussions • Submit a Pull Request: Improvements, examples, docs, and tests are all welcome. • Send me a message! 👉 [email protected] is my email.

Even a short comment like “Tried it — worked for me” helps guide development. Thank you for trying SHDL!

Rafa Rayes
Email: [email protected] GitHub: rafa-rrayes

联系我们 contact @ memedata.com