emu remake and hart code

This commit is contained in:
inixyz
2023-10-15 20:12:17 +03:00
parent 351f856cb9
commit a0c07323e8
3 changed files with 45 additions and 92 deletions

View File

@@ -1,8 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include "tinyriscv.h"
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "tinyriscv.h" #include <stdlib.h>
typedef enum{ typedef enum{
RESET = 0, DEFAULT = 39, RESET = 0, DEFAULT = 39,
@@ -16,59 +16,49 @@ typedef enum{
WHITE = 37, BRIGHT_WHITE = 97 WHITE = 37, BRIGHT_WHITE = 97
}Color; }Color;
static inline void set_color(const Color color){ static inline void set_fg_color(const Color color){
printf("\033[%dm", color); printf("\x1b[%dm", color);
} }
void regdump(const trv_cpu* cpu){ void regdump(const tinyriscv_hart* hart){
printf("\n ABI Reg Hex │ ABI Reg Hex\n"); printf("\n");
printf(" ABI Reg Hex │ ABI Reg Hex \n");
printf("────────────────────┼───────────────────\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++){ for(unsigned int i = 0; i < 32; i++){
printf(" "); printf(" %-5s", abi[i]);
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(" "); set_fg_color(BRIGHT_YELLOW);
set_color(BRIGHT_YELLOW); printf(i < 10 ? " x%d " : " x%d", i);
printf(i < 10 ? "x%d " : "x%d", i); set_fg_color(RESET);
set_color(RESET);
printf(" "); if(hart->x[i]) set_fg_color(BRIGHT_GREEN);
if(cpu->regs[i]) set_color(BRIGHT_GREEN); printf(" %-8x", hart->x[i]);
printf("%-8x", cpu->regs[i]); set_fg_color(RESET);
set_color(RESET);
printf(i % 2 ? " \n" : ""); printf(i % 2 ? " \n" : "");
} }
printf(" pc "); printf(" pc ");
set_color(BRIGHT_YELLOW); set_fg_color(BRIGHT_YELLOW);
printf("x32"); printf(" x32");
if(cpu->pc) set_color(BRIGHT_GREEN); if(hart->pc) set_fg_color(BRIGHT_GREEN);
printf(" %-8x", cpu->pc); printf(" %-8x", hart->pc);
set_color(RESET); set_fg_color(RESET);
printf("\n\n"); printf("\n\n");
} }
void help(void){ void help(const char* message){
if(strlen(message)) puts(message);
printf("Usage: tinyricv [OPTION] [VALUE] ...\n\n"); printf("Usage: tinyricv [OPTION] [VALUE] ...\n\n");
printf(" --help Display this information.\n"); printf(" --help Display this information.\n");
printf(" --bin <file> Load <file> into memory.\n"); printf(" --bin <file> Load <file> into memory.\n");
@@ -84,12 +74,10 @@ int main(int argc, char** argv){
unsigned int do_regdump = 0; unsigned int do_regdump = 0;
//command line arguments //command line arguments
if(argc < 2){ if(argc < 2) help("Not enough arguments.");
printf("Not enough arguments.\n");
help();
}
for(unsigned int i = 1; i < argc; i++){ 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")){ else if(!strcmp(argv[i], "--bin")){
assert("Invalid file path." && argv[i + 1]); assert("Invalid file path." && argv[i + 1]);
strncpy(file_path, argv[i++ + 1], 256); 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 if(!strcmp(argv[i], "--regdump")) do_regdump = 1;
else{ else{
printf("Invalid argument: %s\n", argv[i]); printf("Invalid option: %s\n", argv[i]);
help(); printf("Try 'tinyriscv --help' for more information.\n");
} }
} }
trv_cpu cpu; tinyriscv_hart hart;
cpu.mem = malloc(cpu.mem_size = mem_size); hart.mem = malloc(hart.mem_size = mem_size);
//file handling //file handling
FILE* file = fopen(file_path, "rb"); FILE* file = fopen(file_path, "rb");
@@ -119,14 +107,15 @@ int main(int argc, char** argv){
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
const unsigned int file_size = ftell(file); const unsigned int file_size = ftell(file);
rewind(file); rewind(file);
fread(cpu.mem, 1, file_size, file); fread(hart.mem, 1, file_size, file);
fclose(file); fclose(file);
//program execution //program execution
trv_init(&cpu); tinyriscv_init(&hart);
while(cpu.pc < file_size + trv_MEM_OFFSET) trv_step(&cpu); while(hart.pc < file_size + tinyriscv_MEM_OFFSET) tinyriscv_step(&hart);
if(do_regdump) regdump(&hart);
//cleanup //cleanup
if(do_regdump) regdump(&cpu); free(hart.mem);
free(cpu.mem);
} }

View File

@@ -1,36 +0,0 @@
#ifndef TINYRISCV_H
#define TINYRISCV_H
#include <stdint.h>
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