Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vibhatsu/signals n slots #33

Merged
merged 8 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ cmake-build-debug/
src/.vscode/
src/dmg_boot.gb
tests/*
.vscode/
.vscode/*
run.sh
output.txt
31 changes: 30 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
49 changes: 32 additions & 17 deletions src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ void APU::test()
printf("APU test\n");
}

// initialize the writehandler of memorymap
void APU::initializeWriteHandler()
{
if (mMap)
mMap->setAudioWriteHandler([this](Word address) { this->onMemoryWrite(address); });
}

void APU::writeByte(Word address, Byte value)
{
printf("APU Address: %04X, Value: %02X\n", address, value);
Expand Down Expand Up @@ -171,18 +178,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;

Expand All @@ -207,8 +202,7 @@ void APU::stepAPU(int cycles)
Word address[] = { 0xFF19, 0xFF1E, 0xFF23, 0xFF26 };
for (auto addr : address)
{
Byte reg = readByte(addr);
mMap->writeBackMemory(addr, reg);
audioRegisterUpdate(addr, AudioWrite);
}
printf("FramerSequencer ends\n");
}
Expand All @@ -230,11 +224,32 @@ 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);
audioRegisterUpdate(address, AudioWrite);
}
}

// Updates APU registers on write in MemoryMap
void APU::onMemoryWrite(Word address)
{
// address where write has occurred
audioRegisterUpdate(address, AudioMemoryWrite);
// Update the audio channel controller register
audioRegisterUpdate(AUDIO_MASTER_CONTROL_REGISTER, AudioWrite);
}

// Write Update
void APU::audioRegisterUpdate(Word address, audioWriteFlag flag)
{
Byte value = 0xFF;
if (flag)
{
value = mMap->readMemory(address);
writeByte(address, value);
}
value = readByte(address);
mMap->MemoryWriteBack(address, value);
}

// PulseChannel

PulseChannel::PulseChannel(Channel channel)
Expand Down Expand Up @@ -762,4 +777,4 @@ void NoiseChannel::trigger()
{
LFSR = 0x7FFF;
enabled = dacEnabled;
}
}
18 changes: 17 additions & 1 deletion src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "types.h"
#include "mmap.h"
#include <stdio.h>
#include <SDL.h>
#include <SDL.h> // SDL Audio

#define AUDIO_MASTER_CONTROL_REGISTER 0xFF26

enum Channel
{
Expand All @@ -12,6 +14,14 @@ enum Channel
CH4 = 3
};

// For checking whether Write is due to memory or APU itself
enum audioWriteFlag
{
AudioWrite = 0,
AudioMemoryWrite = 1

v1bh475u marked this conversation as resolved.
Show resolved Hide resolved
};

class PulseChannel
{
private:
Expand Down Expand Up @@ -172,4 +182,10 @@ class APU
void stepAPU(int cycles);
void clearRegisters();
void setMemoryMap(MemoryMap* map) { mMap = map; }
// Writes back on Memory Write
void onMemoryWrite(Word address);
// Write update
void audioRegisterUpdate(Word address, audioWriteFlag flag);
// initializes the audioWriteHandler of MemoryMap
void initializeWriteHandler();
};
2 changes: 2 additions & 0 deletions src/gameBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ GBE::GBE()

// Unify the APU and MemoryMap
gbe_audio->setMemoryMap(gbe_mMap);
// initialize the write handler
gbe_audio->initializeWriteHandler();

// Open the Boot ROM
if ((bootROM = fopen("../src/dmg_boot.gb", "rb")) == NULL)
Expand Down
32 changes: 17 additions & 15 deletions src/mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,26 @@ MemoryMap::MemoryMap()

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

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)
Expand Down Expand Up @@ -191,9 +192,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)
{
MemoryMap::pushAudioWriteQueue(address, value);
audioWriteHandler(address);
}
}
}
Expand Down Expand Up @@ -221,7 +223,7 @@ void MemoryMap::debugWriteMemory(Word address, Byte value)
romBank0[address] = value;
}

bool MemoryMap::writeBackMemory(Word address, Byte value)
bool MemoryMap::MemoryWriteBack(Word address, Byte value)
{
if (address >= 0xFF10 && address <= 0xFF3F)
{
Expand Down Expand Up @@ -350,4 +352,4 @@ void MemoryMap::unloadBootRom()
{
fseek(romFile, 0x00, SEEK_SET);
fread(romBank0, 1, 256, romFile);
}
}
20 changes: 8 additions & 12 deletions src/mmap.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include "types.h"
#include <stdio.h>
#include <queue>
#include <functional>

// The Memory Map for GBE
// Pulled from https://gbdev.io/pandocs/Memory_Map.html

Expand Down Expand Up @@ -137,8 +138,9 @@ class MemoryMap
// Stays in the I/O Ports at 0xFF4B
Byte* reg_WX;

// Audio write queue
std::queue<audioRegs> audioWriteQueue;
// Audio Write Listener
// updates and writes back after audio write
std::function<void(Word)> audioWriteHandler;

public:
// Audio Unit
Expand Down Expand Up @@ -189,7 +191,7 @@ class MemoryMap
void debugWriteMemory(Word address, Byte value);

// Write Back
bool writeBackMemory(Word address, Byte value);
bool MemoryWriteBack(Word address, Byte value);

// Reads a byte from the memory address
Byte readMemory(Word address);
Expand Down Expand Up @@ -275,12 +277,6 @@ 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();
// sets audiowritehandler function
void setAudioWriteHandler(const std::function<void(Word)>& function) { audioWriteHandler = function; }
};
Loading