AntonLydike / riscemu

RISC-V emulator in python
MIT License
48 stars 13 forks source link

RuntimeError("No write pls") #6

Closed tobiasgrosser closed 2 years ago

tobiasgrosser commented 2 years ago
        addi    a0, zero, 1
        addi    sp, sp, 4
        li      t1, 1
        sw      t1, -4(sp)

this code breaks with:

Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/grosser/Projects/riscemu/riscemu/__main__.py", line 103, in <module>
    cpu.run_loaded(loaded_exe)
  File "/home/grosser/Projects/riscemu/riscemu/CPU.py", line 100, in run_loaded
    self._run()
  File "/home/grosser/Projects/riscemu/riscemu/CPU.py", line 140, in _run
    self.run_instruction(ins)
  File "/home/grosser/Projects/riscemu/riscemu/CPU.py", line 168, in run_instruction
    self.instructions[ins.name](ins)
  File "/home/grosser/Projects/riscemu/riscemu/instructions/RV32I.py", line 56, in instruction_sw
    self.mmu.write(addr, 4, int_to_bytes(self.regs.get(rd), 4))
  File "/home/grosser/Projects/riscemu/riscemu/MMU.py", line 180, in write
    raise RuntimeError("No write pls")

while it works in https://www.kvakil.me/venus/. Any idea what I am doing wrong?

AntonLydike commented 2 years ago

It seems you are writing outside of allocated memory (yes, the error message could probably be improved). The error seems to be your use of the stack pointer, sp should be decremented (stack grows "down") in this emulator (I think that's how I learned it in uni, but I always get the directions confused).

Have you tried:

        addi    a0, zero, 1
        addi    sp, sp, -4
        li      t1, 1
        sw      t1, -4(sp)
tobiasgrosser commented 2 years ago

Indeed. Let's close this for now.