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.
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.
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 mThe 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.