GithubHelp home page GithubHelp logo

sysprog21 / rv32emu-legacy Goto Github PK

View Code? Open in Web Editor NEW
48.0 3.0 18.0 33 KB

RISC-V RV32I[MA] emulator with ELF support

License: MIT License

Makefile 0.61% C 99.39%
risc-v rv32i emulator elf-parser

rv32emu-legacy's Introduction

RISC-V RV32I[MA] emulator with ELF support

This is a RISC-V emulator for the RV32I architecture, based on TinyEMU and stripped down for RV32I only.

Requires libelf-dev:

$ sudo apt-get install libelf-dev

How to compile it:

$ gcc -O3 -Wall emu-rv32i.c -o emu-rv32i -lelf

Run RV32I compliance tests. Assume emu-rv32i in $PATH environment variable.

$ git clone https://github.com/riscv/riscv-compliance
$ cd riscv-compliance
$ make RISCV_PREFIX=riscv-none-embed- RISCV_DEVICE=rv32i TARGET_SIM=emu-rv32i variant

Compiling and running simple code:

$ riscv32-unknown-elf-gcc -O3 -nostdlib test1.c -o test1

or

$ riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib test1.c -o test1

then

$ ./emu-rv32i test1
Hello RISC-V!

RV32M and RV32A instructions may be enabled by commenting #define STRICT_RV32I.

How to build RISC-V toolchain from scratch

https://github.com/riscv/riscv-gnu-toolchain

64-bit universal version (riscv64-unknown-elf-* that can build 32-bit code too):

$ ./configure --prefix=/opt/riscv
$ make

32-bit version (riscv32-unknown-elf-*):

$ ./configure --prefix=/opt/riscv32 --with-arch=rv32i --with-abi=ilp32
$ make

rv32emu-legacy's People

Contributors

hexrabbit avatar jserv avatar manbing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rv32emu-legacy's Issues

[Question] Zephyr support?

Can rv32emu run Zephyr applications?

In the source code, there are some comments regarding Zephyr RTOS, such as exit for Zephyr applications or for zephyr. I'm wondering rv32emu can run Zephyr applications. If so, could you tell me how to build Zephyr applications for rv32emu?

raise_exception: illegal instruction while trying to execute sub instruction

I notice an error raised by the emulator while I am trying to execute emu-rv32i on a binary (See the attached file). The error log is as follows.

tc@debian:~/rv32emu$ ./emu-rv32i ../a.out
begin_signature: 0x00000000
end_signature: 0x00000000
ram_start: 0x00010074
entry point: 0x000100a0
codesize: 0x0000189c (6300)
codesize with offset: 6416
[000100a0]=00002197, mtime: 2d93be3286, mtimecmp: 0
>>> AUIPC
[000100a4]=c4018193, mtime: 2d93be32e3, mtimecmp: 0
>>> ADDI
[000100a8]=c3018513, mtime: 2d93be32f7, mtimecmp: 0
>>> ADDI
[000100ac]=c4c18613, mtime: 2d93be3306, mtimecmp: 0
>>> ADDI
[000100b0]=45818e09, mtime: 2d93be3314, mtimecmp: 0
raise_exception: illegal instruction 0x2 0x45818e09
done interp 5 int=0 mstatus=0 prv=3

Registers:
x0 zero: 00000000
x1 ra:   00000000
x2 sp:   00020074
x3 gp:   00011ce0
x4 tp:   00000000
x5 t0:   00000000
x6 t1:   00000000
x7 t2:   00000000
x8 s0:   00000000
x9 s1:   00000000
x10 a0:  00011910
x11 a1:  00000000
x12 a2:  0001192c
x13 a3:  00000000
x14 a4:  00000000
x15 a5:  00000000
x16 a6:  00000000
x17 a7:  00000000
x18 s2:  00000000
x19 s3:  00000000
x20 s4:  00000000
x21 s5:  00000000
x22 s6:  00000000
x23 s7:  00000000
x24 s8:  00000000
x25 s9:  00000000
x26 s10: 00000000
x27 s11: 00000000
x28 t3:  00000000
x29 t4:  00000000
x30 t5:  00000000
x31 t6:  00000000

