Skip to content

Commit

Permalink
utils/sim: Improve extensibility by using kwargs in constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
colluca committed Dec 15, 2023
1 parent 07a5efa commit 3ef6471
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
48 changes: 36 additions & 12 deletions util/sim/Simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ class BistSimulation(Simulation):
simulation was successful or not.
"""

def __init__(self, elf=None, retcode=0):
super().__init__(elf)
def __init__(self, retcode=0, **kwargs):
"""Constructor for the BistSimulation class.
Arguments:
retcode: The expected return code of the simulation.
kwargs: Arguments passed to the base class constructor.
"""
super().__init__(**kwargs)
self.expected_retcode = retcode
self.actual_retcode = None

Expand All @@ -131,8 +137,14 @@ class RTLSimulation(BistSimulation):
in advance from some RTL design.
"""

def __init__(self, elf=None, retcode=0, sim_bin=None):
super().__init__(elf, retcode)
def __init__(self, sim_bin=None, **kwargs):
"""Constructor for the RTLSimulation class.
Arguments:
sim_bin: The simulation binary.
kwargs: Arguments passed to the base class constructor.
"""
super().__init__(**kwargs)
self.cmd = [str(sim_bin), str(self.elf)]


Expand All @@ -155,7 +167,6 @@ class QuestaVCSSimulation(RTLSimulation):
"""

def get_retcode(self):

# Extract the application's return code from the simulation log
with open(self.log, 'r') as f:
for line in f.readlines():
Expand All @@ -182,8 +193,8 @@ def successful(self):
class QuestaSimulation(QuestaVCSSimulation):
"""An RTL simulation running on QuestaSim."""

def __init__(self, elf=None, retcode=0, sim_bin=None):
super().__init__(elf, retcode, sim_bin)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cmd += ['', '-batch']


Expand All @@ -199,8 +210,14 @@ class BansheeSimulation(BistSimulation):
return code of the command launching the simulation.
"""

def __init__(self, elf=None, retcode=0, banshee_cfg=None):
super().__init__(elf, retcode)
def __init__(self, banshee_cfg=None, **kwargs):
"""Constructor for the BansheeSimulation class.
Arguments:
banshee_cfg: A Banshee config file.
kwargs: Arguments passed to the base class constructor.
"""
super().__init__(**kwargs)
self.cmd = ['banshee', '--no-opt-llvm', '--no-opt-jit', '--configuration',
str(banshee_cfg), '--trace', str(self.elf)]

Expand All @@ -221,9 +238,16 @@ class CustomSimulation(Simulation):
any additional logic here.
"""

def __init__(self, elf=None, sim_bin=None, cmd=None):
super().__init__(elf)
self.dynamic_args = {'sim_bin': str(sim_bin), 'elf': str(elf)}
def __init__(self, sim_bin=None, cmd=None, **kwargs):
"""Constructor for the CustomSimulation class.
Arguments:
sim_bin: The simulation binary.
cmd: The custom command used to launch the simulation.
kwargs: Arguments passed to the base class constructor.
"""
super().__init__(**kwargs)
self.dynamic_args = {'sim_bin': str(sim_bin), 'elf': str(self.elf)}
self.cmd = cmd

def launch(self, run_dir=None, dry_run=False):
Expand Down
44 changes: 35 additions & 9 deletions util/sim/Simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,22 @@ class RTLSimulator(Simulator):
given test, with the custom command and simulation binary.
"""

def __init__(self, name, simulation_cls, binary):
super().__init__(name, simulation_cls)
def __init__(self, binary, **kwargs):
"""Constructor for the RTLSimulator class.
Arguments:
binary: The simulation binary.
kwargs: Arguments passed to the base class constructor.
"""
super().__init__(**kwargs)
self.binary = binary

def get_simulation(self, test):
if 'cmd' in test:
return CustomSimulation(test['elf'], self.binary, test['cmd'])
return CustomSimulation(elf=test['elf'], sim_bin=self.binary, cmd=test['cmd'])
else:
return self.simulation_cls(
test['elf'],
elf=test['elf'],
retcode=test['exit_code'] if 'exit_code' in test else 0,
sim_bin=self.binary
)
Expand All @@ -94,7 +100,12 @@ class VCSSimulator(RTLSimulator):
"""

def __init__(self, binary):
super().__init__('vcs', VCSSimulation, binary)
"""Constructor for the VCSSimulator class.
Arguments:
binary: The VCS simulation binary.
"""
super().__init__(binary, name='vcs', simulation_cls=VCSSimulation)


class QuestaSimulator(RTLSimulator):
Expand All @@ -106,7 +117,12 @@ class QuestaSimulator(RTLSimulator):
"""

def __init__(self, binary):
super().__init__('vsim', QuestaSimulation, binary)
"""Constructor for the QuestaSimulator class.
Arguments:
binary: The QuestaSim simulation binary.
"""
super().__init__(binary, name='vsim', simulation_cls=QuestaSimulation)


class VerilatorSimulator(RTLSimulator):
Expand All @@ -118,7 +134,12 @@ class VerilatorSimulator(RTLSimulator):
"""

def __init__(self, binary):
super().__init__('verilator', VerilatorSimulation, binary)
"""Constructor for the VerilatorSimulator class.
Arguments:
binary: The Verilator simulation binary.
"""
super().__init__(binary, name='verilator', simulation_cls=VerilatorSimulation)


class BansheeSimulator(Simulator):
Expand All @@ -129,7 +150,12 @@ class BansheeSimulator(Simulator):
"""

def __init__(self, cfg):
super().__init__('banshee', BansheeSimulation)
"""Constructor for the BansheeSimulator class.
Arguments:
cfg: A Banshee config file.
"""
super().__init__(name='banshee', simulation_cls=BansheeSimulation)
self.cfg = cfg

def supports(self, test):
Expand All @@ -146,7 +172,7 @@ def supports(self, test):

def get_simulation(self, test):
return self.simulation_cls(
test['elf'],
elf=test['elf'],
retcode=test['exit_code'] if 'exit_code' in test else 0,
banshee_cfg=self.cfg
)

0 comments on commit 3ef6471

Please sign in to comment.