From 5d484f08cf8850bad0842789dab3fccd523e566c Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Sat, 24 Apr 2021 18:24:11 +0200 Subject: [PATCH] Minor fixes like imports and edge-case handling --- riscemu/Executable.py | 3 ++- riscemu/ExecutableParser.py | 1 + riscemu/MMU.py | 5 +++++ riscemu/__init__.py | 2 +- riscemu/helpers.py | 1 - 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/riscemu/Executable.py b/riscemu/Executable.py index 176625a..ed48eb5 100644 --- a/riscemu/Executable.py +++ b/riscemu/Executable.py @@ -12,6 +12,7 @@ from dataclasses import dataclass, field from typing import Dict, List, Tuple, Union, Optional from .Exceptions import * from .helpers import * +from math import log import typing @@ -215,7 +216,7 @@ class LoadedMemorySection: print(FMT_MEM + "{}, viewing {} bytes:".format( self, end - start ) + FMT_NONE) - for i in range(start, end, bytes_per_row): + for i in range(0, end - start, bytes_per_row): data = self.content[start + i: min(start + i + bytes_per_row, end)] if start + i <= highlight <= start + i + bytes_per_row: # do hightlight here! diff --git a/riscemu/ExecutableParser.py b/riscemu/ExecutableParser.py index 550fce0..c3a9fd7 100644 --- a/riscemu/ExecutableParser.py +++ b/riscemu/ExecutableParser.py @@ -75,6 +75,7 @@ class ExecutableParser: :param token: the symbol token """ ASSERT_NOT_IN(token.name, self.symbols) + ASSERT_NOT_NULL(self.active_section) sec_pos = self._curr_sec().size self.symbols[token.name] = (self.active_section, sec_pos) diff --git a/riscemu/MMU.py b/riscemu/MMU.py index 0f2e40e..e4e4bfc 100644 --- a/riscemu/MMU.py +++ b/riscemu/MMU.py @@ -141,6 +141,10 @@ class MMU: :return: The Instruction """ sec = self.get_sec_containing(addr) + if sec is None: + print(FMT_MEM + "[MMU] Trying to read instruction form invalid region! " + "Have you forgotten an exit syscall or ret statement?" + FMT_NONE) + raise RuntimeError("No next instruction available!") return sec.read_instruction(addr - sec.base) def read(self, addr: int, size: int) -> bytearray: @@ -175,6 +179,7 @@ class MMU: """ sec = self.get_sec_containing(addr) if sec is None: + print(FMT_MEM + "[MMU] No section containing addr 0x{:08X}".format(addr) + FMT_NONE) return sec.dump(addr, *args, **kwargs) diff --git a/riscemu/__init__.py b/riscemu/__init__.py index 7bf2073..f030752 100644 --- a/riscemu/__init__.py +++ b/riscemu/__init__.py @@ -13,7 +13,7 @@ from .Exceptions import RiscemuBaseException, LaunchDebuggerException, InvalidSy from .Tokenizer import RiscVInput, RiscVTokenizer -from .Executable import Executable, LoadedExecutable +from .Executable import Executable, LoadedExecutable, LoadedMemorySection from .ExecutableParser import ExecutableParser diff --git a/riscemu/helpers.py b/riscemu/helpers.py index 5faadad..97458cc 100644 --- a/riscemu/helpers.py +++ b/riscemu/helpers.py @@ -96,5 +96,4 @@ def format_bytes(byte_arr: bytearray, fmt: str, group: int = 1, highlight: int = return highlight_in_list([('{:0' + spc + 'd}').format(int_from_bytes(ch, unsigned=True)) for ch in chunks], highlight) if fmt == 'ascii': - print("printing ascii", "".join(chr(b) for b in byte_arr)) return "".join(repr(chr(b))[1:-1] for b in byte_arr)