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
)
27 sig_in
.eq(AnyConst(width
)),
28 dut
.sig_in
.eq(sig_in
),
32 for i
in range(width
):
34 result_next
= Signal
.like(count
, name
="count_%d" % i
)
35 with m
.If(sig_in
[i
] == 1):
36 comb
+= result_next
.eq(width
-i
-1)
38 comb
+= result_next
.eq(result
)
41 result_sig
= Signal
.like(count
)
42 comb
+= result_sig
.eq(result
)
44 comb
+= Assert(result_sig
== count
)
46 # setup the inputs and outputs of the DUT as anyconst
51 class CLZTestCase(FHDLTestCase
):
54 self
.assertFormal(module
, mode
="bmc", depth
=4)
58 vl
= rtlil
.convert(dut
, ports
=[])
59 with
open("clz.il", "w") as f
:
63 if __name__
== '__main__':