Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented RLC and RRC instructions #28

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/CPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class CPU {
void PFX_SLA_HL(); // Shift Left Arithmetically for HL
void PFX_RES_R8();
void PFX_RES_HL();
void PFX_RLC_R8();
void PFX_RLC_HL();
void PFX_RRC_R8();
void PFX_RRC_HL();
void PFX_SRA_R8(); //prefix instruction for arithmetic right shift
void PFX_SRA_HL(); //prefix instruction for arithmetic right shift with HL
void PFX_SRL_R8(); //prefix instruction for logical right shift
Expand Down Expand Up @@ -122,4 +126,4 @@ class CPU {
uint8_t pfx_rs_family_index;
};

#endif
#endif
77 changes: 77 additions & 0 deletions src/cpu/ins_prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ void CPU::PFX()
}
}

if (pfx_rs_family_index == 0b000){
//RLC
if(pfx_register_index == 0b110){
PFX_RLC_HL();
cycles = (3+1);
}
else{
PFX_RLC_R8();
cycles = (1+1);
}
}

if (pfx_bit_index == 0b101){ // SRA
if(pfx_register_index == 0b110){
Expand All @@ -42,6 +53,18 @@ void CPU::PFX()
}
}

if (pfx_rs_family_index == 0b001){
//RRC
if(pfx_register_index == 0b110){
PFX_RRC_HL();
cycles = (3+1);
}
else{
PFX_RRC_R8();
cycles = (1+1);
}
}

if (pfx_bit_index == 0b111){ // SRL
if(pfx_register_index == 0b110){
PFX_SRL_HL();
Expand Down Expand Up @@ -378,3 +401,57 @@ void CPU::PFX_SRL_R8()
set_flag(Flags::zero, 1);
}
}

void CPU::PFX_RLC_R8()
{
uint8_t bit_7 = (*pfx_register_ptr) >> 7;
*pfx_register_ptr <<= 1;
*pfx_register_ptr |= bit_7;

set_flag(Flags::neg, 0);
set_flag(Flags::half_carry, 0);
set_flag(Flags::zero, *pfx_register_ptr == 0);
set_flag(Flags::carry, bit_7);
}

void CPU::PFX_RLC_HL()
{
operand = read8(HL.full);
operand_addr = HL.full;
uint8_t bit_7 = (operand) >> 7;
operand <<= 1;
operand |= bit_7;
write8(operand_addr, operand);

set_flag(Flags::neg, 0);
set_flag(Flags::half_carry, 0);
set_flag(Flags::zero, operand == 0);
set_flag(Flags::carry, bit_7);
}

void CPU::PFX_RRC_R8()
{
uint8_t bit_0 = (*pfx_register_ptr) & 1;
*pfx_register_ptr >>= 1;
*pfx_register_ptr |= (bit_0 << 7);

set_flag(Flags::neg, 0);
set_flag(Flags::half_carry, 0);
set_flag(Flags::zero, *pfx_register_ptr == 0);
set_flag(Flags::carry, bit_0);
}

void CPU::PFX_RRC_HL()
{
operand = read8(HL.full);
operand_addr = HL.full;
uint8_t bit_0 = (operand) & 1;
operand >>= 1;
operand |= (bit_0 << 7);
write8(operand_addr, operand);

set_flag(Flags::neg, 0);
set_flag(Flags::half_carry, 0);
set_flag(Flags::zero, operand == 0);
set_flag(Flags::carry, bit_0);
}