diff --git a/src/main.c b/src/main.c index c46ac08..a210074 100644 --- a/src/main.c +++ b/src/main.c @@ -152,7 +152,7 @@ int main(int argc, char** argv){ //program execution tinyriscv_init(&hart); - while(hart.pc < file_size + tinyriscv_MEM_OFFSET) tinyriscv_step(&hart); + while(tinyriscv_valid_step(&hart)) tinyriscv_step(&hart); if(do_regdump) regdump(&hart); if(do_memdump) memdump(&hart, dump_addr); diff --git a/src/tinyriscv.h b/src/tinyriscv.h index 0a2c25f..639bb74 100644 --- a/src/tinyriscv.h +++ b/src/tinyriscv.h @@ -150,6 +150,22 @@ void tinyriscv_r_type(const u8 func3, const u8 func7, u32 x[32], const u8 rd, //////////////////////////////////////////////////////////////////////////////// +void tinyriscv_init(tinyriscv_hart* hart){ + hart->x[2] = tinyriscv_MEM_OFFSET + hart->mem_size; + hart->pc = tinyriscv_MEM_OFFSET; +} + +//////////////////////////////////////////////////////////////////////////////// + +u8 tinyriscv_valid_step(const tinyriscv_hart* hart){ + if(!load32(hart->mem, hart->pc)) return 0; + if(hart->pc >= hart->mem_size + tinyriscv_MEM_OFFSET) return 0; + + return 1; +} + +//////////////////////////////////////////////////////////////////////////////// + void tinyriscv_step(tinyriscv_hart* hart){ //fetch const u32 inst = load32(hart->mem, hart->pc); @@ -194,11 +210,4 @@ void tinyriscv_step(tinyriscv_hart* hart){ //////////////////////////////////////////////////////////////////////////////// -void tinyriscv_init(tinyriscv_hart* hart){ - hart->x[2] = tinyriscv_MEM_OFFSET + hart->mem_size; - hart->pc = tinyriscv_MEM_OFFSET; -} - -//////////////////////////////////////////////////////////////////////////////// - #endif //TINYRISCV_H \ No newline at end of file