A simple, purely-functional HDL for Python
Project description
snakeHDL: A simple, purely-functional HDL for Python
snakeHDL is a tool for creating logic circuits with a focus on simplicity and accessibility. The goal is not to compete with the industry heavyweights, but to give hackers, makers, and educators a fun and easy way to build hardware with a few lines of Python.
Introduction
snakeHDL compiles trees of primitive binary operations into logic circuits with named inputs and outputs:
$ pip install snakehdl
$ python3
>>> from snakehdl import *
>>> in_a, in_b = input_bits('a'), input_bits('b')
>>> outputs = output(out_xor=xor(in_a, in_b), out_and=conj(in_a, in_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.compiler import PythonCompiler
>>> pickled_func = PythonCompiler().compile(outputs).data
>>> import dill
>>> func = dill.loads(pickled_func)
>>> func(a=1, b=1)
{'out_xor': np.uint64(0), 'out_and': np.uint64(1)}
We can build composite logical structures like adders, multiplexers, and even full ALUs starting from these fundamental BOps. Bit widths throughout the tree are automatically inferred from input widths at compile time.
snakeHDL abstracts hardware-specific concerns away into the compiler backends, leaving you to focus on implementing the pure logic of your circuit.
Since only twelve primitive BOps are specified by snakeHDL, it is straightforward to create compiler backends for new target platforms.
Wanna use this to implement a Python bytecode interpreter on an FPGA and then make Snakeware 2 without Linux? Let's build the SNAKE PROCESSOR!!!
Compiler Targets
- 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.
- Logisim Evolution 3.9.0 .circ files
- Verilog
- Arduino
- OpenCL kernels
- FPGAs+ASICs
- Minecraft Redstone
- ...
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:
I/O Operations
- CONST -
const_bits(val: np.uint | int, bits: int=1) -> BOp - INPUT -
input_bits(name: str, bits: int=1) -> BOp - OUTPUT -
output(**kwargs: BOp) -> BOp - BIT -
bit(src: BOp, index: int) -> BOp - JOIN -
join(*args: BOp) -> BOp
At compile time, the root of the tree must be an OUTPUT node, and this node's named outputs will be your circuit's outputs. Any INPUT nodes will be treated as your circuit's named inputs.
The BIT operation is used to select one bit from an n-bit signal.
The JOIN operation is used to combine n 1-bit signals into one n-bit signal.
Combinational Operations
- NOT -
neg(a: BOp) -> BOp - AND -
conj(a: BOp, b: BOp) -> BOp - NAND -
nand(a: BOp, b: BOp) -> BOp - OR -
disj(a: BOp, b: BOp) -> BOp - NOR -
nor(a: BOp, b: BOp) -> BOp - XOR -
xor(a: BOp, b: BOp) -> BOp - XNOR -
xnor(a: BOp, b: BOp) -> BOp
...and that's it! See examples/ for demonstrations of these operations. Sequential logic is not supported by snakeHDL, but sequential elements could be considered for a future expansion.
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.1.0.tar.gz.
File metadata
- Download URL: snakehdl-0.1.0.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
008144afe6c5c6d705c2c8e3243ea339803e7a853bbb1b2e628f380055c17056
|
|
| MD5 |
35cd6f0761c13264a0c6dcc2326fb102
|
|
| BLAKE2b-256 |
f0f64f5052a9ab0f39774e305bde4a42704dce4461c92def9d7bf2a83bf146c8
|
File details
Details for the file snakehdl-0.1.0-py3-none-any.whl.
File metadata
- Download URL: snakehdl-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.3 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 |
0ca5620ea30e06d41782d0371c09c17bb12983ddf0f19ccd1e38fc6a37171fc7
|
|
| MD5 |
b30ebe9a2ca0b8c38fcfb72f27d3757a
|
|
| BLAKE2b-256 |
45e3a019cb80926254d01603c61b5c7290c29731d25d39ae2e21b9693f3463e3
|