Skip to main content

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.

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!!!

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! snakeHDL is based on a RISC-type philosophy. 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.)

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

snakehdl-0.2.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

snakehdl-0.2.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file snakehdl-0.2.0.tar.gz.

File metadata

  • Download URL: snakehdl-0.2.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for snakehdl-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3232a7ad6e3e5a246ed4294fdeea0b0d48d192e0775d9610fe71fe168eb26ba7
MD5 10e37d8a9040187b1110769a03bbb8c8
BLAKE2b-256 8fe7d891aa1493d7dbb54a4b585d02f7410aaef346d465bfda7807f6693555a0

See more details on using hashes here.

File details

Details for the file snakehdl-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: snakehdl-0.2.0-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

Hashes for snakehdl-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9b40899e1f0b1076113b1df46037c92e4692fb3c93761d42a50c5be78aa8c38
MD5 777c6804c8d73153bfec2c03c8fdc3d6
BLAKE2b-256 2167174eeee7bb255b8b98463edb177ce379ee949a7553d734dcd1b04ecf05a0

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