Merge branch 'master' of ssh://git.libre-riscv.org:922/soc
[soc.git] / src / soc / simulator / program.py
index d0b11d670b771b9e110eab5d81ec11c96f4ab4b0..59590a0965c46819847bc072bb4f4417f3e1c1ad 100644 (file)
@@ -1,3 +1,9 @@
+"""POWER Program
+
+takes powerpc assembly instructions and turns them into LE/BE binary
+data.  calls powerpc64-linux-gnu-as, ld and objcopy to do so.
+"""
+
 import tempfile
 import subprocess
 import struct
@@ -10,16 +16,22 @@ bigendian = True
 endian_fmt = "elf64-big"
 obj_fmt = "-be"
 
+
 class Program:
     def __init__(self, instructions):
         if isinstance(instructions, list):
             instructions = '\n'.join(instructions)
-        self.assembly = instructions
+        self.assembly = instructions + '\n' # plus final newline
         self._assemble()
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, type, value, traceback):
+        self.close()
+
     def _get_binary(self, elffile):
         self.binfile = tempfile.NamedTemporaryFile(suffix=".bin")
-        #self.binfile = open("kernel.bin", "wb+")
         args = ["powerpc64-linux-gnu-objcopy",
                 "-O", "binary",
                 "-I", endian_fmt,
@@ -29,7 +41,6 @@ class Program:
 
     def _link(self, ofile):
         with tempfile.NamedTemporaryFile(suffix=".elf") as elffile:
-        #with open("kernel.elf", "wb+") as elffile:
             args = ["powerpc64-linux-gnu-ld",
                     "-o", elffile.name,
                     "-T", memmap,
@@ -40,6 +51,7 @@ class Program:
     def _assemble(self):
         with tempfile.NamedTemporaryFile(suffix=".o") as outfile:
             args = ["powerpc64-linux-gnu-as",
+                    '-mpower9',
                     obj_fmt,
                     "-o",
                     outfile.name]
@@ -54,3 +66,16 @@ class Program:
             if not data:
                 break
             yield struct.unpack('<i', data)[0]
+
+    def reset(self):
+        self.binfile.seek(0)
+
+    def size(self):
+        curpos = self.binfile.tell()
+        self.binfile.seek(0, 2)  # Seek to end of file
+        size = self.binfile.tell()
+        self.binfile.seek(curpos, 0)
+        return size
+
+    def close(self):
+        self.binfile.close()