renamed hart to core
This commit is contained in:
50
src/main.c
50
src/main.c
@@ -66,7 +66,7 @@ void help(const char* message){
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void regdump(const tinyriscv_hart* hart){
|
||||
void regdump(const tinyriscv_core* core){
|
||||
printf("\n");
|
||||
printf(" ABI Reg Hex │ ABI Reg Hex \n");
|
||||
printf(" ───────────────────┼───────────────────\n");
|
||||
@@ -85,8 +85,8 @@ void regdump(const tinyriscv_hart* hart){
|
||||
printf(i < 10 ? " x%d " : " x%d", i);
|
||||
set_fg_color(RESET);
|
||||
|
||||
if(hart->x[i]) set_fg_color(BRIGHT_GREEN);
|
||||
printf(" %-8x", hart->x[i]);
|
||||
if(core->x[i]) set_fg_color(BRIGHT_GREEN);
|
||||
printf(" %-8x", core->x[i]);
|
||||
set_fg_color(RESET);
|
||||
|
||||
printf(i % 2 ? " \n" : " │");
|
||||
@@ -95,8 +95,8 @@ void regdump(const tinyriscv_hart* hart){
|
||||
printf(" pc ");
|
||||
set_fg_color(BRIGHT_YELLOW);
|
||||
printf(" x32");
|
||||
if(hart->pc) set_fg_color(BRIGHT_GREEN);
|
||||
printf(" %-8x", hart->pc);
|
||||
if(core->pc) set_fg_color(BRIGHT_GREEN);
|
||||
printf(" %-8x", core->pc);
|
||||
set_fg_color(RESET);
|
||||
|
||||
printf(" │\n\n");
|
||||
@@ -104,9 +104,9 @@ void regdump(const tinyriscv_hart* hart){
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void memdump(const tinyriscv_hart* hart, uint32_t addr){
|
||||
void memdump(const tinyriscv_core* core, uint32_t addr){
|
||||
addr -= tinyriscv_MEM_OFFSET;
|
||||
if(addr + 256 > hart->mem_size) help("memdump: Address out of memory range.");
|
||||
if(addr + 256 > core->mem_size) help("memdump: Address out of memory range.");
|
||||
|
||||
printf("\n");
|
||||
printf(" Address Memory ASCII\n ");
|
||||
@@ -119,12 +119,12 @@ void memdump(const tinyriscv_hart* hart, uint32_t addr){
|
||||
set_fg_color(RESET);
|
||||
|
||||
for(uint32_t i = addr; i < addr + 16; i++){
|
||||
set_fg_color(hart->mem[i] ? BRIGHT_WHITE : BRIGHT_BLACK);
|
||||
printf("%.2x ", hart->mem[i]);
|
||||
set_fg_color(core->mem[i] ? BRIGHT_WHITE : BRIGHT_BLACK);
|
||||
printf("%.2x ", core->mem[i]);
|
||||
}
|
||||
|
||||
for(uint32_t i = addr; i < addr + 16; i++){
|
||||
const char c = hart->mem[i];
|
||||
const char c = core->mem[i];
|
||||
const unsigned int visible = c > 31 && c < 255 && c != 127;
|
||||
set_fg_color(visible ? BRIGHT_WHITE : BRIGHT_BLACK);
|
||||
printf("%c", visible ? c : '.');
|
||||
@@ -139,7 +139,7 @@ void memdump(const tinyriscv_hart* hart, uint32_t addr){
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void load_mem_from_file(tinyriscv_hart* hart, const char* file_path){
|
||||
void load_mem_from_file(tinyriscv_core* core, const char* file_path){
|
||||
FILE* file = fopen(file_path, "rb");
|
||||
if(!file){
|
||||
perror("ERROR: Can't open file");
|
||||
@@ -150,13 +150,13 @@ void load_mem_from_file(tinyriscv_hart* hart, const char* file_path){
|
||||
const unsigned int file_size = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
if(file_size > hart->mem_size){
|
||||
if(file_size > core->mem_size){
|
||||
printf("ERROR: [file] is bigger than allocated memory.");
|
||||
fclose(file);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
fread(hart->mem, 1, file_size, file);
|
||||
fread(core->mem, 1, file_size, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
@@ -195,34 +195,34 @@ int main(int argc, char** argv){
|
||||
}
|
||||
}
|
||||
|
||||
tinyriscv_hart hart;
|
||||
hart.mem = calloc(hart.mem_size = mem_size, 1);
|
||||
tinyriscv_core core;
|
||||
core.mem = calloc(core.mem_size = mem_size, 1);
|
||||
|
||||
//load program into memory
|
||||
load_mem_from_file(&hart, file_path);
|
||||
load_mem_from_file(&core, file_path);
|
||||
|
||||
//program execution
|
||||
tinyriscv_init(&hart);
|
||||
tinyriscv_init(&core);
|
||||
if(do_debug){
|
||||
while(tinyriscv_valid_step(&hart)){
|
||||
while(tinyriscv_valid_step(&core)){
|
||||
clear_screen();
|
||||
|
||||
if(do_regdump) regdump(&hart);
|
||||
if(do_memdump) memdump(&hart, dump_addr);
|
||||
if(do_regdump) regdump(&core);
|
||||
if(do_memdump) memdump(&core, dump_addr);
|
||||
|
||||
printf("\nPress enter to step.\n");
|
||||
while(getch() != 10);
|
||||
|
||||
tinyriscv_step(&hart);
|
||||
tinyriscv_step(&core);
|
||||
}
|
||||
}
|
||||
else while(tinyriscv_valid_step(&hart)) tinyriscv_step(&hart);
|
||||
else while(tinyriscv_valid_step(&core)) tinyriscv_step(&core);
|
||||
|
||||
if(do_regdump) regdump(&hart);
|
||||
if(do_memdump) memdump(&hart, dump_addr);
|
||||
if(do_regdump) regdump(&core);
|
||||
if(do_memdump) memdump(&core, dump_addr);
|
||||
|
||||
//cleanup
|
||||
free(hart.mem);
|
||||
free(core.mem);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -10,7 +10,7 @@ typedef uint32_t u32; typedef int32_t i32;
|
||||
typedef struct{
|
||||
u32 x[32], pc, mem_size;
|
||||
u8* mem;
|
||||
}tinyriscv_hart;
|
||||
}tinyriscv_core;
|
||||
|
||||
const u32 tinyriscv_MEM_OFFSET = 0x80000000;
|
||||
|
||||
@@ -108,25 +108,25 @@ inline void tinyriscv_R_type(const u8 funct3, const u8 funct7, u32* x,
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void tinyriscv_init(tinyriscv_hart* hart){
|
||||
hart->x[2] = tinyriscv_MEM_OFFSET + hart->mem_size;
|
||||
hart->pc = tinyriscv_MEM_OFFSET;
|
||||
void tinyriscv_init(tinyriscv_core* core){
|
||||
core->x[2] = tinyriscv_MEM_OFFSET + core->mem_size;
|
||||
core->pc = tinyriscv_MEM_OFFSET;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
u8 tinyriscv_valid_step(const tinyriscv_hart* hart){
|
||||
if(!MEM(32, hart->mem, hart->pc)) return 0;
|
||||
if(hart->pc >= hart->mem_size + tinyriscv_MEM_OFFSET) return 0;
|
||||
u8 tinyriscv_valid_step(const tinyriscv_core* core){
|
||||
if(!MEM(32, core->mem, core->pc)) return 0;
|
||||
if(core->pc >= core->mem_size + tinyriscv_MEM_OFFSET) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void tinyriscv_step(tinyriscv_hart* hart){
|
||||
void tinyriscv_step(tinyriscv_core* core){
|
||||
//fetch
|
||||
const u32 inst = MEM(32, hart->mem, hart->pc);
|
||||
hart->pc += 4;
|
||||
const u32 inst = MEM(32, core->mem, core->pc);
|
||||
core->pc += 4;
|
||||
|
||||
//decode
|
||||
#define opcode (inst & 0x7f)
|
||||
@@ -145,45 +145,45 @@ void tinyriscv_step(tinyriscv_hart* hart){
|
||||
(inst >> 9 & 0x800) | (inst >> 20 & 0x7fe))
|
||||
|
||||
//execute
|
||||
hart->x[0] = 0;
|
||||
core->x[0] = 0;
|
||||
|
||||
switch(opcode){
|
||||
case /*LUI*/ 0x37:
|
||||
hart->x[rd] = inst & 0xfffff000;
|
||||
core->x[rd] = inst & 0xfffff000;
|
||||
break;
|
||||
|
||||
case /*AUIPC*/ 0x17:
|
||||
hart->x[rd] = hart->pc - 4 + (inst & 0xfffff000);
|
||||
core->x[rd] = core->pc - 4 + (inst & 0xfffff000);
|
||||
break;
|
||||
|
||||
case /*JAL*/ 0x6f:
|
||||
hart->x[rd] = hart->pc; hart->pc += imm_j - 4;
|
||||
core->x[rd] = core->pc; core->pc += imm_j - 4;
|
||||
break;
|
||||
|
||||
case /*JALR*/ 0x67:
|
||||
const u32 ret_addr = hart->pc;
|
||||
hart->pc = (hart->x[rs1] + (i32)imm_i) & 0xfffffffe;
|
||||
hart->x[rd] = ret_addr;
|
||||
const u32 ret_addr = core->pc;
|
||||
core->pc = (core->x[rs1] + (i32)imm_i) & 0xfffffffe;
|
||||
core->x[rd] = ret_addr;
|
||||
break;
|
||||
|
||||
case /*B-type*/ 0x63:
|
||||
tinyriscv_B_type(funct3, hart->x, &hart->pc, rs1, rs2, imm_b);
|
||||
tinyriscv_B_type(funct3, core->x, &core->pc, rs1, rs2, imm_b);
|
||||
break;
|
||||
|
||||
case /*L-type*/ 0x03:
|
||||
tinyriscv_L_type(funct3, hart->x, hart->mem, rd, rs1, imm_i);
|
||||
tinyriscv_L_type(funct3, core->x, core->mem, rd, rs1, imm_i);
|
||||
break;
|
||||
|
||||
case /*S-type*/ 0x23:
|
||||
tinyriscv_S_type(funct3, hart->x, hart->mem, rs1, rs2, imm_s);
|
||||
tinyriscv_S_type(funct3, core->x, core->mem, rs1, rs2, imm_s);
|
||||
break;
|
||||
|
||||
case /*I-type*/ 0x13:
|
||||
tinyriscv_I_type(funct3, funct7, hart->x, rd, rs1, imm_i, rs2);
|
||||
tinyriscv_I_type(funct3, funct7, core->x, rd, rs1, imm_i, rs2);
|
||||
break;
|
||||
|
||||
case /*R-type*/ 0x33:
|
||||
tinyriscv_R_type(funct3, funct7, hart->x, rd, rs1, rs2);
|
||||
tinyriscv_R_type(funct3, funct7, core->x, rd, rs1, rs2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user