Skip to content

Commit

Permalink
Signals and slots implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
v1bh475u committed Aug 24, 2024
1 parent ee90ffe commit e0dcc5f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 47 deletions.
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"
}
}
38 changes: 22 additions & 16 deletions src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;

Expand All @@ -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");
}
Expand All @@ -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
Expand Down
6 changes: 5 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,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);
};
2 changes: 1 addition & 1 deletion src/gameBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 16 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;

// Constructor
MemoryMap::MemoryMap()
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -350,4 +352,4 @@ void MemoryMap::unloadBootRom()
{
fseek(romFile, 0x00, SEEK_SET);
fread(romBank0, 1, 256, romFile);
}
}
15 changes: 2 additions & 13 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;
// The Memory Map for GBE
// Pulled from https://gbdev.io/pandocs/Memory_Map.html

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

// Audio write queue
std::queue<audioRegs> audioWriteQueue;

public:
// Audio Unit
// I know this is not the best way to do it
Expand Down Expand Up @@ -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();
};

0 comments on commit e0dcc5f

Please sign in to comment.