Skip to content

Commit

Permalink
Fix font color in Megachip, and a few other minor edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
coornio committed Sep 20, 2024
1 parent 414f1ab commit b5e4e00
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/Assistants/BasicVideoSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void BasicVideoSpec::modifyTexture(const std::span<u32> colorData) {
unlockTexture();
}

void BasicVideoSpec::setTextureAlpha(const usz alpha) {
void BasicVideoSpec::setTextureAlpha(const u32 alpha) {
SDL_SetTextureAlphaMod(texture, static_cast<u8>(alpha));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Assistants/BasicVideoSpec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class BasicVideoSpec final {
unlockTexture();
}

void setTextureAlpha(usz);
void setTextureAlpha(u32);
void setAspectRatio(s32, s32, s32);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/Systems/CHIP8/Cores/CHIP8_MODERN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CHIP8_MODERN::CHIP8_MODERN() {

BVS->setBackColor(cBitsColor[0]);
BVS->createTexture(cScreenSizeX, cScreenSizeY);
BVS->setAspectRatio(cScreenSizeX * 8, cScreenSizeY * 8, +2);
BVS->setAspectRatio(cScreenSizeX * cResSizeMult, cScreenSizeY * cResSizeMult, +2);

mCurrentPC = cStartOffset;
mFramerate = cRefreshRate;
Expand Down
2 changes: 2 additions & 0 deletions src/Systems/CHIP8/Cores/CHIP8_MODERN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CHIP8_MODERN final : public Chip8_CoreInterface {
static constexpr u32 cGameLoadPos{ 512 };
static constexpr u32 cStartOffset{ 512 };
static constexpr f32 cRefreshRate{ 60.0f };

static constexpr s32 cResSizeMult{ 8 };
static constexpr s32 cScreenSizeX{ 64 };
static constexpr s32 cScreenSizeY{ 32 };
static constexpr s32 cInstSpeedHi{ 30 };
Expand Down
95 changes: 53 additions & 42 deletions src/Systems/CHIP8/Cores/MEGACHIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ MEGACHIP::MEGACHIP()
{
if (getCoreState() != EmuState::FAILED) {

std::generate(
std::execution::unseq,
mMemoryBank.begin(),
mMemoryBank.end(),
[]() { return Wrand->get<u8>(); }
);

copyGameToMemory(mMemoryBank.data(), cGameLoadPos);
copyFontToMemory(mMemoryBank.data(), 0x0, 0xB4);

Expand All @@ -37,6 +30,7 @@ MEGACHIP::MEGACHIP()

prepDisplayArea(Resolution::LO);
setNewBlendAlgorithm(BlendMode::NORMAL);
initializeFontColors();
}
}

Expand Down Expand Up @@ -365,18 +359,18 @@ void MEGACHIP::prepDisplayArea(const Resolution mode) {

BVS->setBackColor(mColorPalette.at_raw(0));
BVS->createTexture(cScreenMegaX, cScreenMegaY);
BVS->setAspectRatio(cScreenMegaX * 2, cScreenMegaY * 2, -2);
BVS->setAspectRatio(cScreenMegaX * cResMegaMult, cScreenMegaY * cResMegaMult, -2);
BVS->setTextureAlpha(0xFF);

Quirk.waitVblank = false;
mActiveCPF = 3000;
mActiveCPF = cInstSpeedMC;
}
else {
setDisplayResolution(cScreenSizeX, cScreenSizeY);

BVS->setBackColor(cBitsColor[0]);
BVS->createTexture(cScreenSizeX, cScreenSizeY);
BVS->setAspectRatio(cScreenSizeX * 4, cScreenSizeY * 4, +2);
BVS->setAspectRatio(cScreenSizeX * cResSizeMult, cScreenSizeY * cResSizeMult, +2);
BVS->setTextureAlpha(0xFF);

Quirk.waitVblank = !isDisplayLarger();
Expand Down Expand Up @@ -405,7 +399,21 @@ void MEGACHIP::scrollDisplayRT() {

/*==================================================================*/

u32 MEGACHIP::blendPixel(const u32 srcPixel, const u32 dstPixel) const noexcept {
void MEGACHIP::initializeFontColors() noexcept {
for (auto i{ 0 }; i < 10; ++i) {
const auto mult{ 1.0f - 0.045f * i };
const auto R{ 0xFF * mult * 1.03f };
const auto G{ 0xFF * mult * 1.14f };
const auto B{ 0xFF * mult * 1.21f };

mFontColor[i] = 0xFF000000
| static_cast<u32>(std::min(std::round(R), 255.0f)) << 16
| static_cast<u32>(std::min(std::round(G), 255.0f)) << 8
| static_cast<u32>(std::min(std::round(B), 255.0f));
}
}

u32 MEGACHIP::blendPixel(const u32 srcPixel, const u32 dstPixel) const noexcept {
static constexpr f32 minF{ 1.0f / 255.0f };
struct ColorF { f32 A, R, G, B; };
ColorF src, dst;
Expand Down Expand Up @@ -848,53 +856,55 @@ void MEGACHIP::scrollBuffersRT() {
{ triggerInterrupt(Interrupt::FRAME); }

if (isManualRefresh()) {
s32 VX{ mRegisterV[X] };
s32 VY{ mRegisterV[Y] };
const auto originX{ mRegisterV[X] + 0 };
const auto originY{ mRegisterV[Y] + 0 };

mRegisterV[0xF] = 0;

if (!Quirk.wrapSprite && VY >= mDisplayH) { return; }
if (!Quirk.wrapSprite && originY >= mDisplayH) { return; }
if (mRegisterI >= 0xF0) [[likely]] { goto paintTexture; }

for (auto H{ 0 }, _Y{ VY }; H < N; ++H, ++_Y &= mDisplayWb)
for (auto rowN{ 0 }, offsetY{ originY }; rowN < N; ++rowN)
{
if (Quirk.wrapSprite && _Y >= mDisplayH) { continue; }
const auto bytePixel{ readMemoryI(H) };
if (Quirk.wrapSprite && offsetY >= mDisplayH) { continue; }
const auto octoPixelBatch{ readMemoryI(rowN) };

for (auto W{ 7 }, _X{ VX }; W >= 0; --W, ++_X &= mDisplayWb)
for (auto colN{ 7 }, offsetX{ originX }; colN >= 0; --colN)
{
if (bytePixel >> W & 0x1)
if (octoPixelBatch >> colN & 0x1)
{
auto& collideCoord{ mCollisionMap.at_raw(_Y, _X) };
auto& backbufCoord{ mBackgroundBuffer.at_raw(_Y, _X) };
auto& collideCoord{ mCollisionMap.at_raw(offsetY, offsetX) };
auto& backbufCoord{ mBackgroundBuffer.at_raw(offsetY, offsetX) };

if (collideCoord) [[unlikely]] {
collideCoord = 0;
backbufCoord = 0;
mRegisterV[0xF] = 1;
} else {
collideCoord = 254;
backbufCoord = mFontColor[H];
backbufCoord = mFontColor[rowN];
}
}
if (!Quirk.wrapSprite && _X == mDisplayWb) { break; }
if (!Quirk.wrapSprite && offsetX == mDisplayWb) { break; }
else { ++offsetX &= mDisplayWb; }
}
if (!Quirk.wrapSprite && _Y == mDisplayWb) { break; }
if (!Quirk.wrapSprite && offsetY == mDisplayWb) { break; }
else { ++offsetY &= mDisplayWb; }
}
return;

paintTexture:
for (auto H{ 0 }, _Y{ VY }; H < mTexture.H; ++H, ++_Y &= mDisplayWb)
for (auto rowN{ 0 }, offsetY{ originY }; rowN < mTexture.H; ++rowN)
{
if (Quirk.wrapSprite && _Y >= mDisplayH) { continue; }
auto I = H * mTexture.W;
if (Quirk.wrapSprite && offsetY >= mDisplayH) { continue; }
auto I = rowN * mTexture.W;

for (auto W{ 0 }, _X{ VX }; W < mTexture.W; ++W, ++_X &= mDisplayWb)
for (auto colN{ 0 }, offsetX{ originX }; colN < mTexture.W; ++colN, ++I)
{
if (const auto sourceColorIdx{ readMemoryI(I++) }; sourceColorIdx)
if (const auto sourceColorIdx{ readMemoryI(I) }; sourceColorIdx)
{
auto& collideCoord{ mCollisionMap.at_raw(_Y, _X) };
auto& backbufCoord{ mBackgroundBuffer.at_raw(_Y, _X) };
auto& collideCoord{ mCollisionMap.at_raw(offsetY, offsetX) };
auto& backbufCoord{ mBackgroundBuffer.at_raw(offsetY, offsetX) };

if (collideCoord == mTexture.collide)
[[unlikely]] { mRegisterV[0xF] = 1; }
Expand All @@ -904,15 +914,17 @@ void MEGACHIP::scrollBuffersRT() {
mColorPalette.at_raw(sourceColorIdx),
backbufCoord);
}
if (!Quirk.wrapSprite && _X == mDisplayWb) { break; }
if (!Quirk.wrapSprite && offsetX == mDisplayWb) { break; }
else { ++offsetX &= mDisplayWb; }
}
if (!Quirk.wrapSprite && _Y == mDisplayWb) { break; }
if (!Quirk.wrapSprite && offsetY == mDisplayWb) { break; }
else { ++offsetY &= mDisplayWb; }
}
} else {
if (isDisplayLarger()) {
const s32 offsetX{ 8 - (mRegisterV[X] & 7) };
const s32 originX{ mRegisterV[X] & 0x78 };
const s32 originY{ mRegisterV[Y] & 0x3F };
const auto offsetX{ 8 - (mRegisterV[X] & 7) };
const auto originX{ mRegisterV[X] & 0x78 };
const auto originY{ mRegisterV[Y] & 0x3F };

mRegisterV[0xF] = 0;

Expand Down Expand Up @@ -940,23 +952,22 @@ void MEGACHIP::scrollBuffersRT() {
}
}
else {
const s32 offsetX{ 8 - (mRegisterV[X] * 2 & 7) };
const s32 originX{ mRegisterV[X] * 2 & 0x78 };
const s32 originY{ mRegisterV[Y] * 2 & 0x3F };
const s32 lengthN{ N == 0 ? 16 : N };
const auto offsetX{ 8 - (mRegisterV[X] * 2 & 7) };
const auto originX{ mRegisterV[X] * 2 & 0x78 };
const auto originY{ mRegisterV[Y] * 2 & 0x3F };
const auto lengthN{ N == 0 ? 16 : N };

mRegisterV[0xF] = 0;

for (auto rowN{ 0 }; rowN < lengthN; ++rowN) {
const auto offsetY{ originY + rowN * 2 };

mRegisterV[0xF] += drawDoubleBytes(
mRegisterV[0xF] |= drawDoubleBytes(
originX, offsetY, offsetX ? 24 : 16,
bitBloat(readMemoryI(rowN)) << offsetX
);
if (offsetY == 0x3E) { break; }
}
mRegisterV[0xF] = mRegisterV[0xF] != 0;
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/Systems/CHIP8/Cores/MEGACHIP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ class MEGACHIP final : public Chip8_CoreInterface {
static constexpr u32 cSafezoneOOB{ 32 };
static constexpr u32 cGameLoadPos{ 512 };
static constexpr u32 cStartOffset{ 512 };
static constexpr f32 cRefreshRate{ 64.0f };
static constexpr f32 cRefreshRate{ 50.0f };

static constexpr s32 cResSizeMult{ 4 };
static constexpr s32 cScreenSizeX{ 128 };
static constexpr s32 cScreenSizeY{ 64 };
static constexpr s32 cScreenMegaX{ 256 };
static constexpr s32 cScreenMegaY{ 192 };
static constexpr s32 cInstSpeedHi{ 45 };
static constexpr s32 cInstSpeedLo{ 30 };

static constexpr s32 cResMegaMult{ 2 };
static constexpr s32 cScreenMegaX{ 256 };
static constexpr s32 cScreenMegaY{ 192 };
static constexpr s32 cInstSpeedMC{ 4000 };

/*==================================================================*/

Map2D<u8> mDisplayBuffer[1];
Map2D<u8> mDisplayBuffer[1];

Map2D<u32> mForegroundBuffer;
Map2D<u32> mBackgroundBuffer;
Expand All @@ -36,6 +41,8 @@ class MEGACHIP final : public Chip8_CoreInterface {

std::array<u32, 10> mFontColor{};

void initializeFontColors() noexcept;

struct Texture {
s32 W{}, H{};
s32 collide{ 0xFF };
Expand Down
19 changes: 9 additions & 10 deletions src/Systems/CHIP8/Cores/SCHIP_LEGACY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SCHIP_LEGACY::SCHIP_LEGACY()

BVS->setBackColor(cBitsColor[0]);
BVS->createTexture(cScreenSizeX, cScreenSizeY);
BVS->setAspectRatio(cScreenSizeX * 4, cScreenSizeY * 4, +2);
BVS->setAspectRatio(cScreenSizeX * cResSizeMult, cScreenSizeY * cResSizeMult, +2);

mCurrentPC = cStartOffset;
mFramerate = cRefreshRate;
Expand Down Expand Up @@ -533,9 +533,9 @@ void SCHIP_LEGACY::scrollDisplayRT() {
{ triggerInterrupt(Interrupt::FRAME); }

if (isDisplayLarger()) {
const s32 offsetX{ 8 - (mRegisterV[X] & 7) };
const s32 originX{ mRegisterV[X] & 0x78 };
const s32 originY{ mRegisterV[Y] & 0x3F };
const auto offsetX{ 8 - (mRegisterV[X] & 7) };
const auto originX{ mRegisterV[X] & 0x78 };
const auto originY{ mRegisterV[Y] & 0x3F };

mRegisterV[0xF] = 0;

Expand Down Expand Up @@ -563,23 +563,22 @@ void SCHIP_LEGACY::scrollDisplayRT() {
}
}
else {
const s32 offsetX{ 8 - (mRegisterV[X] * 2 & 7) };
const s32 originX{ mRegisterV[X] * 2 & 0x78 };
const s32 originY{ mRegisterV[Y] * 2 & 0x3F };
const s32 lengthN{ N == 0 ? 16 : N };
const auto offsetX{ 8 - (mRegisterV[X] * 2 & 7) };
const auto originX{ mRegisterV[X] * 2 & 0x78 };
const auto originY{ mRegisterV[Y] * 2 & 0x3F };
const auto lengthN{ N == 0 ? 16 : N };

mRegisterV[0xF] = 0;

for (auto rowN{ 0 }; rowN < lengthN; ++rowN) {
const auto offsetY{ originY + rowN * 2 };

mRegisterV[0xF] += drawDoubleBytes(
mRegisterV[0xF] |= drawDoubleBytes(
originX, offsetY, offsetX ? 24 : 16,
bitBloat(readMemoryI(rowN)) << offsetX
);
if (offsetY == 0x3E) { break; }
}
mRegisterV[0xF] = mRegisterV[0xF] != 0;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Systems/CHIP8/Cores/SCHIP_LEGACY.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SCHIP_LEGACY final : public Chip8_CoreInterface {
static constexpr u32 cGameLoadPos{ 512 };
static constexpr u32 cStartOffset{ 512 };
static constexpr f32 cRefreshRate{ 64.0f };

static constexpr s32 cResSizeMult{ 4 };
static constexpr s32 cScreenSizeX{ 128 };
static constexpr s32 cScreenSizeY{ 64 };
static constexpr s32 cInstSpeedHi{ 45 };
Expand Down
2 changes: 1 addition & 1 deletion src/Systems/CHIP8/Cores/SCHIP_MODERN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SCHIP_MODERN::SCHIP_MODERN()

BVS->setBackColor(cBitsColor[0]);
BVS->createTexture(cScreenSizeX, cScreenSizeY);
BVS->setAspectRatio(cScreenSizeX * 8, cScreenSizeY * 8, +2);
BVS->setAspectRatio(cScreenSizeX * cResSizeMult, cScreenSizeY * cResSizeMult, +2);

mCurrentPC = cStartOffset;
mFramerate = cRefreshRate;
Expand Down
2 changes: 2 additions & 0 deletions src/Systems/CHIP8/Cores/SCHIP_MODERN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SCHIP_MODERN final : public Chip8_CoreInterface {
static constexpr u32 cGameLoadPos{ 512 };
static constexpr u32 cStartOffset{ 512 };
static constexpr f32 cRefreshRate{ 60.0f };

static constexpr s32 cResSizeMult{ 8 };
static constexpr s32 cScreenSizeX{ 64 };
static constexpr s32 cScreenSizeY{ 32 };
static constexpr s32 cInstSpeedHi{ 45 };
Expand Down
2 changes: 1 addition & 1 deletion src/Systems/CHIP8/Cores/XOCHIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ XOCHIP::XOCHIP()

BVS->setBackColor(cBitsColor[0]);
BVS->createTexture(cScreenSizeX, cScreenSizeY);
BVS->setAspectRatio(cScreenSizeX * 8, cScreenSizeY * 8, +2);
BVS->setAspectRatio(cScreenSizeX * cResSizeMult, cScreenSizeY * cResSizeMult, +2);

std::copy_n(
std::execution::unseq,
Expand Down
2 changes: 2 additions & 0 deletions src/Systems/CHIP8/Cores/XOCHIP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class XOCHIP final : public Chip8_CoreInterface {
static constexpr u32 cGameLoadPos{ 512 };
static constexpr u32 cStartOffset{ 512 };
static constexpr f32 cRefreshRate{ 60.0f };

static constexpr s32 cResSizeMult{ 8 };
static constexpr s32 cScreenSizeX{ 64 };
static constexpr s32 cScreenSizeY{ 32 };
static constexpr s32 cInstSpeedHi{ 50000 };
Expand Down

0 comments on commit b5e4e00

Please sign in to comment.