Skip to content

Commit

Permalink
Freed from global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
v1bh475u committed Aug 25, 2024
1 parent e0dcc5f commit 843cfa6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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
7 changes: 7 additions & 0 deletions src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,11 @@ class APU
void onWrite(Word address, Byte value);
// Write update
void writeUpdate(Word address, Byte value, bool WriteMem = false);

void setSignalCallback()
{
if (mMap)
mMap->connectObserver([this](Word address, Byte value)
{ this->onWrite(address, value); });
}
};
3 changes: 2 additions & 1 deletion 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 All @@ -40,7 +41,7 @@ GBE::GBE()
// printf("game rom file not opened");

// Open the Game ROM
if ((gameROM = fopen("../tests/dmg_sound/rom_singles/03-trigger.gb", "rb")) == NULL)
if ((gameROM = fopen("../tests/dmg_sound/rom_singles/02-len ctr.gb", "rb")) == NULL)
printf("game rom file not opened\n");

// Set the Boot ROM
Expand Down
3 changes: 2 additions & 1 deletion src/mmap.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "mmap.h"
#include <cstring>

std::function<void(Word, Byte)> globalFunction = nullptr;
// std::function<void(Word, Byte)> globalFunction = nullptr;

// Constructor
MemoryMap::MemoryMap()
Expand Down Expand Up @@ -105,6 +105,7 @@ MemoryMap::MemoryMap()

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

mbcMode = 0x0;
}
Expand Down
7 changes: 6 additions & 1 deletion src/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "types.h"
#include <stdio.h>
#include <functional>
extern std::function<void(Word, Byte)> globalFunction;
// 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 @@ -138,6 +138,8 @@ class MemoryMap
// Stays in the I/O Ports at 0xFF4B
Byte* reg_WX;

std::function<void(Word, Byte)> globalFunction;

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

0 comments on commit 843cfa6

Please sign in to comment.