#include <cpu.h>
#include <instructions/instructions.h>

void instruction_mov_r_r(cpu_status_t* status) {
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] = status->r[HI_NIBBLE(cpu_read8(status,status->pc+1))];
    status->pc += 2;
}

void instruction_mov_imm_r(cpu_status_t* status){
    if ((cpu_read8(status,status->pc+1) & 0x80) == 0)
        status->r[LO_NIBBLE(cpu_read8(status,status->pc))] = (0x000000FF & cpu_read8(status,status->pc+1));
    else
        status->r[LO_NIBBLE(cpu_read8(status,status->pc))] = (0xFFFFFF00 | cpu_read8(status,status->pc+1));

    status->pc += 2;
}

void instruction_movt_r(cpu_status_t* status){
    if (status->t == 1)
        status->r[LO_NIBBLE(cpu_read8(status,status->pc))] = 0x00000001;
    else
        status->r[LO_NIBBLE(cpu_read8(status,status->pc))] = 0x00000000;
    status->pc += 2;
}

void instruction_swapb_r_r(cpu_status_t* status){
    int n = status->r[LO_NIBBLE(cpu_read8(status,status->pc))];
    int m = status->r[HI_NIBBLE(cpu_read8(status,status->pc+1))];
    unsigned long temp0, temp1;
    temp0 = status->r[m] & 0xFFFF0000;
    temp1 = (status->r[m] & 0x000000FF) << 8;
    status->r[n] = (status->r[m] & 0x0000FF00) >> 8;
    status->r[n] = status->r[n] | temp1 | temp0;
    status->pc += 2;
}

void instruction_mova_disp_pc_r0(cpu_status_t* status){
    unsigned int disp;
    disp = (unsigned int)(0x000000FF & cpu_read8(status,status->pc+1));
    status->r[0] = (status->pc & 0xFFFFFFFC) + 4 + (disp << 2);
    status->pc += 2;
}

