add divide-by-zero test to test_div_sim.py
[soc.git] / src / soc / simulator / program.py
index b3c1e87fc2fa2bc62ffa645dbc0710a021160fba..fdcea64201580ab7432f767f6a16c1c6d9580e7a 100644 (file)
@@ -1,7 +1,14 @@
+"""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
 import os
+import sys
 
 filedir = os.path.dirname(os.path.realpath(__file__))
 memmap = os.path.join(filedir, "memmap")
@@ -17,6 +24,7 @@ class Program:
             instructions = '\n'.join(instructions)
         self.assembly = instructions + '\n' # plus final newline
         self._assemble()
+        self._instructions = list(self._get_instructions())
 
     def __enter__(self):
         return self
@@ -45,21 +53,28 @@ class Program:
     def _assemble(self):
         with tempfile.NamedTemporaryFile(suffix=".o") as outfile:
             args = ["powerpc64-linux-gnu-as",
+                    '-mpower9',
                     obj_fmt,
                     "-o",
                     outfile.name]
             p = subprocess.Popen(args, stdin=subprocess.PIPE)
             p.communicate(self.assembly.encode('utf-8'))
-            assert(p.wait() == 0)
+            if p.wait() != 0:
+                print("Error in program:")
+                print(self.assembly)
+                sys.exit(1)
             self._link(outfile)
 
-    def generate_instructions(self):
+    def _get_instructions(self):
         while True:
             data = self.binfile.read(4)
             if not data:
                 break
             yield struct.unpack('<i', data)[0]
 
+    def generate_instructions(self):
+        yield from self._instructions
+
     def reset(self):
         self.binfile.seek(0)