gbemu/lib/cpu.c

58 lines
1.5 KiB
C
Raw Normal View History

2025-01-30 14:30:19 -07:00
#include <cpu.h>
2025-01-30 16:27:27 -07:00
#include <bus.h>
2025-01-30 21:59:05 -07:00
#include <common.h>
2025-01-31 12:24:55 -07:00
#include <emu.h>
2025-01-30 16:27:27 -07:00
cpu_context ctx = {0};
2025-01-30 14:30:19 -07:00
void cpu_init() {
2025-01-30 16:27:27 -07:00
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);
}
2025-01-30 14:30:19 -07:00
2025-01-30 16:27:27 -07:00
static void execute() {
2025-01-30 17:03:56 -07:00
IN_PROC proc = inst_get_processor(ctx.cur_inst->type);
if (!proc) {
2025-01-30 21:59:05 -07:00
printf("no implementation(%02X)\n", ctx.cur_opcode);
2025-01-30 17:03:56 -07:00
NO_IMPL
}
proc(&ctx);
2025-01-30 14:30:19 -07:00
}
bool cpu_step() {
2025-01-30 16:27:27 -07:00
if(!ctx.halted) {
u16 pc = ctx.regs.pc;
fetch_instruction();
2025-01-31 12:24:55 -07:00
fetch_data();
char flags[16];
sprintf(flags, "%c%c%c%c",
ctx.regs.f & (1 << 7) ? 'Z' : '-',
ctx.regs.f & (1 << 6) ? 'N' : '-',
ctx.regs.f & (1 << 5) ? 'H' : '-',
ctx.regs.f & (1 << 4) ? 'C' : '-'
);
printf("%08lX - %04X: %-7s (%02X %02X %02X) A: %02X F: %s BC: %02X%02X DE: %02X%02X HL: %02X%02X SP: %04X PC: %04X\n", emu_get_context()->ticks, pc, inst_name(ctx.cur_inst->type), ctx.cur_opcode, bus_read(pc+1), bus_read(pc+2), ctx.regs.a, flags, ctx.regs.b, ctx.regs.c, ctx.regs.d, ctx.regs.e, ctx.regs.h, ctx.regs.l, ctx.regs.sp, ctx.regs.pc);
2025-01-30 17:03:56 -07:00
if(ctx.cur_inst == NULL){
printf("Unknown Instruction! %02X\n", ctx.cur_opcode);
exit(-7);
}
2025-01-30 16:27:27 -07:00
execute();
}
return true;
2025-01-30 21:59:05 -07:00
}
u8 cpu_get_ie_register(){
return ctx.ie_register;
}
void cpu_set_ie_register(u8 ie){
ctx.ie_register = ie;
2025-01-30 14:30:19 -07:00
}