mibuild/sim: use the same architecture we use for others backends

This commit is contained in:
Florent Kermarrec 2015-03-27 14:14:49 +01:00
parent de31103cce
commit ec080479da
5 changed files with 44 additions and 18 deletions

View File

@ -1,5 +1,5 @@
from mibuild.generic_platform import * from mibuild.generic_platform import *
from mibuild.sim.verilator import VerilatorPlatform from mibuild.sim import SimPlatform
class SimPins(Pins): class SimPins(Pins):
def __init__(self, n): def __init__(self, n):
@ -31,13 +31,13 @@ _io = [
), ),
] ]
class Platform(VerilatorPlatform): class Platform(SimPlatform):
is_sim = True is_sim = True
default_clk_name = "sys_clk" default_clk_name = "sys_clk"
default_clk_period = 1000 # on modern computers simulate at ~ 1MHz default_clk_period = 1000 # on modern computers simulate at ~ 1MHz
def __init__(self): def __init__(self):
VerilatorPlatform.__init__(self, "SIM", _io) SimPlatform.__init__(self, "SIM", _io)
def do_finalize(self, fragment): def do_finalize(self, fragment):
pass pass

View File

@ -0,0 +1 @@
from mibuild.sim.platform import SimPlatform

1
mibuild/sim/common.py Normal file
View File

@ -0,0 +1 @@
sim_special_overrides = {}

19
mibuild/sim/platform.py Normal file
View File

@ -0,0 +1,19 @@
from mibuild.generic_platform import GenericPlatform
from mibuild.sim import common, verilator
class SimPlatform(GenericPlatform):
def __init__(self, *args, toolchain="verilator", **kwargs):
GenericPlatform.__init__(self, *args, **kwargs)
if toolchain == "verilator":
self.toolchain = verilator.SimVerilatorToolchain()
else:
raise ValueError("Unknown toolchain")
def get_verilog(self, *args, special_overrides=dict(), **kwargs):
so = dict(common.sim_special_overrides)
so.update(special_overrides)
return GenericPlatform.get_verilog(self, *args, special_overrides=so, **kwargs)
def build(self, *args, **kwargs):
return self.toolchain.build(self, *args, **kwargs)

View File

@ -7,13 +7,16 @@ from migen.fhdl.std import *
from migen.fhdl.structure import _Fragment from migen.fhdl.structure import _Fragment
from mibuild.generic_platform import * from mibuild.generic_platform import *
def _build_tb(platform, serial, template): from mibuild import tools
from mibuild.sim import common
def _build_tb(platform, vns, serial, template):
def io_name(ressource, subsignal=None): def io_name(ressource, subsignal=None):
res = platform.lookup_request(ressource) res = platform.lookup_request(ressource)
if subsignal is not None: if subsignal is not None:
res = getattr(res, subsignal) res = getattr(res, subsignal)
return platform.vns.get_name(res) return vns.get_name(res)
ios = """ ios = """
#define SYS_CLK dut->{sys_clk} #define SYS_CLK dut->{sys_clk}
@ -79,7 +82,7 @@ def _build_tb(platform, serial, template):
f.close() f.close()
tools.write_to_file("dut_tb.cpp", content) tools.write_to_file("dut_tb.cpp", content)
def _build_sim(platform, build_name, include_paths, sim_path, serial, verbose): def _build_sim(platform, vns, build_name, include_paths, sim_path, serial, verbose):
include = "" include = ""
for path in include_paths: for path in include_paths:
include += "-I"+path+" " include += "-I"+path+" "
@ -95,7 +98,7 @@ make -j -C obj_dir/ -f Vdut.mk Vdut
build_script_file = "build_" + build_name + ".sh" build_script_file = "build_" + build_name + ".sh"
tools.write_to_file(build_script_file, build_script_contents, force_unix=True) tools.write_to_file(build_script_file, build_script_contents, force_unix=True)
_build_tb(platform, serial, os.path.join("..", sim_path,"dut_tb.cpp")) _build_tb(platform, vns, serial, os.path.join("..", sim_path,"dut_tb.cpp"))
if verbose: if verbose:
r = subprocess.call(["bash", build_script_file]) r = subprocess.call(["bash", build_script_file])
else: else:
@ -112,32 +115,34 @@ def _run_sim(build_name):
if r != 0: if r != 0:
raise OSError("Subprocess failed") raise OSError("Subprocess failed")
class VerilatorPlatform(GenericPlatform): class SimVerilatorToolchain:
# XXX fir sim_path # XXX fir sim_path
def build(self, soc, build_dir="build", build_name="top", def build(self, platform, fragment, build_dir="build", build_name="top",
sim_path="../migen/mibuild/sim/", serial="console", sim_path="../migen/mibuild/sim/", serial="console",
run=True, verbose=False): run=True, verbose=False):
tools.mkdir_noerror(build_dir) tools.mkdir_noerror(build_dir)
os.chdir(build_dir) os.chdir(build_dir)
self.soc = soc if not isinstance(fragment, _Fragment):
fragment = soc.get_fragment() fragment = fragment.get_fragment()
self.finalize(fragment) platform.finalize(fragment)
v_src, vns = self.get_verilog(fragment)
named_sc, named_pc = self.resolve_signals(vns) v_src, vns = platform.get_verilog(fragment)
self.vns = vns named_sc, named_pc = platform.resolve_signals(vns)
v_file = "dut.v" v_file = "dut.v"
tools.write_to_file(v_file, v_src) tools.write_to_file(v_file, v_src)
include_paths = [] include_paths = []
for source in self.sources: for source in platform.sources:
path = os.path.dirname(source[0]).replace("\\", "\/") path = os.path.dirname(source[0]).replace("\\", "\/")
if path not in include_paths: if path not in include_paths:
include_paths.append(path) include_paths.append(path)
include_paths += self.verilog_include_paths include_paths += platform.verilog_include_paths
_build_sim(self, build_name, include_paths, sim_path, serial, verbose) _build_sim(platform, vns, build_name, include_paths, sim_path, serial, verbose)
if run: if run:
_run_sim(build_name) _run_sim(build_name)
os.chdir("..") os.chdir("..")
return vns