diff --git a/src/main.c b/src/main.c index c99887e..4d3df88 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,8 @@ #include -#include +#include "tinyriscv.h" #include #include -#include "tinyriscv.h" +#include typedef enum{ RESET = 0, DEFAULT = 39, @@ -13,62 +13,52 @@ typedef enum{ BLUE = 34, BRIGHT_BLUE = 94, MAGENTA = 35, BRIGHT_MAGENTA = 95, CYAN = 36, BRIGHT_CYAN = 96, - WHITE = 37, BRIGHT_WHITE = 97 + WHITE = 37, BRIGHT_WHITE = 97 }Color; -static inline void set_color(const Color color){ - printf("\033[%dm", color); +static inline void set_fg_color(const Color color){ + printf("\x1b[%dm", color); } -void regdump(const trv_cpu* cpu){ - printf("\n ABI Reg Hex │ ABI Reg Hex\n"); +void regdump(const tinyriscv_hart* hart){ + printf("\n"); + printf(" ABI Reg Hex │ ABI Reg Hex \n"); printf("────────────────────┼───────────────────\n"); + const char abi[][6] = { + "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", + "s0/fp", "s1", "a0", "a1", "a2", "a3", "a4", "a5", + "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", + "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6" + }; + for(unsigned int i = 0; i < 32; i++){ - printf(" "); - switch(i){ - case 0: printf("zero "); break; case 1: printf("ra "); break; - case 2: printf("sp "); break; case 3: printf("gp "); break; - case 4: printf("tp "); break; case 5: printf("t0 "); break; - case 6: printf("t1 "); break; case 7: printf("t2 "); break; - case 8: printf("s0/fp"); break; case 9: printf("s1 "); break; - case 10: printf("a0 "); break; case 11: printf("a1 "); break; - case 12: printf("a2 "); break; case 13: printf("a3 "); break; - case 14: printf("a4 "); break; case 15: printf("a5 "); break; - case 16: printf("a6 "); break; case 17: printf("a7 "); break; - case 18: printf("s2 "); break; case 19: printf("s3 "); break; - case 20: printf("s4 "); break; case 21: printf("s5 "); break; - case 22: printf("s6 "); break; case 23: printf("s7 "); break; - case 24: printf("s8 "); break; case 25: printf("s9 "); break; - case 26: printf("s10 "); break; case 27: printf("s11 "); break; - case 28: printf("t3 "); break; case 29: printf("t4 "); break; - case 30: printf("t5 "); break; case 31: printf("t6 "); break; - } + printf(" %-5s", abi[i]); + + set_fg_color(BRIGHT_YELLOW); + printf(i < 10 ? " x%d " : " x%d", i); + set_fg_color(RESET); - printf(" "); - set_color(BRIGHT_YELLOW); - printf(i < 10 ? "x%d " : "x%d", i); - set_color(RESET); - - printf(" "); - if(cpu->regs[i]) set_color(BRIGHT_GREEN); - printf("%-8x", cpu->regs[i]); - set_color(RESET); + if(hart->x[i]) set_fg_color(BRIGHT_GREEN); + printf(" %-8x", hart->x[i]); + set_fg_color(RESET); printf(i % 2 ? " \n" : " │"); } - printf(" pc "); - set_color(BRIGHT_YELLOW); - printf("x32"); - if(cpu->pc) set_color(BRIGHT_GREEN); - printf(" %-8x", cpu->pc); - set_color(RESET); + printf(" pc "); + set_fg_color(BRIGHT_YELLOW); + printf(" x32"); + if(hart->pc) set_fg_color(BRIGHT_GREEN); + printf(" %-8x", hart->pc); + set_fg_color(RESET); printf(" │\n\n"); } -void help(void){ +void help(const char* message){ + if(strlen(message)) puts(message); + printf("Usage: tinyricv [OPTION] [VALUE] ...\n\n"); printf(" --help Display this information.\n"); printf(" --bin Load into memory.\n"); @@ -84,12 +74,10 @@ int main(int argc, char** argv){ unsigned int do_regdump = 0; //command line arguments - if(argc < 2){ - printf("Not enough arguments.\n"); - help(); - } + if(argc < 2) help("Not enough arguments."); + for(unsigned int i = 1; i < argc; i++){ - if(!strcmp(argv[i], "--help")) help(); + if(!strcmp(argv[i], "--help")) help(NULL); else if(!strcmp(argv[i], "--bin")){ assert("Invalid file path." && argv[i + 1]); strncpy(file_path, argv[i++ + 1], 256); @@ -100,13 +88,13 @@ int main(int argc, char** argv){ } else if(!strcmp(argv[i], "--regdump")) do_regdump = 1; else{ - printf("Invalid argument: %s\n", argv[i]); - help(); + printf("Invalid option: %s\n", argv[i]); + printf("Try 'tinyriscv --help' for more information.\n"); } } - trv_cpu cpu; - cpu.mem = malloc(cpu.mem_size = mem_size); + tinyriscv_hart hart; + hart.mem = malloc(hart.mem_size = mem_size); //file handling FILE* file = fopen(file_path, "rb"); @@ -119,14 +107,15 @@ int main(int argc, char** argv){ fseek(file, 0, SEEK_END); const unsigned int file_size = ftell(file); rewind(file); - fread(cpu.mem, 1, file_size, file); + fread(hart.mem, 1, file_size, file); fclose(file); //program execution - trv_init(&cpu); - while(cpu.pc < file_size + trv_MEM_OFFSET) trv_step(&cpu); + tinyriscv_init(&hart); + while(hart.pc < file_size + tinyriscv_MEM_OFFSET) tinyriscv_step(&hart); + + if(do_regdump) regdump(&hart); //cleanup - if(do_regdump) regdump(&cpu); - free(cpu.mem); + free(hart.mem); } \ No newline at end of file diff --git a/test.bin b/tests/test.bin similarity index 100% rename from test.bin rename to tests/test.bin diff --git a/tinyriscv.h b/tinyriscv.h deleted file mode 100644 index ece731a..0000000 --- a/tinyriscv.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef TINYRISCV_H -#define TINYRISCV_H - -#include - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; - -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; - -void tinyriscv_step(){ - - switch(opcode){ - case /*R-type*/ 0x33: - switch(func3){ - case /*ADD*/ 0x0: x[rd] = x[rs1] + x[rs2]; break; - #ifdef TINYRISCV_VERBOSE - printf("add rd, rs1, rs2\n"); - #endif - } - break; - } -} - - - - -case ADD: x[rd] = x[rs1] + x[rs2]; break; - - - - -#endif //TINYRISCV_H \ No newline at end of file