LD, LDH, WRAM, and HRAM

This commit is contained in:
2025-01-30 21:59:05 -07:00
parent 1bf98447a3
commit 022c24dd35
11 changed files with 458 additions and 58 deletions

View File

@ -1,11 +1,11 @@
#include <cpu.h>
#include <bus.h>
#include <common.h>
cpu_context ctx = {0};
void cpu_init() {
ctx.regs.pc = 0x100;
ctx.regs.a = 0x01;
}
static void fetch_instruction() {
@ -13,42 +13,11 @@ static void fetch_instruction() {
ctx.cur_inst = instruction_by_opcode(ctx.cur_opcode);
}
static void fetch_data() {
ctx.mem_dest = 0;
ctx.dest_is_mem = false;
if(ctx.cur_inst == NULL) return;
switch(ctx.cur_inst->mode) {
case AM_IMP: return;
case AM_R:
ctx.fetched_data = cpu_read_reg(ctx.cur_inst->reg_1);
return;
case AM_R_D8:
ctx.fetched_data = bus_read(ctx.regs.pc);
emu_cycles(1);
ctx.regs.pc++;
return;
case AM_D16: {
u16 lo = bus_read(ctx.regs.pc);
emu_cycles(1);
u16 hi = bus_read(ctx.regs.pc+1);
emu_cycles(1);
ctx.fetched_data = lo | (hi << 8);
ctx.regs.pc += 2;
return;
}
default:
printf("Unknown Addressing Mode! %d (%02X)\n", ctx.cur_inst->mode, ctx.cur_opcode);
exit(-7);
return;
}
}
static void execute() {
IN_PROC proc = inst_get_processor(ctx.cur_inst->type);
if (!proc) {
printf("no implementation(%02X)\n", ctx.cur_opcode);
NO_IMPL
}
@ -60,14 +29,22 @@ bool cpu_step() {
if(!ctx.halted) {
u16 pc = ctx.regs.pc;
fetch_instruction();
fetch_data();
printf("%04X: %-7s (%02X %02X %02X) A: %02X B: %02X C: %02X\n", pc, inst_name(ctx.cur_inst->type), ctx.cur_opcode, bus_read(pc+1), bus_read(pc+2), ctx.regs.a, ctx.regs.b, ctx.regs.c);
printf("%04X: %-7s (%02X %02X %02X) AF: %02X%02X BC: %02X%02X DE: %02X%02X HL: %02X%02X SP: %04X PC: %04X\n", pc, inst_name(ctx.cur_inst->type), ctx.cur_opcode, bus_read(pc+1), bus_read(pc+2), ctx.regs.a, ctx.regs.f, ctx.regs.b, ctx.regs.c, ctx.regs.d, ctx.regs.e, ctx.regs.h, ctx.regs.l, ctx.regs.sp, ctx.regs.pc);
if(ctx.cur_inst == NULL){
printf("Unknown Instruction! %02X\n", ctx.cur_opcode);
exit(-7);
}
fetch_data();
execute();
}
return true;
}
u8 cpu_get_ie_register(){
return ctx.ie_register;
}
void cpu_set_ie_register(u8 ie){
ctx.ie_register = ie;
}