Add a simple boot section

This commit is contained in:
Konstantin Nazarov 2024-12-06 23:50:21 +00:00
parent 234221cbff
commit 797a308764
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
4 changed files with 17 additions and 4 deletions

View file

@ -1,4 +1,6 @@
example: example.c
riscv32-none-elf-gcc -fno-builtin -fvisibility=hidden -nostdlib -nostartfiles -march=rv32im -mabi=ilp32 -o example example.c -T linker.ld
example: example.c Makefile boot.s linker.ld
riscv32-none-elf-as -march=rv32i -mabi=ilp32 boot.s -o boot.o
riscv32-none-elf-gcc -fno-builtin -fvisibility=hidden -nostdlib -nostartfiles -march=rv32im -mabi=ilp32 -c example.c -o example.o
riscv32-none-elf-ld boot.o example.o -T linker.ld -o example
riscv32-none-elf-strip -R .riscv.attributes example
riscv32-none-elf-strip -R .comment example

6
example/boot.s Normal file
View file

@ -0,0 +1,6 @@
.globl _boot
_boot:
lui x2, 0x80004
jal main
sbreak
j .

View file

@ -1,7 +1,7 @@
ENTRY(_start)
SECTIONS {
. = 0x1000; /* Start address of the program */
. = 0x0000; /* Start address of the program */
.text : {
*(.text) /* Place all .text sections (code) here */

View file

@ -23,7 +23,8 @@ void eval(uint8_t* memory, size_t memory_size) {
return instruction;
};
while (pc < memory_size) {
bool running = true;
while (pc < memory_size && running) {
uint32_t instr = fetch_instruction();
if (instr == 0) break;
// std::cout << "pc: " << pc << "\n";
@ -248,6 +249,10 @@ void eval(uint8_t* memory, size_t memory_size) {
registers[rd] = pc + (imm << 12); // Add the immediate (shifted left) to the current PC
break;
}
case 0x73: { // EBREAK
running = false; // Flag to stop the emulator
break;
}
default:
throw std::runtime_error("Unknown opcode");
}