Magma-lang

3.0.2 · active · verified Fri Apr 17

Magma-lang is an embedded domain-specific language (eDSL) in Python for designing and generating hardware circuits. It enables hierarchical and compositional circuit design, targeting various hardware description languages like Verilog. The current version is 3.0.2, with active development incorporating community feedback and feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This example defines a basic 2-input AND gate using Magma's class-based circuit definition and then simulates its behavior using the PythonSimulator.

import magma
from magma import Circuit, In, Out, Bit
from magma.simulator import PythonSimulator

# Define a simple 2-input AND gate circuit
class And2(Circuit):
    name = "And2"
    io = In(a=Bit, b=Bit), Out(o=Bit)
    def definition(self):
        # Use the @= operator for wiring
        self.o @= self.a & self.b

# Instantiate the circuit
and_gate = And2()

# Print a representation of the circuit
print(f"Defined circuit: {and_gate.name}")

# Simulate the circuit
simulator = PythonSimulator(and_gate)

# Test case: 1 AND 0
simulator.set_value(and_gate.a, 1)
simulator.set_value(and_gate.b, 0)
simulator.evaluate()
result_1_0 = simulator.get_value(and_gate.o)
print(f"1 AND 0 = {result_1_0}") # Expected: 0

# Test case: 1 AND 1
simulator.set_value(and_gate.a, 1)
simulator.set_value(and_gate.b, 1)
simulator.evaluate()
result_1_1 = simulator.get_value(and_gate.o)
print(f"1 AND 1 = {result_1_1}") # Expected: 1

view raw JSON →