Assemble whole program instead of instruction by instruction
[soc.git] / src / soc / simulator / program.py
1 import tempfile
2 import subprocess
3 import struct
4 import os
5
6 filedir = os.path.dirname(os.path.realpath(__file__))
7 memmap = os.path.join(filedir, "memmap")
8
9 bigendian = True
10 endian_fmt = "elf64-big"
11 obj_fmt = "-be"
12
13 class Program:
14 def __init__(self, instructions):
15 if isinstance(instructions, list):
16 instructions = '\n'.join(instructions)
17 self.assembly = instructions
18 self._assemble()
19
20 def _get_binary(self, elffile):
21 self.binfile = tempfile.NamedTemporaryFile(suffix=".bin")
22 #self.binfile = open("kernel.bin", "wb+")
23 args = ["powerpc64-linux-gnu-objcopy",
24 "-O", "binary",
25 "-I", endian_fmt,
26 elffile.name,
27 self.binfile.name]
28 subprocess.check_output(args)
29
30 def _link(self, ofile):
31 with tempfile.NamedTemporaryFile(suffix=".elf") as elffile:
32 #with open("kernel.elf", "wb+") as elffile:
33 args = ["powerpc64-linux-gnu-ld",
34 "-o", elffile.name,
35 "-T", memmap,
36 ofile.name]
37 subprocess.check_output(args)
38 self._get_binary(elffile)
39
40 def _assemble(self):
41 with tempfile.NamedTemporaryFile(suffix=".o") as outfile:
42 args = ["powerpc64-linux-gnu-as",
43 obj_fmt,
44 "-o",
45 outfile.name]
46 p = subprocess.Popen(args, stdin=subprocess.PIPE)
47 p.communicate(self.assembly.encode('utf-8'))
48 assert(p.wait() == 0)
49 self._link(outfile)
50
51 def generate_instructions(self):
52 while True:
53 data = self.binfile.read(4)
54 if not data:
55 break
56 yield struct.unpack('<i', data)[0]