added run config

float_support
Anton Lydike 4 years ago
parent 6bc939572b
commit 4159d1609b

@ -4,25 +4,20 @@ from collections import defaultdict
from .Exceptions import * from .Exceptions import *
from .helpers import * from .helpers import *
from .Config import RunConfig
import typing import typing
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from . import MMU, Executable, LoadedExecutable, LoadedInstruction from . import MMU, Executable, LoadedExecutable, LoadedInstruction
COLOR = True
FMT_ORANGE = '\033[33m'
FMT_GRAY = '\033[37m'
FMT_BOLD = '\033[1m'
FMT_NONE = '\033[0m'
FMT_UNDERLINE = '\033[4m'
class Registers: class Registers:
def __init__(self): def __init__(self, conf: RunConfig):
self.vals = defaultdict(lambda: 0) self.vals = defaultdict(lambda: 0)
self.last_mod = None self.last_mod = None
self.last_access = None self.last_access = None
self.conf = conf
def dump(self, small=False): def dump(self, small=False):
named_regs = [self.reg_repr(reg) for reg in Registers.named_registers()] named_regs = [self.reg_repr(reg) for reg in Registers.named_registers()]
@ -67,6 +62,8 @@ class Registers:
def reg_repr(self, reg): def reg_repr(self, reg):
txt = '{:4}=0x{:08X}'.format(reg, self.get(reg)) txt = '{:4}=0x{:08X}'.format(reg, self.get(reg))
if not self.conf.color:
return txt
if reg == 'fp': if reg == 'fp':
reg = 's0' reg = 's0'
if reg == self.last_mod: if reg == self.last_mod:
@ -112,14 +109,16 @@ class Registers:
def named_registers(): def named_registers():
return ['zero', 'ra', 'sp', 'gp', 'tp', 'fp'] return ['zero', 'ra', 'sp', 'gp', 'tp', 'fp']
class CPU: class CPU:
def __init__(self): def __init__(self, conf: RunConfig):
from . import MMU, Executable, LoadedExecutable, LoadedInstruction from . import MMU, Executable, LoadedExecutable, LoadedInstruction
self.mmu = MMU() self.mmu = MMU(conf)
self.regs = Registers() self.regs = Registers()
self.pc = 0 self.pc = 0
self.exit = False self.exit = False
self.conf = conf
self.syscall_int = SyscallInterface() self.syscall_int = SyscallInterface()
@ -289,7 +288,6 @@ class CPU:
if self.regs.get(reg1) < self.regs.get(reg2): if self.regs.get(reg1) < self.regs.get(reg2):
self.pc = dest self.pc = dest
def instruction_bge(self, ins: 'LoadedInstruction'): def instruction_bge(self, ins: 'LoadedInstruction'):
INS_NOT_IMPLEMENTED(ins) INS_NOT_IMPLEMENTED(ins)
@ -325,8 +323,9 @@ class CPU:
INS_NOT_IMPLEMENTED(ins) INS_NOT_IMPLEMENTED(ins)
def instruction_dbg(self, ins: 'LoadedInstruction'): def instruction_dbg(self, ins: 'LoadedInstruction'):
import code if self.conf.debug_instruction:
code.interact(local=dict(globals(), **locals())) import code
code.interact(local=dict(globals(), **locals()))
@staticmethod @staticmethod
def all_instructions(): def all_instructions():
@ -345,5 +344,3 @@ class SyscallInterface:
def handle_syscall(self, scall: Syscall): def handle_syscall(self, scall: Syscall):
print("syscall {} received!".format(scall.id)) print("syscall {} received!".format(scall.id))
scall.registers.dump_reg_a() scall.registers.dump_reg_a()

@ -0,0 +1,8 @@
from dataclasses import dataclass
@dataclass()
class RunConfig:
color = True
preffered_stack_size = None
debug_instruction = True

@ -53,7 +53,7 @@ class InstructionMemorySection(MemorySection):
] ]
@dataclass(frozen=True) @dataclass()
class Executable: class Executable:
run_ptr: Tuple[str, int] run_ptr: Tuple[str, int]
sections: Dict[str, MemorySection] sections: Dict[str, MemorySection]

@ -1,5 +1,4 @@
from dataclasses import dataclass from .Config import RunConfig
from .Executable import Executable, LoadedExecutable, LoadedMemorySection from .Executable import Executable, LoadedExecutable, LoadedMemorySection
from .helpers import align_addr from .helpers import align_addr
from .Exceptions import OutOfMemoryEsception from .Exceptions import OutOfMemoryEsception
@ -14,10 +13,11 @@ class MMU:
binaries: List[LoadedExecutable] binaries: List[LoadedExecutable]
last_bin: Optional[LoadedExecutable] = None last_bin: Optional[LoadedExecutable] = None
def __init__(self): def __init__(self, conf: RunConfig):
self.sections = list() self.sections = list()
self.binaries = list() self.binaries = list()
self.last_bin = None self.last_bin = None
self.conf = conf
def load_bin(self, bin: Executable): def load_bin(self, bin: Executable):
if self.last_bin is None: if self.last_bin is None:
@ -27,6 +27,10 @@ class MMU:
# align to 8 byte word # align to 8 byte word
addr = align_addr(addr) addr = align_addr(addr)
# apply preferred stack size from config
if bin.stack_pref is None:
bin.stack_pref = self.conf.preffered_stack_size
loaded_bin = LoadedExecutable(bin, addr) loaded_bin = LoadedExecutable(bin, addr)
if loaded_bin.size + addr > self.max_size: if loaded_bin.size + addr > self.max_size:

@ -11,3 +11,5 @@ from .ExecutableParser import ExecutableParser
from .MMU import MMU from .MMU import MMU
from .CPU import CPU, Registers, Syscall, SyscallInterface from .CPU import CPU, Registers, Syscall, SyscallInterface
from .Config import RunConfig

@ -36,3 +36,8 @@ def int_from_bytes(bytes):
return num - 2 ** (8 * len(bytes)) return num - 2 ** (8 * len(bytes))
return num return num
FMT_ORANGE = '\033[33m'
FMT_GRAY = '\033[37m'
FMT_BOLD = '\033[1m'
FMT_NONE = '\033[0m'
FMT_UNDERLINE = '\033[4m'
Loading…
Cancel
Save