diff --git a/Makefile b/Makefile index dffd512c6818eb51b0be0a3c4610ed897417b937..72f9fa90b06528e61b8e0b9956c0db5cddc66467 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ include Makefile.git CC = gcc LD = gcc INCLUDES = $(addprefix -I, $(INC_DIR)) -CFLAGS += -O2 -MMD -Wall -Werror -ggdb3 $(INCLUDES) -D__ISA__=$(ISA) -fomit-frame-pointer +CFLAGS += -O2 -MMD -Wall -Werror -ggdb3 $(INCLUDES) -D__ISA__=$(ISA) -D__ISA_$(ISA)__ -fomit-frame-pointer QEMU_DIFF_PATH = $(NEMU_HOME)/tools/qemu-diff QEMU_SO = $(QEMU_DIFF_PATH)/build/$(ISA)-qemu-so diff --git a/include/common.h b/include/common.h index a5d8474c3b6a43b8a085c465955e73a0bbdc306d..1937464186c019ef4acd6089e5668aa432604ae7 100644 --- a/include/common.h +++ b/include/common.h @@ -4,7 +4,9 @@ //#define DEBUG //#define DIFF_TEST -#define ISA64 +#ifdef __ISA_riscv64__ +# define ISA64 +#endif #if _SHARE // do not enable these features while building a reference design @@ -22,17 +24,18 @@ typedef uint8_t bool; #ifdef ISA64 -typedef uint64_t rtlreg_t; - -typedef uint32_t paddr_t; -typedef uint64_t vaddr_t; +typedef uint64_t word_t; +typedef int64_t sword_t; +#define FMT_WORD "0x%016lx" #else -typedef uint32_t rtlreg_t; - -typedef uint32_t paddr_t; -typedef uint32_t vaddr_t; +typedef uint32_t word_t; +typedef int32_t sword_t; +#define FMT_WORD "0x%08x" #endif +typedef word_t rtlreg_t; +typedef word_t vaddr_t; +typedef uint32_t paddr_t; typedef uint16_t ioaddr_t; #define false 0 diff --git a/include/cpu/decode.h b/include/cpu/decode.h index 86b61dce12c19b670ca65f4588844d5239acef4a..f37c33534215dcecd3af68d290ae00c38defc52f 100644 --- a/include/cpu/decode.h +++ b/include/cpu/decode.h @@ -12,21 +12,12 @@ enum { OP_TYPE_REG, OP_TYPE_MEM, OP_TYPE_IMM }; typedef struct { uint32_t type; int width; -#ifdef ISA64 - union { - uint64_t reg; - rtlreg_t addr; - uint64_t imm; - int64_t simm; - }; -#else union { uint32_t reg; rtlreg_t addr; - uint32_t imm; - int32_t simm; + word_t imm; + sword_t simm; }; -#endif rtlreg_t val; char str[OP_STR_SIZE]; } Operand; diff --git a/include/device/map.h b/include/device/map.h index e50801d5d3de3e62eb20b53c774aab9e1db60693..047ce7e0ba0d1a091eb2f85b7080bd5f5c9abd21 100644 --- a/include/device/map.h +++ b/include/device/map.h @@ -34,7 +34,7 @@ static inline int find_mapid_by_addr(IOMap *maps, int size, paddr_t addr) { void add_pio_map(char *name, ioaddr_t addr, uint8_t *space, int len, io_callback_t callback); void add_mmio_map(char *name, paddr_t addr, uint8_t* space, int len, io_callback_t callback); -uint32_t map_read(paddr_t addr, int len, IOMap *map); -void map_write(paddr_t addr, uint32_t data, int len, IOMap *map); +word_t map_read(paddr_t addr, int len, IOMap *map); +void map_write(paddr_t addr, word_t data, int len, IOMap *map); #endif diff --git a/include/memory/memory.h b/include/memory/memory.h index b1ec27a0ababcfdbbb6bc909016b9186731abfed..e70ec9b9ee2c6a781f2aa6fb55b0bcee9b635370 100644 --- a/include/memory/memory.h +++ b/include/memory/memory.h @@ -15,26 +15,14 @@ extern uint8_t pmem[]; void register_pmem(paddr_t base); -uint32_t isa_vaddr_read(vaddr_t, int); -void isa_vaddr_write(vaddr_t, uint32_t, int); -#ifdef ISA64 -uint64_t isa_vaddr_read64(vaddr_t, int); -void isa_vaddr_write64(vaddr_t, uint64_t, int); -#endif +word_t isa_vaddr_read(vaddr_t, int); +void isa_vaddr_write(vaddr_t, word_t, int); #define vaddr_read isa_vaddr_read #define vaddr_write isa_vaddr_write -#ifdef ISA64 -#define vaddr_read64 isa_vaddr_read64 -#define vaddr_write64 isa_vaddr_write64 -#endif -uint32_t paddr_read(paddr_t, int); -void paddr_write(paddr_t, uint32_t, int); -#ifdef ISA64 -uint64_t paddr_read64(paddr_t, int); -void paddr_write64(paddr_t, uint64_t, int); -#endif +word_t paddr_read(paddr_t, int); +void paddr_write(paddr_t, word_t, int); #define PAGE_SIZE 4096 #define PAGE_MASK (PAGE_SIZE - 1) diff --git a/include/monitor/diff-test.h b/include/monitor/diff-test.h index 8aef569037382adbc7ee241267be6a2388178605..d17388c84d1b2f8346ed8c92b66cefc9e2dc8681 100644 --- a/include/monitor/diff-test.h +++ b/include/monitor/diff-test.h @@ -21,13 +21,8 @@ extern void (*ref_difftest_exec)(uint64_t n); static inline bool difftest_check_reg(const char *name, vaddr_t pc, rtlreg_t ref, rtlreg_t dut) { if (ref != dut) { -#ifdef ISA64 - Log("%s is different after executing instruction at pc = 0x%016lx, right = 0x%016lx, wrong = 0x%016lx", + Log("%s is different after executing instruction at pc = " FMT_WORD ", right = " FMT_WORD ", wrong = " FMT_WORD, name, pc, ref, dut); -#else - Log("%s is different after executing instruction at pc = 0x%08x, right = 0x%08x, wrong = 0x%08x", - name, pc, ref, dut); -#endif return false; } return true; diff --git a/include/monitor/expr.h b/include/monitor/expr.h index c2405057afea2a4c70431e37b617ad12813e4d31..0a752a4be126d22f394c915781519b534d468917 100644 --- a/include/monitor/expr.h +++ b/include/monitor/expr.h @@ -3,6 +3,6 @@ #include "common.h" -uint32_t expr(char *, bool *); +word_t expr(char *, bool *); #endif diff --git a/include/monitor/watchpoint.h b/include/monitor/watchpoint.h index 28108041a4d096b9956a3461a1042c5ebfbed02d..6b861a56bd2ccea345fe4c42b35b8ce0fbbf2599 100644 --- a/include/monitor/watchpoint.h +++ b/include/monitor/watchpoint.h @@ -10,8 +10,8 @@ typedef struct watchpoint { /* TODO: Add more members if necessary */ char *expr; - uint32_t new_val; - uint32_t old_val; + word_t new_val; + word_t old_val; } WP; int set_watchpoint(char *e); diff --git a/include/rtl/rtl.h b/include/rtl/rtl.h index 3d9ecd9610eaa6e2769645da27f2752559f098dc..9a3abf4708b1b3996d3adbcd6a8c15e1430d90fb 100644 --- a/include/rtl/rtl.h +++ b/include/rtl/rtl.h @@ -87,21 +87,11 @@ static inline void interpret_rtl_idiv64_r(rtlreg_t* dest, } static inline void interpret_rtl_lm(rtlreg_t *dest, const rtlreg_t* addr, int len) { -#ifdef ISA64 - if (len == 8) - *dest = vaddr_read64(*addr, len); - else -#endif - *dest = vaddr_read(*addr, len); + *dest = vaddr_read(*addr, len); } static inline void interpret_rtl_sm(const rtlreg_t* addr, const rtlreg_t* src1, int len) { -#ifdef ISA64 - if (len == 8) - vaddr_write64(*addr, *src1, len); - else -#endif - vaddr_write(*addr, *src1, len); + vaddr_write(*addr, *src1, len); } static inline void interpret_rtl_host_lm(rtlreg_t* dest, const void *addr, int len) { diff --git a/src/cpu/inv.c b/src/cpu/inv.c index 4afab7e907729ad957b3abafdfc601892f3e2c77..9a53e9083007e3ce55eab708990a8894bb3497d4 100644 --- a/src/cpu/inv.c +++ b/src/cpu/inv.c @@ -3,17 +3,10 @@ void display_inv_msg(vaddr_t pc) { extern char isa_logo[]; -#ifdef ISA64 printf("There are two cases which will trigger this unexpected exception:\n" - "1. The instruction at PC = 0x%016lx is not implemented.\n" + "1. The instruction at PC = " FMT_WORD " is not implemented.\n" "2. Something is implemented incorrectly.\n", pc); - printf("Find this PC(0x%016lx) in the disassembling result to distinguish which case it is.\n\n", pc); -#else - printf("There are two cases which will trigger this unexpected exception:\n" - "1. The instruction at PC = 0x%08x is not implemented.\n" - "2. Something is implemented incorrectly.\n", pc); - printf("Find this PC(0x%08x) in the disassembling result to distinguish which case it is.\n\n", pc); -#endif + printf("Find this PC(" FMT_WORD ") in the disassembling result to distinguish which case it is.\n\n", pc); printf("\33[1;31mIf it is the first case, see\n%s\nfor more details.\n\nIf it is the second case, remember:\n" "* The machine is always right!\n" "* Every line of untested code is always wrong!\33[0m\n\n", isa_logo); diff --git a/src/cpu/relop.c b/src/cpu/relop.c index 8a60e9d94cd070cdf4ed025404fbfd00fc6d0725..aa18f6fac144dbbf32eef57f2228511994691bbb 100644 --- a/src/cpu/relop.c +++ b/src/cpu/relop.c @@ -7,10 +7,10 @@ bool interpret_relop(uint32_t relop, const rtlreg_t src1, const rtlreg_t src2) { case RELOP_TRUE: return true; case RELOP_EQ: return src1 == src2; case RELOP_NE: return src1 != src2; - case RELOP_LT: return (int32_t)src1 < (int32_t)src2; - case RELOP_LE: return (int32_t)src1 <= (int32_t)src2; - case RELOP_GT: return (int32_t)src1 > (int32_t)src2; - case RELOP_GE: return (int32_t)src1 >= (int32_t)src2; + case RELOP_LT: return (sword_t)src1 < (sword_t)src2; + case RELOP_LE: return (sword_t)src1 <= (sword_t)src2; + case RELOP_GT: return (sword_t)src1 > (sword_t)src2; + case RELOP_GE: return (sword_t)src1 >= (sword_t)src2; case RELOP_LTU: return src1 < src2; case RELOP_LEU: return src1 <= src2; case RELOP_GTU: return src1 > src2; diff --git a/src/device/io/map.c b/src/device/io/map.c index 8f969fd78eb4544a2081bc71bced95d4b777032b..290e485f0d84b469584c96ead9250187e463dbdc 100644 --- a/src/device/io/map.c +++ b/src/device/io/map.c @@ -17,35 +17,29 @@ uint8_t* new_space(int size) { } static inline void check_bound(IOMap *map, paddr_t addr) { -#ifdef ISA64 Assert(map != NULL && addr <= map->high && addr >= map->low, - "address (0x%08x) is out of bound {%s} [0x%08x, 0x%08x] at pc = 0x%016lx", + "address (0x%08x) is out of bound {%s} [0x%08x, 0x%08x] at pc = " FMT_WORD, addr, (map ? map->name : "???"), (map ? map->low : 0), (map ? map->high : 0), cpu.pc); -#else - Assert(map != NULL && addr <= map->high && addr >= map->low, - "address (0x%08x) is out of bound {%s} [0x%08x, 0x%08x] at pc = 0x%08x", - addr, (map ? map->name : "???"), (map ? map->low : 0), (map ? map->high : 0), cpu.pc); -#endif } -static inline void invoke_callback(io_callback_t c, uint32_t offset, int len, bool is_write) { +static inline void invoke_callback(io_callback_t c, paddr_t offset, int len, bool is_write) { if (c != NULL) { c(offset, len, is_write); } } -uint32_t map_read(paddr_t addr, int len, IOMap *map) { - assert(len >= 1 && len <= 4); +word_t map_read(paddr_t addr, int len, IOMap *map) { + assert(len >= 1 && len <= 8); check_bound(map, addr); - uint32_t offset = addr - map->low; + paddr_t offset = addr - map->low; invoke_callback(map->callback, offset, len, false); // prepare data to read - uint32_t data = *(uint32_t *)(map->space + offset) & (~0u >> ((4 - len) << 3)); + word_t data = *(word_t *)(map->space + offset) & (~0u >> ((8 - len) << 3)); return data; } -void map_write(paddr_t addr, uint32_t data, int len, IOMap *map) { - assert(len >= 1 && len <= 4); +void map_write(paddr_t addr, word_t data, int len, IOMap *map) { + assert(len >= 1 && len <= 8); check_bound(map, addr); - uint32_t offset = addr - map->low; + paddr_t offset = addr - map->low; memcpy(map->space + offset, &data, len); diff --git a/src/isa/mips32/decode.c b/src/isa/mips32/decode.c index d1c7b0f366d72b1633295e27cb486b8bafe92483..85654fda89854a7264b309b8b9ffc908832b48e1 100644 --- a/src/isa/mips32/decode.c +++ b/src/isa/mips32/decode.c @@ -70,7 +70,7 @@ make_DHelper(st) { } make_DHelper(B) { - int32_t offset = (decinfo.isa.instr.simm << 2); + sword_t offset = (decinfo.isa.instr.simm << 2); decinfo.jmp_pc = cpu.pc + offset + 4; decinfo.seq_pc += 4; // skip the delay slot diff --git a/src/isa/mips32/mmu.c b/src/isa/mips32/mmu.c index abe8f070bfd36e7d483def10e7a98760562954d5..48cb932904cb89f7e71176281becceb56dd44241 100644 --- a/src/isa/mips32/mmu.c +++ b/src/isa/mips32/mmu.c @@ -107,10 +107,10 @@ static inline paddr_t va2pa(vaddr_t addr, bool write) { return addr; } -uint32_t isa_vaddr_read(vaddr_t addr, int len) { +word_t isa_vaddr_read(vaddr_t addr, int len) { return paddr_read(va2pa(addr, false), len); } -void isa_vaddr_write(vaddr_t addr, uint32_t data, int len) { +void isa_vaddr_write(vaddr_t addr, word_t data, int len) { paddr_write(va2pa(addr, true), data, len); } diff --git a/src/isa/mips32/reg.c b/src/isa/mips32/reg.c index 634d8da0f5a53e6fe0ace901505f31333a50041f..38af3384468bb9df2344bd803482400291e64b2f 100644 --- a/src/isa/mips32/reg.c +++ b/src/isa/mips32/reg.c @@ -18,7 +18,7 @@ void isa_reg_display() { printf("pc: 0x%08x\n", cpu.pc); } -uint32_t isa_reg_str2val(const char *s, bool *success) { +word_t isa_reg_str2val(const char *s, bool *success) { int i; *success = true; for (i = 0; i < 32; i ++) { diff --git a/src/isa/riscv32/decode.c b/src/isa/riscv32/decode.c index 2f5614ab667ecd99940d93cc9c3bf4ddd64c829e..8d84e53c4e0cc1b934f99f6f81dd1a41451f7547 100644 --- a/src/isa/riscv32/decode.c +++ b/src/isa/riscv32/decode.c @@ -42,7 +42,7 @@ make_DHelper(U) { } make_DHelper(J) { - int32_t offset = (decinfo.isa.instr.simm20 << 20) | (decinfo.isa.instr.imm19_12 << 12) | + sword_t offset = (decinfo.isa.instr.simm20 << 20) | (decinfo.isa.instr.imm19_12 << 12) | (decinfo.isa.instr.imm11_ << 11) | (decinfo.isa.instr.imm10_1 << 1); decinfo.jmp_pc = cpu.pc + offset; decode_op_i(id_src, decinfo.jmp_pc, true); @@ -52,7 +52,7 @@ make_DHelper(J) { } make_DHelper(B) { - int32_t offset = (decinfo.isa.instr.simm12 << 12) | (decinfo.isa.instr.imm11 << 11) | + sword_t offset = (decinfo.isa.instr.simm12 << 12) | (decinfo.isa.instr.imm11 << 11) | (decinfo.isa.instr.imm10_5 << 5) | (decinfo.isa.instr.imm4_1 << 1); decinfo.jmp_pc = cpu.pc + offset; decode_op_i(id_dest, decinfo.jmp_pc, true); @@ -75,7 +75,7 @@ make_DHelper(ld) { make_DHelper(st) { decode_op_r(id_src, decinfo.isa.instr.rs1, true); - int32_t simm = (decinfo.isa.instr.simm11_5 << 5) | decinfo.isa.instr.imm4_0; + sword_t simm = (decinfo.isa.instr.simm11_5 << 5) | decinfo.isa.instr.imm4_0; decode_op_i(id_src2, simm, true); print_Dop(id_src->str, OP_STR_SIZE, "%d(%s)", id_src2->val, reg_name(id_src->reg, 4)); diff --git a/src/isa/riscv32/mmu.c b/src/isa/riscv32/mmu.c index c963d86bedb8449446984667c2e1dbcc22f7c69b..b854b6609b7e89be9daf8218911f1aab69a539ed 100644 --- a/src/isa/riscv32/mmu.c +++ b/src/isa/riscv32/mmu.c @@ -57,12 +57,12 @@ static inline paddr_t page_translate(vaddr_t addr, bool is_write) { return page_walk(addr, is_write) | (addr & PAGE_MASK); } -uint32_t isa_vaddr_read(vaddr_t addr, int len) { +word_t isa_vaddr_read(vaddr_t addr, int len) { paddr_t paddr = (cpu.satp.mode ? page_translate(addr, false) : addr); return paddr_read(paddr, len); } -void isa_vaddr_write(vaddr_t addr, uint32_t data, int len) { +void isa_vaddr_write(vaddr_t addr, word_t data, int len) { paddr_t paddr = (cpu.satp.mode ? page_translate(addr, true) : addr); paddr_write(paddr, data, len); } diff --git a/src/isa/riscv32/reg.c b/src/isa/riscv32/reg.c index 4cc1df7f88cd762d8685eace168bf823c7733a38..266fe28f12c07540c1f8ddb95e39f71192e8595b 100644 --- a/src/isa/riscv32/reg.c +++ b/src/isa/riscv32/reg.c @@ -18,7 +18,7 @@ void isa_reg_display() { printf("pc: 0x%08x\n", cpu.pc); } -uint32_t isa_reg_str2val(const char *s, bool *success) { +word_t isa_reg_str2val(const char *s, bool *success) { int i; *success = true; for (i = 0; i < 32; i ++) { diff --git a/src/isa/riscv64/decode.c b/src/isa/riscv64/decode.c index e9b133bbc00f735f3e9347358b071fc6aecd92fb..15967fbbf95f8d8b9b7e92925ce4fb2c995e74ca 100644 --- a/src/isa/riscv64/decode.c +++ b/src/isa/riscv64/decode.c @@ -42,7 +42,7 @@ make_DHelper(U) { } make_DHelper(J) { - int32_t offset = (decinfo.isa.instr.simm20 << 20) | (decinfo.isa.instr.imm19_12 << 12) | + sword_t offset = (decinfo.isa.instr.simm20 << 20) | (decinfo.isa.instr.imm19_12 << 12) | (decinfo.isa.instr.imm11_ << 11) | (decinfo.isa.instr.imm10_1 << 1); decinfo.jmp_pc = cpu.pc + offset; decode_op_i(id_src, decinfo.jmp_pc, true); @@ -52,7 +52,7 @@ make_DHelper(J) { } make_DHelper(B) { - int32_t offset = (decinfo.isa.instr.simm12 << 12) | (decinfo.isa.instr.imm11 << 11) | + sword_t offset = (decinfo.isa.instr.simm12 << 12) | (decinfo.isa.instr.imm11 << 11) | (decinfo.isa.instr.imm10_5 << 5) | (decinfo.isa.instr.imm4_1 << 1); decinfo.jmp_pc = cpu.pc + offset; decode_op_i(id_dest, decinfo.jmp_pc, true); @@ -75,7 +75,7 @@ make_DHelper(ld) { make_DHelper(st) { decode_op_r(id_src, decinfo.isa.instr.rs1, true); - int32_t simm = (decinfo.isa.instr.simm11_5 << 5) | decinfo.isa.instr.imm4_0; + sword_t simm = (decinfo.isa.instr.simm11_5 << 5) | decinfo.isa.instr.imm4_0; decode_op_i(id_src2, simm, true); print_Dop(id_src->str, OP_STR_SIZE, "%ld(%s)", id_src2->val, reg_name(id_src->reg, 4)); diff --git a/src/isa/riscv64/exec/compute.c b/src/isa/riscv64/exec/compute.c index 4162edd273b27f11eb97f9f36c3ec01ec414a659..0f53a4ada2755f6aa91f23960146838cfad81efb 100644 --- a/src/isa/riscv64/exec/compute.c +++ b/src/isa/riscv64/exec/compute.c @@ -15,11 +15,7 @@ make_EHelper(sub) { } make_EHelper(sll) { -#ifdef ISA64 rtl_andi(&id_src2->val, &id_src2->val, 0x3f); -#else - rtl_andi(&id_src2->val, &id_src2->val, 0x1f); -#endif rtl_shl(&s0, &id_src->val, &id_src2->val); rtl_sr(id_dest->reg, &s0, 4); @@ -27,11 +23,7 @@ make_EHelper(sll) { } make_EHelper(srl) { -#ifdef ISA64 rtl_andi(&id_src2->val, &id_src2->val, 0x3f); -#else - rtl_andi(&id_src2->val, &id_src2->val, 0x1f); -#endif if (decinfo.isa.instr.funct7 == 32) { // sra @@ -145,4 +137,4 @@ make_EHelper(srlw) { make_EHelper(sraw) { exec_srlw(NULL); -} \ No newline at end of file +} diff --git a/src/isa/riscv64/exec/exec.c b/src/isa/riscv64/exec/exec.c index cf6f501ec728088eea155c9b4ad26eb04d2ffc0e..6b06e8af5f81feb8ca5120a193b9147d0007fb92 100644 --- a/src/isa/riscv64/exec/exec.c +++ b/src/isa/riscv64/exec/exec.c @@ -2,7 +2,7 @@ #include "all-instr.h" static OpcodeEntry load_table [8] = { - EXW(lds, 1), EXW(lds, 2), EXW(ld, 4), EXW(ld, 8), EXW(ld, 1), EXW(ld, 2), EMPTY, EMPTY + EXW(lds, 1), EXW(lds, 2), EXW(lds, 4), EXW(ld, 8), EXW(ld, 1), EXW(ld, 2), EXW(ld, 4), EMPTY }; static make_EHelper(load) { diff --git a/src/isa/riscv64/exec/ldst.c b/src/isa/riscv64/exec/ldst.c index 13f04649aa893a540b9f6da30b6b1a1c58ed1caa..0778bbd3505cb85ba0fb7eee452a1056986cc697 100644 --- a/src/isa/riscv64/exec/ldst.c +++ b/src/isa/riscv64/exec/ldst.c @@ -2,18 +2,11 @@ make_EHelper(ld) { rtl_lm(&s0, &id_src->addr, decinfo.width); - -#ifdef ISA64 - if (decinfo.width == 4) { // for lw - rtl_sext(&s0, &s0, 4); - } -#endif - - rtl_sr(id_dest->reg, &s0, 4); + rtl_sr(id_dest->reg, &s0, 0); switch (decinfo.width) { case 8: print_asm_template2(ld); break; - case 4: print_asm_template2(lw); break; + case 4: print_asm_template2(lwu); break; case 2: print_asm_template2(lhu); break; case 1: print_asm_template2(lbu); break; default: assert(0); @@ -24,9 +17,10 @@ make_EHelper(ld) { make_EHelper(lds) { rtl_lm(&s0, &id_src->addr, decinfo.width); rtl_sext(&s0, &s0, decinfo.width); - rtl_sr(id_dest->reg, &s0, 4); + rtl_sr(id_dest->reg, &s0, 0); switch (decinfo.width) { + case 4: print_asm_template2(lw); break; case 2: print_asm_template2(lh); break; case 1: print_asm_template2(lb); break; default: assert(0); diff --git a/src/isa/riscv64/mmu.c b/src/isa/riscv64/mmu.c index 09ad02ef48e98f951d30ce185d7cfc983ad3793f..5eec5dc19a0c1b85eefea0a82f12357f0e8e083b 100644 --- a/src/isa/riscv64/mmu.c +++ b/src/isa/riscv64/mmu.c @@ -58,22 +58,12 @@ static inline paddr_t page_translate(vaddr_t addr, bool is_write) { return page_walk(addr, is_write) | (addr & PAGE_MASK); } -uint32_t isa_vaddr_read(vaddr_t addr, int len) { +word_t isa_vaddr_read(vaddr_t addr, int len) { paddr_t paddr = (cpu.satp.mode ? page_translate(addr, false) : addr); return paddr_read(paddr, len); } -void isa_vaddr_write(vaddr_t addr, uint32_t data, int len) { +void isa_vaddr_write(vaddr_t addr, word_t data, int len) { paddr_t paddr = (cpu.satp.mode ? page_translate(addr, true) : addr); paddr_write(paddr, data, len); } - -uint64_t isa_vaddr_read64(vaddr_t addr, int len) { - paddr_t paddr = (cpu.satp.mode ? page_translate(addr, false) : addr); - return paddr_read64(paddr, len); -} - -void isa_vaddr_write64(vaddr_t addr, uint64_t data, int len) { - paddr_t paddr = (cpu.satp.mode ? page_translate(addr, true) : addr); - paddr_write64(paddr, data, len); -} \ No newline at end of file diff --git a/src/isa/x86/decode/modrm.c b/src/isa/x86/decode/modrm.c index c550aca9f0f2a95a1226f54bdbbd32f3724ecb7f..1ae2f79cfbc62705fbe653de6ef2835b40834f08 100644 --- a/src/isa/x86/decode/modrm.c +++ b/src/isa/x86/decode/modrm.c @@ -3,7 +3,7 @@ void load_addr(vaddr_t *pc, ModR_M *m, Operand *rm) { assert(m->mod != 3); - int32_t disp = 0; + sword_t disp = 0; int disp_size = 4; int base_reg = -1, index_reg = -1, scale = 0; rtl_li(&s0, 0); diff --git a/src/isa/x86/mmu.c b/src/isa/x86/mmu.c index 87b457412c74904976fcb962f11095e6dd34f4e6..76dca3ec6a51f30180f9aa072517307726602aa0 100644 --- a/src/isa/x86/mmu.c +++ b/src/isa/x86/mmu.c @@ -44,8 +44,8 @@ static inline paddr_t page_translate(vaddr_t addr, bool is_write) { return page_walk(addr, is_write) | (addr & PAGE_MASK); } -uint32_t isa_vaddr_read(vaddr_t addr, int len) { - uint32_t data; +word_t isa_vaddr_read(vaddr_t addr, int len) { + word_t data; if (cpu.cr0.paging) { paddr_t paddr = page_translate(addr, false); uint32_t remain_byte = PAGE_SIZE - (addr & PAGE_MASK); @@ -67,7 +67,7 @@ uint32_t isa_vaddr_read(vaddr_t addr, int len) { return data; } -void isa_vaddr_write(vaddr_t addr, uint32_t data, int len) { +void isa_vaddr_write(vaddr_t addr, word_t data, int len) { if (cpu.cr0.paging) { paddr_t paddr = page_translate(addr, true); uint32_t remain_byte = PAGE_SIZE - (addr & PAGE_MASK); diff --git a/src/isa/x86/reg.c b/src/isa/x86/reg.c index acae4fd4a81e1960b4e4e724f21d5863473244f6..5727e51e75422f51519d5aa3e46964bf88010f26 100644 --- a/src/isa/x86/reg.c +++ b/src/isa/x86/reg.c @@ -8,8 +8,8 @@ const char *regsb[] = {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"}; void reg_test() { srand(time(0)); - uint32_t sample[8]; - uint32_t pc_sample = rand(); + word_t sample[8]; + word_t pc_sample = rand(); cpu.pc = pc_sample; int i; @@ -48,7 +48,7 @@ void isa_reg_display() { printf("pc: 0x%08x\n", cpu.pc); } -uint32_t isa_reg_str2val(const char *s, bool *success) { +word_t isa_reg_str2val(const char *s, bool *success) { int i; *success = true; for (i = 0; i < 8; i ++) { diff --git a/src/memory/memory.c b/src/memory/memory.c index ea970e1ab77e95ab9ffc91591229317b82cbf48f..36bb437d26bc3a333f3164c8e9fdc911d7766b06 100644 --- a/src/memory/memory.c +++ b/src/memory/memory.c @@ -20,46 +20,22 @@ IOMap* fetch_mmio_map(paddr_t addr); /* Memory accessing interfaces */ -uint32_t paddr_read(paddr_t addr, int len) { +word_t paddr_read(paddr_t addr, int len) { if (map_inside(&pmem_map, addr)) { - uint32_t offset = addr - pmem_map.low; - return *(uint32_t *)(pmem + offset) & (~0u >> ((4 - len) << 3)); + paddr_t offset = addr - pmem_map.low; + return *(uint64_t *)(pmem + offset) & (~0LLu >> ((8 - len) << 3)); } else { return map_read(addr, len, fetch_mmio_map(addr)); } } -void paddr_write(paddr_t addr, uint32_t data, int len) { +void paddr_write(paddr_t addr, word_t data, int len) { if (map_inside(&pmem_map, addr)) { - uint32_t offset = addr - pmem_map.low; + paddr_t offset = addr - pmem_map.low; memcpy(pmem + offset, &data, len); } else { return map_write(addr, data, len, fetch_mmio_map(addr)); } } - -uint64_t paddr_read64(paddr_t addr, int len) { - if (map_inside(&pmem_map, addr)) { - uint64_t offset = addr - pmem_map.low; - return *(uint64_t *)(pmem + offset); - } - else { - // TODO - printf("TODO"); - return 0; - } -} - -void paddr_write64(paddr_t addr, uint64_t data, int len) { - if (map_inside(&pmem_map, addr)) { - uint64_t offset = addr - pmem_map.low; - memcpy(pmem + offset, &data, len); - } - else { - // TODO - printf("TODO"); - return; - } -} \ No newline at end of file diff --git a/src/monitor/cpu-exec.c b/src/monitor/cpu-exec.c index 54e7fb96211f8e3852d49b6195c2551486544b9f..4b24b118b68fb0b6a250fe3b811146a70c03b0f5 100644 --- a/src/monitor/cpu-exec.c +++ b/src/monitor/cpu-exec.c @@ -61,11 +61,7 @@ void cpu_exec(uint64_t n) { /* TODO: check watchpoints here. */ WP *wp = scan_watchpoint(); if(wp != NULL) { -#ifdef ISA64 - printf("\n\nHint watchpoint %d at address 0x%016lx, expr = %s\n", wp->NO, ori_pc, wp->expr); -#else - printf("\n\nHint watchpoint %d at address 0x%08x, expr = %s\n", wp->NO, ori_pc, wp->expr); -#endif + printf("\n\nHint watchpoint %d at address " FMT_WORD ", expr = %s\n", wp->NO, ori_pc, wp->expr); printf("old value = %#08x\nnew value = %#08x\n", wp->old_val, wp->new_val); wp->old_val = wp->new_val; return; @@ -87,17 +83,10 @@ void cpu_exec(uint64_t n) { case NEMU_RUNNING: nemu_state.state = NEMU_STOP; break; case NEMU_END: case NEMU_ABORT: -#ifdef ISA64 - _Log("nemu: %s\33[0m at pc = 0x%016lx\n\n", + _Log("nemu: %s\33[0m at pc = " FMT_WORD "\n\n", (nemu_state.state == NEMU_ABORT ? "\33[1;31mABORT" : (nemu_state.halt_ret == 0 ? "\33[1;32mHIT GOOD TRAP" : "\33[1;31mHIT BAD TRAP")), nemu_state.halt_pc); -#else - _Log("nemu: %s\33[0m at pc = 0x%08x\n\n", - (nemu_state.state == NEMU_ABORT ? "\33[1;31mABORT" : - (nemu_state.halt_ret == 0 ? "\33[1;32mHIT GOOD TRAP" : "\33[1;31mHIT BAD TRAP")), - nemu_state.halt_pc); -#endif monitor_statistic(); } } diff --git a/src/monitor/debug/expr.c b/src/monitor/debug/expr.c index f870f01fb04c30413f952082234c23f73f15eede..2eca87e058d2b8fb3277655ce3dd26c1999f3edc 100644 --- a/src/monitor/debug/expr.c +++ b/src/monitor/debug/expr.c @@ -173,7 +173,7 @@ static int find_dominated_op(int s, int e, bool *success) { return dominated_op; } -uint32_t isa_reg_str2val(const char*, bool *); +word_t isa_reg_str2val(const char*, bool *); static rtlreg_t eval(int s, int e, bool *success) { if (s > e) { @@ -236,7 +236,7 @@ static rtlreg_t eval(int s, int e, bool *success) { } } -rtlreg_t expr(char *e, bool *success) { +word_t expr(char *e, bool *success) { if (!make_token(e)) { *success = false; return 0; diff --git a/src/monitor/debug/log.c b/src/monitor/debug/log.c index d917a858471ed642fbfe105d0099f24194fa62a1..66e606fc2cfcae6e90f140bcc38d818f49cc1b33 100644 --- a/src/monitor/debug/log.c +++ b/src/monitor/debug/log.c @@ -22,13 +22,8 @@ void strcatf(char *buf, const char *fmt, ...) { } void asm_print(vaddr_t ori_pc, int instr_len, bool print_flag) { -#ifdef ISA64 - snprintf(tempbuf, sizeof(tempbuf), "%016lx: %s%*.s%s", ori_pc, log_bytebuf, + snprintf(tempbuf, sizeof(tempbuf), FMT_WORD ": %s%*.s%s", ori_pc, log_bytebuf, 50 - (12 + 3 * instr_len), "", log_asmbuf); -#else - snprintf(tempbuf, sizeof(tempbuf), "%8x: %s%*.s%s", ori_pc, log_bytebuf, - 50 - (12 + 3 * instr_len), "", log_asmbuf); -#endif log_write("%s\n", tempbuf); if (print_flag) { puts(tempbuf); diff --git a/src/monitor/debug/ui.c b/src/monitor/debug/ui.c index 11fd05063455a80133d5f4bd53f9af6051cd9792..9e3db05982f2ccd6375f98eb22a821c1415154b6 100644 --- a/src/monitor/debug/ui.c +++ b/src/monitor/debug/ui.c @@ -88,13 +88,8 @@ static int cmd_d(char *args) { static int cmd_p(char *args) { if (args != NULL) { bool success; -#ifdef ISA64 - uint64_t r = expr(args, &success); - if(success) { printf("0x%lx\n", r); } -#else - uint32_t r = expr(args, &success); - if(success) { printf("%d\n", r); } -#endif + word_t r = expr(args, &success); + if(success) { printf(FMT_WORD "\n", r); } else { printf("Bad expression\n"); } } return 0; @@ -127,14 +122,10 @@ static int cmd_x(char *args) { if (success) { for (i = 0; i < n; i ++) { if (i % 4 == 0) { -#ifdef ISA64 - printf("0x%016lx: ", addr); -#else - printf("0x%08x: ", addr); -#endif + printf(FMT_WORD ": ", addr); } - printf("0x%08x ", vaddr_read(addr, 4)); + printf("0x%08x ", (uint32_t)vaddr_read(addr, 4)); addr += 4; if (i % 4 == 3) { printf("\n"); diff --git a/src/monitor/debug/watchpoint.c b/src/monitor/debug/watchpoint.c index e64a7d32173d1ecf633a1f7aec5a94f268ee0f5f..a4b869a1d67e26f2d41aeed178c0819e4b7773a6 100644 --- a/src/monitor/debug/watchpoint.c +++ b/src/monitor/debug/watchpoint.c @@ -36,9 +36,8 @@ static void free_WP(WP *p) { } int set_watchpoint(char *e) { - uint32_t val; bool success; - val = expr(e, &success); + word_t val = expr(e, &success); if (!success) return -1; WP *p = new_WP(); @@ -74,7 +73,7 @@ void list_watchpoint() { printf("%8s\t%8s\t%8s\n", "NO", "Address", "Enable"); WP *p; for (p = head; p != NULL; p = p->next) { - printf("%8d\t%s\t%#08x\n", p->NO, p->expr, p->old_val); + printf("%8d\t%s\t" FMT_WORD "\n", p->NO, p->expr, p->old_val); } } diff --git a/src/monitor/diff-test/diff-test.c b/src/monitor/diff-test/diff-test.c index 76ae5d95f9ce845fd100891947e4297bbdc88d0e..115fb135ff691eb273fbf0996145711f5c0eca09 100644 --- a/src/monitor/diff-test/diff-test.c +++ b/src/monitor/diff-test/diff-test.c @@ -95,11 +95,7 @@ void difftest_step(vaddr_t ori_pc, vaddr_t next_pc) { } skip_dut_nr_instr --; if (skip_dut_nr_instr == 0) -#ifdef ISA64 - panic("can not catch up with ref.pc = %lx at pc = %lx", ref_r.pc, ori_pc); -#else - panic("can not catch up with ref.pc = %x at pc = %x", ref_r.pc, ori_pc); -#endif + panic("can not catch up with ref.pc = " FMT_WORD " at pc = " FMT_WORD, ref_r.pc, ori_pc); return; }