summaryrefslogtreecommitdiff
path: root/tb/sim/sim.py
diff options
context:
space:
mode:
authorAlejandro Soto <alejandro@34project.org>2022-11-09 09:54:13 -0600
committerAlejandro Soto <alejandro@34project.org>2022-11-09 09:54:13 -0600
commit1c1620e48ff6b807aed0c955792b4e8a17614c20 (patch)
treeabaed0528fcffc841bfc6e25417625cb7f243acc /tb/sim/sim.py
parent5d798386c3b1c1dc45a2fbc382c9367ccc27c524 (diff)
Implement initial state randomization in sim
Diffstat (limited to '')
-rwxr-xr-xtb/sim/sim.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/tb/sim/sim.py b/tb/sim/sim.py
index cc1f205..4e0bdf1 100755
--- a/tb/sim/sim.py
+++ b/tb/sim/sim.py
@@ -1,11 +1,13 @@
#!/usr/bin/env python3
-import importlib.util, os, pathlib, subprocess, sys
+import importlib.util, os, pathlib, random, subprocess, sys
module_path, verilated, image = sys.argv[1:]
test_name = pathlib.Path(module_path).stem
module = None
+seed = os.getenv('SIM_SEED', str(random.randint(0, 0x7fff_ffff)))
+
all_regs = [
('r0', 'r0'),
('r1', 'r1'),
@@ -123,7 +125,14 @@ COLOR_YELLOW = '\033[33;1m'
COLOR_BLUE = '\033[34;1m'
def exit(*, success):
- status, color = ('passed', COLOR_GREEN) if success else ('failed', COLOR_RED)
+ global seed
+
+ if not success:
+ while_running()
+ if exec_args:
+ print('cmdline:', subprocess.list2cmdline(exec_args), file=sys.stderr)
+
+ status, color = ('passed', COLOR_GREEN) if success else (f'failed (seed: {seed})', COLOR_RED)
print( \
f'{color}Test \'{COLOR_YELLOW}{test_name}{COLOR_RESET}{color}\' ' +
f'{status}{COLOR_RESET}', file=sys.stderr)
@@ -164,9 +173,6 @@ def test_assert(condition, message):
while_running()
print(f'{COLOR_RED}{message()}{COLOR_RESET}', file=sys.stderr)
- if exec_args:
- print('cmdline:', subprocess.list2cmdline(exec_args), file=sys.stderr)
-
if regs:
dump_regs()
@@ -258,6 +264,8 @@ for r, value in init_regs.items():
init_regs = None
exec_args.append(image)
+exec_args.extend([f'+verilator+seed+{seed}', '+verilator+rand+reset+2'])
+
output = subprocess.run(exec_args, stdout=subprocess.PIPE, text=True)
if output.returncode != 0:
exit(success=False)