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 5 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"
}
}
46 changes: 29 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");
}

// Connecting CallBack function
void APU::setSignalCallback()
{
if (mMap)
mMap->connectObserver([this](Word address, Byte value) { this->onWrite(address, value); });
v1bh475u marked this conversation as resolved.
Show resolved Hide resolved
}

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);
writeUpdate(addr, 0xFF);
}
printf("FramerSequencer ends\n");
}
Expand All @@ -230,11 +224,29 @@ 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)
v1bh475u marked this conversation as resolved.
Show resolved Hide resolved
{
if (MemWrite)
{
value = mMap->readMemory(address);
writeByte(address, value);
}
value = readByte(address);
mMap->writeBackMemory(address, value);
}

// PulseChannel

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

enum Channel
{
Expand Down Expand Up @@ -172,4 +172,10 @@ 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);
v1bh475u marked this conversation as resolved.
Show resolved Hide resolved

void setSignalCallback();
};
1 change: 1 addition & 0 deletions src/gameBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GBE::GBE()

// Unify the APU and MemoryMap
gbe_audio->setMemoryMap(gbe_mMap);
gbe_audio->setSignalCallback();

// Open the Boot ROM
if ((bootROM = fopen("../src/dmg_boot.gb", "rb")) == NULL)
Expand Down
31 changes: 17 additions & 14 deletions src/mmap.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "mmap.h"
#include <cstring>

// std::function<void(Word, Byte)> globalFunction = nullptr;
v1bh475u marked this conversation as resolved.
Show resolved Hide resolved

// Constructor
MemoryMap::MemoryMap()
{
Expand Down Expand Up @@ -103,25 +105,26 @@ MemoryMap::MemoryMap()

bootRomFile = nullptr;
romFile = nullptr;
globalFunction = 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 @@ -193,7 +196,7 @@ bool MemoryMap::writeMemory(Word address, Byte value)
ioPorts[address - 0xFF00] = value;
if (address >= 0xFF10 && address <= 0xFF3F)
{
MemoryMap::pushAudioWriteQueue(address, value);
globalFunction(address, value);
}
}
}
Expand Down Expand Up @@ -350,4 +353,4 @@ void MemoryMap::unloadBootRom()
{
fseek(romFile, 0x00, SEEK_SET);
fread(romBank0, 1, 256, romFile);
}
}
17 changes: 6 additions & 11 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>
// extern std::function<void(Word, Byte)> globalFunction;
v1bh475u marked this conversation as resolved.
Show resolved Hide resolved
// The Memory Map for GBE
// Pulled from https://gbdev.io/pandocs/Memory_Map.html

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

// Audio write queue
std::queue<audioRegs> audioWriteQueue;
// Audio Write Listener
std::function<void(Word, Byte)> globalFunction;

public:
// Audio Unit
Expand Down Expand Up @@ -275,12 +276,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();
// connects the global function
void connectObserver(const std::function<void(Word, Byte)>& function) { globalFunction = function; }
};
Loading