started to eliminate magic numbers in new TIA code

This commit is contained in:
thrust26 2019-01-05 15:34:19 +01:00
parent 2806c0da58
commit 5e89b4d02b
7 changed files with 76 additions and 66 deletions

View File

@ -184,7 +184,7 @@ void Ball::tick(bool isReceivingMclock)
myIsRendering = true;
myRenderCounter = Count::renderCounterOffset;
uInt8 starfieldDelta = (myCounter + 160 - myLastMovementTick) % 4;
uInt8 starfieldDelta = (myCounter + TIA::H_PIXEL - myLastMovementTick) % 4;
if (starfieldEffect && starfieldDelta == 3 && myWidth < 4) ++myRenderCounter;
switch (starfieldDelta) {
@ -204,7 +204,7 @@ void Ball::tick(bool isReceivingMclock)
} else if (myIsRendering && ++myRenderCounter >= (starfieldEffect ? myEffectiveWidth : myWidth))
myIsRendering = false;
if (++myCounter >= 160)
if (++myCounter >= TIA::H_PIXEL)
myCounter = 0;
}
@ -268,10 +268,10 @@ uInt8 Ball::getPosition() const
// clock count after decode until first pixel +
// 1 (it'll take another cycle after the decode for the rendter counter to start ticking)
//
// The result may be negative, so we add 160 and do the modulus -> 317 = 156 + 160 + 1
// The result may be negative, so we add 160 and do the modulus -> 317 = 156 + TIA::H_PIXEL + 1
//
// Mind the sign of renderCounterOffset: it's defined negative above
return (317 - myCounter - Count::renderCounterOffset + myTIA->getPosition()) % 160;
return (317 - myCounter - Count::renderCounterOffset + myTIA->getPosition()) % TIA::H_PIXEL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -280,7 +280,7 @@ void Ball::setPosition(uInt8 newPosition)
myTIA->flushLineCache();
// See getPosition for an explanation
myCounter = (317 - newPosition - Count::renderCounterOffset + myTIA->getPosition()) % 160;
myCounter = (317 - newPosition - Count::renderCounterOffset + myTIA->getPosition()) % TIA::H_PIXEL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -212,7 +212,7 @@ void Missile::tick(uInt8 hclock, bool isReceivingMclock)
if (++myRenderCounter >= (myIsMoving ? myEffectiveWidth : myWidth)) myIsRendering = false;
}
if (++myCounter >= 160) myCounter = 0;
if (++myCounter >= TIA::H_PIXEL) myCounter = 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -288,7 +288,7 @@ uInt8 Missile::getPosition() const
// The result may be negative, so we add 160 and do the modulus
//
// Mind the sign of renderCounterOffset: it's defined negative above
return (317 - myCounter - Count::renderCounterOffset + myTIA->getPosition()) % 160;
return (317 - myCounter - Count::renderCounterOffset + myTIA->getPosition()) % TIA::H_PIXEL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -297,7 +297,7 @@ void Missile::setPosition(uInt8 newPosition)
myTIA->flushLineCache();
// See getPosition for an explanation
myCounter = (317 - newPosition - Count::renderCounterOffset + myTIA->getPosition()) % 160;
myCounter = (317 - newPosition - Count::renderCounterOffset + myTIA->getPosition()) % TIA::H_PIXEL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -104,7 +104,7 @@ void Player::nusiz(uInt8 value, bool hblank)
myDecodes != oldDecodes &&
myIsRendering &&
(myRenderCounter - Count::renderCounterOffset) < 2 &&
!myDecodes[(myCounter - myRenderCounter + Count::renderCounterOffset + 159) % 160]
!myDecodes[(myCounter - myRenderCounter + Count::renderCounterOffset + TIA::H_PIXEL - 1) % TIA::H_PIXEL]
) {
myIsRendering = false;
}
@ -307,7 +307,7 @@ void Player::tick()
if (mySampleCounter > 7) myIsRendering = false;
}
if (++myCounter >= 160) myCounter = 0;
if (++myCounter >= TIA::H_PIXEL) myCounter = 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -338,13 +338,13 @@ uInt8 Player::getRespClock() const
switch (myDivider)
{
case 1:
return (myCounter + 160 - 5) % 160;
return (myCounter + TIA::H_PIXEL - 5) % TIA::H_PIXEL;
case 2:
return (myCounter + 160 - 9) % 160;
return (myCounter + TIA::H_PIXEL - 9) % TIA::H_PIXEL;
case 4:
return (myCounter + 160 - 12) % 160;
return (myCounter + TIA::H_PIXEL - 12) % TIA::H_PIXEL;
default:
throw runtime_error("invalid width");
@ -420,12 +420,12 @@ uInt8 Player::getPosition() const
// (current counter - 156 (the decode clock of copy 0)) +
// clock count after decode until first pixel +
// shift (accounts for wide player shift) +
// 1 (it'll take another cycle after the decode for the rendter counter to start ticking)
// 1 (it'll take another cycle after the decode for the render counter to start ticking)
//
// The result may be negative, so we add 160 and do the modulus -> 317 = 156 + 160 + 1
// The result may be negative, so we add TIA::H_PIXEL and do the modulus -> 317 = 156 + TIA::H_PIXEL + 1
//
// Mind the sign of renderCounterOffset: it's defined negative above
return (317 - myCounter - Count::renderCounterOffset + shift + myTIA->getPosition()) % 160;
return (317 - myCounter - Count::renderCounterOffset + shift + myTIA->getPosition()) % TIA::H_PIXEL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -436,7 +436,7 @@ void Player::setPosition(uInt8 newPosition)
const uInt8 shift = myDivider == 1 ? 0 : 1;
// See getPosition for an explanation
myCounter = (317 - newPosition - Count::renderCounterOffset + shift + myTIA->getPosition()) % 160;
myCounter = (317 - newPosition - Count::renderCounterOffset + shift + myTIA->getPosition()) % TIA::H_PIXEL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -188,7 +188,7 @@ void Playfield::tick(uInt32 x)
{
myX = x;
if (myX == 80 || myX == 0) myRefp = myReflected;
if (myX == TIA::H_PIXEL / 2 || myX == 0) myRefp = myReflected;
if (x & 0x03) return;
@ -196,7 +196,7 @@ void Playfield::tick(uInt32 x)
if (myEffectivePattern == 0) {
currentPixel = 0;
} else if (x < 80) {
} else if (x < TIA::H_PIXEL / 2) {
currentPixel = myEffectivePattern & (1 << (x >> 2));
} else if (myRefp) {
currentPixel = myEffectivePattern & (1 << (39 - (x >> 2)));
@ -249,10 +249,10 @@ void Playfield::applyColors()
uInt8 Playfield::getColor() const
{
if (!myDebugEnabled)
return myX < 80 ? myColorLeft : myColorRight;
return myX < TIA::H_PIXEL / 2 ? myColorLeft : myColorRight;
else
{
if (myX < 80)
if (myX < TIA::H_PIXEL / 2)
{
// left side:
if(myX < 16)
@ -265,16 +265,16 @@ uInt8 Playfield::getColor() const
// right side:
if(!myReflected)
{
if(myX < 80 + 16)
if(myX < TIA::H_PIXEL / 2 + 16)
return myDebugColor - 2; // PF0
if(myX < 80 + 48)
if(myX < TIA::H_PIXEL / 2 + 48)
return myDebugColor; // PF1
}
else
{
if(myX >= 160 - 16)
if(myX >= TIA::H_PIXEL - 16)
return myDebugColor - 2; // PF0
if(myX >= 160 - 48)
if(myX >= TIA::H_PIXEL - 48)
return myDebugColor; // PF1
}
}

View File

@ -64,7 +64,7 @@ enum ResxCounter: uInt8 {
// This parameter still has room for tuning. If we go lower than 73, long005 will show
// a slight artifact (still have to crosscheck on real hardware), if we go lower than
// 70, the G.I. Joe will show an artifact (hole in roof).
static constexpr uInt8 resxLateHblankThreshold = 73;
static constexpr uInt8 resxLateHblankThreshold = TIA::H_CYCLES - 3;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TIA::TIA(Console& console, Settings& settings)
@ -192,9 +192,9 @@ void TIA::reset()
setFixedColorPalette(mySettings.getString("tia.dbgcolors"));
// Blank the various framebuffers; they may contain graphical garbage
memset(myBackBuffer, 0, 160 * TIAConstants::frameBufferHeight);
memset(myFrontBuffer, 0, 160 * TIAConstants::frameBufferHeight);
memset(myFramebuffer, 0, 160 * TIAConstants::frameBufferHeight);
memset(myBackBuffer, 0, H_PIXEL * TIAConstants::frameBufferHeight);
memset(myFrontBuffer, 0, H_PIXEL * TIAConstants::frameBufferHeight);
memset(myFramebuffer, 0, H_PIXEL * TIAConstants::frameBufferHeight);
#ifdef DEBUGGER_SUPPORT
createAccessBase();
@ -797,9 +797,9 @@ bool TIA::saveDisplay(Serializer& out) const
{
try
{
out.putByteArray(myFramebuffer, 160* TIAConstants::frameBufferHeight);
out.putByteArray(myBackBuffer, 160 * TIAConstants::frameBufferHeight);
out.putByteArray(myFrontBuffer, 160 * TIAConstants::frameBufferHeight);
out.putByteArray(myFramebuffer, H_PIXEL * TIAConstants::frameBufferHeight);
out.putByteArray(myBackBuffer, H_PIXEL * TIAConstants::frameBufferHeight);
out.putByteArray(myFrontBuffer, H_PIXEL * TIAConstants::frameBufferHeight);
out.putInt(myFramesSinceLastRender);
}
catch(...)
@ -817,9 +817,9 @@ bool TIA::loadDisplay(Serializer& in)
try
{
// Reset frame buffer pointer and data
in.getByteArray(myFramebuffer, 160 * TIAConstants::frameBufferHeight);
in.getByteArray(myBackBuffer, 160 * TIAConstants::frameBufferHeight);
in.getByteArray(myFrontBuffer, 160 * TIAConstants::frameBufferHeight);
in.getByteArray(myFramebuffer, H_PIXEL * TIAConstants::frameBufferHeight);
in.getByteArray(myBackBuffer, H_PIXEL * TIAConstants::frameBufferHeight);
in.getByteArray(myFrontBuffer, H_PIXEL * TIAConstants::frameBufferHeight);
myFramesSinceLastRender = in.getInt();
}
catch(...)
@ -846,7 +846,7 @@ void TIA::renderToFrameBuffer()
myFramesSinceLastRender = 0;
memcpy(myFramebuffer, myFrontBuffer, 160 * TIAConstants::frameBufferHeight);
memcpy(myFramebuffer, myFrontBuffer, H_PIXEL * TIAConstants::frameBufferHeight);
myFrameBufferScanlines = myFrontBufferScanlines;
}
@ -890,7 +890,7 @@ bool TIA::electronBeamPos(uInt32& x, uInt32& y) const
{
uInt8 clocks = clocksThisLine();
x = (clocks < 68) ? 0 : clocks - 68;
x = (clocks < H_BLANK_CLOCKS) ? 0 : clocks - H_BLANK_CLOCKS;
y = myFrameManager->getY();
return isRendering();
@ -1137,7 +1137,7 @@ TIA& TIA::updateScanlineByStep()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TIA& TIA::updateScanlineByTrace(int target)
{
uInt32 count = 10000; // only try up to 100 steps
uInt32 count = 10000; // only try up to 10000 steps
while (mySystem->m6502().getPC() != target && count-- &&
mySystem->m6502().execute(1));
@ -1155,10 +1155,10 @@ void TIA::updateEmulation()
{
const uInt64 systemCycles = mySystem->cycles();
if (mySubClock > 2)
if (mySubClock > CYCLE_CLOCKS - 1)
throw runtime_error("subclock exceeds range");
const uInt32 cyclesToRun = 3 * uInt32(systemCycles - myLastCycle) + mySubClock;
const uInt32 cyclesToRun = CYCLE_CLOCKS * uInt32(systemCycles - myLastCycle) + mySubClock;
mySubClock = 0;
myLastCycle = systemCycles;
@ -1203,9 +1203,9 @@ void TIA::onFrameComplete()
// Blank out any extra lines not drawn this frame
const Int32 missingScanlines = myFrameManager->missingScanlines();
if (missingScanlines > 0)
memset(myBackBuffer + 160 * myFrameManager->getY(), 0, missingScanlines * 160);
memset(myBackBuffer + H_PIXEL * myFrameManager->getY(), 0, missingScanlines * H_PIXEL);
memcpy(myFrontBuffer, myBackBuffer, 160 * TIAConstants::frameBufferHeight);
memcpy(myFrontBuffer, myBackBuffer, H_PIXEL * TIAConstants::frameBufferHeight);
myFrontBufferScanlines = scanlinesLastFrame();
@ -1215,9 +1215,9 @@ void TIA::onFrameComplete()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TIA::onHalt()
{
mySubClock += (228 - myHctr) % 228;
mySystem->incrementCycles(mySubClock / 3);
mySubClock %= 3;
mySubClock += (H_CLOCKS - myHctr) % H_CLOCKS;
mySystem->incrementCycles(mySubClock / CYCLE_CLOCKS);
mySubClock %= CYCLE_CLOCKS;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1243,7 +1243,7 @@ void TIA::cycle(uInt32 colorClocks)
if (myCollisionUpdateRequired && !myFrameManager->vblank()) updateCollision();
}
if (++myHctr >= 228)
if (++myHctr >= H_CLOCKS)
nextLine();
#ifdef SOUND_SUPPORT
@ -1285,23 +1285,23 @@ void TIA::tickHblank()
myExtendedHblank = false;
break;
case 67:
case H_BLANK_CLOCKS - 1:
if (!myExtendedHblank) myHstate = HState::frame;
break;
case 75:
case H_BLANK_CLOCKS + 7:
if (myExtendedHblank) myHstate = HState::frame;
break;
}
if (myExtendedHblank && myHctr > 67) myPlayfield.tick(myHctr - 68 - myHctrDelta);
if (myExtendedHblank && myHctr > H_BLANK_CLOCKS - 1) myPlayfield.tick(myHctr - H_BLANK_CLOCKS - myHctrDelta);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TIA::tickHframe()
{
const uInt32 y = myFrameManager->getY();
const uInt32 x = myHctr - 68 - myHctrDelta;
const uInt32 x = myHctr - H_BLANK_CLOCKS - myHctrDelta;
myCollisionUpdateRequired = true;
@ -1319,13 +1319,13 @@ void TIA::tickHframe()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TIA::applyRsync()
{
const uInt32 x = myHctr > 68 ? myHctr - 68 : 0;
const uInt32 x = myHctr > H_BLANK_CLOCKS ? myHctr - H_BLANK_CLOCKS : 0;
myHctrDelta = 225 - myHctr;
myHctrDelta = H_CLOCKS - 3 - myHctr;
if (myFrameManager->isRendering())
memset(myBackBuffer + myFrameManager->getY() * 160 + x, 0, 160 - x);
memset(myBackBuffer + myFrameManager->getY() * H_PIXEL + x, 0, H_PIXEL - x);
myHctr = 225;
myHctr = H_CLOCKS - 3;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1364,7 +1364,7 @@ void TIA::cloneLastLine()
uInt8* buffer = myBackBuffer;
memcpy(buffer + y * 160, buffer + (y-1) * 160, 160);
memcpy(buffer + y * H_PIXEL, buffer + (y-1) * H_PIXEL, H_PIXEL);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1389,7 +1389,7 @@ void TIA::updateCollision()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TIA::renderPixel(uInt32 x, uInt32 y)
{
if (x >= 160) return;
if (x >= H_PIXEL) return;
uInt8 color = 0;
@ -1441,7 +1441,7 @@ void TIA::renderPixel(uInt32 x, uInt32 y)
}
}
myBackBuffer[y * 160 + x] = color;
myBackBuffer[y * H_PIXEL + x] = color;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1467,7 +1467,7 @@ void TIA::flushLineCache()
void TIA::clearHmoveComb()
{
if (myFrameManager->isRendering() && myHstate == HState::blank)
memset(myBackBuffer + myFrameManager->getY() * 160, myColorHBlank, 8);
memset(myBackBuffer + myFrameManager->getY() * H_PIXEL, myColorHBlank, 8);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -100,6 +100,11 @@ class TIA : public Device
HBLANK_WHITE = 0x0e
};
static constexpr uInt16
H_PIXEL = 160, H_CYCLES = 76, CYCLE_CLOCKS = 3,
H_CLOCKS = H_CYCLES * CYCLE_CLOCKS, // = 228
H_BLANK_CLOCKS = H_CLOCKS - H_PIXEL; // = 68
public:
friend class TIADebug;
friend class RiotDebug;
@ -242,7 +247,7 @@ class TIA : public Device
/**
Answers dimensional info about the framebuffer.
*/
uInt32 width() const { return 160; }
uInt32 width() const { return H_PIXEL; }
uInt32 height() const { return myFrameManager->height(); }
uInt32 ystart() const { return myFrameManager->ystart(); }
@ -415,7 +420,7 @@ class TIA : public Device
/**
Enables/disables slower playfield values.
@param slow Wether to enable slow playfield delays
@param slow Wether to enable slow playfield delays
*/
void setPFDelay(bool slow);
@ -447,7 +452,7 @@ class TIA : public Device
uInt8 getPosition() const {
uInt8 realHctr = myHctr - myHctrDelta;
return (realHctr < 68) ? 0 : (realHctr - 68);
return (realHctr < H_BLANK_CLOCKS) ? 0 : (realHctr - H_BLANK_CLOCKS);
}
/**
@ -700,12 +705,12 @@ class TIA : public Device
LatchedInput myInput1;
// Pointer to the internal color-index-based frame buffer
uInt8 myFramebuffer[160 * TIAConstants::frameBufferHeight];
uInt8 myFramebuffer[H_PIXEL * TIAConstants::frameBufferHeight];
// The frame is rendered to the backbuffer and only copied to the framebuffer
// upon completion
uInt8 myBackBuffer[160 * TIAConstants::frameBufferHeight];
uInt8 myFrontBuffer[160 * TIAConstants::frameBufferHeight];
uInt8 myBackBuffer[H_PIXEL * TIAConstants::frameBufferHeight];
uInt8 myFrontBuffer[H_PIXEL * TIAConstants::frameBufferHeight];
// We snapshot frame statistics when the back buffer is copied to the front buffer
// and when the front buffer is copied to the frame buffer

View File

@ -1,5 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.168
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Stella", "Stella.vcxproj", "{D7FCEC7F-33E1-49DD-A4B0-D5FC222250AD}"
EndProject
Global
@ -22,4 +24,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {37AF7F05-558D-4805-9259-3F11FD60BB83}
EndGlobalSection
EndGlobal