GithubHelp home page GithubHelp logo

bit-hack / riscv-vm Goto Github PK

View Code? Open in Web Editor NEW
65.0 7.0 14.0 1.89 MB

A Small RISC-V Virtual Machine

License: MIT License

CMake 0.86% C++ 11.02% C 87.55% Objective-C 0.58%
risc-v simulator c c-plus-plus isa-emulator virtual-machine

riscv-vm's Introduction

RISC-V Virtual Machine

This is a RISCV-V Virtual Machine and instruction set emulator implementing a 32 bit RISCV-V processor model. I started this project as a learning exercise to get more familiar with the RISC-V eco system and have increased the scope of the project as it matures. The project itself is still very much in the early stages however.

Features:

  • Support for RV32I and RV32M
  • Partial support for RV32F and RV32A
  • Syscall emulation and host passthrough
  • Emulation using Dynamic Binary Translation
  • It can run Doom, Quake and SmallPT

There are broadly two elements to this project:

  • An ISA emulator core written in C which presents a low level API for interfacing.
  • The VM frontend written in C++ which interfaces the ISA emulator core with the host computer.

Note: The Binary Translation emulator is currently only available when building for x64 Windows. This is due to the generated code being tailored to that ABI, however in time Linux support for the code generator will follow.

See news for a development log and updates.


Build Status

Build Status Build status


Build requirements

  • C++ 14 compatible compiler (VisualStudio 15+, GCC 4.9+)
  • CMake 3.0 or above
  • SDL1.2 (if you want video support)

Compiling the project

mkdir build
cd build
cmake ..
make

Executing a basic program

A simple RISC-V program (i.e. main.c) can be compiled and executed as follows:

riscv64-unknown-elf-gcc -march=rv32im -mabi=ilp32 main.c
riscv_vm a.out

Testing

Please note that while the riscv-vm simulator is provided under the MIT license, any of the materials in the tests folder may not be. These tests were taken from a wide range of places to give the project a reasonable test coverage. If you are the author of any of these tests and are unhappy with its inclusion here then please get in touch and I will remove it from the project and its git history.


Notes

  • Written referencing the 20191214-draft Volume I: Unprivileged ISA document
  • Complete RV32G compliance is the end goal
  • Testing is done currently using a private fork of the compliance test suite
    • As the project develops testing will become a key focus
  • It is enough that it can run doom, see the README

Newlib support

There is rudimentary support for target programs that make use of the newlib library. Currently a number of syscalls have been implemented via the ecall instruction. This is just enough to run a number of simpler programs and do some basic file io operations.

Try the following example program:

#include <stdio.h>

int main(int argc, const char **args) {
  printf("Hello World!\n");
  return 0;
}

Spoiler:

Hello World!

riscv-vm's People

Contributors

bit-hack 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

riscv-vm's Issues

Error on Make

I get a few errors when I call make:

Building CXX object CMakeFiles/riscv_vm.dir/riscv_vm/syscall.cpp.o syscall.cpp: In function ‘void syscall_write(riscv_t*)’:

syscall.cpp:97:29: error: ‘alloca’ was not declared in this scope 97 | uint8_t *temp = (uint8_t*)alloca(count);

syscall.cpp:231:29: error: ‘alloca’ was not declared in this scope 231 | uint8_t *temp = (uint8_t*)alloca(count); | ^~~~~~ make[2]: *** [CMakeFiles/riscv_vm.dir/build.make:104: CMakeFiles/riscv_vm.dir/riscv_vm/syscall.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:196: CMakeFiles/riscv_vm.dir/all] Error 2 make: *** [Makefile:91: all] Error 2

How negative value in a registers are handled?

Could @bit-hack please explain how negative values in registers are handled?
For example say ADDI r5, r0, 100 and say r0 has value -100 (in 2's complement)
will following give correct answer ?

82    case 0: // ADDI    Aidan Dodds, 20/09/20 16:10 • tidy and fix SRAI bug                                                                                                                                      
  1     rv->X[rd] = (int32_t)(rv->X[rs1]) + imm;                                                                                                                                                                  
  2     break;          

because X stores unsigned integer AFAIU then above seems not handled correctly.

Understand how ELF is used within riscv-vm.

  1. How to locate the start of actual instruction from the .text section and how to figure out where to stop?
  2. I see that your implementation reads all PT_LOAD sections into a buffer but then how to distinguish between .text (.rodata) and .data ?

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.