initial commit

This commit is contained in:
2026-04-14 00:02:09 +02:00
commit 8d6775c51d
12 changed files with 562 additions and 0 deletions

75
src/main.c Normal file
View File

@@ -0,0 +1,75 @@
#define TINYRISCV_M
#include "tinyriscv.h"
#include "elf.h"
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define TEST_A 5u
#define TEST_B 7u
#define MAX_STEPS 1024u
int main(){
uint8_t memory[EMU_MEM_SIZE]; //4kb of internal memory
tinyriscv_core cpu; //create the emulation core
memset(&cpu, 0, sizeof(cpu));
cpu.mem = memory; //use the uint8_t array as the memory
cpu.mem_size = EMU_MEM_SIZE;
memset(memory, 0, sizeof(memory));
int load_status = load_elf_object_sections(EMU_PROGRAM_PATH, memory, cpu.mem_size);
if (load_status != LOAD_OK) {
fprintf(stderr, "ELF load failed with code %d for '%s'\n", load_status, EMU_PROGRAM_PATH);
fflush(stderr);
printf("ELF load failed with code %d\n", load_status);
fflush(stdout);
return 1;
}
uint32_t entry_offset = 0u;
int symbol_status = resolve_elf_symbol_memory_offset(
EMU_PROGRAM_PATH, EMU_ENTRY_SYMBOL, cpu.mem_size, &entry_offset);
if (symbol_status != LOAD_OK) {
fprintf(stderr, "Entry symbol '%s' resolve failed with code %d for '%s'\n",
EMU_ENTRY_SYMBOL, symbol_status, EMU_PROGRAM_PATH);
return 2;
}
tinyriscv_init(&cpu); //initialise core for execution
cpu.pc = tinyriscv_MEM_OFFSET + entry_offset;
// Call operation(a, b) using RV32 calling convention:
// a0=x10, return value in a0, return address in ra=x1.
cpu.x[10] = TEST_A;
cpu.x[1] = tinyriscv_MEM_OFFSET + cpu.mem_size;
uint32_t steps = 0u;
while (tinyriscv_valid_step(&cpu) && steps < MAX_STEPS) {
tinyriscv_step(&cpu);
++steps;
}
if (steps == MAX_STEPS) {
fprintf(stderr, "Execution did not halt within %u steps\n", MAX_STEPS);
return 2;
}
uint32_t actual = (uint32_t)cpu.x[10];
uint32_t expected = ((TEST_A << 3) ^ 0xFF) >> 3;
printf("operation(%u) -> %u (expected %u)\n",
(unsigned)TEST_A,
(unsigned)actual, (unsigned)expected);
if (actual != expected) {
fprintf(stderr, "Verification failed\n");
return 3;
}
return 0;
}