0
I am developing a Gameboy emulator and one of the tasks is to set the flags after each instruction:
...
//Representa pares de registradores do gameboy
union RegPair {
std::uint16_t reg;
struct pair
{
std::uint8_t lo;
std::uint8_t hi;
}p;
};
struct Registers{
RegPair AF, BC, DE, HL;
std::uint16_t SP, PC;
};
...
Set of Gameboy flags
...
//Define as flags do processador
#define FLAG_ZERO_FLAG 7
#define FLAG_SUBSTRCAT_FLAG 6
#define FLAG_HALF_CARRY 5
#define FLAG_CARRY_FLAG 4
#define FLAG_NONE 0
...
Initialization of registers:
...
reg.AF.reg = 0x01B0;
reg.BC.reg = 0x0013;
reg.DE.reg = 0x00D8;
reg.HL.reg = 0x014D;
reg.SP = 0xFFFE;
reg.PC = 0x0000; //Ponto de entrada também pode ser 0x100
...
I’ll use the opcode example 0xD
Decrement Register C, opcode table can be found here:
void CPU::DEC8(std::uint8_t& target, int cyclesToUpdate)
{
cycles += cyclesToUpdate;
target--;
if (target == 0) {
wxLogDebug("ZERO FLAG OCORREU");
reg.AF.p.lo |= FLAG_ZERO_FLAG;
}
reg.PC++;
}
It turns out that if there is no zero flag I have to specifically reset the zero flag, and it can also occur to add the half carry flag, and if there is no reset to its position, I found this answer in stackoverflow but I found it confusing, for example, when passing a parameter to fstream:
("filename", std::ios::in | std::ios::binary)
It seems to be something simpler than the way it is in the answer.
Something like that? https://answall.com/q/175345/101
– Maniero
FLAG_ZERO_FLAG
is7
which corresponds to111
in binary right when doingreg.AF.p.lo |= FLAG_ZERO_FLAG;
is putting the last 3 bits to 1. If you wanted them to zeroand
binary with reversed mask:reg.AF.p.lo &= ~FLAG_ZERO_FLAG;
– Isac
@Isac but this way would affect the other flags that are in this register or just this specific flag?
– Samuel Ives
It only refers to this flag.
– Isac