Git Product home page Git Product logo

rvemu-for-book's Introduction

rvemu-for-book

Reference implementation of the book, Writing a RISC-V Emulator from Scratch in 10 Steps. The goal of this code and the book is runnning xv6 in our emulator.

This is based on original RISC-V emulator rvemu in Rust.

How to run xv6

$ cd step10 // move to the step10 directory
$ cargo run ./xv6-kernel.bin ./xv6-fs.img

demo.png

Step to implement a RISC-V emulator

See https://book.rvemu.app/

  • Step 1: Setup and Implement Two Instructions
  • Step 2: RV64I Base Integer Instruction Set
  • Step 3: Control and Status Registers
  • Step 4: Privileged Instruction Set
  • Step 5: Exceptions
  • Step 6: UART (a universal asynchronous receiver-transmitter)
  • Step 7: PLIC (a platform-level interrupt controller) and CLINT (a core-local interrupter)
  • Step 8: Interrupts
  • Step 9: Virtio
  • Step 10: Virtual Memory System

Each step has a diff file generated by diff -x target -r step<previous> step<current> > diff_<previous>_<current>.

rvemu-for-book's People

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

rvemu-for-book's Issues

Incorrect opcode?

0x13 => {
// addi
let imm = ((inst & 0xfff00000) as i32 as i64 >> 20) as u64;
self.regs[rd] = self.regs[rs1].wrapping_add(imm);

You mention that this is the instruction for adding 64-bit integers. From the RISC-V spec this opcode is only for 32-bit integers.

How to disable the C extension

Hello, I compiled the xv6 kernel myself and find the compiler uses the C extension of riscv, so the emulator failed to run the binary.
How did you modify the makefile of xv6 to disable the C extension?

Question : why read/write SIE register is different from other register?

Excuse me ,I am a new hand, and the particularity of SIE confuses me a lot.
Could me tell me why read/write SIE register is different from other register?

    /// Load a value from a CSR.
    pub fn load_csr(&self, addr: usize) -> u64 {
        match addr {
            SIE => self.csrs[MIE] & self.csrs[MIDELEG],
            _ => self.csrs[addr],
        }
    }

    /// Store a value to a CSR.
    pub fn store_csr(&mut self, addr: usize, value: u64) {
        match addr {
            SIE => {
                self.csrs[MIE] =
                    (self.csrs[MIE] & !self.csrs[MIDELEG]) | (value & self.csrs[MIDELEG]);
            }
            _ => self.csrs[addr] = value,
        }
    }

A completed tutorial based on yours

Thanks for your kindness. You have written such a tutorial and shared your code, which encourages me to learn it and complete the tutorial.

The new project is available at here and the online book is hosted on github page.

I have used most of your code and follow the structure of yours. I hope this tutorial will be helpful for anyone who want to learn about RISC-V emulator.

Hope to see the book soon!

hey, d0iasm,
your project rvemu is awesome and makes me excting! but the rvemu for book seems not to be finished, i really look forworad your book! Hope to see it soon!

Endianess of RISC-V

The section explaining why rvemu is little-endian may mislead readers because it is not precise enough.

While the base memory system does not need to be little-endian with regards to instruction decoding, the current standardized memory operations do have to be little-endian. I think this may have been a requirement added after the 1.0 release of the specification, but I don't want to track down when it was added.

From: https://book.rvemu.app/setup-and-implement-two-instructions

RISC-V has either little-endian or big-endian byte order, but our emulator will implement a little-endian system since it is currently dominant commercially like x86 systems.

Section 1.2 of RISC-V Specification v2.0

The base RISC-V ISA has a little-endian memory system, but non-standard variants can provide a big-endian or bi-endian memory system

The section goes on to explain the exact (rather complex) memory requirements of the RISC-V.

Section 2.6 of RISC-V Specification v2.0

RV32I provides a 32-bit user address space that is byte-addressed and little-endian.

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.