Skip to content

Commit

Permalink
started working on emulating VBLANK during VSYNC
Browse files Browse the repository at this point in the history
  • Loading branch information
thrust26 committed Dec 29, 2024
1 parent debec03 commit 9b1a72a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/emucore/tia/TIA.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,6 @@ bool TIA::poke(uInt16 address, uInt8 value)
updateDumpPorts(value);

myDelayQueue.push(VBLANK, value, Delay::vblank);

break;

case AUDV0:
Expand Down Expand Up @@ -1936,7 +1935,7 @@ void TIA::delayedWrite(uInt8 address, uInt8 value)
{
case VBLANK:
flushLineCache();
myFrameManager->setVblank(value & 0x02);
myFrameManager->setVblank(value & 0x02, mySystem->cycles());
break;

case HMOVE:
Expand Down
4 changes: 2 additions & 2 deletions src/emucore/tia/frame-manager/AbstractFrameManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ void AbstractFrameManager::clearHandlers()
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void AbstractFrameManager::setVblank(bool vblank)
void AbstractFrameManager::setVblank(bool vblank, uInt64 cycles)
{
if (vblank == myVblank) return;

myVblank = vblank;

onSetVblank();
onSetVblank(cycles);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
4 changes: 2 additions & 2 deletions src/emucore/tia/frame-manager/AbstractFrameManager.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AbstractFrameManager : public Serializable
/**
* Called by TIA on VBLANK writes.
*/
void setVblank(bool vblank);
void setVblank(bool vblank, uInt64 cycles);

/**
* Called by TIA on VSYNC writes.
Expand Down Expand Up @@ -218,7 +218,7 @@ class AbstractFrameManager : public Serializable
/**
* Called if vblank changes.
*/
virtual void onSetVblank() {}
virtual void onSetVblank(uInt64 cycles) {}

/**
* Called if vsync changes.
Expand Down
45 changes: 36 additions & 9 deletions src/emucore/tia/frame-manager/FrameManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void FrameManager::onReset()
myState = State::waitForVsyncStart;
myLineInState = 0;
myTotalFrames = 0;
myVsyncLines = 0;
myVsyncLineCount = 0;
myY = 0;

myJitterEmulation.reset();
Expand All @@ -50,14 +50,19 @@ void FrameManager::onNextLine()
{
case State::waitForVsyncStart:
if ((myCurrentFrameTotalLines > myFrameLines - 3) || myTotalFrames == 0)
++myVsyncLines;
{
if (myVblank)
++myVsyncLineCount;
}

if (myVsyncLines > Metrics::maxLinesVsync) setState(State::waitForFrameStart);
if (myVsyncLineCount > Metrics::maxLinesVsync) setState(State::waitForFrameStart);

break;

case State::waitForVsyncEnd:
if (++myVsyncLines > Metrics::maxLinesVsync)
if (myVblank)
++myVsyncLineCount;
if (myVsyncLineCount > Metrics::maxLinesVsync)
setState(State::waitForFrameStart);

break;
Expand Down Expand Up @@ -112,16 +117,38 @@ void FrameManager::setAdjustVSize(Int32 adjustVSize)
recalculateMetrics();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FrameManager::onSetVblank(uInt64 cycles)
{
if (myState == State::waitForVsyncEnd)
if (myVblank) // VBLANK switched on
{
myVblankStart = cycles;
cerr << ", VBLANK ON " << cycles;
}
else // VBLANK switched off
{
myVblankCycles += cycles - myVblankStart;
cerr << ", VBLANK OFF " << cycles;
}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FrameManager::onSetVsync(uInt64 cycles)
{
if (myState == State::waitForVsyncEnd) {
myVsyncEnd = cycles;
if(myVblankStart != INT64_MAX)
myVblankCycles += cycles - myVblankStart;
setState(State::waitForFrameStart);
cerr << ", VSYNC waitForVsyncEnd " << cycles << " \n";
}
else {
myVsyncStart = cycles;
myVblankStart = myVblank ? cycles : INT64_MAX;
myVblankCycles = 0;
setState(State::waitForVsyncEnd);
cerr << ", VSYNC waitForFrameStart " << cycles;
}
}

Expand All @@ -139,15 +166,15 @@ void FrameManager::setState(FrameManager::State state)

if (myTotalFrames > Metrics::initialGarbageFrames)
myJitterEmulation.frameComplete(myCurrentFrameFinalLines,
static_cast<Int32>(myVsyncEnd - myVsyncStart));
static_cast<Int32>(myVsyncEnd - myVsyncStart), static_cast<Int32>(myVblankCycles));

notifyFrameStart();

myVsyncLines = 0;
myVsyncLineCount = 0;
break;

case State::frame:
myVsyncLines = 0;
myVsyncLineCount = 0;
myY = 0;
break;

Expand Down Expand Up @@ -176,7 +203,7 @@ bool FrameManager::onSave(Serializer& out) const

out.putInt(static_cast<uInt32>(myState));
out.putInt(myLineInState);
out.putInt(myVsyncLines);
out.putInt(myVsyncLineCount);
out.putInt(myY);
out.putInt(myLastY);

Expand All @@ -200,7 +227,7 @@ bool FrameManager::onLoad(Serializer& in)

myState = static_cast<State>(in.getInt());
myLineInState = in.getInt();
myVsyncLines = in.getInt();
myVsyncLineCount = in.getInt();
myY = in.getInt();
myLastY = in.getInt();

Expand Down
6 changes: 5 additions & 1 deletion src/emucore/tia/frame-manager/FrameManager.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class FrameManager: public AbstractFrameManager {

void setLayout(FrameLayout mode) override { layout(mode); }

void onSetVblank(uInt64 cycles) override;

void onSetVsync(uInt64 cycles) override;

void onNextLine() override;
Expand Down Expand Up @@ -116,7 +118,7 @@ class FrameManager: public AbstractFrameManager {

State myState{State::waitForVsyncStart};
uInt32 myLineInState{0};
uInt32 myVsyncLines{0};
uInt32 myVsyncLineCount{0};
uInt32 myY{0}, myLastY{0};

uInt32 myVblankLines{0};
Expand All @@ -129,6 +131,8 @@ class FrameManager: public AbstractFrameManager {

uInt64 myVsyncStart{0};
uInt64 myVsyncEnd{0};
uInt64 myVblankStart{0};
uInt64 myVblankCycles{0};
bool myJitterEnabled{false};

JitterEmulation myJitterEmulation;
Expand Down
5 changes: 4 additions & 1 deletion src/emucore/tia/frame-manager/JitterEmulation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ void JitterEmulation::setSensitivity(Int32 sensitivity)
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles)
void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles, Int32 vblankCycles)
{
//#ifdef DEBUG_BUILD
// const int vsyncLines = round((vsyncCycles - 2) / 76.0);
// cerr << "TV jitter " << myJitter << " - " << scanlineCount << ", " << vsyncCycles << ", " << vsyncLines << '\n';
//#endif

if (mySensitivity >= 6)
vsyncCycles = std::min(vsyncCycles, vblankCycles);

// Check if current frame size is stable compared to previous frame
const bool scanlinesStable = scanlineCount == myLastFrameScanlines;
const bool vsyncCyclesStable = vsyncCycles >= myVsyncCycles;
Expand Down
2 changes: 1 addition & 1 deletion src/emucore/tia/frame-manager/JitterEmulation.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class JitterEmulation : public Serializable
void setRecovery(Int32 recoveryFactor) { myJitterRecovery = recoveryFactor; }
void setYStart(Int32 ystart) { myYStart = ystart; }

void frameComplete(Int32 scanlineCount, Int32 vsyncCycles);
void frameComplete(Int32 scanlineCount, Int32 vsyncCycles, Int32 vblankCycles);
Int32 jitter() const { return myJitter; }

bool vsyncCorrect() const { return myVsyncCorrect; }
Expand Down

0 comments on commit 9b1a72a

Please sign in to comment.