2018-12-15 07:20:10 -07:00
|
|
|
from nmigen import *
|
2018-12-13 11:17:58 -07:00
|
|
|
from nmigen.back import rtlil, verilog, pysim
|
2018-12-11 13:50:56 -07:00
|
|
|
|
|
|
|
|
2019-04-21 02:52:57 -06:00
|
|
|
class Counter(Elaboratable):
|
2018-12-15 13:42:52 -07:00
|
|
|
def __init__(self, width):
|
|
|
|
self.v = Signal(width, reset=2**width-1)
|
2018-12-11 13:50:56 -07:00
|
|
|
self.o = Signal()
|
2019-08-12 07:37:18 -06:00
|
|
|
self.en = Signal()
|
2018-12-11 13:50:56 -07:00
|
|
|
|
2019-01-25 19:31:12 -07:00
|
|
|
def elaborate(self, platform):
|
2018-12-12 05:38:24 -07:00
|
|
|
m = Module()
|
|
|
|
m.d.sync += self.v.eq(self.v + 1)
|
|
|
|
m.d.comb += self.o.eq(self.v[-1])
|
2019-08-12 07:37:18 -06:00
|
|
|
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)
|
2018-12-13 11:17:58 -07:00
|
|
|
|
2019-08-12 07:37:18 -06:00
|
|
|
print(verilog.convert(ctr, ports=[ctr.o, ctr.en]))
|
2018-12-13 11:17:58 -07:00
|
|
|
|
2019-01-26 09:25:05 -07:00
|
|
|
with pysim.Simulator(ctr,
|
2018-12-14 05:42:39 -07:00
|
|
|
vcd_file=open("ctrl.vcd", "w"),
|
|
|
|
gtkw_file=open("ctrl.gtkw", "w"),
|
2019-08-12 07:37:18 -06:00
|
|
|
traces=[ctr.en, ctr.v, ctr.o]) as sim:
|
2018-12-14 05:42:39 -07:00
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def ce_proc():
|
|
|
|
yield; yield; yield
|
2019-08-12 07:37:18 -06:00
|
|
|
yield ctr.en.eq(1)
|
2018-12-14 05:42:39 -07:00
|
|
|
yield; yield; yield
|
2019-08-12 07:37:18 -06:00
|
|
|
yield ctr.en.eq(0)
|
2018-12-14 05:42:39 -07:00
|
|
|
yield; yield; yield
|
2019-08-12 07:37:18 -06:00
|
|
|
yield ctr.en.eq(1)
|
2018-12-14 05:42:39 -07:00
|
|
|
sim.add_sync_process(ce_proc())
|
|
|
|
sim.run_until(100e-6, run_passive=True)
|