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-31 14:39:38 -07:00
|
|
|
#include <interrupts.h>
|
2025-01-31 17:07:09 -07:00
|
|
|
#include <dbg.h>
|
2025-02-01 00:48:49 -07:00
|
|
|
#include <timer.h>
|
2025-01-30 16:27:27 -07:00
|
|
|
|
|
|
|
cpu_context ctx = {0};
|
2025-02-01 00:48:49 -07:00
|
|
|
#define CPU_DEBUG 0
|
|
|
|
#define FILE_LOG 0
|
|
|
|
|
|
|
|
#if FILE_LOG == 1
|
|
|
|
FILE *log;
|
|
|
|
#endif
|
2025-01-30 14:30:19 -07:00
|
|
|
|
|
|
|
void cpu_init() {
|
2025-02-01 00:48:49 -07:00
|
|
|
ctx.regs.a = 0x01;
|
|
|
|
ctx.regs.f = 0xB0;
|
|
|
|
ctx.regs.b = 0x00;
|
|
|
|
ctx.regs.c = 0x13;
|
|
|
|
ctx.regs.d = 0x00;
|
|
|
|
ctx.regs.e = 0xD8;
|
|
|
|
ctx.regs.h = 0x01;
|
|
|
|
ctx.regs.l = 0x4D;
|
|
|
|
ctx.regs.sp = 0xFFFE;
|
2025-01-30 16:27:27 -07:00
|
|
|
ctx.regs.pc = 0x100;
|
2025-02-01 00:48:49 -07:00
|
|
|
ctx.ie_register = 0;
|
|
|
|
ctx.int_flags = 0;
|
|
|
|
ctx.int_master_enabled = false;
|
|
|
|
ctx.enabling_ime = false;
|
|
|
|
|
|
|
|
timer_get_context()->div = 0xABCC;
|
|
|
|
#if FILE_LOG == 1
|
|
|
|
log = fopen("log.txt", "w");
|
|
|
|
#endif
|
2025-01-30 16:27:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02-01 00:48:49 -07:00
|
|
|
emu_cycles(1);
|
|
|
|
#if FILE_LOG == 1
|
|
|
|
fprintf(log, "A:%02X F:%02X B:%02X C:%02X D:%02X E:%02X H:%02X L:%02X SP:%04X PC:%04X PCMEM:%02X,%02X,%02X,%02X\n",
|
|
|
|
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, pc,
|
|
|
|
bus_read(pc), bus_read(pc+1), bus_read(pc+2), bus_read(pc+3)
|
|
|
|
);
|
|
|
|
#endif
|
2025-01-31 12:24:55 -07:00
|
|
|
fetch_data();
|
2025-02-01 00:48:49 -07:00
|
|
|
#if CPU_DEBUG == 1
|
2025-01-31 12:24:55 -07:00
|
|
|
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' : '-'
|
|
|
|
);
|
2025-01-31 17:07:09 -07:00
|
|
|
|
|
|
|
char inst[16];
|
|
|
|
inst_to_str(&ctx, inst);
|
|
|
|
|
|
|
|
printf("%08lX - %04X: %-12s (%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, 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-31 17:07:09 -07:00
|
|
|
|
|
|
|
dbg_update();
|
|
|
|
dbg_print();
|
2025-02-01 00:48:49 -07:00
|
|
|
#endif
|
2025-01-31 17:07:09 -07:00
|
|
|
|
2025-01-30 16:27:27 -07:00
|
|
|
execute();
|
2025-01-31 14:39:38 -07:00
|
|
|
} else {
|
|
|
|
//is halted
|
|
|
|
emu_cycles(1);
|
|
|
|
|
|
|
|
if(ctx.int_flags) {
|
|
|
|
ctx.halted = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx.int_master_enabled) {
|
|
|
|
cpu_handle_interrupts(&ctx);
|
|
|
|
ctx.enabling_ime = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx.enabling_ime) {
|
|
|
|
ctx.int_master_enabled = true;
|
2025-01-30 16:27:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02-01 00:48:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_request_interrupt(interrupt_type t) {
|
|
|
|
ctx.int_flags |= t;
|
2025-01-30 14:30:19 -07:00
|
|
|
}
|