summaryrefslogtreecommitdiffstats
path: root/src/core/cpu_manager.cpp
blob: 70ddbdcca7c9e7e74d7a9e4095259a728e789b3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include "core/arm/exclusive_monitor.h"
#include "core/core.h"
#include "core/core_manager.h"
#include "core/core_timing.h"
#include "core/cpu_manager.h"
#include "core/gdbstub/gdbstub.h"

namespace Core {

CpuManager::CpuManager(System& system) : system{system} {}
CpuManager::~CpuManager() = default;

void CpuManager::Initialize() {
    for (std::size_t index = 0; index < core_managers.size(); ++index) {
        core_managers[index] = std::make_unique<CoreManager>(system, index);
    }
}

void CpuManager::Shutdown() {
    for (auto& cpu_core : core_managers) {
        cpu_core.reset();
    }
}

CoreManager& CpuManager::GetCoreManager(std::size_t index) {
    return *core_managers.at(index);
}

const CoreManager& CpuManager::GetCoreManager(std::size_t index) const {
    return *core_managers.at(index);
}

CoreManager& CpuManager::GetCurrentCoreManager() {
    // Otherwise, use single-threaded mode active_core variable
    return *core_managers[active_core];
}

const CoreManager& CpuManager::GetCurrentCoreManager() const {
    // Otherwise, use single-threaded mode active_core variable
    return *core_managers[active_core];
}

void CpuManager::RunLoop(bool tight_loop) {
    if (GDBStub::IsServerEnabled()) {
        GDBStub::HandlePacket();

        // If the loop is halted and we want to step, use a tiny (1) number of instructions to
        // execute. Otherwise, get out of the loop function.
        if (GDBStub::GetCpuHaltFlag()) {
            if (GDBStub::GetCpuStepFlag()) {
                tight_loop = false;
            } else {
                return;
            }
        }
    }

    auto& core_timing = system.CoreTiming();
    core_timing.ResetRun();
    bool keep_running{};
    do {
        keep_running = false;
        for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) {
            core_timing.SwitchContext(active_core);
            if (core_timing.CanCurrentContextRun()) {
                core_managers[active_core]->RunLoop(tight_loop);
            }
            keep_running |= core_timing.CanCurrentContextRun();
        }
    } while (keep_running);

    if (GDBStub::IsServerEnabled()) {
        GDBStub::SetCpuStepFlag(false);
    }
}

} // namespace Core