|
| 1 | +# RISC-V ISA Simulator |
| 2 | + |
| 3 | +This project is a course assignment for the **Computer Architecture and Organization Lab** at **Peking University** in the **Fall semester of 2025**, developed as an extension of the framework provided by the course. |
| 4 | + |
| 5 | +## ✨ Features |
| 6 | + |
| 7 | +* Implements a majority of the instructions from the RV64IM instruction set. |
| 8 | +* Implements the **`exit`** and **`write`** system calls (with `write` currently supporting output only to **stdout**), and enables the use of **`printf`** within the virtual RISC-V environment. |
| 9 | +* Includes a simple built-in debugger with support for single-stepping, printing registers, and scanning memory. |
| 10 | +* Supports function call tracing (**ftrace**). |
| 11 | +* Supports instruction execution history logging (**itrace**). |
| 12 | + |
| 13 | +``` |
| 14 | +. |
| 15 | +├── .github/workflows/ # GitHub CI/CD workflow configurations |
| 16 | +├── build/ # Directory for compiled artifacts |
| 17 | +├── doc/ # Directory for docs about the implement of some features |
| 18 | +├── sim/ # Simulator core source code |
| 19 | +│ ├── include/ # Header files |
| 20 | +│ └── src/ # Source files |
| 21 | +├── test/ # Test cases |
| 22 | +│ ├── include/ |
| 23 | +│ ├── lib/ |
| 24 | +│ ├── src/ # Test program source code |
| 25 | +│ └── trm/ # Trap and Run Machine environment |
| 26 | +├── trace/ # Reference trace files for instruction execution |
| 27 | +├── Dockerfile # Dockerfile for building the development environment |
| 28 | +├── docker-compose.yml # Docker Compose configuration file |
| 29 | +└── Makefile # Main project Makefile |
| 30 | +``` |
| 31 | + |
| 32 | +## 🚀 Getting Started |
| 33 | + |
| 34 | +### Prerequisites |
| 35 | + |
| 36 | +This project is best built and run using Docker to avoid the complexities of local environment setup. |
| 37 | + |
| 38 | +* [Docker](https://www.docker.com/) |
| 39 | +* [Docker Compose](https://docs.docker.com/compose/) |
| 40 | + |
| 41 | +If you prefer to build locally, you will need to install the RISC-V GNU toolchain and other build tools. |
| 42 | +* `riscv64-unknown-elf-gcc` |
| 43 | +* `make` |
| 44 | +* `gcc`/`g++` |
| 45 | +* lib `llvm` and `libelf` |
| 46 | + |
| 47 | +### Build and Run (Docker) |
| 48 | + |
| 49 | +1. **Start the Docker container:** |
| 50 | + This command will build the image and create a container named `simulator_dev` based on `docker-compose.yml`. |
| 51 | + |
| 52 | + ```bash |
| 53 | + docker-compose up --build -d |
| 54 | + ``` |
| 55 | + |
| 56 | +2. **Build the project and run all test:** |
| 57 | + |
| 58 | + ```bash |
| 59 | + docker-compose exec dev make test_all |
| 60 | + ``` |
| 61 | + |
| 62 | +### Build and Run (Local) |
| 63 | + |
| 64 | +1. **Install dependencies:** |
| 65 | + Ensure you have installed all the tools listed in the [Prerequisites](#prerequisites) section. |
| 66 | + |
| 67 | +2. **Build the project and run all test** |
| 68 | + Run the `make` command in the project root directory. |
| 69 | + |
| 70 | + ```bash |
| 71 | + make test_all |
| 72 | + ``` |
| 73 | + |
| 74 | +## 🎮 Usage |
| 75 | + |
| 76 | +### Running Test Programs |
| 77 | + |
| 78 | +You can run any test program located in the project's root directory. All test programs are compiled into the `test/build` directory. |
| 79 | +
|
| 80 | +For example, to run the `quicksort` test: |
| 81 | +```bash |
| 82 | +docker-compose exec dev make T=quicksort |
| 83 | +``` |
| 84 | +
|
| 85 | +### Debug Mode |
| 86 | +
|
| 87 | +Start the simulator in debug mode by the make command below: |
| 88 | +```bash |
| 89 | +docker-compose exec dev make debug T=XXX |
| 90 | +``` |
| 91 | +
|
| 92 | +In debug mode, you can use the following commands: |
| 93 | +
|
| 94 | +- `help`: Print the help message of debug commands |
| 95 | +
|
| 96 | +* `si [N]`: Execute a single instruction if `arg N` not provided, or execute N instructions. |
| 97 | +* `c`: Continue execution until the program finishes. |
| 98 | +* `info r`: Print the status of all general-purpose registers (include PC). |
| 99 | +* `x <N> <EXPR>`: Scan `N` 4-byte words of memory starting at address `EXPR`. |
| 100 | +
|
| 101 | +### Other Commands |
| 102 | +
|
| 103 | +To run the Simulator in `itrace`/`ftrace` mode: |
| 104 | +
|
| 105 | +```sh |
| 106 | +docker-compose exec dev make itrace T=XXX |
| 107 | +docker-compose exec dev make ftrace T=XXX |
| 108 | +``` |
| 109 | +
|
| 110 | +## ✅ Testing |
| 111 | +
|
| 112 | +If you want to add your own test cases, place your test source file (`.c` file) in the **`/test/src`** directory. |
| 113 | + For example, if your test is named **`sample`**, you can run it with the following command: |
| 114 | +
|
| 115 | +```bash |
| 116 | +docker-compose exec dev make T=sample |
| 117 | +``` |
0 commit comments