Skip to content

Commit d10a2ad

Browse files
committed
added emulated RAM size parameter
1 parent 07e2913 commit d10a2ad

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ This is a simple and readable **RISC-V RV32I emulator** written in pure Python.
4040
├── syscalls_newlib.S # Syscall stubs for Newlib-nano
4141
├── linker_bare.ld # Simple linker script, no heap support
4242
├── linker_newlib.ld # Linker script supporting Newlib-nano
43-
├── riscv-py.h # Emulator macros
43+
├── riscv-py.h # Emulator macros for user programs
4444
├── tests/test_asm*.S # Example assembly programs
4545
├── tests/test_bare*.C # Example C programs without Newlib support
4646
├── tests/test_newlib*.C # Example C programs with Newlib-nano support
@@ -134,6 +134,7 @@ Argument 3: arg3
134134
| `--start-checks WHEN` | Condition to enable checks (auto, early, main, first-call, 0xADDR) |
135135
| `--init-regs VALUE` | Initial register state (zero, random, 0xDEADBEEF) |
136136
| `--init-ram PATTERN` | Initialize RAM with pattern (zero, random, addr, 0xAA) |
137+
| `--ram-size KBS` | Emulated RAM size (kB, default 1024) |
137138
| `--timer` | Enable machine timer |
138139
| `--raw-tty` | Enable raw terminal mode |
139140
| `--no-color` | Remove ANSI colors in debugging output |
@@ -208,7 +209,7 @@ All unit tests from [riscv-samples](https://gitlab.univ-lille.fr/michael.hauspie
208209
- The emulator relies on ELF symbols for heap management and call tracing: do not strip ELF binaries.
209210
- When a trap condition is triggered, if `mtvec` is set to zero, the emulator's trap handler is invoked and supports Newlib's system calls. If you install your own trap handler (non-zero `mtvec`), you are responsible for all trap behavior including system calls.
210211
- `EBREAK` traps with `a7 >= 0xFFFF0000` are used as a debug bridge, regardless of `mtvec`. See `riscv-py.h` for simple logging macros using this facility. These logging macros do not depend on Newlib.
211-
- The emulated architecure supports unaligned memory accesses and will not trap when they occur.
212+
- The emulated architecture supports unaligned memory accesses and will not trap when they occur.
212213
- The 64-bit registers `mtime` and `mtimecmp` are accessible via CSR instructions (rather than being memory-mapped) at addresses `0x7C0` (low 32 bits of `mtime`), `0x7C1` (high 32 bits of `mtime`), `0x7C2` (low 32 bits of `mtimecmp`), and `0x7C3` (high 32 bits of `mtimecmp`). Writes to `mtime` and `mtimecmp` are atomic for the whole 64-bit register and occur when the second word of the register is written.
213214

214215
### Performance notes

ram.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def initialize_ram(ram, fill='0x00'):
3838

3939
# "Fast" RAM class: no address checks
4040
class RAM:
41-
def __init__(self, size, init=None, logger=None):
41+
def __init__(self, size=1024*1024, init=None, logger=None):
4242
self.memory = bytearray(size)
4343
self.size = size
4444
self.logger = logger
@@ -87,7 +87,7 @@ def load_cstring(self, addr, max_len=1024):
8787

8888
# Safe RAM class: checks all addresses
8989
class SafeRAM:
90-
def __init__(self, size, init=None, logger=None):
90+
def __init__(self, size=1024*1024, init=None, logger=None):
9191
self.memory = bytearray(size)
9292
self.size = size
9393
self.logger = logger

riscv-emu.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
from ram import RAM, SafeRAM
2525
from syscalls import SyscallHandler
2626

27-
MEMORY_SIZE = 1024 * 1024 # 1 Mb
28-
2927
def parse_args():
3028
if "--" in sys.argv:
3129
split_index = sys.argv.index("--")
@@ -51,6 +49,7 @@ def parse_args():
5149
parser.add_argument("--start-checks", metavar="WHEN", default="auto", help="Condition to enable checks (auto, early, main, first-call, 0xADDR)")
5250
parser.add_argument("--init-regs", metavar="VALUE", default="zero", help='Initial register state (zero, random, 0xDEADBEEF)')
5351
parser.add_argument('--init-ram', metavar='PATTERN', default='zero', help='Initialize RAM with pattern (zero, random, addr, 0xAA)')
52+
parser.add_argument('--ram-size', metavar="KBS", type=int, default=1024, help='Emulated RAM size (kB, default 1024)')
5453
parser.add_argument('--timer', action="store_true", help='Enable machine timer')
5554
parser.add_argument("--raw-tty", action="store_true", help="Raw terminal mode")
5655
parser.add_argument("--no-color", action="store_false", help="Remove ANSI colors in terminal output")
@@ -107,6 +106,8 @@ def format(self, record):
107106
args.check_ram = True
108107
args.check_text = True
109108

109+
MEMORY_SIZE = 1024 * args.ram_size # (default 1 Mb)
110+
110111
log = logging.getLogger("riscv-emu")
111112
log.setLevel(logging.DEBUG)
112113

0 commit comments

Comments
 (0)