Skip to content

Commit

Permalink
(De)serialize operands pointer in CPU (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kremi151 committed Jan 2, 2021
1 parent 2cee6a1 commit 5651182
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
57 changes: 53 additions & 4 deletions core/source/emulator/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <emulator/io_registers.h>
#include <operands/registry.h>
#include <operands/tables.h>
#include <operands/prefix.h>
#include <exception/read_exception.h>
#include <exception/state_exception.h>

using namespace FunkyBoy;

Expand Down Expand Up @@ -387,19 +389,66 @@ void CPU::writeAF(FunkyBoy::u16 val) {

void CPU::serialize(std::ostream &ostream) const {
instrContext.serialize(ostream);

const Operand *operandTable;
u8 operandIndex;
if (instrContext.instr == 0xCB) {
if (*operands == Operands::decodePrefix) {
operandIndex = 0;
} else if (*operands == Operands::prefixPlaceholder) {
operandIndex = 1;
} else {
operandTable = Operands::Tables::prefixInstructions[instrContext.cbInstr];
for (int i = 0 ;; i++) {
if (*(operandTable++) == *operands) {
operandIndex = i + 2; // For Operands::decodePrefix and Operands::prefixPlaceholder
break;
} else if (*operandTable == nullptr) {
throw Exception::WrongStateException("Prefix operand index could not be determined");
}
}
}
} else {
operandTable = Operands::Tables::instructions[instrContext.instr];
for (int i = 0 ;; i++) {
if (*(operandTable++) == *operands) {
operandIndex = i;
break;
} else if (*operandTable == nullptr) {
throw Exception::WrongStateException("Operand index could not be determined");
}
}
}
ostream.put(operandIndex);

ostream.put(timerOverflowingCycles);
ostream.put(delayedTIMAIncrease);
ostream.put(joypadWasNotPressed);
}

void CPU::deserialize(std::istream &istream) {
instrContext.deserialize(istream);
char buffer[3];

char buffer[4];
istream.read(buffer, sizeof(buffer));
if (!istream) {
throw Exception::ReadException("Stream is too short");
}
timerOverflowingCycles = buffer[0];
delayedTIMAIncrease = buffer[1];
joypadWasNotPressed = buffer[2];

u8 operandIndex = buffer[0];
if (instrContext.instr == 0xCB) {
if (operandIndex == 0) {
operands = Operands::Tables::instructions[0xCB];
} else if (operandIndex == 1) {
operands = Operands::Tables::instructions[0xCB] + 1;
} else {
operands = Operands::Tables::prefixInstructions[instrContext.cbInstr] + (operandIndex - 2);
}
} else {
operands = Operands::Tables::instructions[instrContext.instr] + operandIndex;
}

timerOverflowingCycles = buffer[1];
delayedTIMAIncrease = buffer[2];
joypadWasNotPressed = buffer[3];
}
5 changes: 5 additions & 0 deletions core/source/operands/prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,8 @@ bool Operands::decodePrefix(InstrContext &context, Memory &memory) {
#endif
return true;
}

bool Operands::prefixPlaceholder(InstrContext &context, Memory &memory) {
// No-op
return true;
}
8 changes: 8 additions & 0 deletions core/source/operands/prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ namespace FunkyBoy::Operands {
*/
bool decodePrefix(InstrContext &context, Memory &memory);

/**
* No-operator as a placeholder for prefix operands to be fetched
* @param context
* @param memory
* @return
*/
bool prefixPlaceholder(InstrContext &context, Memory &memory);

}

#endif //FB_CORE_OPERANDS_PREFIX_PREFIX_H
4 changes: 3 additions & 1 deletion core/source/operands/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ namespace FunkyBoy::Operands::Registry {

const Operand decodePrefix[3] = {
Operands::decodePrefix,
Operands::nop, // This is a workaround, the decodePrefix operand will fetch the effective operand list to use
// This is a workaround, the decodePrefix operand will fetch the effective operand list to use
// When changing this, please also adapt CPU::serialize and CPU::deserialize
Operands::prefixPlaceholder,
nullptr
};
}

0 comments on commit 5651182

Please sign in to comment.