amaranth/examples/basic/ctr_en.py

34 lines
846 B
Python
Raw Normal View History

from nmigen import *
from nmigen.back import rtlil, verilog, pysim
2018-12-11 13:50:56 -07:00
class Counter(Elaboratable):
def __init__(self, width):
self.v = Signal(width, reset=2**width-1)
2018-12-11 13:50:56 -07:00
self.o = Signal()
self.en = Signal()
2018-12-11 13:50:56 -07:00
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 EnableInserter(self.en)(m)
2018-12-11 13:50:56 -07:00
2019-01-26 09:25:05 -07:00
ctr = Counter(width=16)
print(verilog.convert(ctr, ports=[ctr.o, ctr.en]))
sim = pysim.Simulator(ctr)
sim.add_clock(1e-6)
def ce_proc():
yield; yield; yield
yield ctr.en.eq(1)
yield; yield; yield
yield ctr.en.eq(0)
yield; yield; yield
yield ctr.en.eq(1)
sim.add_sync_process(ce_proc)
with sim.write_vcd("ctrl.vcd", "ctrl.gtkw", traces=[ctr.en, ctr.v, ctr.o]):
2018-12-14 05:42:39 -07:00
sim.run_until(100e-6, run_passive=True)