Skip to main content

a Python-based Hardware Generation Language

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

pyhgl-0.2.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyhgl-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pyhgl-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d56682c6a649d53fc99487ceb3d8530487cc688cdc3089d1be3d223d9ca6bf6a
MD5 d1c32f282fe68ea9467719ac184a5fb8
BLAKE2b-256 df8935f9b4ed4189934679fef834d3b1554c85a886fcb7e17d1355d3ed39f7c2

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