Skip to content

Commit

Permalink
Statically store the chip8 interface bit colors for external manipula…
Browse files Browse the repository at this point in the history
…tion, and the likes of xochip can instead copy from there for their own use.
  • Loading branch information
coornio committed Sep 20, 2024
1 parent 567b656 commit 7f827b4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 61 deletions.
13 changes: 12 additions & 1 deletion src/Systems/CHIP8/Chip8_CoreInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

fsPath* Chip8_CoreInterface::sPermaRegsPath{};
fsPath* Chip8_CoreInterface::sSavestatePath{};
std::array<u8, 240> Chip8_CoreInterface::sFontsData{ Chip8_CoreInterface::cFontsData };
std::array<u32, 16> Chip8_CoreInterface::sBitColors{ Chip8_CoreInterface::cBitColors };

Chip8_CoreInterface::Chip8_CoreInterface() noexcept
: ASB{ std::make_unique<AudioSpecBlock>(SDL_AUDIO_S8, 1, 48'000) }
Expand Down Expand Up @@ -308,7 +310,16 @@ void Chip8_CoreInterface::copyFontToMemory(
) noexcept {
std::copy_n(
std::execution::unseq,
cFontData.begin(), size, dest + offset
sFontsData.begin(), size, dest + offset
);
}

void Chip8_CoreInterface::copyColorsToCore(
u32* dest, const u32 size
) noexcept {
std::copy_n(
std::execution::unseq,
sBitColors.begin(), size, dest
);
}

Expand Down
27 changes: 6 additions & 21 deletions src/Systems/CHIP8/Chip8_CoreInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class Chip8_CoreInterface : public EmuInterface {

void copyGameToMemory(u8* dest, const u32 offset) noexcept;
void copyFontToMemory(u8* dest, const u32 offset, const u32 size) noexcept;
void copyColorsToCore(u32* dest, const u32 size) noexcept;

virtual void handlePreFrameInterrupt() noexcept;
virtual void handleEndFrameInterrupt() noexcept;
Expand Down Expand Up @@ -201,7 +202,8 @@ class Chip8_CoreInterface : public EmuInterface {
); }

protected:
static constexpr std::array<u8, 240> cFontData{ {
static std::array<u8, 240> sFontsData;
static constexpr std::array<u8, 240> cFontsData{ {
0x60, 0xA0, 0xA0, 0xA0, 0xC0, // 0
0x40, 0xC0, 0x40, 0x40, 0xE0, // 1
0xC0, 0x20, 0x40, 0x80, 0xE0, // 2
Expand Down Expand Up @@ -237,31 +239,14 @@ class Chip8_CoreInterface : public EmuInterface {
0xFE, 0x62, 0x60, 0x64, 0x7C, 0x64, 0x60, 0x62, 0xFE, 0x00, // E
0xFE, 0x66, 0x62, 0x64, 0x7C, 0x64, 0x60, 0x60, 0xF0, 0x00, // F
} };
static constexpr std::array<u8, 160> cFontDataMega{ {
0x3C, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0x3C, // 0
0x18, 0x38, 0x58, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, // 1
0x3E, 0x7F, 0xC3, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xFF, 0xFF, // 2
0x3C, 0x7E, 0xC3, 0x03, 0x0E, 0x0E, 0x03, 0xC3, 0x7E, 0x3C, // 3
0x06, 0x0E, 0x1E, 0x36, 0x66, 0xC6, 0xFF, 0xFF, 0x06, 0x06, // 4
0xFF, 0xFF, 0xC0, 0xC0, 0xFC, 0xFE, 0x03, 0xC3, 0x7E, 0x3C, // 5
0x3E, 0x7C, 0xC0, 0xC0, 0xFC, 0xFE, 0xC3, 0xC3, 0x7E, 0x3C, // 6
0xFF, 0xFF, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x60, 0x60, // 7
0x3C, 0x7E, 0xC3, 0xC3, 0x7E, 0x7E, 0xC3, 0xC3, 0x7E, 0x3C, // 8
0x3C, 0x7E, 0xC3, 0xC3, 0x7F, 0x3F, 0x03, 0x03, 0x3E, 0x7C, // 9
0x3C, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0x3C, // 0
0x3C, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0x3C, // 0
0x3C, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0x3C, // 0
0x3C, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0x3C, // 0
0x3C, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0x3C, // 0
0x3C, 0x7E, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0x7E, 0x3C // 0
} };

static constexpr std::array<u32, 16> cBitsColor{ { // 0-1 monochrome, 0-15 palette color
static std::array<u32, 16> sBitColors;
static constexpr std::array<u32, 16> cBitColors{ { // 0-1 monochrome, 0-15 palette color
0x0C1218, 0xE4DCD4, 0x8C8884, 0x403C38,
0xD82010, 0x40D020, 0x1040D0, 0xE0C818,
0x501010, 0x105010, 0x50B0C0, 0xF08010,
0xE06090, 0xE0F090, 0xB050F0, 0x704020,
} };

static constexpr std::array<u32, 8> cForeColor{ { // CHIP-8X foreground colors
0x000000, 0xEE1111, 0x1111EE, 0xEE11EE,
0x11EE11, 0xEEEE11, 0x11EEEE, 0xEEEEEE,
Expand Down
12 changes: 6 additions & 6 deletions src/Systems/CHIP8/Cores/CHIP8_MODERN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CHIP8_MODERN::CHIP8_MODERN() {

setDisplayResolution(cScreenSizeX, cScreenSizeY);

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

Expand Down Expand Up @@ -192,10 +192,10 @@ void CHIP8_MODERN::renderAudioData() {
sample = static_cast<s8>(wavePhase > 0.5f ? 16 : -16);
wavePhase = std::fmod(wavePhase + mBuzzerTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[1]);
BVS->setFrameColor(sBitColors[0], sBitColors[1]);
} else {
wavePhase = 0.0f;
BVS->setFrameColor(cBitsColor[0], cBitsColor[0]);
BVS->setFrameColor(sBitColors[0], sBitColors[0]);
}

ASB->pushAudioData<s8>(samplesBuffer);
Expand All @@ -205,11 +205,11 @@ void CHIP8_MODERN::renderVideoData() {
BVS->modifyTexture<u8>(mDisplayBuffer, isPixelTrailing()
? [](const u32 pixel) noexcept {
static constexpr u32 layer[4]{ 0xFF, 0xE7, 0x6F, 0x37 };
const auto alpha{ layer[std::countl_zero(pixel) & 0x3] };
return alpha << 24 | cBitsColor[pixel != 0];
const auto opacity{ layer[std::countl_zero(pixel) & 0x3] };
return opacity << 24 | sBitColors[pixel != 0];
}
: [](const u32 pixel) noexcept {
return 0xFF000000 | cBitsColor[pixel >> 3];
return 0xFF000000 | sBitColors[pixel >> 3];
}
);

Expand Down
14 changes: 7 additions & 7 deletions src/Systems/CHIP8/Cores/MEGACHIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void MEGACHIP::renderAudioData() {
}
}
}
BVS->setFrameColor(0xFF202020, 0xFF202020);
BVS->setFrameColor(0x202020, 0x202020);
} else {
static f32 wavePhase{};

Expand All @@ -325,10 +325,10 @@ void MEGACHIP::renderAudioData() {
sample = static_cast<s8>(wavePhase > 0.5f ? 16 : -16);
wavePhase = std::fmod(wavePhase + mBuzzerTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[1]);
BVS->setFrameColor(sBitColors[0], sBitColors[1]);
} else {
wavePhase = 0.0f;
BVS->setFrameColor(cBitsColor[0], cBitsColor[0]);
BVS->setFrameColor(sBitColors[0], sBitColors[0]);
}
}

Expand All @@ -341,11 +341,11 @@ void MEGACHIP::renderVideoData() {
BVS->modifyTexture<u8>(mDisplayBuffer[0].span(), isPixelTrailing()
? [](const u32 pixel) noexcept {
static constexpr u32 layer[4]{ 0xFF, 0xE7, 0x6F, 0x37 };
const auto alpha{ layer[std::countl_zero(pixel) & 0x3] };
return alpha << 24 | cBitsColor[pixel != 0];
const auto opacity{ layer[std::countl_zero(pixel) & 0x3] };
return opacity << 24 | sBitColors[pixel != 0];
}
: [](const u32 pixel) noexcept {
return 0xFF000000 | cBitsColor[pixel >> 3];
return 0xFF000000 | sBitColors[pixel >> 3];
}
);
}
Expand All @@ -368,7 +368,7 @@ void MEGACHIP::prepDisplayArea(const Resolution mode) {
else {
setDisplayResolution(cScreenSizeX, cScreenSizeY);

BVS->setBackColor(cBitsColor[0]);
BVS->setBackColor(sBitColors[0]);
BVS->createTexture(cScreenSizeX, cScreenSizeY);
BVS->setAspectRatio(cScreenSizeX * cResSizeMult, cScreenSizeY * cResSizeMult, +2);
BVS->setTextureAlpha(0xFF);
Expand Down
12 changes: 6 additions & 6 deletions src/Systems/CHIP8/Cores/SCHIP_LEGACY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SCHIP_LEGACY::SCHIP_LEGACY()

setDisplayResolution(cScreenSizeX, cScreenSizeY);

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

Expand Down Expand Up @@ -232,10 +232,10 @@ void SCHIP_LEGACY::renderAudioData() {
sample = static_cast<s8>(wavePhase > 0.5f ? 16 : -16);
wavePhase = std::fmod(wavePhase + mBuzzerTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[1]);
BVS->setFrameColor(sBitColors[0], sBitColors[1]);
} else {
wavePhase = 0.0f;
BVS->setFrameColor(cBitsColor[0], cBitsColor[0]);
BVS->setFrameColor(sBitColors[0], sBitColors[0]);
}

ASB->pushAudioData<s8>(samplesBuffer);
Expand All @@ -245,11 +245,11 @@ void SCHIP_LEGACY::renderVideoData() {
BVS->modifyTexture<u8>(mDisplayBuffer[0].span(), isPixelTrailing()
? [](const u32 pixel) noexcept {
static constexpr u32 layer[4]{ 0xFF, 0xE7, 0x6F, 0x37 };
const auto alpha{ layer[std::countl_zero(pixel) & 0x3] };
return alpha << 24 | cBitsColor[pixel != 0];
const auto opacity{ layer[std::countl_zero(pixel) & 0x3] };
return opacity << 24 | sBitColors[pixel != 0];
}
: [](const u32 pixel) noexcept {
return 0xFF000000 | cBitsColor[pixel >> 3];
return 0xFF000000 | sBitColors[pixel >> 3];
}
);

Expand Down
12 changes: 6 additions & 6 deletions src/Systems/CHIP8/Cores/SCHIP_MODERN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SCHIP_MODERN::SCHIP_MODERN()

setDisplayResolution(cScreenSizeX, cScreenSizeY);

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

Expand Down Expand Up @@ -224,10 +224,10 @@ void SCHIP_MODERN::renderAudioData() {
sample = static_cast<s8>(wavePhase > 0.5f ? 16 : -16);
wavePhase = std::fmod(wavePhase + mBuzzerTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[1]);
BVS->setFrameColor(sBitColors[0], sBitColors[1]);
} else {
wavePhase = 0.0f;
BVS->setFrameColor(cBitsColor[0], cBitsColor[0]);
BVS->setFrameColor(sBitColors[0], sBitColors[0]);
}

ASB->pushAudioData<s8>(samplesBuffer);
Expand All @@ -237,11 +237,11 @@ void SCHIP_MODERN::renderVideoData() {
BVS->modifyTexture<u8>(mDisplayBuffer[0].span(), isPixelTrailing()
? [](const u32 pixel) noexcept {
static constexpr u32 layer[4]{ 0xFF, 0xE7, 0x6F, 0x37 };
const auto alpha{ layer[std::countl_zero(pixel) & 0x3] };
return alpha << 24 | cBitsColor[pixel != 0];
const auto opacity{ layer[std::countl_zero(pixel) & 0x3] };
return opacity << 24 | sBitColors[pixel != 0];
}
: [](const u32 pixel) noexcept {
return 0xFF000000 | cBitsColor[pixel >> 3];
return 0xFF000000 | sBitColors[pixel >> 3];
}
);

Expand Down
21 changes: 8 additions & 13 deletions src/Systems/CHIP8/Cores/XOCHIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ XOCHIP::XOCHIP()

copyGameToMemory(mMemoryBank.data(), cGameLoadPos);
copyFontToMemory(mMemoryBank.data(), 0x0, 0x50);
copyColorsToCore(mBitColors.data(), mBitColors.size());

setDisplayResolution(cScreenSizeX, cScreenSizeY);

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

std::copy_n(
std::execution::unseq,
cBitsColor.data(), 16,
mBitsColor.data()
);

mCurrentPC = cStartOffset;
mFramerate = cRefreshRate;
mActiveCPF = cInstSpeedLo;
Expand Down Expand Up @@ -271,7 +266,7 @@ void XOCHIP::renderAudioData() {
sample = static_cast<s8>(wavePhase > 0.5f ? 16 : -16);
wavePhase = std::fmod(wavePhase + mBuzzerTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[1]);
BVS->setFrameColor(mBitColors[0], mBitColors[1]);
} else {
const auto audioStep{ std::pow(2.0f, (mAudioPitch - 64.0f) / 48.0f) };
const auto audioTone{ 31.25f / ASB->getFrequency() * audioStep };
Expand All @@ -282,12 +277,12 @@ void XOCHIP::renderAudioData() {
sample = mPatternBuf[step >> 3] & mask ? 16 : -16;
wavePhase = std::fmod(wavePhase + audioTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[0]);
BVS->setFrameColor(mBitColors[0], mBitColors[0]);
}
} else {
wavePhase = 0.0f;
isBuzzerEnabled(false);
BVS->setFrameColor(cBitsColor[0], cBitsColor[0]);
BVS->setFrameColor(mBitColors[0], mBitColors[0]);
}

ASB->pushAudioData<s8>(samplesBuffer);
Expand All @@ -311,7 +306,7 @@ void XOCHIP::renderVideoData() {

BVS->modifyTexture<u8>(textureBuffer,
[this](const u32 pixel) noexcept {
return 0xFF000000 | mBitsColor[pixel];
return 0xFF000000 | mBitColors[pixel];
}
);
}
Expand All @@ -335,7 +330,7 @@ void XOCHIP::setColorBit332(const s32 bit, const s32 color) noexcept {
static constexpr u8 map3b[]{ 0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xFF };
static constexpr u8 map2b[]{ 0x00, 0x60, 0xA0, 0xFF };

mBitsColor[bit & 0xF] = map3b[color >> 5 & 0x7] << 16 // red
mBitColors[bit & 0xF] = map3b[color >> 5 & 0x7] << 16 // red
| map3b[color >> 2 & 0x7] << 8 // green
| map2b[color & 0x3]; // blue
}
Expand Down Expand Up @@ -506,7 +501,7 @@ void XOCHIP::scrollDisplayRT() {
setColorBit332(X - Z, readMemoryI(Z));
}
}
BVS->setBackColor(mBitsColor[0]);
BVS->setBackColor(mBitColors[0]);
}

#pragma endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Systems/CHIP8/Cores/XOCHIP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class XOCHIP final : public Chip8_CoreInterface {
static constexpr s32 cInstSpeedLo{ 1000 };

private:
std::array<u32, 16> mBitsColor{};
std::array<u32, 16> mBitColors{};

void setColorBit332(const s32 bit, const s32 color) noexcept;

Expand Down

0 comments on commit 7f827b4

Please sign in to comment.