#include <instructions/instructions.h>

void instruction_nop(cpu_status_t* status){
    status->pc += 2;
}

void instruction_stsl_mash_amr(cpu_status_t* status){
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] -= 4;

    cpu_write32(status, status->r[LO_NIBBLE(cpu_read8(status,status->pc))], status->mach);

    status->pc += 2;
}

void instruction_stsl_macl_amr(cpu_status_t* status){
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] -= 4;
    cpu_write32(status, status->r[LO_NIBBLE(cpu_read8(status,status->pc))], status->macl);
    status->pc += 2;
}

void instruction_stsl_pr_amr(cpu_status_t* status){
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] -= 4;
    cpu_write32(status, status->r[LO_NIBBLE(cpu_read8(status,status->pc))],status->pr);
    status->pc += 2;
}

void instruction_clrt(cpu_status_t* status){
  status->t = 0;
  status->pc += 2;
}

void instruction_ldsl_arp_pr(cpu_status_t* status){
    status->pr = cpu_read32(status, status->r[LO_NIBBLE(cpu_read8(status,status->pc))]);
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] += 4;
    status->pc += 2;
}

void instruction_ldsl_arp_macl(cpu_status_t* status){
    status->macl = cpu_read32(status,status->r[LO_NIBBLE(cpu_read8(status,status->pc))]);
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] += 4;
    status->pc += 2;
}

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

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

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

void instruction_ldsl_arm_mach(cpu_status_t* status){
    status->mach = cpu_read32(status, status->r[LO_NIBBLE(cpu_read8(status,status->pc))]);

    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] += 4;
    status->pc += 2;
}

void instruction_sts_mach_r(cpu_status_t* status){
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] = status->mach;
    status->r[LO_NIBBLE(cpu_read8(status,status->pc))] |= 0xFFFFFC00;
    status->pc += 2;
}