rv32emu
whitch is an instruction set architecture (ISA) emulator implementing the 32 bit RISC-V processor modelsudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
git clone https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain/
sudo ./configure –prefix=/opt/riscv –enable-multilib
sudo make
export PATH=$PATH:/opt/riscv/bin
riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32 test.c -o test
syscall_sdl.c
we can see that it use three register, a0
is the address of pixel array which SDL will use it to render the scrren, a1
is the width of the screen, and a2
is the height of the scrren。
syscall.c
, we can see that it use fake systemcall number 0xbeef
and 0xbabe
to call SDL, so we have to set the corresponding number to a7
register in order to call it。
register int a asm("a0") = width
just like li a0,800
, and asm volatile("scall")
is ecall
。register int a asm("a0") = width
in a function, it may be discard by compiler due to optimizetion, to avoid that, we can write register int a asm("a0") = width; asm volatile (""::"r"(a0));
instead, the volatile
keyword will prevent optimiztion。change add_subdirectory to boards/emulator
, then we can build quake in another way
here we must add m
in target_link_libraries
, otherwise it will not link math.h library and cause undefied reference if we use function in math.h。
We can find it in /port/boards/stm32h747i_disco/gcc/ and replace it to project root directory
Change the toolchain from arm to x86, then add some neccessary C_FLAGS and remove all unecessary C_FLAGS
Dowload shareware data and place it in project root directory
There is a little error that we have to fixed, and some code we have to change in winquake source code, in order to run quake correctly。
Here, I write the build step to a shell script for convenience
Since we successfully run the emulator code in x86 ISA, we can now modify it to other ISA easyly by making some essential change。
We can specify the toolchain and the flag in this file, and when we use cmake, use -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake flag to Cross Compiling
Remove minifb in target_link_libraries, since we will use rv32emu SDL systemcall to rendering game screen
In port/boards/emulator/display.c
, rewrite the function related to game screen init and refresh
Original
Chage to
Dowload shareware data and place it in project root directory
open->fopen
usleep
in <unistd.h>, so we have to replace it in another way。syscall_sdl.c
syscall.c
Also add two case in syscall_handler
function
port/boards/emulator/main.c
We have to run it in the directory where quake elf file is, otherwise it can't read the shareware data
Each frame of the game in rv32emu is 15 seconds, because the program will stuck in Host_Frame()
for to long, so the game display refresh so slow。
Compared to the game run in x86 ISA, each frame will take only 0.3 second, which make the game display looks more smooth
Use the following repo to reproduce quake on rv32emu
rv32emu
quakembd
syscall.c
, we can use syscall_gettimeofday to get time, moreover we can use clock function in time.h
to trigger this system call, so we can remove the new systemcallmain.c
main.c
So we don't need the new systemcall and can run quake on original rv32emu!