add copyright notices
[nmutil.git] / src / nmutil / sim_util.py
1 # SPDX-License-Identifier: LGPL-3-or-later
2 # Copyright 2021 Jacob Lifshay
3
4 # Funded by NLnet Assure Programme 2021-02-052, https://nlnet.nl/assure part
5 # of Horizon 2020 EU Programme 957073.
6
7 from contextlib import contextmanager
8 from hashlib import sha256
9 from nmutil.get_test_path import get_test_path
10 from nmigen.sim import Simulator
11
12
13 def hash_256(v):
14 return int.from_bytes(
15 sha256(bytes(v, encoding='utf-8')).digest(),
16 byteorder='little'
17 )
18
19
20 @contextmanager
21 def do_sim(test_case, dut, traces=()):
22 sim = Simulator(dut)
23 path = get_test_path(test_case, "sim_test_out")
24 path.parent.mkdir(parents=True, exist_ok=True)
25 vcd_path = path.with_suffix(".vcd")
26 gtkw_path = path.with_suffix(".gtkw")
27 with sim.write_vcd(vcd_path.open("wt", encoding="utf-8"),
28 gtkw_path.open("wt", encoding="utf-8"),
29 traces=traces):
30 yield sim