From 29019b9b9815469df61c7714e803791b770b5305 Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Thu, 17 Apr 2025 23:36:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3lb=20lh=20=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E8=BF=9B=E8=A1=8C=E7=AC=A6=E5=8F=B7=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- cmdline_test.c | 3 +- cpu/riscv.c | 35 +++++++++++++----- main.h | 9 +++++ make.py | 18 +++++----- make_riscv.py | 12 ++++++- riscv/head.h | 8 +++++ riscv/main.c | 4 ++- riscv/test.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 166 insertions(+), 22 deletions(-) create mode 100644 main.h create mode 100644 riscv/head.h create mode 100644 riscv/test.c diff --git a/.gitignore b/.gitignore index d163863..2e51a97 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build/ \ No newline at end of file +build/ +output/ \ No newline at end of file diff --git a/cmdline_test.c b/cmdline_test.c index 9bf6df3..5c319ea 100644 --- a/cmdline_test.c +++ b/cmdline_test.c @@ -2,12 +2,11 @@ #include "stdint.h" #include "soft/debug.h" #include "stddef.h" +#include "main.h" -int get_argv(char** argv[]); - diff --git a/cpu/riscv.c b/cpu/riscv.c index 842b92e..b75b46c 100644 --- a/cpu/riscv.c +++ b/cpu/riscv.c @@ -2,7 +2,12 @@ #include "riscv.h" #include "debug.h" #include "print.h" +#include "stdio.h" +#include "stdlib.h" +#include "errno.h" +#include "string.h" +#include "../main.h" #define device_write(riscv,addr,value) \ for (int i = 0;i < riscv->device_num;i++) {\ @@ -251,7 +256,11 @@ void ins_jalr(riscv_t* riscv, int rs1, int imm, int rd) { void ins_lb(riscv_t* riscv, int rs1, int imm, int rd) { uint32_t addr = riscv->reg[rs1] + imm; // printf("lb %08x,imm=%d\n",addr,imm); - riscv->reg[rd] = (int)mem_wb_read(riscv, addr); + uint32_t r=mem_wb_read(riscv, addr); + if(r&(0x80)){ + r|=0xffffff00; + } + riscv->reg[rd] = r; } void ins_lbu(riscv_t* riscv, int rs1, int imm, int rd) { @@ -263,7 +272,11 @@ void ins_lbu(riscv_t* riscv, int rs1, int imm, int rd) { void ins_lh(riscv_t* riscv, int rs1, int imm, int rd) { uint32_t addr = riscv->reg[rs1] + imm; // printf("lh %08x,imm=%d\n",addr,imm); - riscv->reg[rd] = (int)mem_wh_read(riscv, addr); + uint32_t r=mem_wh_read(riscv, addr); + if(r&(0x8000)){ + r|=0xffff0000; + } + riscv->reg[rd] = r; } void ins_lhu(riscv_t* riscv, int rs1, int imm, int rd) { @@ -658,10 +671,7 @@ int riscv_run(riscv_t* riscv) { return 0; } -#include "stdio.h" -#include "stdlib.h" -#include "errno.h" -#include "string.h" + long get_file_size(FILE *stream) { @@ -693,11 +703,20 @@ 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("riscv.bin", "rb" ); + FILE *file=fopen(bin_name, "rb" ); if(file==NULL) { - printf("open file error\n"); + printf("open file %s error\n",bin_name); return -1; } riscv.rom_size = get_file_size(file); diff --git a/main.h b/main.h new file mode 100644 index 0000000..2d4c92f --- /dev/null +++ b/main.h @@ -0,0 +1,9 @@ + +#ifndef main_h__ +#define main_h__ + +int get_argv(char** argv[]); + + + +#endif diff --git a/make.py b/make.py index d9eb432..9bdb96c 100644 --- a/make.py +++ b/make.py @@ -33,16 +33,16 @@ ASRC = [] BUILD_DIR = 'build' -TARGET = 'hello.exe' +TARGET = 'hello' # CFLAG = ["-Wall -pedantic -specs=nano.specs -mcpu=cortex-m3 -mthumb -lc -lm -lnosys -Og -Tstm32_boot.ld", # f"-Wl,-Map={BUILD_DIR}/{TARGET}.map,--cref -Wl,--gc-sections"] # -pedantic 这一项是ISO语法检验 -CFLAG = ["-Wall -g"] +CFLAG = ["-Wall -g","-pthread"] # 找到指定后缀的文件 -def find_type(path:str,fix:list[str]): +def find_type(path:str,fix:list): dlist=os.listdir(path) file_list=[] for i in dlist: @@ -82,7 +82,7 @@ def tran_path(path:str): # 判断是否需要重新生成 -def check_rebuild(dst:str,src:list[str]): +def check_rebuild(dst:str,src:list): # print(f"src:{src}") if(not os.path.exists(dst)): return True @@ -118,7 +118,7 @@ def read_depend_files(name:str): # 生成依赖关系 -def build_depend(src:list[str]): +def build_depend(src:list): flags=f"{' '.join(CINC)} {' '.join(CDEF)} {' '.join(CFLAG)}" for i in src: name=tran_path(i).split('.')[0] @@ -134,7 +134,7 @@ def build_depend(src:list[str]): print(f"{i} 没有更新源文件") # 生成中间文件 -def build_object(src:list[str]): +def build_object(src:list): flags=f"{' '.join(CINC)} {' '.join(CDEF)} {' '.join(CFLAG)}" for i in src: name_l=tran_path(i).split('.') @@ -164,7 +164,7 @@ def build_object(src:list[str]): # 生成可执行文件 -def build_target(src:list[str]): +def build_target(src:list): flags=f"{' '.join(CINC)} {' '.join(CDEF)} {' '.join(CFLAG)}" obj_list=[] for i in src: @@ -184,8 +184,8 @@ def build_target(src:list[str]): def main(): global CSRC global ASRC - CSRC+=find_type('.\\soft',['c','C']) - CSRC+=find_type('.\\cpu',['c','C']) + CSRC+=find_type('soft',['c','C']) + CSRC+=find_type('cpu',['c','C']) # ASRC+=find_type('./',['s','S','asm','ASM']) if(not os.path.exists(BUILD_DIR)): diff --git a/make_riscv.py b/make_riscv.py index 61df2ea..09d49cd 100644 --- a/make_riscv.py +++ b/make_riscv.py @@ -2,7 +2,7 @@ import os import sys import time - +import shutil @@ -29,6 +29,7 @@ CFLAG=[ SRC=[ "riscv/main.c", + "riscv/test.c", "riscv/start.S" ] @@ -36,7 +37,16 @@ LD_FILE="riscv.ld" TARGET="riscv" +OUTPUT="output" + if __name__ == "__main__": + if not os.path.exists(OUTPUT): + os.mkdir(OUTPUT) os.system(f"{CC} {' '.join(CFLAG)} {' '.join(SRC)} -T{LD_FILE} -Wall -Wextra -nostartfiles -Wl,-Map,\"{TARGET}.map\" -o {TARGET}.elf") os.system(f"{OBJCPY} -O binary {TARGET}.elf {TARGET}.bin") + os.system(f"{OBJCPY} -O ihex {TARGET}.elf {TARGET}.hex") os.system(f"{OBJDUMP} -d {TARGET}.elf > {TARGET}.lst") + tagets_list=[f"{TARGET}.bin",f"{TARGET}.hex",f"{TARGET}.lst",f"{TARGET}.map",f"{TARGET}.elf"] + for item in tagets_list: + if os.path.exists(item): + shutil.move(item,f"{OUTPUT}/{item}") diff --git a/riscv/head.h b/riscv/head.h new file mode 100644 index 0000000..4c1d4af --- /dev/null +++ b/riscv/head.h @@ -0,0 +1,8 @@ + +#ifndef head_h__ +#define head_h__ + +int my_printf(const char* fmt, ...); +void cpu_test(); + +#endif diff --git a/riscv/main.c b/riscv/main.c index efc1d72..093bc21 100644 --- a/riscv/main.c +++ b/riscv/main.c @@ -1,6 +1,7 @@ #include "stdint.h" #include "stdio.h" #include "stdarg.h" +#include "head.h" #define PRINT_BASE_ADDR *(uint8_t *)0x40000000 @@ -46,8 +47,9 @@ int main() int c = add(a, b); my_printf("Hello World! %s\n", "Andy"); my_printf("add(%d, %d)=%d\n", a, b, c); - a = 67;b = 78; + a = 67;b = -78; my_printf("mul(%d, %d)=%d\n", a, b, a * b); my_printf("ram_val test: %s\n", g_string); + cpu_test(); return 0; } \ No newline at end of file diff --git a/riscv/test.c b/riscv/test.c new file mode 100644 index 0000000..926244d --- /dev/null +++ b/riscv/test.c @@ -0,0 +1,96 @@ + +#include "stdint.h" +#include "head.h" + + + +// 无符号大小比较 +void test1(uint32_t a,uint32_t b){ + if(a>b){ + my_printf("%u>%u\n",a,b); + }else if(ab){ + my_printf("%d>%d\n",a,b); + }else if(ab){ + my_printf("%u>%u\n",a,b); + }else if(ab){ + my_printf("%d>%d\n",a,b); + }else if(ab){ + my_printf("%u>%u\n",a,b); + }else if(ab){ + my_printf("%d>%d\n",a,b); + }else if(a