• Stars
    star
    393
  • Rank 109,518 (Top 3 %)
  • Language
    C
  • License
    MIT License
  • Created about 4 years ago
  • Updated 28 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compact and Efficient RISC-V RV32I[MAFC] emulator

RISC-V RV32I[MACF] emulator

GitHub Actions

                       /--===============------\
      ______     __    | |⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺|     |
     |  _ \ \   / /    | |               |     |
     | |_) \ \ / /     | |   Emulator!   |     |
     |  _ < \ V /      | |               |     |
     |_| \_\ \_/       | |_______________|     |
      _________        |                   ::::|
     |___ /___ \       '======================='
       |_ \ __) |      //-'-'-'-'-'-'-'-'-'-'-\\
      ___) / __/      //_'_'_'_'_'_'_'_'_'_'_'_\\
     |____/_____|     [-------------------------]

rv32emu is an emulator for the 32 bit RISC-V processor model, implementing the RISC-V instruction set architecture (ISA).

Features:

  • Fast interpreter for executing RV32 ISA
  • Full coverage of RV32I and M, A, C extensions
  • Partial support for F extension
  • Memory-efficient
  • Built-in ELF loader
  • Implementation of common newlib system calls
  • Experimental SDL-based display/event system calls for running video games
  • Support for remote GDB debugging

Build and Verify

rv32emu relies on certain third-party packages for full functionality and access to all its features. To ensure proper operation, the target system should have the SDL2 library installed.

  • macOS: brew install sdl2
  • Ubuntu Linux / Debian: sudo apt install libsdl2-dev

Install RISCOF.

python3 -m pip install git+https://github.com/riscv/riscof

Build the emulator.

make

Run sample RV32I[M] programs:

make check

Run Doom, the classical video game, via rv32emu:

make doom

The build script will then download data file for Doom automatically. When Doom is loaded and run, an SDL2-based window ought to appear.

If RV32F support is enabled (turned on by default), Quake demo program can be launched via:

make quake

The usage and limitations of Doom and Quake demo are listed in docs/demo.md.

RISCOF

RISCOF - RISC-V Compatibility Framework is a python based framework which enables testing of RISC-V target against a golden reference model.

The RISC-V Architectural Tests, also known as riscv-arch-test, provides the fundamental set of tests that can be used to confirm that the behavior of the risc-v model adheres to the RISC-V standards while executing certain applications. (not intended to replace thorough design verification)

There are reference signatures that generated by the formal RISC-V model RISC-V SAIL in the Executable and Linkable Format (ELF) files. ELF files that have multiple testing instructions, data, and signatures, like cadd-01.elf. The specific data places that must be written by the testing model (this emulator) throughout the test are known as test signatures. The test signatures will be written after it has been completed, and they will be compared to the reference signature. When both signatures exactly match, the test is successful.

RISC-V GNU Compiler Toolchain should be prepared in advance. You can obtain prebuilt GNU toolchain for riscv32-elf via Automated Nightly Release. Then, run the following command:

make arch-test

Users of macOS might need to install sdiff first.

brew install diffutils

To run the tests for specific extension, set the environmental variable RISCV_DEVICE to one of I, M, C, Zifencei, privilege.

make arch-test RISCV_DEVICE=I

Current progress of this emulator in riscv-arch-test (RV32):

  • Passed Tests
    • I: Base Integer Instruction Set
    • M: Standard Extension for Integer Multiplication and Division
    • C: Standard Extension for Compressed Instruction
    • Zifencei: Instruction-Fetch Fence
    • privilege: RISCV Privileged Specification
  • Unsupported tests (runnable but incomplete)
    • F Standard Extension for Single-Precision Floating-Point

Detail in riscv-arch-test:

Customization

rv32emu is configurable, and you can override the below variable(s) to fit your expectations:

  • ENABLE_EXT_M: Standard Extension for Integer Multiplication and Division
  • ENABLE_EXT_A: Standard Extension for Atomic Instructions
  • ENABLE_EXT_C: Standard Extension for Compressed Instructions (RV32C.F excluded)
  • ENABLE_EXT_F: Standard Extension for Single-Precision Floating Point Instructions
  • ENABLE_Zicsr: Control and Status Register (CSR)
  • ENABLE_Zifencei: Instruction-Fetch Fence
  • ENABLE_GDBSTUB : GDB remote debugging support
  • ENABLE_SDL : Experimental Display and Event System Calls

