Skip to main content

Magia generates Synthesizable SystemVerilog in pythonic syntax

Project description

Magia

Asta e magia ea a căntat.

What is Magia?

Magia generates Synthesizable SystemVerilog in pythonic syntax.

The goal of Magia is

  • To provide a simple and intuitive way to generate HDL code.
  • Take advantage of the Python to produce reconfigurable, reusable HDL code.

What is not Magia?

  • Magia IS NOT a High Level Synthesis (HLS) framework, which compile arbitrary Python code into HDL.
  • Magia DOES NOT support simulation.
    • Major reason is we have reliable and mature simulation tools like
    • We want to focus on the Design Capturing / HDL generation part, instead of rebuilding the whole workflow.

Position.drawio.svg

Project Roadmap

Please refer to the Magia Roadmap.

Installation

pip install magia-hdl

# Install with full dependencies if advanced features are required

pip install magia-hdl[full]

Examples

Magia generates Synthesizable SystemVerilog code with the following command:

Refer the Syntax Documentation for more details.

from magia import Elaborator, Module, Input, Output


# Define a module
class TopLevel(Module):
    def __init__(self, width, **kwargs):
        super().__init__(**kwargs)
        # Define IO ports
        self.io += [
            Input("clk", 1),
            Input("a", width),
            Input("b", width),
            Output("dout", width),
        ]

        # Implement the logic
        clk = self.io.clk
        self.io.dout <<= (self.io.a + self.io.b).reg(clk)


# Specialize the module
top = TopLevel(width=16, name="TopModule")

# Elaborate SystemVerilog code
result = Elaborator.to_string(top)

# Obtain SystemVerilog code of the top module
sv_code_of_top = Elaborator.to_dict(top)["TopModule"]

# Write SystemVerilog code to a directory
Elaborator.to_files("/tmp/output_dir", top)

Simulation with cocotb

Although Magia does not support simulation, we can use cocotb to simulate the generated code.

Make sure you have installed cocotb and simulator required (e.g. verilator).

import cocotb
from cocotb.runner import get_runner
from magia import Elaborator, Module
from pathlib import Path

TOP_LEVEL_NAME = "TopLevel"
OUTPUT_FILE = "TopLevel.sv"


# Define a module
class TopLevel(Module):
    ...


# Define your test
@cocotb.test()
async def test_smoke(dut):
    ...


if __name__ == "__main__":
    # Elaborate SystemVerilog code to a file
    Elaborator.to_file(OUTPUT_FILE, TopLevel(width=16, name=TOP_LEVEL_NAME))

    runner = get_runner("verilator")
    runner.build(
        verilog_sources=[OUTPUT_FILE],
        hdl_toplevel=TOP_LEVEL_NAME,
        always=True,
    )
    runner.test(
        hdl_toplevel=TOP_LEVEL_NAME,
        testcase="test_smoke",

        # Let cocotb locates this file
        test_dir=Path(__file__).parent.absolute(),
        test_module=Path(__file__).stem,
    )

Documentation

Related Projects

  • Magia Flow: Design flow integration and automation with Magia.
  • Magia IP: IP libraries designed with Magia.

Contributing

The project is still a personal project, in a very early stage. Feel free to open an issue for any bug / feature wishlist.

We also have a Contribution Guideline and Code of Conduct. Please take a look before you contribute.

In case you are interested in this project, contact me via: https://github.com/khwong-c

Reference

There are many attempts to generate HDL code in Python. Similar projects are listed below:

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

magia_hdl-0.5.0.tar.gz (30.9 kB view details)

Uploaded Source

Built Distribution

magia_hdl-0.5.0-py3-none-any.whl (34.3 kB view details)

Uploaded Python 3

File details

Details for the file magia_hdl-0.5.0.tar.gz.

File metadata

  • Download URL: magia_hdl-0.5.0.tar.gz
  • Upload date:
  • Size: 30.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for magia_hdl-0.5.0.tar.gz
Algorithm Hash digest
SHA256 4b95124c3b4d90f13e2148456d4f93753d1afa13810f6f03e50ffa2d3c1caa63
MD5 cd5f71d443543475d674f0f47134731b
BLAKE2b-256 5f63caa49d900e5daaa0092557d91dc09f5d8c1fb903efe7da83576bd49242bf

See more details on using hashes here.

File details

Details for the file magia_hdl-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: magia_hdl-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 34.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for magia_hdl-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0193aa571d1a7a7ed5d3dfb7da2c214b5e5a7f498b29fe0f7c856d11d9c9dc44
MD5 d7fc60fcd6d7e5094f4845291c62e02c
BLAKE2b-256 e6311192288c8b88bcc43246a7aed467a005797c92bfc491fd60b5f41468376b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page