Skip to content

Commit

Permalink
Do not calculate audio specifics in the hot loop but only in the func…
Browse files Browse the repository at this point in the history
…tion that generates samples.
  • Loading branch information
coornio committed Sep 13, 2024
1 parent c9f1cad commit eb2b5d0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Systems/CHIP8/Chip8_CoreInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void Chip8_CoreInterface::copyFontToMemory(
) noexcept {
std::copy_n(
std::execution::unseq,
cFontData, size, dest + offset
cFontData.begin(), size, dest + offset
);
}

Expand Down
20 changes: 10 additions & 10 deletions src/Systems/CHIP8/Chip8_CoreInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Chip8_CoreInterface : public EmuInterface {
); }

protected:
static constexpr u8 cFontData[]{
static constexpr std::array<u8, 240> cFontData{ {
0x60, 0xA0, 0xA0, 0xA0, 0xC0, // 0
0x40, 0xC0, 0x40, 0x40, 0xE0, // 1
0xC0, 0x20, 0x40, 0x80, 0xE0, // 2
Expand Down Expand Up @@ -233,8 +233,8 @@ class Chip8_CoreInterface : public EmuInterface {
0xF8, 0x6C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x6C, 0xF8, 0x00, // D
0xFE, 0x62, 0x60, 0x64, 0x7C, 0x64, 0x60, 0x62, 0xFE, 0x00, // E
0xFE, 0x66, 0x62, 0x64, 0x7C, 0x64, 0x60, 0x60, 0xF0, 0x00, // F
};
static constexpr u8 cFontDataMega[]{
} };
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
Expand All @@ -251,19 +251,19 @@ class Chip8_CoreInterface : public EmuInterface {
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 u32 cBitsColor[]{ // 0-1 monochrome, 0-15 palette color
static constexpr std::array<u32, 16> cBitsColor{ { // 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 u32 cForeColor[]{ // CHIP-8X foreground colors
} };
static constexpr std::array<u32, 8> cForeColor{ { // CHIP-8X foreground colors
0x000000, 0xEE1111, 0x1111EE, 0xEE11EE,
0x11EE11, 0xEEEE11, 0x11EEEE, 0xEEEEEE,
};
static constexpr u32 cBackColor[]{ // CHIP-8X background colors
} };
static constexpr std::array<u32, 4> cBackColor{ { // CHIP-8X background colors
0x111133, 0x111111, 0x113311, 0x331111,
};
} };
};
22 changes: 10 additions & 12 deletions src/Systems/CHIP8/Cores/XOCHIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
/*==================================================================*/

XOCHIP::XOCHIP()

: mAudioStep{ 4'000.0f / 128.0f / ASB->getFrequency() }
, mAudioTone{ mAudioStep }
, mDisplayBuffer{
: mDisplayBuffer{
{cScreenSizeY, cScreenSizeX},
{cScreenSizeY, cScreenSizeX},
{cScreenSizeY, cScreenSizeX},
Expand All @@ -37,7 +34,7 @@ XOCHIP::XOCHIP()

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

Expand Down Expand Up @@ -273,20 +270,22 @@ void XOCHIP::renderAudioData() {

static f32 wavePhase{};


if (mSoundTimer) {
if (isBuzzerEnabled()) {
for (auto& sample : samplesBuffer) {
sample = static_cast<s8>(wavePhase > 0.5f ? 16 : -16);
sample = static_cast<s8>(wavePhase > 0.5f ? 16 : -16);
wavePhase = std::fmod(wavePhase + mBuzzerTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[1]);
} else {
const auto audioStep{ std::pow(2.0f, (mAudioPitch - 64.0f) / 48.0f) };
const auto audioTone{ 31.25f / ASB->getFrequency() * audioStep };

for (auto& sample : samplesBuffer) {
const auto step{ static_cast<s32>(std::clamp(wavePhase * 128.0f, 0.0f, 127.0f)) };
const auto mask{ 1 << (7 ^ step & 7) };
sample = mSamplePattern[step >> 3] & mask ? 16 : -16;
wavePhase = std::fmod(wavePhase + mAudioTone, 1.0f);
sample = mPatternBuf[step >> 3] & mask ? 16 : -16;
wavePhase = std::fmod(wavePhase + audioTone, 1.0f);
}
BVS->setFrameColor(cBitsColor[0], cBitsColor[0]);
}
Expand Down Expand Up @@ -770,7 +769,7 @@ void XOCHIP::scrollDisplayRT(const s32) {
// F002 - load 16-byte audio pattern from RAM at I
void XOCHIP::instruction_F002() noexcept {
for (auto idx{ 0 }; idx < 16; ++idx) {
mSamplePattern[idx] = readMemoryI(idx);
mPatternBuf[idx] = readMemoryI(idx);
}
isBuzzerEnabled(false);
}
Expand Down Expand Up @@ -816,8 +815,7 @@ void XOCHIP::scrollDisplayRT(const s32) {
}
// FX3A - set sound pitch = VX
void XOCHIP::instruction_Fx3A(const s32 X) noexcept {
const auto stepValue{ (mRegisterV[X] - 64.0f) / 48.0f };
mAudioTone = mAudioStep * std::pow(2.0f, stepValue);
mAudioPitch = mRegisterV[X];
isBuzzerEnabled(false);
}
// FN55 - store V0..VX to RAM at I..I+N
Expand Down
6 changes: 2 additions & 4 deletions src/Systems/CHIP8/Cores/XOCHIP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ class XOCHIP final : public Chip8_CoreInterface {

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

std::array<u8, 16> mSamplePattern{};

f32 mAudioStep{};
f32 mAudioTone{};
std::array<u8, 16> mPatternBuf{};
s32 mAudioPitch{ 64 };

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

Expand Down

0 comments on commit eb2b5d0

Please sign in to comment.