add stand-alone simulator bitmanip test
[openpower-isa.git] / src / openpower / test / bitmanip / bitmanip_cases.py
index 8b1120942a2e4f09783c94d0889433922afaf83e..f38e06665dc211dd5ba816be1ed19fa629787069 100644 (file)
@@ -1,51 +1,51 @@
+from openpower.sv.trans.svp64 import SVP64Asm
 from openpower.test.common import TestAccumulatorBase, skip_case
 from openpower.endian import bigendian
 from openpower.simulator.program import Program
-from hashlib import sha256
-
-
-def hash_256(v):
-    return int.from_bytes(
-        sha256(bytes(v, encoding='utf-8')).digest(),
-        byteorder='little'
-    )
+from openpower.test.state import ExpectedState
+from nmutil.sim_util import hash_256
 
 
 class BitManipTestCase(TestAccumulatorBase):
-    def do_case_ternlogi(self, rt_v, ra_v, rb_v, imm, rc):
-        po = 5
-        xo = 0
-        rt = 3
-        ra = 4
-        rb = 5
-        instr = po
-        instr = (instr << 5) | rt
-        instr = (instr << 5) | ra
-        instr = (instr << 5) | rb
-        instr = (instr << 8) | imm
-        instr = (instr << 2) | xo
-        instr = (instr << 1) | rc
-        asm = f"ternlogi{'.' * rc} {rt}, {ra}, {rb}, {imm}"
-        lst = [f".4byte {hex(instr)} # {asm}"]
+    def do_case_ternlogi(self, rt, ra, rb, imm):
+        lst = [f"ternlogi 3, 4, 5, {imm}"]
         initial_regs = [0] * 32
-        initial_regs[3] = rt_v % 2 ** 64
-        initial_regs[4] = ra_v % 2 ** 64
-        initial_regs[5] = rb_v % 2 ** 64
-        self.add_case(Program(lst, bigendian), initial_regs)
+        rt %= 2 ** 64
+        ra %= 2 ** 64
+        rb %= 2 ** 64
+        initial_regs[3] = rt
+        initial_regs[4] = ra
+        initial_regs[5] = rb
+        lst = list(SVP64Asm(lst, bigendian))
+        e = ExpectedState(pc=4)
+        expected = 0
+        for i in range(64):
+            lut_index = 0
+            if rb & 2 ** i:
+                lut_index |= 2 ** 0
+            if ra & 2 ** i:
+                lut_index |= 2 ** 1
+            if rt & 2 ** i:
+                lut_index |= 2 ** 2
+            if imm & 2 ** lut_index:
+                expected |= 2 ** i
+        e.intregs[3] = expected
+        e.intregs[4] = ra
+        e.intregs[5] = rb
+        self.add_case(Program(lst, bigendian), initial_regs, expected=e)
 
     def case_ternlogi_0(self):
         self.do_case_ternlogi(0x8000_0000_FFFF_0000,
                               0x8000_0000_FF00_FF00,
-                              0x8000_0000_F0F0_F0F0, 0x80, rc=1)
+                              0x8000_0000_F0F0_F0F0, 0x80)
 
     def case_ternlogi_FF(self):
-        self.do_case_ternlogi(0, 0, 0, 0xFF, rc=1)
+        self.do_case_ternlogi(0, 0, 0, 0xFF)
 
-    @skip_case
     def case_ternlogi_random(self):
         for i in range(100):
             imm = hash_256(f"ternlogi imm {i}") & 0xFF
-            rt_v = hash_256(f"ternlogi rt {i}") % 2 ** 64
-            ra_v = hash_256(f"ternlogi ra {i}") % 2 ** 64
-            rb_v = hash_256(f"ternlogi rb {i}") % 2 ** 64
-            self.do_case_ternlogi(rt_v, ra_v, rb_v, imm, rc=1)
+            rt = hash_256(f"ternlogi rt {i}") % 2 ** 64
+            ra = hash_256(f"ternlogi ra {i}") % 2 ** 64
+            rb = hash_256(f"ternlogi rb {i}") % 2 ** 64
+            self.do_case_ternlogi(rt, ra, rb, imm)