1 from nmigen
import Module
, Signal
, Elaboratable
, Mux
, Const
2 from nmigen
.asserts
import Assert
, AnyConst
, Assume
3 from nmutil
.formaltest
import FHDLTestCase
4 from nmigen
.cli
import rtlil
6 from nmutil
.clz
import CLZ
10 # This defines a module to drive the device under test and assert
11 # properties about its outputs
12 class Driver(Elaboratable
):
17 def elaborate(self
, platform
):
22 m
.submodules
.dut
= dut
= CLZ(width
)
23 sig_in
= Signal
.like(dut
.sig_in
)
24 count
= Signal
.like(dut
.lz
)
28 sig_in
.eq(AnyConst(width
)),
29 dut
.sig_in
.eq(sig_in
),
33 for i
in range(width
):
35 result_next
= Signal
.like(count
, name
="count_%d" % i
)
36 with m
.If(sig_in
[i
] == 1):
37 comb
+= result_next
.eq(width
-i
-1)
39 comb
+= result_next
.eq(result
)
42 result_sig
= Signal
.like(count
)
43 comb
+= result_sig
.eq(result
)
45 comb
+= Assert(result_sig
== count
)
47 # setup the inputs and outputs of the DUT as anyconst
51 class CLZTestCase(FHDLTestCase
):
54 self
.assertFormal(module
, mode
="bmc", depth
=4)
57 vl
= rtlil
.convert(dut
, ports
=[])
58 with
open("clz.il", "w") as f
:
61 if __name__
== '__main__':