Skip to main content

RISC-V userspace and privileged emulator

Project description

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 asembly 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 aribtrary 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 memeory, 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.

debuggin 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 runnint 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

riscemu-2.0.0a4.tar.gz (50.2 kB view details)

Uploaded Source

Built Distribution

riscemu-2.0.0a4-py3-none-any.whl (62.0 kB view details)

Uploaded Python 3

File details

Details for the file riscemu-2.0.0a4.tar.gz.

File metadata

  • Download URL: riscemu-2.0.0a4.tar.gz
  • Upload date:
  • Size: 50.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for riscemu-2.0.0a4.tar.gz
Algorithm Hash digest
SHA256 2d11cfa48641b3a14268d013f67e267935a1d976ef364696b4d118f18ab75f79
MD5 f5c7ad925de4c19140f3edcb90bf1839
BLAKE2b-256 384bffcdb7f407f85a9d27219e559bdb00e199f8508860869a66394ac138597d

See more details on using hashes here.

File details

Details for the file riscemu-2.0.0a4-py3-none-any.whl.

File metadata

  • Download URL: riscemu-2.0.0a4-py3-none-any.whl
  • Upload date:
  • Size: 62.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for riscemu-2.0.0a4-py3-none-any.whl
Algorithm Hash digest
SHA256 8980da833b9eb2899c5e0976ead24ea20eed20bf403a4106db9ad7dabad05e77
MD5 294190ea9b49442a84d8a1f5099f31f3
BLAKE2b-256 f28a234ded46143217e115e732bb9db20bfe1b8f17c5a9d8731794aff4eb27f8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page