2025-01-30 14:30:19 -07:00
|
|
|
#include <cpu.h>
|
2025-01-30 16:27:27 -07:00
|
|
|
#include <bus.h>
|
|
|
|
|
|
|
|
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;
|
2025-01-30 18:38:29 -07:00
|
|
|
ctx.regs.a = 0x01;
|
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 fetch_data() {
|
|
|
|
ctx.mem_dest = 0;
|
|
|
|
ctx.dest_is_mem = false;
|
|
|
|
|
2025-01-30 17:03:56 -07:00
|
|
|
if(ctx.cur_inst == NULL) return;
|
|
|
|
|
2025-01-30 16:27:27 -07:00
|
|
|
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:
|
2025-01-30 18:38:29 -07:00
|
|
|
printf("Unknown Addressing Mode! %d (%02X)\n", ctx.cur_inst->mode, ctx.cur_opcode);
|
2025-01-30 16:27:27 -07:00
|
|
|
exit(-7);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void execute() {
|
2025-01-30 17:03:56 -07:00
|
|
|
IN_PROC proc = inst_get_processor(ctx.cur_inst->type);
|
|
|
|
|
|
|
|
if (!proc) {
|
|
|
|
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();
|
|
|
|
fetch_data();
|
2025-01-30 18:38:29 -07:00
|
|
|
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);
|
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 14:30:19 -07:00
|
|
|
}
|