Instructions Stat:
AUIPC   = 1
ADDI    = 3

Five Most Frequent:
1) ADDI = 3 (60.00%)
2) AUIPC        = 1 (20.00%)
3) LUI  = 0 (0.00%)
4) LUI  = 0 (0.00%)
5) LUI  = 0 (0.00%)

Memory Reading Area 100a0...100b3
Memory Writing Area NONE

>>> Execution time: 16680 ns
>>> Instruction count: 5 (IPS=299760)
>>> Jumps: 0 (0.00%) - 0 forwards, 0 backwards
>>> Branching T=0 (-nan%) F=0 (-nan%)

By observing the objdump output, I notice the error was raised by the following line of instruction
ๅœ–็‰‡

objdump Output

objdump.txt

I am not sure if this is a bug in the emulator , the compiler tool-chain or it is a platform / code dependent issue. So I decide to create this issue to see if anyone can reproduce this issue.

System Information

Guest OS: Ubuntu 20.04
Host OS: Windows 10 Pro 21H1
Virtualization Technology: Virtual Box 6.1.28 r147628

Tool Chain version info (riscv-none-embed-gcc -v)

Using built-in specs.
COLLECT_GCC=riscv-none-embed-gcc
COLLECT_LTO_WRAPPER=/home/tc/riscv-none-gcc/bin/../libexec/gcc/riscv-none-embed/10.1.0/lto-wrapper
Target: riscv-none-embed
Configured with: /Host/home/ilg/Work/riscv-none-embed-gcc-10.1.0-1.1/linux-x64/sources/riscv-gcc-10.1.0-1.1/configure --prefix=/Host/home/ilg/Work/riscv-none-embed-gcc-10.1.0-1.1/linux-x64/install/riscv-none-embed-gcc --infodir=/Host/home/ilg/Work/riscv-none-embed-gcc-10.1.0-1.1/linux-x64/install/riscv-none-embed-gcc/share/doc/info --mandir=/Host/home/ilg/Work/riscv-none-embed-gcc-10.1.0-1.1/linux-x64/install/riscv-none-embed-gcc/share/doc/man --htmldir=/Host/home/ilg/Work/riscv-none-embed-gcc-10.1.0-1.1/linux-x64/install/riscv-none-embed-gcc/share/doc/html --pdfdir=/Host/home/ilg/Work/riscv-none-embed-gcc-10.1.0-1.1/linux-x64/install/riscv-none-embed-gcc/share/doc/pdf --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=riscv-none-embed --with-pkgversion='xPack GNU RISC-V Embedded GCC\x2C 64-bit' --with-bugurl=https://github.com/sifive/freedom-tools/issues --enable-languages=c,c++ --disable-mingw-wildcard --enable-plugins --enable-lto --enable-checking=yes --disable-tls --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-threads --with-gnu-as --with-gnu-ld --with-newlib --with-headers=yes --with-python-dir=share/gcc-riscv-none-embed --with-sysroot=/Host/home/ilg/Work/riscv-none-embed-gcc-10.1.0-1.1/linux-x64/install/riscv-none-embed-gcc/riscv-none-embed --with-native-system-header-dir=/include --with-abi=ilp32 --with-arch=rv32imac --disable-build-format-warnings --with-system-zlib
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 10.1.0 (xPack GNU RISC-V Embedded GCC, 64-bit)

Source Code

#include<stdio.h>
int main(){
  volatile int i, j, temp;
  volatile int nums[9]={0,1,1,1,2,2,2,0,1,0};
  volatile int numsSize=9;
        for (i = numsSize-1; i >=0; --i){
        for (j = 0; j < i ; ++j){
                        if (nums[j] > nums[j + 1]){
                                temp = nums[j];
                                nums[j] = nums[j + 1];
                                nums[j + 1] = temp;
                        }
                }
        }
}

Binary File

The source file is as follows.
a.zip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.