1 # Proof of correctness for partitioned equal signal combiner
2 # Copyright (C) 2020 Michael Nolan <mtnolan2640@gmail.com>
4 from nmigen
import (Module
, Signal
, Elaboratable
, Mux
, Cat
, Repl
,
6 from nmigen
.asserts
import Assert
, AnyConst
, Assume
, Cover
7 from nmigen
.test
.utils
import FHDLTestCase
8 from nmigen
.cli
import rtlil
10 from soc
.fu
.alu
.main_stage
import ALUMainStage
11 from soc
.fu
.alu
.pipe_data
import ALUPipeSpec
12 from soc
.fu
.alu
.alu_input_record
import CompALUOpSubset
13 from soc
.decoder
.power_enums
import InternalOp
17 # This defines a module to drive the device under test and assert
18 # properties about its outputs
19 class Driver(Elaboratable
):
24 def elaborate(self
, platform
):
28 rec
= CompALUOpSubset()
30 # Setup random inputs for dut.op
34 comb
+= p
.eq(AnyConst(width
))
36 pspec
= ALUPipeSpec(id_wid
=2, op_wid
=recwidth
)
37 m
.submodules
.dut
= dut
= ALUMainStage(pspec
)
39 # convenience variables
42 carry_in
= dut
.i
.xer_ca
[0]
43 carry_in32
= dut
.i
.xer_ca
[1]
45 carry_out
= dut
.o
.xer_ca
.data
[0]
46 carry_out32
= dut
.o
.xer_ca
.data
[1]
50 comb
+= [a
.eq(AnyConst(64)),
52 carry_in
.eq(AnyConst(0b11)),
53 so_in
.eq(AnyConst(1))]
55 comb
+= dut
.i
.ctx
.op
.eq(rec
)
57 # Assert that op gets copied from the input to output
58 for rec_sig
in rec
.ports():
60 dut_sig
= getattr(dut
.o
.ctx
.op
, name
)
61 comb
+= Assert(dut_sig
== rec_sig
)
63 # signed and signed/32 versions of input a
64 a_signed
= Signal(signed(64))
65 a_signed_32
= Signal(signed(32))
66 comb
+= a_signed
.eq(a
)
67 comb
+= a_signed_32
.eq(a
[0:32])
69 comb
+= Assume(a
[32:64] == 0)
70 comb
+= Assume(b
[32:64] == 0)
71 # main assertion of arithmetic operations
72 with m
.Switch(rec
.insn_type
):
73 with m
.Case(InternalOp
.OP_ADD
):
75 comb
+= Assert(Cat(o
, carry_out
) == (a
+ b
+ carry_in
))
77 # CA32 - XXX note this fails! replace with carry_in and it works
78 comb
+= Assert((a
[0:32] + b
[0:32] + carry_in
)[32]
80 with m
.Case(InternalOp
.OP_EXTS
):
82 with m
.If(rec
.data_len
== i
):
83 comb
+= Assert(o
[0:i
*8] == a
[0:i
*8])
84 comb
+= Assert(o
[i
*8:64] == Repl(a
[i
*8-1], 64-(i
*8)))
85 with m
.Case(InternalOp
.OP_CMP
):
86 # CMP is defined as not taking in carry
87 comb
+= Assume(carry_in
== 0)
88 comb
+= Assert(o
== (a
+b
)[0:64])
90 with m
.Case(InternalOp
.OP_CMPEQB
):
94 comb
+= eqs
[i
].eq(src1
== b
[i
*8:(i
+1)*8])
95 comb
+= Assert(dut
.o
.cr0
[2] == eqs
.any())
100 class ALUTestCase(FHDLTestCase
):
101 def test_formal(self
):
103 self
.assertFormal(module
, mode
="bmc", depth
=2)
104 self
.assertFormal(module
, mode
="cover", depth
=2)
105 def test_ilang(self
):
107 vl
= rtlil
.convert(dut
, ports
=[])
108 with
open("alu_main_stage.il", "w") as f
:
112 if __name__
== '__main__':