Skip to main content

PyHGL

PyHGL is a simple and flexible Python based Hardware Generation Language. PyHGL has rich features including:

  • A few but necessary extended grammars to reduce grammar noises
  • RTL and Gate Level hardware description, generates SystemVerilog files
  • Support any two-valued logic circuit, including multi-clocks and combinational loop
  • Object-oriented modeling, easy to distinguish between synthetic and not synthetic
  • Asynchronous delay accurate event based simulation in pure Python
  • SVA like assertions for verification

Install

python -m pip install pyhgl

Examples

N-bit Adder

from pyhgl.logic import *
import random

@module FullAdder:
    a, b, cin = UInt('0'), UInt('0'), UInt('0')
    s = a ^ b ^ cin 
    cout = a & b | (a ^ b) & cin 

@module AdderN(w: int):
    x = UInt[w](0)
    y = UInt[w](0)
    out = UInt[w](0)

    adders = Array(FullAdder() for _ in range(w))
    adders[:,'a'] <== x.split()
    adders[:,'b'] <== y.split()
    adders[1:, 'cin'] <== adders[:-1, 'cout']
    out <== Cat(adders[:,'s']) 
    cout = adders[-1, 'cout']

#--------------------------------- test ----------------------------------

with Session() as sess:
    w = 8
    mask = (1 << w) - 1 
    dut = AdderN(w)
    sess.track(dut.x, dut.y, dut.out)

    for _ in range(100):
        x = random.randint(0, mask)
        y = random.randint(0, mask)
        setv(dut.x, x) 
        setv(dut.y, y) 
        sess.step(100) 
        out = getv(dut.out)
        print(f'{x} + {y} = {out}\t{(x+y)&mask==out}') 

    sess.dumpVCD('Adder.vcd')
    sess.dumpVerilog('Adder.sv')
    sess.dumpGraph('Adder.gv')
    print(sess)

Vending Machine

@module VendingMachine:
    nickel, dime, valid = UInt('0'), UInt('0'), UInt('0') 
 
    switch s:=EnumReg():
        once 'sIdle':
            when nickel: 
                s <== 's5'
            when dime:
                s <== 's10' 
        once 's5':
            when nickel:
                s <== 's10'
            when dime: 
                s <== 's15'
        once 's10':
            when nickel: 
                s <== 's15'
            when dime: 
                s <== 'sOk' 
        once 's15': 
            when nickel: 
                s <== 'sOk'
            when dime: 
                s <== 'sOk'
        once 'sOk':
            s <== 'sIdle'
            valid <== 1

Metadata

Release files for pyhgl 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pyhgl 0.2.0
File Interpreter ABI Platform
pyhgl-0.2.0-py3-none-any.whl Python 3 none any Details

Release files / pyhgl-0.2.0-py3-none-any.whl

Download URL pyhgl-0.2.0-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d56682c6a649d53fc99487ceb3d8530487cc688cdc3089d1be3d223d9ca6bf6a
BLAKE2b-256 checksum
How to use checksums
df8935f9b4ed4189934679fef834d3b1554c85a886fcb7e17d1355d3ed39f7c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page