From f06e08abdf6d1fa5d8b9b8990cc6355fb951006d Mon Sep 17 00:00:00 2001 From: inixyz Date: Wed, 11 Oct 2023 23:45:26 +0300 Subject: [PATCH] running the first instruction yay --- src/main.c | 27 ++++++++++++++++++++++++--- test.bin | Bin 0 -> 4 bytes 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test.bin diff --git a/src/main.c b/src/main.c index 5f807c1..e8aa6c6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include typedef uint8_t u8; typedef uint16_t u16; @@ -175,7 +176,7 @@ void init(Cpu* cpu){ cpu->pc = MEM_OFFSET; } -void dump_registers(const Cpu* cpu){ +void regdump(const Cpu* cpu){ printf("pc = 0x%.8x\n\n", cpu->pc); for(unsigned int i = 0; i < 32; i++){ @@ -185,10 +186,30 @@ void dump_registers(const Cpu* cpu){ } int main(int argc, char** argv){ + if(argc < 2){ + printf("USAGE: tinyricv \n"); + exit(0); + } + + FILE* file = fopen(argv[1], "rb"); + if(!file){ + perror("ERROR: Can't open file: "); + exit(-1); + } + Cpu cpu; + cpu.mem = malloc(4096); + + fseek(file, 0, SEEK_END); + unsigned int file_size = ftell(file); + rewind(file); + fread(cpu.mem, 1, file_size, file); + fclose(file); + init(&cpu); - //FILE* file = fopen(argv[1], "rb"); + while(cpu.pc < file_size + MEM_OFFSET) step(&cpu); - dump_registers(&cpu); + regdump(&cpu); + free(cpu.mem); } \ No newline at end of file diff --git a/test.bin b/test.bin new file mode 100644 index 0000000000000000000000000000000000000000..abd647727d66ff661fbba4aa2236b3a20037756d GIT binary patch literal 4 LcmWe;`0yV911|yt literal 0 HcmV?d00001