From 234221cbff0997d7b31357b884396e6845b8704f Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Fri, 6 Dec 2024 23:11:24 +0000 Subject: [PATCH] Add readme file and make the example compile --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ example/Makefile | 2 ++ example/example.c | 4 ++-- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f3ce66 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# A simple RISC-V emulator + +This is a toy emulator for RISC-V, made for educational purposes. +The goal is to have a base rv32i instruction set (the bare minimum) plus a M-extension for division and multiplication. In theory, it should be enough to execute simple C programs compiled with GCC and sprinkled with a few linker scripts. Of course, no libc because there's no OS. + +## Compiling and running + +You'd need nix. Install it and run: + +```sh +nix develop + +eval "$configurePhase" +ninja +``` + +You should get the `rve` binary. Run it: + +```sh +./rve +``` + +At the moment the program is hardcoded into the `main()` function directly in machine code. As a follow-up, I'm working on compiling programs with the gcc cross-toolchain, but it will require some time to fiddle with options. + +## Cross-toolchain and an example program in C + +There is an example program which you can compile, but not yet run. Do this: + +```sh +cd example +make +``` + +As a result, you'll get an `example` binary. To view the disassembly: + +```sh +riscv32-none-elf-objdump -D example +``` + +As you can see, it's quite barebones, with the program starting at 0x1000. The reason it can't yet be run with the emulator is mostly in the absence of the boot section which initializes the stack and calls the main function. Stay tuned. diff --git a/example/Makefile b/example/Makefile index 57666ed..1f98c74 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,2 +1,4 @@ example: example.c riscv32-none-elf-gcc -fno-builtin -fvisibility=hidden -nostdlib -nostartfiles -march=rv32im -mabi=ilp32 -o example example.c -T linker.ld + riscv32-none-elf-strip -R .riscv.attributes example + riscv32-none-elf-strip -R .comment example diff --git a/example/example.c b/example/example.c index 460520c..483913d 100644 --- a/example/example.c +++ b/example/example.c @@ -1,9 +1,9 @@ -static int mem; +static int mem = 56; int main() { int a = 42; int b = 5; - mem = a*b + 3; + mem = mem + a*b + 3; return 0; }