Skip to content

Commit

Permalink
naming fixed, redundancies removed
Browse files Browse the repository at this point in the history
  • Loading branch information
v1bh475u committed Aug 30, 2024
1 parent ca1bc90 commit efc9386
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
25 changes: 14 additions & 11 deletions src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);
Expand Down
18 changes: 14 additions & 4 deletions src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdio.h>
#include <SDL.h> // SDL Audio

#define AUDIO_CHANNEL_CONTROL 0xFF26

enum Channel
{
CH1 = 0,
Expand All @@ -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:
Expand Down Expand Up @@ -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();
};
5 changes: 3 additions & 2 deletions src/gameBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ MemoryMap::MemoryMap()

bootRomFile = nullptr;
romFile = nullptr;
globalFunction = nullptr;
audioWriteHandler = nullptr;

mbcMode = 0x0;
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class MemoryMap
Byte* reg_WX;

// Audio Write Listener
std::function<void(Word, Byte)> globalFunction;
// updates and writes back after audio write
std::function<void(Word)> audioWriteHandler;

public:
// Audio Unit
Expand Down Expand Up @@ -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<void(Word, Byte)>& function) { globalFunction = function; }
// sets audiowritehandler function
void setAduioWriteHandler(const std::function<void(Word)>& function) { audioWriteHandler = function; }
};

0 comments on commit efc9386

Please sign in to comment.