From b64401d5560f0cc92d5bf99d0606d64c2df172be Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Tue, 24 Jun 2025 17:59:23 +0800 Subject: [PATCH] =?UTF-8?q?try=20catch=20=E4=B8=8D=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=86=85=E5=AD=98=EF=BC=8C=E6=8B=86=E5=88=86?= =?UTF-8?q?riscv=5Frest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- make.py | 6 +-- {cpu => riscv_cpu}/print.c | 0 {cpu => riscv_cpu}/print.h | 0 {cpu => riscv_cpu}/riscv.c | 65 +------------------------- {cpu => riscv_cpu}/riscv.h | 8 ++++ {cpu => riscv_cpu}/timer.c | 0 {cpu => riscv_cpu}/timer.h | 0 soft/clexical.c | 1 + soft/exception.h | 11 +++-- soft/mysignal.c | 3 ++ soft/{mystdlib.c => mystdlib.cbak} | 0 soft/mystdlib.h | 24 ++++++---- test/riscv_test.c | 75 ++++++++++++++++++++++++++++++ test/signal_test.c | 5 +- 14 files changed, 115 insertions(+), 83 deletions(-) rename {cpu => riscv_cpu}/print.c (100%) rename {cpu => riscv_cpu}/print.h (100%) rename {cpu => riscv_cpu}/riscv.c (93%) rename {cpu => riscv_cpu}/riscv.h (96%) rename {cpu => riscv_cpu}/timer.c (100%) rename {cpu => riscv_cpu}/timer.h (100%) rename soft/{mystdlib.c => mystdlib.cbak} (100%) create mode 100644 test/riscv_test.c diff --git a/make.py b/make.py index 3249861..bf42e90 100644 --- a/make.py +++ b/make.py @@ -25,9 +25,9 @@ CC = 'gcc' # HEX = 'C:\\ARM_GCC\\bin\\arm-none-eabi-objcopy' + ' -O ihex' # BIN = 'C:\\ARM_GCC\\bin\\arm-none-eabi-objcopy' + ' -O binary -S' -CSRC = ["main.c","test/signal_test.c"] +CSRC = ["main.c","test/riscv_test.c","test/signal_test.c"] -CINC = ['-Isoft',"-Icpu", "-I./","-Itest"] +CINC = ['-Isoft',"-Iriscv_cpu", "-I./","-Itest"] CDEF = ["-DTEST","-DLINUX"] @@ -194,7 +194,7 @@ def main(): os.makedirs(BUILD_DIR) CSRC+=find_type('soft',['c','C']) - CSRC+=find_type('cpu',['c','C']) + CSRC+=find_type('riscv_cpu',['c','C']) CSRC=search_lambda(CSRC) moc_file_create(f"{BUILD_DIR}/moc_tmp.c",list(item[2:] for item in CINC)) CSRC.append(f"{BUILD_DIR}/moc_tmp.c") diff --git a/cpu/print.c b/riscv_cpu/print.c similarity index 100% rename from cpu/print.c rename to riscv_cpu/print.c diff --git a/cpu/print.h b/riscv_cpu/print.h similarity index 100% rename from cpu/print.h rename to riscv_cpu/print.h diff --git a/cpu/riscv.c b/riscv_cpu/riscv.c similarity index 93% rename from cpu/riscv.c rename to riscv_cpu/riscv.c index 32c892e..0974a0c 100644 --- a/cpu/riscv.c +++ b/riscv_cpu/riscv.c @@ -6,9 +6,7 @@ #include "errno.h" #include "string.h" -// cpu外设 -#include "print.h" -#include "timer.h" + #include "../main.h" @@ -827,64 +825,3 @@ int riscv_run(riscv_t* riscv) { } - -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_r(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; -} diff --git a/cpu/riscv.h b/riscv_cpu/riscv.h similarity index 96% rename from cpu/riscv.h rename to riscv_cpu/riscv.h index f490b3e..9724d5c 100644 --- a/cpu/riscv.h +++ b/riscv_cpu/riscv.h @@ -248,4 +248,12 @@ typedef struct riscv_t{ #define IRQ_M_TIMER 7 #define IRQ_M_EXT 11 + + +int riscv_register_device(riscv_t* riscv, const device_t* device); +int riscv_init(riscv_t* riscv, uint32_t* rom, uint32_t rom_addr_base, uint32_t rom_size); +int riscv_device_init(riscv_t* riscv); +int riscv_run(riscv_t* riscv); + + #endif \ No newline at end of file diff --git a/cpu/timer.c b/riscv_cpu/timer.c similarity index 100% rename from cpu/timer.c rename to riscv_cpu/timer.c diff --git a/cpu/timer.h b/riscv_cpu/timer.h similarity index 100% rename from cpu/timer.h rename to riscv_cpu/timer.h diff --git a/soft/clexical.c b/soft/clexical.c index 5a16102..169b000 100644 --- a/soft/clexical.c +++ b/soft/clexical.c @@ -8,6 +8,7 @@ #include "debug.h" #include "string.h" #include "ctype.h" +#include "mystdlib.h" // c语言词法分析 diff --git a/soft/exception.h b/soft/exception.h index e4d80c3..4e120e7 100644 --- a/soft/exception.h +++ b/soft/exception.h @@ -4,7 +4,8 @@ #define exception_h__ #include "setjmp.h" -#include "mystdlib.h" +#include "stdlib.h" +// #include "mystdlib.h" #define THROW_LOG__MAX_SIZE 1024 @@ -12,7 +13,7 @@ typedef struct _jmp_fram{ struct _jmp_fram *next; struct _jmp_fram *last; - map_def *mem; + // map_def *mem; jmp_buf fram; }jmp_fram; @@ -41,9 +42,9 @@ typedef struct{ #define jmp_last(j) {\ if((*j)){\ jmp_fram *l=(*j)->last;\ - if(l){\ + /*if(l){\ __mem_mov(&(l)->mem,&(*j)->mem);\ - }\ + }*/\ free(*j);\ (*j)=l;\ if(*j){\ @@ -56,7 +57,7 @@ typedef struct{ #define jmp_clear(j) {\ if((*j)){\ jmp_fram *l=(*j)->last;\ - __mem_clear(&(*j)->mem);\ + /*__mem_clear(&(*j)->mem);*/\ free(*j);\ (*j)=l;\ if(*j){\ diff --git a/soft/mysignal.c b/soft/mysignal.c index 8a29c74..ea09f21 100644 --- a/soft/mysignal.c +++ b/soft/mysignal.c @@ -88,3 +88,6 @@ mythread_t *sigthread_init() { } +__attribute__((weak)) void* signal_find_slot_func(const char* name) { + return 0; +} diff --git a/soft/mystdlib.c b/soft/mystdlib.cbak similarity index 100% rename from soft/mystdlib.c rename to soft/mystdlib.cbak diff --git a/soft/mystdlib.h b/soft/mystdlib.h index 1ec31af..afcc456 100644 --- a/soft/mystdlib.h +++ b/soft/mystdlib.h @@ -11,18 +11,22 @@ -typedef struct _map_def{ - size_t map_size; - size_t mam_used; - size_t mem_map[0]; -}map_def; +// typedef struct _map_def{ +// size_t map_size; +// size_t mam_used; +// size_t mem_map[0]; +// }map_def; -void __mem_clear(map_def **m); -void __mem_mov(map_def **d,map_def **s); +// void __mem_clear(map_def **m); +// void __mem_mov(map_def **d,map_def **s); -void *mem_calloc(size_t memb_num, size_t memb_size); -void *mem_malloc(size_t size); -void mem_free(void *p); +// void *mem_calloc(size_t memb_num, size_t memb_size); +// void *mem_malloc(size_t size); +// void mem_free(void *p); + +#define mem_calloc calloc +#define mem_malloc malloc +#define mem_free free #endif diff --git a/test/riscv_test.c b/test/riscv_test.c new file mode 100644 index 0000000..a71993d --- /dev/null +++ b/test/riscv_test.c @@ -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; +} + + diff --git a/test/signal_test.c b/test/signal_test.c index dbe33c9..9b5f06a 100644 --- a/test/signal_test.c +++ b/test/signal_test.c @@ -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; }