Closes #9.
self.o = Signal(width)
self.co = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
with m.If(self.sel == 0b00):
m.d.comb += self.o.eq(self.a | self.b)
m.d.comb += self.o.eq(self.a ^ self.b)
with m.Else():
m.d.comb += Cat(self.o, self.co).eq(self.a - self.b)
- return m.lower(platform)
+ return m
if __name__ == "__main__":
self.b = Signal(width)
self.o = Signal(width)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.comb += self.o.eq(self.a + self.b)
- return m.lower(platform)
+ return m
class Subtractor:
self.b = Signal(width)
self.o = Signal(width)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.comb += self.o.eq(self.a - self.b)
- return m.lower(platform)
+ return m
class ALU:
self.add = Adder(width)
self.sub = Subtractor(width)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.submodules.add = self.add
m.submodules.sub = self.sub
m.d.comb += self.o.eq(self.sub.o)
with m.Else():
m.d.comb += self.o.eq(self.add.o)
- return m.lower(platform)
+ return m
if __name__ == "__main__":
self.v = Signal(factor)
self.o = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.sync += self.v.eq(self.v + 1)
m.d.comb += self.o.eq(self.v[-1])
- return m.lower(platform)
+ return m
if __name__ == "__main__":
ctr = ClockDivisor(factor=16)
- frag = ctr.get_fragment(platform=None)
+ frag = ctr.elaborate(platform=None)
frag.add_domains(ClockDomain("sync", async_reset=True))
main(frag, ports=[ctr.o])
self.v = Signal(width, reset=2**width-1)
self.o = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.sync += self.v.eq(self.v + 1)
m.d.comb += self.o.eq(self.v[-1])
- return m.lower(platform)
+ return m
ctr = Counter(width=16)
self.o = Signal()
self.ce = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.sync += self.v.eq(self.v + 1)
m.d.comb += self.o.eq(self.v[-1])
ctr = Counter(width=16)
-frag = ctr.get_fragment(platform=None)
+frag = ctr.elaborate(platform=None)
# print(rtlil.convert(frag, ports=[ctr.o, ctr.ce]))
print(verilog.convert(frag, ports=[ctr.o, ctr.ce]))
self.ack = Signal()
self.err = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
ctr = Signal(max=self.divisor)
with m.State("ERROR"):
pass
- return m.lower(platform)
+ return m
if __name__ == "__main__":
self.pins = pins
self.bus = bus
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.comb += self.bus.r_data.eq(self.pins[self.bus.addr])
with m.If(self.bus.we):
m.d.sync += self.pins[self.bus.addr].eq(self.bus.w_data)
- return m.lower(platform)
+ return m
if __name__ == "__main__":
self.dat_w = Signal(8)
self.we = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.submodules.cpu = Instance("CPU",
p_RESET_ADDR=0xfff0,
o_d_dat_w=self.dat_w,
i_d_we =self.we,
)
- return m.lower(platform)
+ return m
if __name__ == "__main__":
self.we = Signal()
self.mem = Memory(width=8, depth=16, init=[0xaa, 0x55])
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.submodules.rdport = rdport = self.mem.read_port()
m.submodules.wrport = wrport = self.mem.write_port()
wrport.data.eq(self.dat_w),
wrport.en.eq(self.we),
]
- return m.lower(platform)
+ return m
if __name__ == "__main__":
self.c = Signal(width)
self.o = Signal(width)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
with m.Switch(self.s):
with m.Case("--1"):
m.d.comb += self.o.eq(self.c)
with m.Case():
m.d.comb += self.o.eq(0)
- return m.lower(platform)
+ return m
if __name__ == "__main__":
class Simulator:
def __init__(self, fragment, vcd_file=None, gtkw_file=None, traces=()):
- self._fragment = fragment
+ self._fragment = Fragment.get(fragment, platform=None)
self._signal_slots = SignalDict() # Signal -> int/slot
self._slot_signals = list() # int/slot -> Signal
self._run_called = False
- while not isinstance(self._fragment, Fragment):
- self._fragment = self._fragment.get_fragment(platform=None)
-
@staticmethod
def _check_process(process):
if inspect.isgeneratorfunction(process):
import argparse
+from .hdl.ir import Fragment
from .back import rtlil, verilog, pysim
def main_runner(parser, args, design, platform=None, name="top", ports=()):
if args.action == "generate":
- fragment = design.get_fragment(platform=platform)
+ fragment = Fragment.get(design, platform)
generate_type = args.generate_type
if generate_type is None and args.generate_file:
if args.generate_file.name.endswith(".v"):
print(output)
if args.action == "simulate":
- fragment = design.get_fragment(platform=platform)
+ fragment = Fragment.get(design, platform)
with pysim.Simulator(fragment,
vcd_file=args.vcd_file,
gtkw_file=args.gtkw_file,
@extend(NativeMemory)
@deprecated("it is not necessary or permitted to add Memory as a special or submodule")
-def get_fragment(self, platform):
+def elaborate(self, platform):
return Fragment()
import warnings
+from ...hdl import Fragment
from ...back import verilog
from .conv_output import ConvOutput
# TODO: attr_translate
v_output = verilog.convert(
- fragment=fi.get_fragment().get_fragment(platform=None),
+ fragment=Fragment.get(fi.get_fragment(), platform=None),
name=name,
ports=ios or (),
ensure_sync_exists=create_clock_domains
assert not special_overrides
if hasattr(fragment_or_module, "get_fragment"):
- fragment = fragment_or_module.get_fragment().get_fragment(platform=None)
+ fragment = fragment_or_module.get_fragment()
else:
fragment = fragment_or_module
from contextlib import contextmanager
import warnings
-from ..tools import flatten, bits_for
+from ..tools import flatten, bits_for, deprecated
from .ast import *
from .ir import *
from .xfrm import *
self._statements.append(assign)
def _add_submodule(self, submodule, name=None):
- if not hasattr(submodule, "get_fragment"):
- raise TypeError("Trying to add '{!r}', which does not implement .get_fragment(), as "
- "a submodule".format(submodule))
+ if not hasattr(submodule, "elaborate"):
+ if hasattr(submodule, "get_fragment"): # :deprecated:
+ warnings.warn("Adding '{!r}', which implements .get_fragment() but not "
+ ".elaborate(), as a submodule. .get_fragment() is deprecated, "
+ "and .elaborate() should be provided instead.".format(submodule),
+ DeprecationWarning, stacklevel=2)
+ else:
+ raise TypeError("Trying to add '{!r}', which does not implement .elaborate(), as "
+ "a submodule".format(submodule))
self._submodules.append((submodule, name))
def _add_domain(self, cd):
while self._ctrl_stack:
self._pop_ctrl()
- def lower(self, platform):
+ @deprecated("`m.get_fragment(...)` is deprecated; use `m` instead")
+ def get_fragment(self, platform): # :deprecated:
+ return self.elaborate(platform)
+
+ @deprecated("`m.lower(...)` is deprecated; use `m` instead")
+ def lower(self, platform): # :deprecated:
+ return self.elaborate(platform)
+
+ def elaborate(self, platform):
self._flush()
fragment = Fragment()
for submodule, name in self._submodules:
- fragment.add_subfragment(submodule.get_fragment(platform), name)
+ fragment.add_subfragment(Fragment.get(submodule, platform), name)
statements = SampleDomainInjector("sync")(self._statements)
fragment.add_statements(statements)
for signal, domain in self._driving.items():
fragment.add_domains(self._domains)
fragment.generated.update(self._generated)
return fragment
-
- get_fragment = lower
class Fragment:
+ @staticmethod
+ def get(obj, platform):
+ if isinstance(obj, Fragment):
+ return obj
+ if not hasattr(obj, "elaborate"): # :deprecated:
+ return Fragment.get(obj.get_fragment(platform), platform)
+ return Fragment.get(obj.elaborate(platform), platform)
+
def __init__(self):
self.ports = SignalDict()
self.drivers = OrderedDict()
item, = path
return self.generated[item]
- def get_fragment(self, platform):
+ def elaborate(self, platform):
return self
def _merge_subfragment(self, subfragment):
else:
self.en = Const(1)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
f = Instance("$memrd",
p_MEMID=self.memory,
p_ABITS=self.addr.nbits,
self.en = Signal(memory.width // granularity,
name="{}_w_en".format(memory.name))
- def get_fragment(self, platform):
+ def elaborate(self, platform):
f = Instance("$memwr",
p_MEMID=self.memory,
p_ABITS=self.addr.nbits,
reset=reset, reset_less=True, attrs={"no_retiming": True})
for i in range(n)]
- def get_fragment(self, platform):
+ def elaborate(self, platform):
if hasattr(platform, "get_multi_reg"):
return platform.get_multi_reg(self)
for i, o in zip((self.i, *self._regs), self._regs):
m.d[self.odomain] += o.eq(i)
m.d.comb += self.o.eq(self._regs[-1])
- return m.lower(platform)
+ return m
self.o = Signal(max=max(2, width))
self.n = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
with m.Switch(self.i):
for j in range(self.width):
m.d.comb += self.o.eq(j)
with m.Case():
m.d.comb += self.n.eq(1)
- return m.lower(platform)
+ return m
class PriorityEncoder:
self.o = Signal(max=max(2, width))
self.n = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
for j in reversed(range(self.width)):
with m.If(self.i[j]):
m.d.comb += self.o.eq(j)
m.d.comb += self.n.eq(self.i == 0)
- return m.lower(platform)
+ return m
class Decoder:
self.n = Signal()
self.o = Signal(width)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
with m.Switch(self.i):
for j in range(len(self.o)):
m.d.comb += self.o.eq(1 << j)
with m.If(self.n):
m.d.comb += self.o.eq(0)
- return m.lower(platform)
+ return m
class PriorityDecoder(Decoder):
self.i = Signal(width)
self.o = Signal(width)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.comb += self.o.eq(self.i ^ self.i[1:])
- return m.lower(platform)
+ return m
class GrayDecoder:
self.i = Signal(width)
self.o = Signal(width)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.comb += self.o[-1].eq(self.i[-1])
for i in reversed(range(self.width - 1)):
m.d.comb += self.o[i].eq(self.o[i + 1] ^ self.i[i])
- return m.lower(platform)
+ return m
self.level = Signal(max=depth + 1)
self.replace = Signal()
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.d.comb += [
self.writable.eq(self.level != self.depth),
with m.If(produce < consume):
m.d.comb += Assert(self.level == (self.depth + produce - consume))
- return m.lower(platform)
+ return m
class SyncFIFOBuffered(FIFOInterface):
self.level = Signal(max=depth + 1)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
# Effectively, this queue treats the output register of the non-FWFT inner queue as
m.d.comb += self.level.eq(fifo.level + self.readable)
- return m.lower(platform)
+ return m
class AsyncFIFO(FIFOInterface):
except ValueError as e:
raise ValueError("AsyncFIFO only supports power-of-2 depths") from e
- def get_fragment(self, platform):
+ def elaborate(self, platform):
# The design of this queue is the "style #2" from Clifford E. Cummings' paper "Simulation
# and Synthesis Techniques for Asynchronous FIFO Design":
# http://www.sunburst-design.com/papers/CummingsSNUG2002SJ_FIFO1.pdf
self.dout.eq(rdport.data),
]
- return m.lower(platform)
+ return m
class AsyncFIFOBuffered(FIFOInterface):
def __init__(self, width, depth):
super().__init__(width, depth, fwft=True)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.submodules.unbuffered = fifo = AsyncFIFO(self.width, self.depth - 1)
m.d.comb += \
fifo.re.eq(1)
- return m.lower(platform)
+ return m
def __len__(self):
return len(self.o)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
return Fragment()
def get_tristate(self, io):
self.triple = triple
self.io = io
- def get_fragment(self, platform):
+ def elaborate(self, platform):
if hasattr(platform, "get_tristate"):
return platform.get_tristate(self.triple, self.io)
o_Y=self.io,
)
- f = m.lower(platform)
+ f = m.elaborate(platform)
f.flatten = True
return f
m.d.sync += o1.eq(Past(i))
m.d.pix += o2.eq(Past(i))
m.d.pix += o3.eq(Past(i, domain="sync"))
- f = m.lower(platform=None)
+ f = m.elaborate(platform=None)
self.assertRepr(f.statements, """
(
(eq (sig o1) (sample (sig i) @ sync[1]))
"(sig b)": "sync",
})
- frag = m.lower(platform=None)
+ frag = m.elaborate(platform=None)
fsm = frag.find_generated("fsm")
self.assertIsInstance(fsm.state, Signal)
self.assertEqual(fsm.encoding, OrderedDict({
def test_submodule_wrong(self):
m = Module()
with self.assertRaises(TypeError,
- msg="Trying to add '1', which does not implement .get_fragment(), as a submodule"):
+ msg="Trying to add '1', which does not implement .elaborate(), as a submodule"):
m.submodules.foo = 1
with self.assertRaises(TypeError,
- msg="Trying to add '1', which does not implement .get_fragment(), as a submodule"):
+ msg="Trying to add '1', which does not implement .elaborate(), as a submodule"):
m.submodules += 1
def test_domain_named_implicit(self):
m2.d.sync += self.c3.eq(self.s3)
m1.submodules.foo = m2
- f1 = m1.lower(platform=None)
+ f1 = m1.elaborate(platform=None)
self.assertRepr(f1.statements, """
(
(eq (sig c1) (sig s1))
def setUp_memory(self):
self.m = Memory(width=8, depth=4)
- self.fr = self.m.read_port().get_fragment(platform=None)
- self.fw = self.m.write_port().get_fragment(platform=None)
+ self.fr = self.m.read_port().elaborate(platform=None)
+ self.fw = self.m.write_port().elaborate(platform=None)
self.f1 = Fragment()
self.f2 = Fragment()
self.f2.add_subfragment(self.fr)
self.decoder_cls = decoder_cls
self.coder_args = args
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
enc, dec = self.encoder_cls(*self.coder_args), self.decoder_cls(*self.coder_args)
m.submodules += enc, dec
dec.i.eq(enc.o),
Assert(enc.i == dec.o)
]
- return m.lower(platform)
+ return m
class HammingDistanceSpec:
self.encoder_cls = encoder_cls
self.coder_args = args
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
enc1, enc2 = self.encoder_cls(*self.coder_args), self.encoder_cls(*self.coder_args)
m.submodules += enc1, enc2
Assume(enc1.i + 1 == enc2.i),
Assert(sum(enc1.o ^ enc2.o) == self.distance)
]
- return m.lower(platform)
+ return m
class GrayCoderTestCase(FHDLTestCase):
class FIFOSmokeTestCase(FHDLTestCase):
def assertSyncFIFOWorks(self, fifo, xfrm=lambda x: x):
- with Simulator(xfrm(fifo.get_fragment(None)), vcd_file=open("test.vcd", "w")) as sim:
+ with Simulator(xfrm(Fragment.get(fifo, None)), vcd_file=open("test.vcd", "w")) as sim:
sim.add_clock(1e-6)
def process():
yield from fifo.write(1)
self.replace = Signal()
self.level = Signal(max=self.depth + 1)
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
storage = Memory(self.width, self.depth)
m.d.comb += Assert(ResetSignal(self.rdomain) == ResetSignal(self.wdomain))
- return m.lower(platform)
+ return m
class FIFOModelEquivalenceSpec:
self.rdomain = rdomain
self.wdomain = wdomain
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.submodules.dut = dut = self.fifo
m.submodules.gold = gold = FIFOModel(dut.width, dut.depth, dut.fwft,
Past(dut.re, domain=self.rdomain))
.implies(dut.dout == gold.dout))
- return m.lower(platform)
+ return m
class FIFOContractSpec:
self.wdomain = wdomain
self.bound = bound
- def get_fragment(self, platform):
+ def elaborate(self, platform):
m = Module()
m.submodules.dut = fifo = self.fifo
m.d.comb += Assume(Rose(ClockSignal(self.wdomain)) |
Rose(ClockSignal(self.rdomain)))
- return m.lower(platform)
+ return m
class FIFOFormalCase(FHDLTestCase):
class SimulatorIntegrationTestCase(FHDLTestCase):
@contextmanager
def assertSimulation(self, module, deadline=None):
- with Simulator(module.lower(platform=None)) as sim:
+ with Simulator(module.elaborate(platform=None)) as sim:
yield sim
if deadline is None:
sim.run()
from contextlib import contextmanager
from ..hdl.ast import *
+from ..hdl.ir import *
from ..back import rtlil
mode=mode,
depth=depth,
script=script,
- rtlil=rtlil.convert(spec.get_fragment("formal"))
+ rtlil=rtlil.convert(Fragment.get(spec, platform="formal"))
)
with subprocess.Popen(["sby", "-f", "-d", spec_name], cwd=spec_dir,
universal_newlines=True,