#include <cpu.h>
#include <bus.h>
#include <common.h>
#include <emu.h>
#include <interrupts.h>
#include <dbg.h>
#include <timer.h>
#include <memory.h>
#include <debug.h>

static cpu_context ctx = {0};
#define CPU_DEBUG 1
#define FILE_LOG 0

#if FILE_LOG == 1
FILE *log;
#endif

void cpu_init() {
    ctx.regs.a = 0x01;
    ctx.regs.f = 0xB0;
    ctx.regs.b = 0x0;
    ctx.regs.c = 0x13;
    ctx.regs.d = 0x00;
    ctx.regs.e = 0xD8;
    ctx.regs.h = 0x01;
    ctx.regs.l = 0x4D;
    ctx.regs.sp = 0xFFFE;
    ctx.regs.pc = 0x100;
    ctx.ie_register = 0;
    ctx.int_flags = 0xE1;
    ctx.int_master_enabled = false;
    ctx.enabling_ime = false;

    timer_get_context()->div = 0xABCC;
#if FILE_LOG == 1
    log = fopen("log.txt", "w");
#endif
}

void fetch_instruction(cpu_context *ctx) {
    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;
        ctx.inst_pc = pc;
        fetch_instruction(&ctx);
        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
        fetch_data(&ctx);
        if(debug_get_context()->breakpoint && pc == debug_get_context()->breakpoint) {
            emu_get_context()->paused = true;
            emu_get_context()->debug = true;
            debug_get_context()->state = DS_DISASSEMBLE;
        }
        while(emu_get_context()->paused && !emu_get_context()->step)
            delay(10);
        if(emu_get_context()->paused) {
            emu_get_context()->step--;
            if(emu_get_context()->step == 0) {
                //printf("debug!\n");
                emu_get_context()->debug = true;
            }
        }
#if CPU_DEBUG == 1
        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' : '-'
        );

        char inst[16];
        inst_to_str(&ctx, inst);
        //printf("%-12s\n", 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);
        if(ctx.cur_inst == NULL){
            printf("Unknown Instruction! %02X\n", ctx.cur_opcode);
            exit(-7);
        }

        //dbg_update();
        //dbg_print();
#endif

        execute();
    } 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;
    }

    return true;
}

u8 cpu_get_ie_register(){
    return ctx.ie_register;
}

void cpu_set_ie_register(u8 ie){
    ctx.ie_register = ie;
}

void cpu_request_interrupt(interrupt_type t) {
    ctx.int_flags |= t;
}

cpu_context *cpu_get_context() {
    return &ctx;
}

void cpu_save_state(cpu_state* state) {
    state->ctx = ctx;
}

void cpu_load_state(const cpu_state* state) {
    ctx = state->ctx;
}