Skip to content

Instantly share code, notes, and snippets.

@nevercast
Created July 10, 2020 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevercast/982b7243adcc33ce2d025c0b6f90444e to your computer and use it in GitHub Desktop.
Save nevercast/982b7243adcc33ce2d025c0b6f90444e to your computer and use it in GitHub Desktop.
from sys import argv
from typing import Callable, List
from nmigen.build.plat import Platform
from nmigen.hdl.ir import Elaboratable
from nmigen.back.pysim import Simulator
def sim_or_program(top: Elaboratable, platform: Platform, clock:int=1E-6, sync:List[Callable]=None) -> None:
_ = argv.pop(0) # executable
if '--program' in argv:
print("Programming...")
platform.build(top, do_program=True)
elif '--build' in argv:
print("Building only...")
platform.build(top, do_program=False)
else: # sim
print("Simulating...")
sim = Simulator(top)
sim.add_clock(clock)
def sync_defer_gen(dut, entrypoint):
def deferred():
yield from entrypoint(dut)
return deferred
for sync_process in sync:
sim.add_sync_process(sync_defer_gen(dut=top, entrypoint=sync_process))
with sim.write_vcd("dump.vcd", "dump.gtkw"):
sim.run_until(1E-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment