From 797a3087647aa2a377cf3ef7c776d0c7dd4dafbb Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Fri, 6 Dec 2024 23:50:21 +0000 Subject: [PATCH] Add a simple boot section --- example/Makefile | 6 ++++-- example/boot.s | 6 ++++++ example/linker.ld | 2 +- src/vm.cpp | 7 ++++++- 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 example/boot.s diff --git a/example/Makefile b/example/Makefile index 1f98c74..372e116 100644 --- a/example/Makefile +++ b/example/Makefile @@ -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 diff --git a/example/boot.s b/example/boot.s new file mode 100644 index 0000000..35fd4a2 --- /dev/null +++ b/example/boot.s @@ -0,0 +1,6 @@ +.globl _boot +_boot: + lui x2, 0x80004 + jal main + sbreak + j . diff --git a/example/linker.ld b/example/linker.ld index a5ea337..bb9558b 100644 --- a/example/linker.ld +++ b/example/linker.ld @@ -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 */ diff --git a/src/vm.cpp b/src/vm.cpp index 73f3505..c7f2098 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -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"); }