From e0dcc5f2e471abd55acbade310d4661e85da03a4 Mon Sep 17 00:00:00 2001 From: vibhatsu Date: Sun, 25 Aug 2024 01:32:21 +0530 Subject: [PATCH] Signals and slots implemented --- .gitignore | 2 +- .vscode/settings.json | 31 ++++++++++++++++++++++++++++++- src/audio.cpp | 38 ++++++++++++++++++++++---------------- src/audio.h | 6 +++++- src/gameBoy.cpp | 2 +- src/mmap.cpp | 30 ++++++++++++++++-------------- src/mmap.h | 15 ++------------- 7 files changed, 77 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 2f4aa67..a66f954 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,6 @@ cmake-build-debug/ src/.vscode/ src/dmg_boot.gb tests/* -.vscode/ +.vscode/* run.sh output.txt \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 31053f6..3300a6c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -54,6 +54,35 @@ "ios": "cpp", "queue": "cpp", "semaphore": "cpp", - "cinttypes": "cpp" + "cinttypes": "cpp", + "any": "cpp", + "hash_map": "cpp", + "strstream": "cpp", + "charconv": "cpp", + "codecvt": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "unordered_set": "cpp", + "optional": "cpp", + "source_location": "cpp", + "format": "cpp", + "fstream": "cpp", + "future": "cpp", + "iomanip": "cpp", + "iostream": "cpp", + "istream": "cpp", + "mutex": "cpp", + "shared_mutex": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdfloat": "cpp", + "text_encoding": "cpp", + "cfenv": "cpp", + "typeindex": "cpp", + "valarray": "cpp", + "variant": "cpp" } } \ No newline at end of file diff --git a/src/audio.cpp b/src/audio.cpp index 6071407..263fe28 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -20,6 +20,7 @@ APU::APU() channel2 = new PulseChannel(CH2); channel3 = new WaveChannel(); channel4 = new NoiseChannel(); + globalFunction = std::bind(&APU::onWrite, this, std::placeholders::_1, std::placeholders::_2); } bool APU::init() @@ -171,18 +172,6 @@ Byte APU::readByte(Word address) void APU::stepAPU(int cycles) { - // Audio write regsisters - while (!mMap->isQueueEmpty()) - { - printf("APU working\n"); - audioRegs writtenRegister = mMap->popAudioWriteQueue(); - writeByte(writtenRegister.address, writtenRegister.value); - Byte value = readByte(writtenRegister.address); - mMap->writeBackMemory(writtenRegister.address, value); - value = readByte(0xFF26); - mMap->writeBackMemory(0xFF26, value); - } - sampleCounter += cycles; frameSequencerCounter += cycles; @@ -207,8 +196,7 @@ void APU::stepAPU(int cycles) Word address[] = { 0xFF19, 0xFF1E, 0xFF23, 0xFF26 }; for (auto addr : address) { - Byte reg = readByte(addr); - mMap->writeBackMemory(addr, reg); + writeUpdate(addr, 0xFF); } printf("FramerSequencer ends\n"); } @@ -230,9 +218,27 @@ 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++) { - Byte reg = readByte(address); - mMap->writeBackMemory(address, reg); + writeUpdate(address, 0xFF); + } +} + +// Write on Memory Write +void APU::onWrite(Word address, Byte value) +{ + writeUpdate(address, value, true); + writeUpdate(0xFF26, 0xFF); +} + +// Write Update +void APU::writeUpdate(Word address, Byte value, bool MemWrite) +{ + if (MemWrite) + { + value = mMap->readMemory(address); + writeByte(address, value); } + value = readByte(address); + mMap->writeBackMemory(address, value); } // PulseChannel diff --git a/src/audio.h b/src/audio.h index fdee437..8e23789 100644 --- a/src/audio.h +++ b/src/audio.h @@ -2,7 +2,7 @@ #include "types.h" #include "mmap.h" #include -#include +#include // SDL Audio enum Channel { @@ -172,4 +172,8 @@ class APU void stepAPU(int cycles); void clearRegisters(); void setMemoryMap(MemoryMap* map) { mMap = map; } + // Writes back on Memory Write + void onWrite(Word address, Byte value); + // Write update + void writeUpdate(Word address, Byte value, bool WriteMem = false); }; \ No newline at end of file diff --git a/src/gameBoy.cpp b/src/gameBoy.cpp index 672145a..f373b9c 100644 --- a/src/gameBoy.cpp +++ b/src/gameBoy.cpp @@ -40,7 +40,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 db92347..e22a8a0 100644 --- a/src/mmap.cpp +++ b/src/mmap.cpp @@ -1,6 +1,8 @@ #include "mmap.h" #include +std::function globalFunction = nullptr; + // Constructor MemoryMap::MemoryMap() { @@ -107,21 +109,21 @@ MemoryMap::MemoryMap() mbcMode = 0x0; } -// Push to audio write queue -void MemoryMap::pushAudioWriteQueue(Word address, Byte value) +// MemoryMap Destructor +MemoryMap::~MemoryMap() { - audioRegs temp = { address, value }; - MemoryMap::audioWriteQueue.push(temp); + delete romBank0; + delete romBank1; + delete videoRam; + delete externalRam; + delete workRam; + delete oamTable; + delete ioPorts; + delete highRam; + delete interruptEnableRegister; + delete joyPadState; } -// remove the first element from queue -audioRegs MemoryMap::popAudioWriteQueue() -{ - audioRegs t = audioWriteQueue.front(); - audioWriteQueue.pop(); - return t; -}; - // Write to memory // TODO: Make emulation memory secure bool MemoryMap::writeMemory(Word address, Byte value) @@ -193,7 +195,7 @@ bool MemoryMap::writeMemory(Word address, Byte value) ioPorts[address - 0xFF00] = value; if (address >= 0xFF10 && address <= 0xFF3F) { - MemoryMap::pushAudioWriteQueue(address, value); + globalFunction(address, value); } } } @@ -350,4 +352,4 @@ void MemoryMap::unloadBootRom() { fseek(romFile, 0x00, SEEK_SET); fread(romBank0, 1, 256, romFile); -} \ No newline at end of file +} diff --git a/src/mmap.h b/src/mmap.h index 29e280a..65dd9e7 100644 --- a/src/mmap.h +++ b/src/mmap.h @@ -1,7 +1,8 @@ #pragma once #include "types.h" #include -#include +#include +extern std::function globalFunction; // The Memory Map for GBE // Pulled from https://gbdev.io/pandocs/Memory_Map.html @@ -137,9 +138,6 @@ class MemoryMap // Stays in the I/O Ports at 0xFF4B Byte* reg_WX; - // Audio write queue - std::queue audioWriteQueue; - public: // Audio Unit // I know this is not the best way to do it @@ -274,13 +272,4 @@ class MemoryMap // sets the ROM file void setRomFile(FILE* file) { romFile = file; } - - // push to queue - void pushAudioWriteQueue(Word address, Byte value); - - // is queue empty - bool isQueueEmpty() { return audioWriteQueue.empty(); }; - - // pop queue - audioRegs popAudioWriteQueue(); }; \ No newline at end of file