Skip to content
systemcoding.dev
Hardware

Define an Amaranth Elaboratable with an elaborate method

Subclass Elaboratable in Amaranth 0.5, declare ports in __init__ and build the hardware in elaborate(), which receives the platform or None.

1 min read
Summarize in AI

In Amaranth, a hardware block is a Python class that derives from Elaboratable. The constructor declares the interface signals, and the elaborate method builds the logic and returns a Module.

blinky.pypython
from amaranth import *

class Blinky(Elaboratable):
    def __init__(self):
        self.led = Signal()

    def elaborate(self, platform):
        m = Module()
        counter = Signal(24)
        m.d.sync += counter.eq(counter + 1)
        m.d.comb += self.led.eq(counter[-1])
        return m

The split matters. __init__ runs when the object is created, so ports exist before anything is connected, and a parent can wire blinky.led to a pin without elaborating the child first. elaborate runs later, when the design is converted or simulated, and may run more than once.

The platform argument is the board when you build for hardware and None in simulation or conversion to Verilog, so code that touches the platform needs an if platform is not None guard.

Python in elaborate runs at build time and only describes hardware. A Python for loop there unrolls into repeated logic, and a Python if on a plain integer is evaluated once, not in the circuit. Use with m.If(...) for a condition the hardware tests at run time.

This is the Amaranth 0.5 style. Newer code often uses wiring.Component, but the base class remains.

Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.