diff --git a/README.txt b/README.txt index d4af1d1..be5e293 100644 --- a/README.txt +++ b/README.txt @@ -4,7 +4,7 @@ So don't use it. Example programs: - return.bin: returns -1094647826 (puts it to register 10) -- return2.bin: returns a different value with more steps +- return2.bin: returns a different value with more steps. if it returns DADD, something's wrong Emulator exit codes: - <0 = OK diff --git a/src/cpu.c b/src/cpu.c index f8714a1..a6fb4e9 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -23,6 +23,7 @@ typedef struct CPU_s CPU; CPU create_cpu(AddressSpace *addressSpace) { CPU cpu = cpu_default; cpu.addressSpace = addressSpace; + cpu.registers[2] = addressSpace->romSize + addressSpace->ramSize - 1; // stack pointer return cpu; } diff --git a/src/instruction_executor.c b/src/instruction_executor.c index f9538c7..6a81984 100644 --- a/src/instruction_executor.c +++ b/src/instruction_executor.c @@ -56,14 +56,14 @@ int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) { // TODO conside printf("BLT: x%d %d < x%d %d?", rs1, registers[rs1], rs2, registers[rs2]); branch = (int32_t)registers[rs1] < (int32_t)registers[rs2]; } else if (funct3 == 0b101) { // BGE - printf("BGE: x%d %d > x%d %d?", rs1, registers[rs1], rs2, registers[rs2]); - branch = (int32_t)registers[rs1] > (int32_t)registers[rs2]; + printf("BGE: x%d %d >= x%d %d?", rs1, registers[rs1], rs2, registers[rs2]); + branch = (int32_t)registers[rs1] >= (int32_t)registers[rs2]; } else if (funct3 == 0b110) { // BLTU printf("BLTU: x%d %u < x%d %u?", rs1, registers[rs1], rs2, registers[rs2]); branch = registers[rs1] < registers[rs2]; } else if (funct3 == 0b111) { // BGEU - printf("BGEU: x%d %u > x%d %u?", rs1, registers[rs1], rs2, registers[rs2]); - branch = registers[rs1] > registers[rs2]; + printf("BGEU: x%d %u >= x%d %u?", rs1, registers[rs1], rs2, registers[rs2]); + branch = registers[rs1] >= registers[rs2]; } else { // TODO illegal instruction } @@ -83,7 +83,7 @@ int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) { // TODO conside switch (funct3) { case 0b000: // ADDI - printf("ADDI - x%u %d + %d = %d -> x%u", rs1, registers[rs1], imm, registers[rs1] + imm, rd); + printf("ADDI - x%u 0x%X (%d) + 0x%X (%d) = 0x%X -> x%u", rs1, registers[rs1], registers[rs1], imm, imm, registers[rs1] + imm, rd); registers[rd] = registers[rs1] + imm; break; default: @@ -128,6 +128,7 @@ int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) { // TODO conside uint8_t rs1 = instruction >> 15 & 0x1F; uint8_t rs2 = instruction >> 20 & 0x1F; int32_t imm = ((int32_t)instruction >> 20 & 0xffffffe0) | rd; + uint32_t og = registers[rs1]; // for log only uint32_t addr = registers[rs1] + imm; if (funct3 == 0b000) { // SB @@ -135,18 +136,19 @@ int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) { // TODO conside uint8_t val = registers[rs2] & 0xFF; write_address_space(addressSpace, addr, 1, &val); - printf("SB: x%d %u -> %u", rs2, val, addr); + printf("SB (8bit): x%d 0x%X (%u) -> 0x%X + %d = 0x%X", rs2, rs2, val, og, imm, addr); } else if (funct3 == 0b001) { // SH // SH stores a 16-bit value from the low bits of register rs2 to memory. - uint8_t val = registers[rs2] & 0xFFFF; + uint16_t val = registers[rs2] & 0xFFFF; write_address_space(addressSpace, addr, 2, &val); - printf("SH: x%d %u -> %u", rs2, val, addr); + printf("SH (16bit): x%d 0x%X (%u) -> 0x%X + %d = 0x%X", rs2, rs2, val, og, imm, addr); } else if (funct3 == 0b010) { // SW // SW stores a 32-bit value from the low bits of register rs2 to memory. - write_address_space(addressSpace, addr, 4, registers + rs2); + uint32_t val = registers[rs2]; + write_address_space(addressSpace, addr, 4, &val); - printf("SW: x%d %u -> %u", rs2, registers[rs2], addr); + printf("SW (32bit): x%d 0x%X (%u) -> 0x%X + %d = 0x%X", rs2, rs2, val, og, imm, addr); } else { // TODO illegal funct3 }