From b5e4e0071ff06a3a63ec034114edea6a1d0cf2b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3=CF=84=CE=AD=CF=86=CE=B1=CE=BD=CE=BF=CF=82=20=22Coor?= =?UTF-8?q?nio/8924th=22=20=CE=92=CE=BB=CE=B1=CF=83=CF=84=CF=8C=CF=82?= <8924th@gmail.com> Date: Fri, 20 Sep 2024 16:21:58 +0300 Subject: [PATCH] Fix font color in Megachip, and a few other minor edits. --- src/Assistants/BasicVideoSpec.cpp | 2 +- src/Assistants/BasicVideoSpec.hpp | 2 +- src/Systems/CHIP8/Cores/CHIP8_MODERN.cpp | 2 +- src/Systems/CHIP8/Cores/CHIP8_MODERN.hpp | 2 + src/Systems/CHIP8/Cores/MEGACHIP.cpp | 95 +++++++++++++----------- src/Systems/CHIP8/Cores/MEGACHIP.hpp | 15 +++- src/Systems/CHIP8/Cores/SCHIP_LEGACY.cpp | 19 +++-- src/Systems/CHIP8/Cores/SCHIP_LEGACY.hpp | 2 + src/Systems/CHIP8/Cores/SCHIP_MODERN.cpp | 2 +- src/Systems/CHIP8/Cores/SCHIP_MODERN.hpp | 2 + src/Systems/CHIP8/Cores/XOCHIP.cpp | 2 +- src/Systems/CHIP8/Cores/XOCHIP.hpp | 2 + 12 files changed, 86 insertions(+), 61 deletions(-) diff --git a/src/Assistants/BasicVideoSpec.cpp b/src/Assistants/BasicVideoSpec.cpp index c4ed2c7..75d52e1 100644 --- a/src/Assistants/BasicVideoSpec.cpp +++ b/src/Assistants/BasicVideoSpec.cpp @@ -146,7 +146,7 @@ void BasicVideoSpec::modifyTexture(const std::span colorData) { unlockTexture(); } -void BasicVideoSpec::setTextureAlpha(const usz alpha) { +void BasicVideoSpec::setTextureAlpha(const u32 alpha) { SDL_SetTextureAlphaMod(texture, static_cast(alpha)); } diff --git a/src/Assistants/BasicVideoSpec.hpp b/src/Assistants/BasicVideoSpec.hpp index a608014..6e1c2d8 100644 --- a/src/Assistants/BasicVideoSpec.hpp +++ b/src/Assistants/BasicVideoSpec.hpp @@ -116,7 +116,7 @@ class BasicVideoSpec final { unlockTexture(); } - void setTextureAlpha(usz); + void setTextureAlpha(u32); void setAspectRatio(s32, s32, s32); private: diff --git a/src/Systems/CHIP8/Cores/CHIP8_MODERN.cpp b/src/Systems/CHIP8/Cores/CHIP8_MODERN.cpp index ac5791c..6fcffbf 100644 --- a/src/Systems/CHIP8/Cores/CHIP8_MODERN.cpp +++ b/src/Systems/CHIP8/Cores/CHIP8_MODERN.cpp @@ -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; diff --git a/src/Systems/CHIP8/Cores/CHIP8_MODERN.hpp b/src/Systems/CHIP8/Cores/CHIP8_MODERN.hpp index 8272f6d..015386f 100644 --- a/src/Systems/CHIP8/Cores/CHIP8_MODERN.hpp +++ b/src/Systems/CHIP8/Cores/CHIP8_MODERN.hpp @@ -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 }; diff --git a/src/Systems/CHIP8/Cores/MEGACHIP.cpp b/src/Systems/CHIP8/Cores/MEGACHIP.cpp index f1996dd..1fafac7 100644 --- a/src/Systems/CHIP8/Cores/MEGACHIP.cpp +++ b/src/Systems/CHIP8/Cores/MEGACHIP.cpp @@ -17,13 +17,6 @@ MEGACHIP::MEGACHIP() { if (getCoreState() != EmuState::FAILED) { - std::generate( - std::execution::unseq, - mMemoryBank.begin(), - mMemoryBank.end(), - []() { return Wrand->get(); } - ); - copyGameToMemory(mMemoryBank.data(), cGameLoadPos); copyFontToMemory(mMemoryBank.data(), 0x0, 0xB4); @@ -37,6 +30,7 @@ MEGACHIP::MEGACHIP() prepDisplayArea(Resolution::LO); setNewBlendAlgorithm(BlendMode::NORMAL); + initializeFontColors(); } } @@ -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(); @@ -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(std::min(std::round(R), 255.0f)) << 16 + | static_cast(std::min(std::round(G), 255.0f)) << 8 + | static_cast(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; @@ -848,25 +856,25 @@ 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; @@ -874,27 +882,29 @@ void MEGACHIP::scrollBuffersRT() { 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; } @@ -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; @@ -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; } } } diff --git a/src/Systems/CHIP8/Cores/MEGACHIP.hpp b/src/Systems/CHIP8/Cores/MEGACHIP.hpp index 3e061db..d489241 100644 --- a/src/Systems/CHIP8/Cores/MEGACHIP.hpp +++ b/src/Systems/CHIP8/Cores/MEGACHIP.hpp @@ -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 mDisplayBuffer[1]; + Map2D mDisplayBuffer[1]; Map2D mForegroundBuffer; Map2D mBackgroundBuffer; @@ -36,6 +41,8 @@ class MEGACHIP final : public Chip8_CoreInterface { std::array mFontColor{}; + void initializeFontColors() noexcept; + struct Texture { s32 W{}, H{}; s32 collide{ 0xFF }; diff --git a/src/Systems/CHIP8/Cores/SCHIP_LEGACY.cpp b/src/Systems/CHIP8/Cores/SCHIP_LEGACY.cpp index c4eb4ea..a33241b 100644 --- a/src/Systems/CHIP8/Cores/SCHIP_LEGACY.cpp +++ b/src/Systems/CHIP8/Cores/SCHIP_LEGACY.cpp @@ -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; @@ -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; @@ -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; } } diff --git a/src/Systems/CHIP8/Cores/SCHIP_LEGACY.hpp b/src/Systems/CHIP8/Cores/SCHIP_LEGACY.hpp index be2bb58..c60f118 100644 --- a/src/Systems/CHIP8/Cores/SCHIP_LEGACY.hpp +++ b/src/Systems/CHIP8/Cores/SCHIP_LEGACY.hpp @@ -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 }; diff --git a/src/Systems/CHIP8/Cores/SCHIP_MODERN.cpp b/src/Systems/CHIP8/Cores/SCHIP_MODERN.cpp index 9a24587..13da33c 100644 --- a/src/Systems/CHIP8/Cores/SCHIP_MODERN.cpp +++ b/src/Systems/CHIP8/Cores/SCHIP_MODERN.cpp @@ -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; diff --git a/src/Systems/CHIP8/Cores/SCHIP_MODERN.hpp b/src/Systems/CHIP8/Cores/SCHIP_MODERN.hpp index 225b0c3..ac7456a 100644 --- a/src/Systems/CHIP8/Cores/SCHIP_MODERN.hpp +++ b/src/Systems/CHIP8/Cores/SCHIP_MODERN.hpp @@ -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 }; diff --git a/src/Systems/CHIP8/Cores/XOCHIP.cpp b/src/Systems/CHIP8/Cores/XOCHIP.cpp index ec644c1..400f2bd 100644 --- a/src/Systems/CHIP8/Cores/XOCHIP.cpp +++ b/src/Systems/CHIP8/Cores/XOCHIP.cpp @@ -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, diff --git a/src/Systems/CHIP8/Cores/XOCHIP.hpp b/src/Systems/CHIP8/Cores/XOCHIP.hpp index 7b019fe..fb518f1 100644 --- a/src/Systems/CHIP8/Cores/XOCHIP.hpp +++ b/src/Systems/CHIP8/Cores/XOCHIP.hpp @@ -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 };