From c423f90dde0e8f43b2ec7df45e9840d041186edf Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Sun, 12 May 2024 22:19:03 -0400 Subject: Fixed loading binary into emulated memory --- makefile | 4 ++++ qdme.c | 57 +++++++++++++++++++++++++++++++++++++-------------------- qdme.h | 2 +- qdme.o | Bin 0 -> 11584 bytes test.bin | Bin 32 -> 0 bytes 5 files changed, 42 insertions(+), 21 deletions(-) mode change 100644 => 100755 qdme.h create mode 100644 qdme.o delete mode 100755 test.bin diff --git a/makefile b/makefile index d8e53c5..843c699 100755 --- a/makefile +++ b/makefile @@ -1,6 +1,7 @@ CC=gcc CFLAGS=-Wall -Werror -pedantic --std=c17 OBJS=qdme.o +SRCS=qdme.c TARGET=qdme %.o: %.c %.h @@ -8,6 +9,9 @@ TARGET=qdme all: $(TARGET) +debug: $(SRCS) + $(CC) $(CFLAGS) -g -o $(TARGET) $^ + mips: $(OBJS) $(CC) $(CFLAGS) -o $@ $^ diff --git a/qdme.c b/qdme.c index c957609..821c487 100755 --- a/qdme.c +++ b/qdme.c @@ -4,7 +4,7 @@ #include #include "qdme.h" -uint8_t memory[MEM_SIZE] = {0}; +uint8_t memory[MEM_SIZE * WORD_SIZE] = {0}; uint32_t pc = 0, hi = 0, lo = 0; uint32_t regFile[32] = {0}; @@ -13,7 +13,6 @@ uint32_t regFile[32] = {0}; void InstFetch(inst_t *inst) { uint32_t value = 0; memcpy(&value, memory + pc, WORD_SIZE); - value = ntohl(value); switch ((value & 0xFC000000) >> 24) { case 0: inst->type = RTYPE; @@ -185,6 +184,7 @@ void InstExec(const inst_t inst) { if (inst.type == NOP) { return; } + int16_t imm = inst.i.imm; switch (inst.j.op) { case 0: // R-type instruction ExecRtype(inst); @@ -197,43 +197,41 @@ void InstExec(const inst_t inst) { break; case 4: // beq pc = (regFile[inst.i.rs] == regFile[inst.i.rt]) ? pc + 4 + - inst.i.imm : pc; + imm : pc; break; case 5: // bne pc = (regFile[inst.i.rs] != regFile[inst.i.rt]) ? pc + 4 + - inst.i.imm : pc; + imm : pc; break; case 8: // addi - regFile[inst.i.rt] = (signed)inst.i.rs + (signed)inst.i.imm; + regFile[inst.i.rt] = (signed)inst.i.rs + imm; break; case 9: // addiu - regFile[inst.i.rt] = inst.i.rs + inst.i.imm; + regFile[inst.i.rt] = inst.i.rs + (uint16_t)imm; break; case 12: // andi - regFile[inst.i.rt] = inst.i.rs & inst.i.imm; + regFile[inst.i.rt] = inst.i.rs & (uint16_t)imm; break; case 13: // ori - regFile[inst.i.rt] = inst.i.rs | inst.i.imm; + regFile[inst.i.rt] = inst.i.rs | (uint16_t)imm; break; case 14: // xori - regFile[inst.i.rt] = inst.i.rs ^ inst.i.imm; + regFile[inst.i.rt] = inst.i.rs ^ (uint16_t)imm; break; case 10: // slti - regFile[inst.i.rt] = (signed)inst.i.rs < (signed)inst.i.imm; + regFile[inst.i.rt] = (signed)inst.i.rs < imm; break; case 11: // sltiu - regFile[inst.i.rt] = inst.i.rs < inst.i.imm; + regFile[inst.i.rt] = inst.i.rs < (uint16_t)imm; break; case 15: // lui - regFile[inst.i.rt] = inst.i.imm << 16; + regFile[inst.i.rt] = (uint16_t)imm << 16; break; case 35: // lw - memcpy(regFile + inst.i.rs, memory + inst.i.rt + inst.i.imm, - WORD_SIZE); + memcpy(regFile + inst.i.rs, memory + inst.i.rt + imm, WORD_SIZE); break; case 43: // sw - memcpy(memory + inst.i.rt + inst.i.imm, regFile + inst.i.rs, - WORD_SIZE); + memcpy(memory + inst.i.rt + imm, regFile + inst.i.rs, WORD_SIZE); break; } } @@ -255,12 +253,31 @@ int LoadBinary(const char * const path) { perror("LoadBinary"); return -1; } - int8_t byte = 0; - size_t i = 0; - while ((byte = fgetc(fp)) != EOF && i < MEM_SIZE) { - memory[i++] = byte; + + uint8_t *freeMem = memory; + size_t i = 0, offset = 0; + while ((i = fread(freeMem + offset, sizeof(memory[0]), (MEM_SIZE * WORD_SIZE) - offset, fp)) != 0) { + offset += i; + } + + if (ferror(fp) || !feof(fp)) { + perror("fread"); + return -1; } + fclose(fp); + + /* Convert from Big Endian to Little Endian */ + uint32_t test = 10; + if (test != ntohl(test)) { + for (size_t j = 0; j < offset; j += WORD_SIZE) { + uint32_t word = 0; + memcpy(&word, memory + j, WORD_SIZE); + word = ntohl(word); + memcpy(memory + j, &word, WORD_SIZE); + } + } + return 0; } diff --git a/qdme.h b/qdme.h old mode 100644 new mode 100755 index 50652f0..26f043e --- a/qdme.h +++ b/qdme.h @@ -3,7 +3,7 @@ #include -#define MEM_SIZE 36 +#define MEM_SIZE 4096 #define RA 31 #define V0 2 #define V1 3 diff --git a/qdme.o b/qdme.o new file mode 100644 index 0000000..fae379c Binary files /dev/null and b/qdme.o differ diff --git a/test.bin b/test.bin deleted file mode 100755 index 2cf7b8d..0000000 Binary files a/test.bin and /dev/null differ -- cgit v1.2.3