modify
so I underestimated and something is very wrong I don't know what just yet
This commit is contained in:
parent
86d73692f9
commit
cb95bf4e85
3 changed files with 14 additions and 11 deletions
|
@ -4,7 +4,7 @@ So don't use it.
|
||||||
|
|
||||||
Example programs:
|
Example programs:
|
||||||
- return.bin: returns -1094647826 (puts it to register 10)
|
- 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:
|
Emulator exit codes:
|
||||||
- <0 = OK
|
- <0 = OK
|
||||||
|
|
|
@ -23,6 +23,7 @@ typedef struct CPU_s CPU;
|
||||||
CPU create_cpu(AddressSpace *addressSpace) {
|
CPU create_cpu(AddressSpace *addressSpace) {
|
||||||
CPU cpu = cpu_default;
|
CPU cpu = cpu_default;
|
||||||
cpu.addressSpace = addressSpace;
|
cpu.addressSpace = addressSpace;
|
||||||
|
cpu.registers[2] = addressSpace->romSize + addressSpace->ramSize - 1; // stack pointer
|
||||||
|
|
||||||
return cpu;
|
return cpu;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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]);
|
printf("BLT: x%d %d < x%d %d?", rs1, registers[rs1], rs2, registers[rs2]);
|
||||||
branch = (int32_t)registers[rs1] < (int32_t)registers[rs2];
|
branch = (int32_t)registers[rs1] < (int32_t)registers[rs2];
|
||||||
} else if (funct3 == 0b101) { // BGE
|
} else if (funct3 == 0b101) { // BGE
|
||||||
printf("BGE: x%d %d > x%d %d?", rs1, registers[rs1], rs2, 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];
|
branch = (int32_t)registers[rs1] >= (int32_t)registers[rs2];
|
||||||
} else if (funct3 == 0b110) { // BLTU
|
} else if (funct3 == 0b110) { // BLTU
|
||||||
printf("BLTU: x%d %u < x%d %u?", rs1, registers[rs1], rs2, registers[rs2]);
|
printf("BLTU: x%d %u < x%d %u?", rs1, registers[rs1], rs2, registers[rs2]);
|
||||||
branch = registers[rs1] < registers[rs2];
|
branch = registers[rs1] < registers[rs2];
|
||||||
} else if (funct3 == 0b111) { // BGEU
|
} else if (funct3 == 0b111) { // BGEU
|
||||||
printf("BGEU: x%d %u > x%d %u?", rs1, registers[rs1], rs2, registers[rs2]);
|
printf("BGEU: x%d %u >= x%d %u?", rs1, registers[rs1], rs2, registers[rs2]);
|
||||||
branch = registers[rs1] > registers[rs2];
|
branch = registers[rs1] >= registers[rs2];
|
||||||
} else {
|
} else {
|
||||||
// TODO illegal instruction
|
// TODO illegal instruction
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ int execute_instruction_on_cpu(CPU *cpu, uint32_t instruction) { // TODO conside
|
||||||
|
|
||||||
switch (funct3) {
|
switch (funct3) {
|
||||||
case 0b000: // ADDI
|
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;
|
registers[rd] = registers[rs1] + imm;
|
||||||
break;
|
break;
|
||||||
default:
|
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 rs1 = instruction >> 15 & 0x1F;
|
||||||
uint8_t rs2 = instruction >> 20 & 0x1F;
|
uint8_t rs2 = instruction >> 20 & 0x1F;
|
||||||
int32_t imm = ((int32_t)instruction >> 20 & 0xffffffe0) | rd;
|
int32_t imm = ((int32_t)instruction >> 20 & 0xffffffe0) | rd;
|
||||||
|
uint32_t og = registers[rs1]; // for log only
|
||||||
uint32_t addr = registers[rs1] + imm;
|
uint32_t addr = registers[rs1] + imm;
|
||||||
|
|
||||||
if (funct3 == 0b000) { // SB
|
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;
|
uint8_t val = registers[rs2] & 0xFF;
|
||||||
write_address_space(addressSpace, addr, 1, &val);
|
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
|
} else if (funct3 == 0b001) { // SH
|
||||||
// SH stores a 16-bit value from the low bits of register rs2 to memory.
|
// 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);
|
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
|
} else if (funct3 == 0b010) { // SW
|
||||||
// SW stores a 32-bit value from the low bits of register rs2 to memory.
|
// 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 {
|
} else {
|
||||||
// TODO illegal funct3
|
// TODO illegal funct3
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue