From 3f7e6c771c7f8df65e67e6dc350964a54aee8066 Mon Sep 17 00:00:00 2001 From: Michael Nolan Date: Wed, 25 Mar 2020 17:16:07 -0400 Subject: [PATCH] Assemble whole program instead of instruction by instruction --- src/soc/simulator/memmap | 11 +++++++ src/soc/simulator/program.py | 56 +++++++++++++++++++++++++++++++++++ src/soc/simulator/test_sim.py | 22 +++++--------- 3 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 src/soc/simulator/memmap create mode 100644 src/soc/simulator/program.py diff --git a/src/soc/simulator/memmap b/src/soc/simulator/memmap new file mode 100644 index 00000000..8cc1a16e --- /dev/null +++ b/src/soc/simulator/memmap @@ -0,0 +1,11 @@ + +MEMORY +{ + ram : ORIGIN = 0x20000000, LENGTH = 128M +} + +SECTIONS +{ + .text : { *(.text*) } > ram + .bss : { *(.text*) } > ram +} diff --git a/src/soc/simulator/program.py b/src/soc/simulator/program.py new file mode 100644 index 00000000..d0b11d67 --- /dev/null +++ b/src/soc/simulator/program.py @@ -0,0 +1,56 @@ +import tempfile +import subprocess +import struct +import os + +filedir = os.path.dirname(os.path.realpath(__file__)) +memmap = os.path.join(filedir, "memmap") + +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._assemble() + + 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, + elffile.name, + self.binfile.name] + subprocess.check_output(args) + + 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, + ofile.name] + subprocess.check_output(args) + self._get_binary(elffile) + + def _assemble(self): + 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(self.assembly.encode('utf-8')) + assert(p.wait() == 0) + self._link(outfile) + + def generate_instructions(self): + while True: + data = self.binfile.read(4) + if not data: + break + yield struct.unpack('