gbemu/lib/cpu.c

50 lines
1.2 KiB
C

#include <cpu.h>
#include <bus.h>
#include <common.h>
cpu_context ctx = {0};
void cpu_init() {
ctx.regs.pc = 0x100;
}
static void fetch_instruction() {
ctx.cur_opcode = bus_read(ctx.regs.pc++);
ctx.cur_inst = instruction_by_opcode(ctx.cur_opcode);
}
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
}
proc(&ctx);
}
bool cpu_step() {
if(!ctx.halted) {
u16 pc = ctx.regs.pc;
fetch_instruction();
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;
}