Move gnu assembler interface to separate file
authorMichael Nolan <mtnolan2640@gmail.com>
Mon, 23 Mar 2020 13:41:35 +0000 (09:41 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Mon, 23 Mar 2020 14:24:21 +0000 (10:24 -0400)
src/soc/decoder/test/test_decoder_gas.py
src/soc/simulator/gas.py [new file with mode: 0644]

index ad4faf67d5c4cb62faadb2718b76429fcae6bc94..4ed591e6c64caacc2408edf073339c7ddf683873 100644 (file)
@@ -9,9 +9,7 @@ from soc.decoder.power_enums import (Function, InternalOp,
                                      single_bit_flags, Form, SPR,
                                      get_signal_name, get_csv)
 from soc.decoder.power_decoder2 import (PowerDecode2)
-import tempfile
-import subprocess
-import struct
+from soc.simulator.gas import get_assembled_instruction
 import random
 
 
@@ -379,32 +377,6 @@ class BranchRel:
 
 class DecoderTestCase(FHDLTestCase):
 
-    def get_assembled_instruction(self, instruction, bigendian=False):
-        if bigendian:
-            endian_fmt = "elf64-big"
-            obj_fmt = "-be"
-        else:
-            endian_fmt = "elf64-little"
-            obj_fmt = "-le"
-        with tempfile.NamedTemporaryFile(suffix=".o") as outfile:
-            args = ["powerpc64-linux-gnu-as",
-                    obj_fmt,
-                    "-o",
-                    outfile.name]
-            p = subprocess.Popen(args, stdin=subprocess.PIPE)
-            p.communicate(instruction.encode('utf-8'))
-            assert(p.wait() == 0)
-
-            with tempfile.NamedTemporaryFile(suffix=".bin") as binfile:
-                args = ["powerpc64-linux-gnu-objcopy",
-                        "-I", endian_fmt,
-                        "-O", "binary",
-                        outfile.name,
-                        binfile.name]
-                subprocess.check_output(args)
-                binary = struct.unpack('>i', binfile.read(4))[0]
-                return binary
-
     def run_tst(self, kls, name):
         m = Module()
         comb = m.d.comb
@@ -424,7 +396,7 @@ class DecoderTestCase(FHDLTestCase):
                 for mode in [0, 1]:
 
                     # turn the instruction into binary data (endian'd)
-                    ibin = self.get_assembled_instruction(ins, mode)
+                    ibin = get_assembled_instruction(ins, mode)
                     print("code", mode, hex(ibin), bin(ibin))
 
                     # ask the decoder to decode this binary data (endian'd)
diff --git a/src/soc/simulator/gas.py b/src/soc/simulator/gas.py
new file mode 100644 (file)
index 0000000..571302b
--- /dev/null
@@ -0,0 +1,30 @@
+import tempfile
+import subprocess
+import struct
+
+
+def get_assembled_instruction(instruction, bigendian=False):
+    if bigendian:
+        endian_fmt = "elf64-big"
+        obj_fmt = "-be"
+    else:
+        endian_fmt = "elf64-little"
+        obj_fmt = "-le"
+    with tempfile.NamedTemporaryFile(suffix=".o") as outfile:
+        args = ["powerpc64-linux-gnu-as",
+                obj_fmt,
+                "-o",
+                outfile.name]
+        p = subprocess.Popen(args, stdin=subprocess.PIPE)
+        p.communicate(instruction.encode('utf-8'))
+        assert(p.wait() == 0)
+
+        with tempfile.NamedTemporaryFile(suffix=".bin") as binfile:
+            args = ["powerpc64-linux-gnu-objcopy",
+                    "-I", endian_fmt,
+                    "-O", "binary",
+                    outfile.name,
+                    binfile.name]
+            subprocess.check_output(args)
+            binary = struct.unpack('>i', binfile.read(4))[0]
+            return binary