radix: reading first page table entry
[soc.git] / src / soc / simulator / gas.py
1 # License: LPGLv3
2 # Copyright (C) 2020 Michael Nolan <mtnolan2640@gmail.com>
3 # Copyright (C) 2020 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
4
5 import tempfile
6 import subprocess
7 import struct
8
9
10 def get_assembled_instruction(instruction, bigendian=False):
11 if bigendian:
12 endian_fmt = "elf64-big"
13 obj_fmt = "-be"
14 else:
15 endian_fmt = "elf64-little"
16 obj_fmt = "-le"
17 with tempfile.NamedTemporaryFile(suffix=".o") as outfile:
18 args = ["powerpc64-linux-gnu-as",
19 obj_fmt,
20 "-o",
21 outfile.name]
22 p = subprocess.Popen(args, stdin=subprocess.PIPE)
23 p.communicate(instruction.encode('utf-8'))
24 assert(p.wait() == 0)
25
26 with tempfile.NamedTemporaryFile(suffix=".bin") as binfile:
27 args = ["powerpc64-linux-gnu-objcopy",
28 "-I", endian_fmt,
29 "-O", "binary",
30 outfile.name,
31 binfile.name]
32 subprocess.check_output(args)
33 binary = struct.unpack('>i', binfile.read(4))[0]
34 return binary