Browse Source

Implement BEQ, LUI, and fix memory issue

master
Forest Belton 5 months ago
parent
commit
bed1fec19b
4 changed files with 25 additions and 7 deletions
  1. +2
    -2
      include/cpu.h
  2. +1
    -1
      src/boot.c
  3. +1
    -1
      src/cpu.c
  4. +21
    -3
      src/insn.c

+ 2
- 2
include/cpu.h View File

@ -7,7 +7,7 @@
#define REG_SP 29 // Stack pointer
#define REG_FP 30 // Frame pointer
#define MAIN_RAM_SIZE 0x200000
#define MAIN_RAM_SIZE 0x200000 * 4
#define SCRATCHPAD_BASE 0x1F800000
#define SCRATCHPAD_END 0x1F8003FF
#define SCRATCHPAD_SIZE (SCRATCHPAD_END - SCRATCHPAD_BASE + 1)
@ -15,7 +15,7 @@
typedef struct {
uint32_t regs[32];
uint32_t pc;
uint32_t main_ram[MAIN_RAM_SIZE];
uint8_t main_ram[MAIN_RAM_SIZE];
uint32_t sratchpad_ram[SCRATCHPAD_SIZE];
} cpu_t;

+ 1
- 1
src/boot.c View File

@ -79,6 +79,6 @@ void boot_psx_prgm(byte_arr_t *prgm) {
const uint32_t insn = cpu_read32(cpu, psx.cpu->pc);
insn_execute(cpu, insn);
psx.cpu->pc += 1;
psx.cpu->pc += 4;
}
}

+ 1
- 1
src/cpu.c View File

@ -23,5 +23,5 @@ void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x) {
uint32_t cpu_read32(cpu_t *cpu, uint32_t addr) {
debug("Read [%08x]", addr);
addr = translate_addr(addr);
return cpu->main_ram[addr];
return *(uint32_t *)&cpu->main_ram[addr];
}

+ 21
- 3
src/insn.c View File

@ -98,10 +98,28 @@ void insn_sll(cpu_t *cpu, uint32_t insn) {
cpu->regs[rd] = cpu->regs[rt] << imm5;
}
void insn_beq(cpu_t *cpu, uint32_t insn) {
const uint8_t rs = RS(insn);
const uint8_t rt = RT(insn);
const int16_t imm = IMM(insn);
debug("BEQ %s, %s, %d", REG_NAMES[rs], REG_NAMES[rt], imm);
if (cpu->regs[rs] == cpu->regs[rt]) {
cpu->pc += imm * 4;
}
}
void insn_lui(cpu_t *cpu, uint32_t insn) {
const uint8_t rt = RT(insn);
const uint16_t imm = IMM(insn);
debug("LUI %s, %x", REG_NAMES[rt], imm);
cpu->regs[rt] = imm << 16;
}
static cpu_insn_handler primary_insn_handler[TABLE_SIZE] = {
[LW] = insn_lw,
[ADDIU] = insn_addiu,
[SW] = insn_sw,
[BEQ] = insn_beq, [LW] = insn_lw, [LUI] = insn_lui,
[ADDIU] = insn_addiu, [SW] = insn_sw,
};
static cpu_insn_handler secondary_insn_handler[TABLE_SIZE] = {

Loading…
Cancel
Save