move do_sim and hash_256 to separate module
[nmutil.git] / src / nmutil / sim_util.py
1 # SPDX-License-Identifier: LGPL-3-or-later
2 # See Notices.txt for copyright information
3
4 from contextlib import contextmanager
5 from hashlib import sha256
6 from nmutil.get_test_path import get_test_path
7 from nmigen.sim import Simulator
8
9
10 def hash_256(v):
11 return int.from_bytes(
12 sha256(bytes(v, encoding='utf-8')).digest(),
13 byteorder='little'
14 )
15
16
17 @contextmanager
18 def do_sim(test_case, dut, traces=()):
19 sim = Simulator(dut)
20 path = get_test_path(test_case, "sim_test_out")
21 path.parent.mkdir(parents=True, exist_ok=True)
22 vcd_path = path.with_suffix(".vcd")
23 gtkw_path = path.with_suffix(".gtkw")
24 with sim.write_vcd(vcd_path.open("wt", encoding="utf-8"),
25 gtkw_path.open("wt", encoding="utf-8"),
26 traces=traces):
27 yield sim