Skip to main content

RiscEmu - RISC-V (userspace) emulator in python

Documentation Status

Implementing a basic RISC-V emulator, aimed at being easily extendable. Check out the docs at readthedocs or riscemu.datenvorr.at.

This emulator contains:

  • RISC-V Assembly parser
  • RISC-V Assembly loader
  • Emulation for most parts of the basic RISC-V instruction set and the M and A extensions
  • Naive memory emulator
  • Basic implementation of some syscalls
  • A debugging environment

Installation:

$ pip install riscemu

Running simple Assembly:

A couple of basic assembly programs are provided inside examples/, such as hello-world.asm.

You can run it by typing python -m riscemu examples/hello-world.asm. It will produce output similar to:

[MMU] Successfully loaded: LoadedExecutable[examples/hello-world.asm](base=0x00000100, size=24bytes, sections=data text, run_ptr=0x00000110)
[CPU] Started running from 0x00000110 (examples/hello-world.asm)
Hello world

Program exited with code 0

The read syscall defaults to readline behaviour. Reading "true chunks" (ignoring newlines) is currently not supported.

See the docs on assembly for more detail on how to write assembly code for this emulator. See the list of implemented syscalls for more details on how to syscall.

Currently, symbols (such as main or loop) are looked-up at runtime. This allows for better debugging, I believe.

Basic IO should work, as open, read, write and close are supported for stdin/stdout/stderr and even arbitrary file paths (if enabled)

When trying to run an assembly program, the emulator first tries to find a symbol named _start, then a symbol named main. if both symbols were not found in the file, it simply starts at the beginning of the .text segment.

Using the CLI:

Current CLI is not final, options may change frequently until a stable version is reached

This is how the interface is used:

usage: riscemu [-h] [--options OPTIONS] [--syscall-opts SYSCALL_OPTS] [--instruction-sets INSTRUCTION_SETS] [--stack_size stack-size] file.asm [file.asm ...]



OPTIONS and SYSCALL_OPTIONS is a list of comma-separated flags that will be enabled

--options OPTIONS: (-o)
disable_debug           Disable the ebreak and sbreak instructions
no_syscall_symbols      Don't make syscall symbols globally available
fail_on_ex              Do not launch an interactive debugger when the CPU loop catches an exception
add_accept_imm          accept "add rd, rs, imm" instructions, even though they are not standard

--syscall-opts SYSCALL_OPTS: (-so)
                        Options to control syscall behaviour
fs_access               Allow access to the filesystem
disable_io              Disallow reading/writing from stdin/stdout/stderr

--instruction-sets INSTRUCTION_SETS: (-is)
                        A list of comma separated instruction sets you want to load:
                        Currently implemented: RV32I, RV32M

If multiple files are specified, all are loaded into memory, but only the last one is executed. This might be improved later, maybe the _init section of each binary is executed before the main loop starts?

If stack_size is greater than zero, a stack is allocated and initialized, with the sp register pointing to the end of the stack.

Debugging

Debugging is done using the ebreak (formerly sbreak) instruction, which will launch a debugging session if encountered. See docs/debugging.md for more info.

debugging the fibs program

The source code:

Check out the documentation.

Accessing local documentation:

To generate your local documentation, first install everything in sphinx-docs/requirements.txt. Then run ./generate-docs.sh, which will generate and make all doc files for you. Finally, you can open the docs locall by running open sphinx-docs/build/html/index.html.

Resources:

TODO:

  • Correctly handle 12 and 20 bit immediate (currently not limited to bits at all)
  • Add a cycle limit to the options and CPU to catch infinite loops
  • Move away from print and use logging.logger instead
  • Writer proper tests

How To Release:

Create a new commit that:

  1. Changes the "Upcoming" heading to the new versions number
  2. Increments the version in the pyproject.toml to the next version

Commit this, and tag it with v<version>. Push the commit and the tag:

git push
git push origin "v<version>"

On GitHub, draft a new release, and then approve the workflow run here.

Metadata

Release files for riscemu 2.2.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for riscemu 2.2.7
File Size Uploaded Provenance
riscemu-2.2.7.tar.gz 63.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for riscemu 2.2.7
File Interpreter ABI Platform
riscemu-2.2.7-py3-none-any.whl Python 3 none any Details

Total release size: 146.6 kB

Release files / riscemu-2.2.7.tar.gz

Download URL riscemu-2.2.7.tar.gz
Size 63.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9f206b195371cef4246a0d04cef6a439dc026396860c19a8850a761fd700c499
BLAKE2b-256 checksum
How to use checksums
66f63aff01fdfc277cd2c2eecb153c39daa521624b41463e297a5e4214f7df1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / riscemu-2.2.7-py3-none-any.whl

Download URL riscemu-2.2.7-py3-none-any.whl
Size 83.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8539c439e641ac7a0bb77d8f5039d664518d0ebaa26205cf4718573aa7c63a5
BLAKE2b-256 checksum
How to use checksums
506cef41c869bd557f7a3e81ccea97aa8f57e4f8d637c24431299aa0547902ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Security for riscemu 2.2.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page