A simple, purely-functional HDL for Python
Project description
snakeHDL: A simple, purely-functional HDL for Python
snakeHDL is a tool for creating digital logic circuits with a focus on simplicity and accessibility. This project is intended to be a fun and easy way for anyone to design real hardware with a few lines of Python. Compile your circuit to Verilog, VHDL, or a dill-pickled Python function!
snakeHDL is Free Software licensed under the terms of the MIT License. The project is still early in development, so bugs and general instability may occur.
Join us on Discord!
The 16-bit HACK ALU from "The Elements of Computing Systems" by Nisan and Schocken implemented in snakeHDL, compiled to VHDL, and imported to Logisim. See examples/HACK_ALU.py
Introduction
snakeHDL has two main components: an API for expressing abstract trees of boolean logic, and an optimizing compiler that translates these abstract logic trees into logic circuits. The compiler handles hardware-specific concerns, so you can focus purely on your circuit's logic.
In short, snakeHDL lets you express what your circuit should do, instead of how it should do it.
$ pip install snakehdl
$ python3
>>> from snakehdl import input_bits, output, xor
>>> out = output(res=xor(input_bits('a'), input_bits('b')))
This creates a simple BOp tree representing a circuit with one output named res that is the XOR of two 1-bit inputs named a and b.
BOps are naturally composable into larger circuits because they are lazily evaluated. When you create a tree of BOps, nothing actually happens until you compile it:
>>> from snakehdl.compilers import VerilogCompiler
>>> VerilogCompiler(out, name='xor_ab').compile().save('xor_ab.v')
We can build composite logical structures like adders, multiplexers, and even full ALUs starting from these fundamental BOps. Output bit widths are automatically inferred based on the tree structure at compile time.
Since only twelve primitive BOps are specified by snakeHDL, it is straightforward to create new compiler backends.
We will use snakeHDL to implement the ALU and control logic for the Snake Processing Unit, and then combine this with the necessary sequential elements (registers, stack, RAM) to make the snakePU a mega-fast Python coprocessor chip.
Binary Operations (BOps)
The following binary operations are specified by the snakeHDL API and must be implemented in hardware (or simulated hardware) by the compiler backends:
Combinational Operations
- AND
- NAND
- OR
- NOR
- XOR
- XNOR
- NOT (unary)
I/O Operations
- CONST
- INPUT
- OUTPUT
- BIT
- JOIN
...and that's it! Check out the BOp documentation to learn more or look at the examples to see BOps in action.
Compiler Targets
- Verilog
- VHDL
- Python - compile your circuit to a pickled Python function that accepts your named inputs as kwargs and returns the result as a dict of your named outputs. Useful for automated logic testing.
- Arduino
- Minecraft Redstone
- ...
Optimizations
The following is a list of current and planned compiler optimizations:
- Cached BOp hashing
- Common Subexpression Elimination
- Constant folding (i.e.
A & 0 -> 0) - Gate pruning (i.e.
AND(A, AND(A, B)) -> AND(A, B)) - ...
TODO
- Improve documentation
- Add more components to component library
- Add useful examples demonstrating snakeHDL functionality
- Optimize compiler output (constant folding, gate pruning, etc.)
- MAKE THE snakePU REAL
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file snakehdl-0.2.1.tar.gz.
File metadata
- Download URL: snakehdl-0.2.1.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4c0e412a677b178857aefbcc054dc00767b707733ef47093dd2158276d54981
|
|
| MD5 |
8a0e534a85dcf75666cc833e7ef991e1
|
|
| BLAKE2b-256 |
c1b078cda13368030823b82a27a5e00df6d7aeae218d88118faa6aeeda3e1ff6
|
File details
Details for the file snakehdl-0.2.1-py3-none-any.whl.
File metadata
- Download URL: snakehdl-0.2.1-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8866850353b57268e4d7ea80e50031367db3cb50ef8817d0d2873d1800137011
|
|
| MD5 |
da3eda84557e64114e22d479e763999e
|
|
| BLAKE2b-256 |
56d6f7caf7044b9947b893425a1184cadc69a488c997a2c9fa4e454198b2bc30
|