解决lb lh 指令没有进行符号扩展的问题
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
build/
|
||||
build/
|
||||
output/
|
@@ -2,12 +2,11 @@
|
||||
#include "stdint.h"
|
||||
#include "soft/debug.h"
|
||||
#include "stddef.h"
|
||||
#include "main.h"
|
||||
|
||||
|
||||
|
||||
|
||||
int get_argv(char** argv[]);
|
||||
|
||||
|
||||
|
||||
|
||||
|
35
cpu/riscv.c
35
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);
|
||||
|
9
main.h
Normal file
9
main.h
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
#ifndef main_h__
|
||||
#define main_h__
|
||||
|
||||
int get_argv(char** argv[]);
|
||||
|
||||
|
||||
|
||||
#endif
|
18
make.py
18
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)):
|
||||
|
@@ -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}")
|
||||
|
8
riscv/head.h
Normal file
8
riscv/head.h
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
#ifndef head_h__
|
||||
#define head_h__
|
||||
|
||||
int my_printf(const char* fmt, ...);
|
||||
void cpu_test();
|
||||
|
||||
#endif
|
@@ -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;
|
||||
}
|
96
riscv/test.c
Normal file
96
riscv/test.c
Normal file
@@ -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(a<b){
|
||||
my_printf("%u<%u\n",a,b);
|
||||
}else{
|
||||
my_printf("%u==%u\n",a,b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 有符号大小比较
|
||||
void test2(int32_t a,int32_t b){
|
||||
if(a>b){
|
||||
my_printf("%d>%d\n",a,b);
|
||||
}else if(a<b){
|
||||
my_printf("%d<%d\n",a,b);
|
||||
}else{
|
||||
my_printf("%d==%d\n",a,b);
|
||||
}
|
||||
}
|
||||
|
||||
// uint16_t 大小比较
|
||||
void test3(uint16_t a,uint16_t b){
|
||||
if(a>b){
|
||||
my_printf("%u>%u\n",a,b);
|
||||
}else if(a<b){
|
||||
my_printf("%u<%u\n",a,b);
|
||||
}else{
|
||||
my_printf("%u==%u\n",a,b);
|
||||
}
|
||||
}
|
||||
|
||||
void test4(int16_t a,int16_t b){
|
||||
if(a>b){
|
||||
my_printf("%d>%d\n",a,b);
|
||||
}else if(a<b){
|
||||
my_printf("%d<%d\n",a,b);
|
||||
}else{
|
||||
my_printf("%d==%d\n",a,b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// uint8_t 大小比较
|
||||
void test5(uint8_t a,uint8_t b){
|
||||
if(a>b){
|
||||
my_printf("%u>%u\n",a,b);
|
||||
}else if(a<b){
|
||||
my_printf("%u<%u\n",a,b);
|
||||
}else{
|
||||
my_printf("%u==%u\n",a,b);
|
||||
}
|
||||
}
|
||||
|
||||
void test6(int8_t a,int8_t b){
|
||||
if(a>b){
|
||||
my_printf("%d>%d\n",a,b);
|
||||
}else if(a<b){
|
||||
my_printf("%d<%d\n",a,b);
|
||||
}else{
|
||||
my_printf("%d==%d\n",a,b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void cpu_test() {
|
||||
|
||||
my_printf("test 32 bit\n");
|
||||
test1(1,2);
|
||||
test1(1,-2);
|
||||
test2(1,2);
|
||||
test2(1,-2);
|
||||
|
||||
my_printf("test 16 bit\n");
|
||||
test3(1,2);
|
||||
test3(1,-2);
|
||||
test4(1,2);
|
||||
test4(1,-2);
|
||||
|
||||
my_printf("test 8 bit\n");
|
||||
test5(1,2);
|
||||
test5(1,-2);
|
||||
test6(1,2);
|
||||
test6(1,-2);
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user