calls, jumps, and stack.

This commit is contained in:
2025-01-30 22:54:33 -07:00
parent 022c24dd35
commit dd1c6d2e0e
7 changed files with 146 additions and 4 deletions

View File

@ -1,6 +1,7 @@
#include <cpu.h>
#include <emu.h>
#include <bus.h>
#include <stack.h>
//process CPU instructions...
@ -28,6 +29,21 @@ static void proc_nop(cpu_context *ctx) {
}
static void proc_pop(cpu_context *ctx) {
u16 n = stack_pop16();
cpu_set_reg(ctx->cur_inst->reg_1, n);
emu_cycles(2);
if (ctx->cur_inst->reg_1 == RT_AF) {
cpu_set_reg(ctx->cur_inst->reg_1, n & 0xFFF0);
}
}
static void proc_push(cpu_context *ctx) {
stack_push16(cpu_read_reg(ctx->cur_inst->reg_1));
emu_cycles(2);
}
static void proc_ldh(cpu_context *ctx) {
if (!ctx->dest_is_mem) {
cpu_set_reg(ctx->cur_inst->reg_1, bus_read(0XFF00 | ctx->fetched_data));
@ -83,13 +99,56 @@ static bool check_condition(cpu_context *ctx) {
return false;
}
static void proc_jp(cpu_context *ctx) {
static void goto_addr(cpu_context *ctx, u16 addr, bool pushpc){
if (check_condition(ctx)) {
ctx->regs.pc = ctx->fetched_data;
if(pushpc) {
stack_push16(ctx->regs.pc);
emu_cycles(2);
}
ctx->regs.pc = addr;
emu_cycles(1);
}
}
static void proc_jp(cpu_context *ctx) {
goto_addr(ctx, ctx->fetched_data, false);
}
static void proc_call(cpu_context *ctx) {
goto_addr(ctx, ctx->fetched_data, true);
}
static void proc_rst(cpu_context *ctx) {
goto_addr(ctx, ctx->cur_inst->param, true);
}
static void proc_ret(cpu_context *ctx) {
if (ctx->cur_inst->cond != CT_NONE) {
emu_cycles(1);
}
if(check_condition(ctx)){
u16 lo = stack_pop();
emu_cycles(1);
u16 hi = stack_pop();
emu_cycles(1);
u16 n = (hi << 8) | lo;
ctx->regs.pc = n;
emu_cycles(1);
}
}
static void proc_reti(cpu_context *ctx) {
ctx->int_master_enabled = true;
proc_ret(ctx);
}
static void proc_jr(cpu_context *ctx) {
char rel = (char)(ctx->fetched_data & 0XFF);
u16 addr = ctx->regs.pc + rel;
goto_addr(ctx, addr, false);
}
IN_PROC processors[] = {
[IN_NONE] = proc_none,
[IN_NOP] = proc_nop,
@ -97,7 +156,14 @@ IN_PROC processors[] = {
[IN_JP] = proc_jp,
[IN_DI] = proc_di,
[IN_XOR] = proc_xor,
[IN_LDH] = proc_ldh
[IN_LDH] = proc_ldh,
[IN_POP] = proc_pop,
[IN_PUSH] = proc_push,
[IN_CALL] = proc_call,
[IN_JR] = proc_jr,
[IN_RET] = proc_ret,
[IN_RETI] = proc_reti,
[IN_RST] = proc_rst
};
IN_PROC inst_get_processor(in_type type) {