diff --git a/src/audio.cpp b/src/audio.cpp index c497095..ac79828 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -55,11 +55,11 @@ void APU::test() printf("APU test\n"); } -// Connecting CallBack function -void APU::setSignalCallback() +// initialize the writehandler of memorymap +void APU::initializeWriteHandler() { if (mMap) - mMap->connectObserver([this](Word address, Byte value) { this->onWrite(address, value); }); + mMap->setAduioWriteHandler([this](Word address) { this->onWrite(address); }); } void APU::writeByte(Word address, Byte value) @@ -202,7 +202,7 @@ void APU::stepAPU(int cycles) Word address[] = { 0xFF19, 0xFF1E, 0xFF23, 0xFF26 }; for (auto addr : address) { - writeUpdate(addr, 0xFF); + writeUpdate(addr, AUDIO_WRITE); } printf("FramerSequencer ends\n"); } @@ -224,21 +224,24 @@ void APU::clearRegisters() // Could be done by simply writing 0s but for checking's sake done as such for (int address = 0xFF10; address <= 0xFF3F; address++) { - writeUpdate(address, 0xFF); + writeUpdate(address, AUDIO_WRITE); } } -// Write on Memory Write -void APU::onWrite(Word address, Byte value) +// Updates APU registers on write in MemoryMap +void APU::onWrite(Word address) { - writeUpdate(address, value, true); - writeUpdate(0xFF26, 0xFF); + // address where write has occurred + writeUpdate(address, AUDIO_MEMORY_WRITE); + // Update the audio channel controller register + writeUpdate(AUDIO_CHANNEL_CONTROL, AUDIO_WRITE); } // Write Update -void APU::writeUpdate(Word address, Byte value, bool MemWrite) +void APU::writeUpdate(Word address, audioWriteFlag flag) { - if (MemWrite) + Byte value = 0xFF; + if (flag) { value = mMap->readMemory(address); writeByte(address, value); diff --git a/src/audio.h b/src/audio.h index 3e0b551..8d58240 100644 --- a/src/audio.h +++ b/src/audio.h @@ -4,6 +4,8 @@ #include #include // SDL Audio +#define AUDIO_CHANNEL_CONTROL 0xFF26 + enum Channel { CH1 = 0, @@ -12,6 +14,14 @@ enum Channel CH4 = 3 }; +// For checking whether Write is due to memory or APU itself +enum audioWriteFlag +{ + AUDIO_WRITE = 0, + AUDIO_MEMORY_WRITE = 1 + +}; + class PulseChannel { private: @@ -173,9 +183,9 @@ class APU void clearRegisters(); void setMemoryMap(MemoryMap* map) { mMap = map; } // Writes back on Memory Write - void onWrite(Word address, Byte value); + void onWrite(Word address); // Write update - void writeUpdate(Word address, Byte value, bool WriteMem = false); - - void setSignalCallback(); + void writeUpdate(Word address, audioWriteFlag flag); + // initializes the audioWriteHandler of MemoryMap + void initializeWriteHandler(); }; \ No newline at end of file diff --git a/src/gameBoy.cpp b/src/gameBoy.cpp index af3bebd..3ff18da 100644 --- a/src/gameBoy.cpp +++ b/src/gameBoy.cpp @@ -30,7 +30,8 @@ GBE::GBE() // Unify the APU and MemoryMap gbe_audio->setMemoryMap(gbe_mMap); - gbe_audio->setSignalCallback(); + // initialize the write handler + gbe_audio->initializeWriteHandler(); // Open the Boot ROM if ((bootROM = fopen("../src/dmg_boot.gb", "rb")) == NULL) @@ -41,7 +42,7 @@ GBE::GBE() // printf("game rom file not opened"); // Open the Game ROM - if ((gameROM = fopen("../tests/dmg_sound/rom_singles/02-len ctr.gb", "rb")) == NULL) + if ((gameROM = fopen("../tests/dmg_sound/rom_singles/03-trigger.gb", "rb")) == NULL) printf("game rom file not opened\n"); // Set the Boot ROM diff --git a/src/mmap.cpp b/src/mmap.cpp index 4e3a4d4..59f2676 100644 --- a/src/mmap.cpp +++ b/src/mmap.cpp @@ -105,7 +105,7 @@ MemoryMap::MemoryMap() bootRomFile = nullptr; romFile = nullptr; - globalFunction = nullptr; + audioWriteHandler = nullptr; mbcMode = 0x0; } @@ -194,9 +194,10 @@ bool MemoryMap::writeMemory(Word address, Byte value) else { ioPorts[address - 0xFF00] = value; + // Checks for write in aduio registers and calls audioWriteHandler if (address >= 0xFF10 && address <= 0xFF3F) { - globalFunction(address, value); + audioWriteHandler(address); } } } diff --git a/src/mmap.h b/src/mmap.h index cffe16e..37b4aaf 100644 --- a/src/mmap.h +++ b/src/mmap.h @@ -139,7 +139,8 @@ class MemoryMap Byte* reg_WX; // Audio Write Listener - std::function globalFunction; + // updates and writes back after audio write + std::function audioWriteHandler; public: // Audio Unit @@ -276,6 +277,6 @@ class MemoryMap // sets the ROM file void setRomFile(FILE* file) { romFile = file; } - // connects the global function - void connectObserver(const std::function& function) { globalFunction = function; } + // sets audiowritehandler function + void setAduioWriteHandler(const std::function& function) { audioWriteHandler = function; } }; \ No newline at end of file