Simplify LR35902 machine timing.

This commit is contained in:
Adrian Conlon 2020-11-13 17:28:35 +00:00
parent eb761bc62f
commit d785b1eae7
2 changed files with 23 additions and 20 deletions

View File

@ -37,6 +37,11 @@ namespace EightBit {
[[nodiscard]] uint8_t flaggedInterrupts(); [[nodiscard]] uint8_t flaggedInterrupts();
[[nodiscard]] uint8_t maskedInterrupts(); [[nodiscard]] uint8_t maskedInterrupts();
Signal<EventArgs> MachineTicked;
void tickMachine(const int extra) { for (int i = 0; i < extra; ++i) tickMachine(); }
void tickMachine() { tick(4); MachineTicked.fire(EventArgs::empty()); }
protected: protected:
virtual int execute() final; virtual int execute() final;
virtual int step() final; virtual int step() final;
@ -45,46 +50,46 @@ namespace EightBit {
void handleINT() final; void handleINT() final;
void memoryWrite() final { void memoryWrite() final {
tick(4); tickMachine();
IntelProcessor::memoryWrite(); IntelProcessor::memoryWrite();
} }
uint8_t memoryRead() final { uint8_t memoryRead() final {
tick(4); tickMachine();
return IntelProcessor::memoryRead(); return IntelProcessor::memoryRead();
} }
void pushWord(register16_t value) final { void pushWord(register16_t value) final {
tick(4); tickMachine();
IntelProcessor::pushWord(value); IntelProcessor::pushWord(value);
} }
void jr(int8_t offset) final { void jr(int8_t offset) final {
IntelProcessor::jr(offset); IntelProcessor::jr(offset);
tick(4); tickMachine();
} }
int jumpConditional(const int condition) final { int jumpConditional(const int condition) final {
if (IntelProcessor::jumpConditional(condition)) if (IntelProcessor::jumpConditional(condition))
tick(4); tickMachine();
return condition; return condition;
} }
int returnConditional(const int condition) final { int returnConditional(const int condition) final {
IntelProcessor::returnConditional(condition); IntelProcessor::returnConditional(condition);
tick(4); tickMachine();
return condition; return condition;
} }
int jrConditional(const int condition) final { int jrConditional(const int condition) final {
if (!IntelProcessor::jrConditional(condition)) if (!IntelProcessor::jrConditional(condition))
tick(4); tickMachine();
return condition; return condition;
} }
void ret() final { void ret() final {
IntelProcessor::ret(); IntelProcessor::ret();
tick(4); tickMachine();
} }
private: private:

View File

@ -7,11 +7,9 @@
EightBit::GameBoy::LR35902::LR35902(Bus& memory) EightBit::GameBoy::LR35902::LR35902(Bus& memory)
: IntelProcessor(memory), : IntelProcessor(memory),
m_bus(memory) { m_bus(memory) {
Ticked.connect([this](EventArgs) { MachineTicked.connect([this](EventArgs) {
if ((cycles() % 4) == 0) {
m_bus.IO().incrementTimers(); m_bus.IO().incrementTimers();
m_bus.IO().transferDma(); m_bus.IO().transferDma();
}
}); });
} }
@ -36,7 +34,7 @@ void EightBit::GameBoy::LR35902::handleRESET() {
IntelProcessor::handleRESET(); IntelProcessor::handleRESET();
di(); di();
SP() = Mask16 - 1; SP() = Mask16 - 1;
tick(4 * 4); tickMachine(4);
} }
void EightBit::GameBoy::LR35902::handleINT() { void EightBit::GameBoy::LR35902::handleINT() {
@ -110,7 +108,7 @@ void EightBit::GameBoy::LR35902::reti() {
EightBit::register16_t EightBit::GameBoy::LR35902::add(uint8_t& f, const register16_t operand, const register16_t value) { EightBit::register16_t EightBit::GameBoy::LR35902::add(uint8_t& f, const register16_t operand, const register16_t value) {
tick(4); tickMachine();
const int addition = operand.word + value.word; const int addition = operand.word + value.word;
const register16_t result = addition; const register16_t result = addition;
@ -508,7 +506,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in
default: default:
UNREACHABLE; UNREACHABLE;
} }
tick(4); tickMachine();
break; break;
case 4: { // 8-bit INC case 4: { // 8-bit INC
auto operand = R(y); auto operand = R(y);
@ -608,7 +606,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in
case 5: { // GB: ADD SP,dd case 5: { // GB: ADD SP,dd
const auto before = SP().word; const auto before = SP().word;
const int8_t value = fetchByte(); const int8_t value = fetchByte();
tick(2 * 4); tickMachine(2);
const auto result = before + value; const auto result = before + value;
SP() = result; SP() = result;
const auto carried = before ^ value ^ (result & Mask16); const auto carried = before ^ value ^ (result & Mask16);
@ -623,7 +621,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in
case 7: { // GB: LD HL,SP + dd case 7: { // GB: LD HL,SP + dd
const auto before = SP().word; const auto before = SP().word;
const int8_t value = fetchByte(); const int8_t value = fetchByte();
tick(4); tickMachine();
const auto result = before + value; const auto result = before + value;
HL() = result; HL() = result;
const auto carried = before ^ value ^ (result & Mask16); const auto carried = before ^ value ^ (result & Mask16);
@ -654,7 +652,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in
break; break;
case 3: // LD SP,HL case 3: // LD SP,HL
SP() = HL(); SP() = HL();
tick(4); tickMachine();
break; break;
default: default:
UNREACHABLE; UNREACHABLE;
@ -694,7 +692,7 @@ void EightBit::GameBoy::LR35902::executeOther(const int x, const int y, const in
switch (y) { switch (y) {
case 0: // JP nn case 0: // JP nn
jump(MEMPTR() = fetchWord()); jump(MEMPTR() = fetchWord());
tick(4); tickMachine();
break; break;
case 1: // CB prefix case 1: // CB prefix
m_prefixCB = true; m_prefixCB = true;