try catch 不处理动态内存,拆分riscv_rest

This commit is contained in:
2025-06-24 17:59:23 +08:00
parent d9baa7f7a3
commit b64401d556
14 changed files with 115 additions and 83 deletions

75
test/riscv_test.c Normal file
View File

@@ -0,0 +1,75 @@
#include "stdio.h"
#include "errno.h"
#include "stdlib.h"
#include "string.h"
#include "../main.h"
#include "../riscv_cpu/riscv.h"
// cpu外设
#include "../riscv_cpu/print.h"
#include "../riscv_cpu/timer.h"
long get_file_size(FILE *stream)
{
long file_size = -1;
long cur_offset = ftell(stream); // 获取当前偏移位置
if (cur_offset == -1) {
printf("ftell failed :%s\n", strerror(errno));
return -1;
}
if (fseek(stream, 0, SEEK_END) != 0) { // 移动文件指针到文件末尾
printf("fseek failed: %s\n", strerror(errno));
return -1;
}
file_size = ftell(stream); // 获取此时偏移值,即文件大小
if (file_size == -1) {
printf("ftell failed :%s\n", strerror(errno));
}
if (fseek(stream, cur_offset, SEEK_SET) != 0) { // 将文件指针恢复初始位置
printf("fseek failed: %s\n", strerror(errno));
return -1;
}
return file_size;
}
riscv_t riscv = { 0 };
int thread_fun(void* t)
{
int argc;
char** argv;
argc = get_argv(&argv);
char *bin_name="riscv.bin";
if(argc>1){
bin_name=argv[1];
}
printf("riscv start\n");
FILE *file=fopen(bin_name, "rb" );
if(file==NULL)
{
printf("open file %s error\n",bin_name);
return -1;
}
riscv.rom_size = get_file_size(file);
printf("rom size: %d\n", riscv.rom_size);
riscv.rom = calloc((riscv.rom_size + 3) / 4, 4);
fread(riscv.rom, 1, riscv.rom_size, file);
fclose(file);
riscv_register_device(&riscv, print_dev());
riscv_register_device(&riscv, timer_dev());
riscv_init(&riscv, riscv.rom, 0x80000000, riscv.rom_size);
riscv_device_init(&riscv);
riscv_run(&riscv);
return 0;
}

View File

@@ -6,6 +6,7 @@
#include "errno.h"
#include "stdio.h"
#include "string.h"
#include "exception.h"
#include "signal_test.h"
@@ -120,7 +121,7 @@ static test_sig_obj g_sig_obj = { .test_var = 1, };
static test_sig_obj2 g_sig_obj2 = { .test_var = 2, };
static test_sig_obj3 g_sig_obj3;
int thread_fun(void* t) {
int thread_fun_s(void* t) {
mythread_t* th = sigthread_init();
printf("thread_fun start\n");
connect(&g_sig_obj, test_signal, th, &g_sig_obj2, test_slot);
@@ -132,6 +133,8 @@ int thread_fun(void* t) {
emit test_signal2(&g_sig_obj, 5, 6,"hello world");
mdelay(1000);
printf("test end\n");
throw_("my err");
return 0;
}