Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 454457c

Browse files
committed
Libretro: Updated code for refactoring + fixed compilation errors
1 parent 08aa617 commit 454457c

File tree

7 files changed

+87
-65
lines changed

7 files changed

+87
-65
lines changed

Core/Console.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ void Console::Init()
7777

7878
void Console::Release(bool forShutdown)
7979
{
80+
if(_slave) {
81+
_slave->Release(true);
82+
_slave.reset();
83+
}
84+
8085
if(forShutdown) {
8186
_videoDecoder->StopThread();
8287
_videoRenderer->StopThread();
@@ -105,11 +110,6 @@ void Console::Release(bool forShutdown)
105110

106111
_systemActionManager.reset();
107112

108-
if(_slave) {
109-
_slave->Release(true);
110-
_slave.reset();
111-
}
112-
113113
_master.reset();
114114
_cpu.reset();
115115
_ppu.reset();
@@ -625,6 +625,9 @@ void Console::RunSingleFrame()
625625

626626
while(_ppu->GetFrameCount() == lastFrameNumber) {
627627
_cpu->Exec();
628+
if(_slave) {
629+
RunSlaveCpu();
630+
}
628631
}
629632

630633
EmulationSettings::DisableOverclocking(_disableOcNextFrame || NsfMapper::GetInstance());
@@ -634,6 +637,20 @@ void Console::RunSingleFrame()
634637
_apu->EndFrame();
635638
}
636639

640+
void Console::RunSlaveCpu()
641+
{
642+
int32_t cycleGap;
643+
while(true) {
644+
//Run the slave until it catches up to the master CPU (and take into account the CPU count overflow that occurs every ~20mins)
645+
cycleGap = _cpu->GetCycleCount() - _slave->_cpu->GetCycleCount();
646+
if(cycleGap > 5 || cycleGap < -10000 || _ppu->GetFrameCount() > _slave->_ppu->GetFrameCount()) {
647+
_slave->_cpu->Exec();
648+
} else {
649+
break;
650+
}
651+
}
652+
}
653+
637654
void Console::Run()
638655
{
639656
Timer clockTimer;
@@ -643,8 +660,6 @@ void Console::Run()
643660
int timeLagDataIndex = 0;
644661
double lastFrameMin = 9999;
645662
double lastFrameMax = 0;
646-
int32_t cycleGap = 0;
647-
uint32_t currentFrameNumber = 0;
648663

649664
uint32_t lastFrameNumber = -1;
650665

@@ -668,21 +683,11 @@ void Console::Run()
668683
while(true) {
669684
_cpu->Exec();
670685

671-
currentFrameNumber = _ppu->GetFrameCount();
672-
673686
if(_slave) {
674-
while(true) {
675-
//Run the slave until it catches up to the master CPU (and take into account the CPU count overflow that occurs every ~20mins)
676-
cycleGap = _cpu->GetCycleCount() - _slave->_cpu->GetCycleCount();
677-
if(cycleGap > 5 || cycleGap < -10000 || currentFrameNumber > _slave->_ppu->GetFrameCount()) {
678-
_slave->_cpu->Exec();
679-
} else {
680-
break;
681-
}
682-
}
687+
RunSlaveCpu();
683688
}
684689

685-
if(currentFrameNumber != lastFrameNumber) {
690+
if(_ppu->GetFrameCount() != lastFrameNumber) {
686691
_soundMixer->ProcessEndOfFrame();
687692
if(_slave) {
688693
_slave->_soundMixer->ProcessEndOfFrame();

Core/Console.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class Console : public std::enable_shared_from_this<Console>
133133
int32_t GetStopCode();
134134

135135
void RunSingleFrame();
136+
void RunSlaveCpu();
136137
bool UpdateHdPackMode();
137138

138139
shared_ptr<SystemActionManager> GetSystemActionManager();

Libretro/LibretroKeyManager.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#include "../Core/KeyManager.h"
55
#include "../Core/FdsSystemActionManager.h"
66
#include "../Core/VsSystemActionManager.h"
7+
#include "../Core/Console.h"
78

89
class LibretroKeyManager : public IKeyManager
910
{
1011
private:
12+
shared_ptr<Console> _console;
1113
retro_input_state_t _getInputState = nullptr;
1214
retro_input_poll_t _pollInput = nullptr;
1315
bool _mouseButtons[3] = { false, false, false };
@@ -28,8 +30,9 @@ class LibretroKeyManager : public IKeyManager
2830
}
2931

3032
public:
31-
LibretroKeyManager()
33+
LibretroKeyManager(shared_ptr<Console> console)
3234
{
35+
_console = console;
3336
KeyManager::RegisterKeyManager(this);
3437
}
3538

@@ -72,7 +75,7 @@ class LibretroKeyManager : public IKeyManager
7275
_mouseButtons[(int)MouseButton::RightButton] = _getInputState(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT) != 0;
7376
_mouseButtons[(int)MouseButton::MiddleButton] = _getInputState(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_MIDDLE) != 0;
7477

75-
shared_ptr<FdsSystemActionManager> fdsSam = Console::GetInstance()->GetSystemActionManager<FdsSystemActionManager>();
78+
shared_ptr<FdsSystemActionManager> fdsSam = _console->GetSystemActionManager<FdsSystemActionManager>();
7679
if(fdsSam) {
7780
if(ProcessAction(RETRO_DEVICE_ID_JOYPAD_L)) {
7881
fdsSam->InsertNextDisk();
@@ -83,7 +86,7 @@ class LibretroKeyManager : public IKeyManager
8386
}
8487
}
8588

86-
shared_ptr<VsSystemActionManager> vsSam = Console::GetInstance()->GetSystemActionManager<VsSystemActionManager>();
89+
shared_ptr<VsSystemActionManager> vsSam = _console->GetSystemActionManager<VsSystemActionManager>();
8790
if(vsSam) {
8891
if(ProcessAction(RETRO_DEVICE_ID_JOYPAD_L2)) {
8992
vsSam->InsertCoin(0);

Libretro/LibretroRenderer.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@
1010
class LibretroRenderer : public IRenderingDevice
1111
{
1212
private:
13+
shared_ptr<Console> _console;
1314
retro_video_refresh_t _sendFrame = nullptr;
1415
retro_environment_t _retroEnv = nullptr;
1516
bool _skipMode = false;
1617
int32_t _previousHeight = -1;
1718
int32_t _previousWidth = -1;
1819

1920
public:
20-
LibretroRenderer()
21+
LibretroRenderer(shared_ptr<Console> console)
2122
{
22-
VideoRenderer::GetInstance()->RegisterRenderingDevice(this);
23+
_console = console;
24+
_console->GetVideoRenderer()->RegisterRenderingDevice(this);
2325
}
2426

2527
~LibretroRenderer()
2628
{
27-
VideoRenderer::GetInstance()->UnregisterRenderingDevice(this);
29+
_console->GetVideoRenderer()->UnregisterRenderingDevice(this);
2830
}
2931

3032
// Inherited via IRenderingDevice
@@ -50,10 +52,10 @@ class LibretroRenderer : public IRenderingDevice
5052

5153
void GetSystemAudioVideoInfo(retro_system_av_info &info, int32_t maxWidth = 0, int32_t maxHeight = 0)
5254
{
53-
info.timing.fps = Console::GetModel() == NesModel::NTSC ? 60.098811862348404716732985230828 : 50.006977968268290848936010226333;
55+
info.timing.fps = _console->GetModel() == NesModel::NTSC ? 60.098811862348404716732985230828 : 50.006977968268290848936010226333;
5456
info.timing.sample_rate = 48000;
5557

56-
float ratio = (float)EmulationSettings::GetAspectRatio();
58+
float ratio = (float)EmulationSettings::GetAspectRatio(_console);
5759
if(ratio == 0.0f) {
5860
ratio = 1.0f;
5961
}

Libretro/LibretroSoundManager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ class LibretroSoundManager : public IAudioDevice
99
private:
1010
retro_audio_sample_batch_t _sendAudioBuffer = nullptr;
1111
bool _skipMode = false;
12+
shared_ptr<Console> _console;
1213

1314
public:
14-
LibretroSoundManager()
15+
LibretroSoundManager(shared_ptr<Console> console)
1516
{
16-
SoundMixer::RegisterAudioDevice(this);
17+
_console = console;
18+
_console->GetSoundMixer()->RegisterAudioDevice(this);
1719
}
1820

1921
~LibretroSoundManager()
2022
{
21-
SoundMixer::RegisterAudioDevice(nullptr);
23+
_console->GetSoundMixer()->RegisterAudioDevice(nullptr);
2224
}
2325

2426
// Inherited via IAudioDevice

Libretro/Makefile.common

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ SOURCES_C := $(SEVENZIP_DIR)/7zAlloc.c \
2121

2222
SOURCES_CXX := $(LIBRETRO_DIR)/libretro.cpp \
2323
$(CORE_DIR)/APU.cpp \
24-
$(CORE_DIR)/ApuLengthCounter.cpp \
2524
$(CORE_DIR)/Assembler.cpp \
2625
$(CORE_DIR)/AutomaticRomTest.cpp \
2726
$(CORE_DIR)/AutoSaveManager.cpp \
2827
$(CORE_DIR)/AviRecorder.cpp \
2928
$(CORE_DIR)/BaseControlDevice.cpp \
29+
$(CORE_DIR)/BaseExpansionAudio.cpp \
3030
$(CORE_DIR)/BaseMapper.cpp \
3131
$(CORE_DIR)/BaseRenderer.cpp \
3232
$(CORE_DIR)/BaseVideoFilter.cpp \
@@ -44,14 +44,15 @@ SOURCES_CXX := $(LIBRETRO_DIR)/libretro.cpp \
4444
$(CORE_DIR)/Debugger.cpp \
4545
$(CORE_DIR)/DebugHud.cpp \
4646
$(CORE_DIR)/DefaultVideoFilter.cpp \
47-
$(CORE_DIR)/RawVideoFilter.cpp \
47+
$(CORE_DIR)/RawVideoFilter.cpp \
4848
$(CORE_DIR)/DeltaModulationChannel.cpp \
4949
$(CORE_DIR)/Disassembler.cpp \
5050
$(CORE_DIR)/DisassemblyInfo.cpp \
5151
$(CORE_DIR)/EmulationSettings.cpp \
5252
$(CORE_DIR)/ExpressionEvaluator.cpp \
5353
$(CORE_DIR)/FceuxMovie.cpp \
5454
$(CORE_DIR)/FDS.cpp \
55+
$(CORE_DIR)/FdsLoader.cpp \
5556
$(CORE_DIR)/GameClient.cpp \
5657
$(CORE_DIR)/GameClientConnection.cpp \
5758
$(CORE_DIR)/GameConnection.cpp \
@@ -62,6 +63,7 @@ SOURCES_CXX := $(LIBRETRO_DIR)/libretro.cpp \
6263
$(CORE_DIR)/HdNesPack.cpp \
6364
$(CORE_DIR)/HdPackBuilder.cpp \
6465
$(CORE_DIR)/HdPackLoader.cpp \
66+
$(CORE_DIR)/HdPpu.cpp \
6567
$(CORE_DIR)/HdVideoFilter.cpp \
6668
$(CORE_DIR)/iNesLoader.cpp \
6769
$(CORE_DIR)/KeyManager.cpp \
@@ -74,11 +76,13 @@ SOURCES_CXX := $(LIBRETRO_DIR)/libretro.cpp \
7476
$(CORE_DIR)/MessageManager.cpp \
7577
$(CORE_DIR)/MovieManager.cpp \
7678
$(CORE_DIR)/MovieRecorder.cpp \
79+
$(CORE_DIR)/NotificationManager.cpp \
80+
$(CORE_DIR)/NsfLoader.cpp \
7781
$(CORE_DIR)/NsfMapper.cpp \
82+
$(CORE_DIR)/NsfPpu.cpp \
7883
$(CORE_DIR)/NtscFilter.cpp \
7984
$(CORE_DIR)/OggMixer.cpp \
8085
$(CORE_DIR)/OggReader.cpp \
81-
$(CORE_DIR)/OpenBusHandler.cpp \
8286
$(CORE_DIR)/PPU.cpp \
8387
$(CORE_DIR)/Profiler.cpp \
8488
$(CORE_DIR)/RecordedRomTest.cpp \

0 commit comments

Comments
 (0)