Writing a RISC-V Emulator from Scratch in 10 Steps

Introduction

This is the book for writing a 64-bit RISC-V emulator from scratch in Rust. It shows us how to implement an emulator in 10 steps. You can run xv6, a simple Unix-like OS, in your emulator in the final step.

You'll learn the following basic computer architecture from making an emulator in Rust:

  • Basic RISC-V architecture

  • How to write code in Rust language

  • Privilege levels

  • Exceptions

  • Interrupts

  • Peripheral devices

  • Virtual memory system

The source code is available at d0iasm/rvemu-for-book.

Step

Content

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

โ€‹PLIC (a platform-level interrupt controller) and CLINT (a core-local interrupter)โ€‹

Step 7

โ€‹UART (a universal asynchronous receiver-transmitter)โ€‹

Step 8

โ€‹Interruptsโ€‹

Step 9

Virtio

Step 10

Virtual Memory System

Congratulations๐ŸŽ‰ Now you can run xv6 in your emulator!

The author is @d0iasm and please feel free to ask and request anything to me via Twitter or GitHub issues!