e.g., run make ENABLE_EXT_F=0 for the build without floating-point support.

GDB Remote Debugging

rv32emu is permitted to operate as gdbstub in an experimental manner since it supports a limited number of GDB Remote Serial Protocol (GDBRSP). You must first build the emulator and set ENABLE_GDBSTUB to 1 in the Makefile in order to activate this feature. After that, you might execute it using the command below.

build/rv32emu --gdbstub <binary>

The <binary> should be the ELF file in RISC-V 32 bit format. Additionally, it is advised that you compile programs with the -g option in order to produce debug information in your ELF files.

You can run riscv-gdb if the emulator starts up correctly without an error. It takes two GDB commands to connect to the emulator after giving GDB the supported architecture of the emulator and any debugging symbols it may have.

$ riscv32-unknown-elf-gdb
(gdb) file <binary>
(gdb) target remote :1234

Congratulate yourself if riscv-gdb does not produce an error message. Now that the GDB command line is available, you can communicate with rv32emu.

Command line options

Dump registers as JSON

If an option --dump-registers [filename] is specified, the emulator outputs registers as JSON format. This can be used for tests using the emulator, such as compiler tests.

You can use the option with --quiet to use the output directly.

# Read the register x10 (a0).
$ build/rv32emu --dump-registers - out.elf --quiet | jq .x10

Contributing

See CONTRIBUTING.md for contribution guidelines.

External sources

In rv32emu repository, there are some prebuilt ELF files for testing purpose.

Reference

License

rv32emu is available under a permissive MIT-style license. Use of this source code is governed by a MIT license that can be found in the LICENSE file.

More Repositories

1

lkmpg

The Linux Kernel Module Programming Guide (updated for 5.0+ kernels)
TeX
7,496
star
2

shecc

A self-hosting and educational C optimizing compiler
C
1,119
star
3

lab0-c

C Programming Lab: Assessing Your C Programming Skills
C
407
star
4

simplefs

A simple native file system for Linux kernel
C
372
star
5

concurrent-programs

Complementary Concurrency Programs for course "Linux Kernel Internals"
C
301
star
6

jitboy

A Game Boy emulator with dynamic recompilation (JIT)
C
299
star
7

semu

A minimalist RISC-V system emulator capable of running Linux kernel
C
252
star
8

cpumemory-zhtw

Traditional Chinese translation of "What Every Programmer Should Know About Memory"
CSS
216
star
9

vwifi

A virtual wireless device driver for Linux
C
201
star
10

kvm-host

A minimalist type 2 hypervisor using Linux Kernel Virtual Machine (KVM)
C
152
star
11

pitifulvm

A shabby implementation of Java virtual machine in C
C
138
star
12

vcam

Virtual camera device driver for Linux
C
89
star
13

sehttpd

A small and efficient web server with 1K lines of C code
C
82
star
14

concurrency-primer

Concurrency Primer
TeX
75
star
15

cserv

An event-driven and non-blocking web server
C
70
star
16

concurrent-ll

concurrent linked list implementation
C
68
star
17

khttpd

An experimental HTTP server implemented as Linux kernel module
C
60
star
18

rv32emu-legacy

RISC-V RV32I[MA] emulator with ELF support
C
47
star
19

raycaster

Wolfenstein 3D-style raycasting implementation
C
44
star
20

linux-list

Linux-like doubly-linked list
C
39
star
21

fibdrv

Linux kernel module that calculates Fibonacci numbers
Shell
37
star
22

gameboy-emu

An efficient and portable Game Boy emulator
C
36
star
23

mado

A window system for resource-constrained devices
C
34
star
24

lkm-hidden

A Linux kernel module which hides itself
C
29
star
25

kecho

A lightweight echo server implementation in Linux kernel mode
C
27
star
26

rubi

