2021-12-09 22:39:50 -07:00
|
|
|
from amaranth import *
|
|
|
|
from amaranth.sim import *
|
|
|
|
from amaranth.back import verilog
|
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
|
|
|
|
sim: split into base, core, and engines.
Before this commit, each simulation engine (which is only pysim at
the moment, but also cxxsim soon) was a subclass of SimulatorCore,
and every simulation engine module would essentially duplicate
the complete structure of a simulator, with code partially shared.
This was a really bad idea: it was inconvenient to use, with
downstream code having to branch between e.g. PySettle and CxxSettle;
it had no well-defined external interface; it had multiple virtually
identical entry points; and it had no separation between simulation
algorithms and glue code.
This commit completely rearranges simulation code.
1. sim._base defines internal simulation interfaces. The clarity of
these internal interfaces is important because simulation
engines mix and match components to provide a consistent API
regardless of the chosen engine.
2. sim.core defines the external simulation interface: the commands
and the simulator facade. The facade provides a single entry
point and, when possible, validates or lowers user input.
It also imports built-in simulation engines by their symbolic
name, avoiding eager imports of pyvcd or ctypes.
3. sim.xxxsim (currently, only sim.pysim) defines the simulator
implementation: time and state management, process scheduling,
and waveform dumping.
The new simulator structure has none of the downsides of the old one.
See #324.
2020-08-27 04:17:02 -06:00
|
|
|
sim = Simulator(ctr)
|
2019-11-22 01:32:41 -07:00
|
|
|
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)
|