Ruby-like high-performance script programming language with JIT compilation
C
25
star
27

threaded-logger

Threaded Logger
C
21
star
28

threadkit

A collection of lightweight threading utilities
C
20
star
29

vinput

A collection of virtual input device drivers for Linux
C
20
star
30

linux-cfs-sim

Simulate Linux Completely Fair Scheduler (CFS) using POSIX Threads
C
18
star
31

rnnoise

A noise suppression library based on a recurrent neural network
C
17
star
32

kcalc

Math expression evaluation as Linux kernel module
C
17
star
33

dict

Ternary Search Tree + Bloom filter
C
15
star
34

jitcalc

A simple integer calculator using JIT compilation
C
15
star
35

y86_64-tools

Y86-64 Tools: assembler, simulator, Verilog designs
C
14
star
36

fastcat

A faster "cat" implementation using splice and sendfile system calls
C
13
star
37

neocon

A simple serial console utility
C
13
star
38

bignum

An incomplete arbitrary-precision integer arithmetic library
C
13
star
39

fiber

A User Space Threading Library
C
13
star
40

compute-pi

Leibniz formula for π
C
12
star
41

ca2023-lab3

Lab3: Construct a single-cycle CPU with Chisel
Scala
12
star
42

mapreduce

A simple C Thread pool implementation
C
12
star
43

prefix-search

Implement prefix search using ternary search tree
C
12
star
44

jit-construct

JIT compiler from scratch, derived from Nick Desaulniers' great work
Lua
11
star
45

datalab

Improved CS:APP Data Lab
C
9
star
46

buddy

Buddy Memory Allocator
C
8
star
47

moxiebox

A secure, sandboxed execution mechanism that enables deterministic input, processing and output
C
8
star
48

phonebook

sample phonebook program to illustrate the impact of cache miss
Shell
8
star
49

raytracing

Small ray tracing program for performance evaluation
C
8
star
50

intrusive-ds

A collection of intrusive data-structures for C
C
8
star
51

kilo

A text editor in less than 1000 LoC with syntax highlight and search
C
8
star
52

gecos

GECOS: A lock-free synchronization mechanism
C
7
star
53

nyancat

Nyancat rendered in your terminal
C
6
star
54

matrix_oo

Sample matrix implementation illustrating object-oriented techniques in C99
Shell
6
star
55

dont-trace

A simple Linux kernel module that kills ptrace tracer and its tracees
C
6
star
56

kfifo-examples

Linux kernel module examples about kfifo
C
5
star
57

mergesort-concurrent

merge sort on singly-linked list utilzing POSIX Thread
C
5
star
58

tinymembench

Measure peak bandwidth of sequential memory accesses and the latency of random memory accesses
C
5
star
59

cirbuf

Circular Buffer implementation with mmap(2) *incomplete*
C
4
star
60

align-bench

Microbenchmark for unaligned memory access
C
4
star
61

kcalc-fixed

Math expression evaluation as Linux kernel module, fixed-point implementation
C
4
star
62

malloc-test-concurrent

concurrent malloc benchmark
C
3
star
63

prefetcher

Evaluate the effects of prefetching
Shell
3
star
64

sched-plugin

A Linux kernel module to allow user processes being handed out with LKM based scheduler
C
3
star
65

phonebook-concurrent

build a phonebook program by concurrent linked list
C
3
star
66

simrupt

A Linux device driver that simulates interrupts
C
3
star
67

tco-test

Test the ability of C compilers performing Tail Call Optimization
C
2
star
68

vsnd

Virtual Linux soundcard driver
C
2
star
69

rv32emu-demo

HTML
2
star
70

balanced-ternary

Ilustrate how balanced ternary works
Shell
2
star
71

bf-runtime

Brainf*ck runtime engine
C
1
star
72

quotient-filter

(Incomplete) in-memory quotient filter
C
1
star
73

ksort

Linux kernel module that implements and validates sorting algorithms
C
1
star
74

arm-assembler-latex-listings

Arm Assembler language definition for the LaTeX listings package
TeX
1
star
75

clz-tests

Evaluate implementations of count leading zero
C
1
star