Lime3DS/src/core/hle/kernel/kernel.cpp

116 lines
2.8 KiB
C++
Raw Normal View History

2014-12-17 05:38:14 +00:00
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
2014-05-10 02:11:18 +00:00
2014-12-04 00:55:45 +00:00
#include <algorithm>
2014-05-10 02:11:18 +00:00
#include "common/common.h"
#include "core/core.h"
2014-05-10 02:11:18 +00:00
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/thread.h"
namespace Kernel {
2014-05-10 02:11:18 +00:00
Handle g_main_thread = 0;
2014-12-13 23:16:13 +00:00
HandleTable g_handle_table;
u64 g_program_id = 0;
2014-05-10 02:11:18 +00:00
2014-12-13 23:16:13 +00:00
HandleTable::HandleTable() {
2014-05-10 02:11:18 +00:00
next_id = INITIAL_NEXT_ID;
}
2014-12-13 23:16:13 +00:00
Handle HandleTable::Create(Object* obj, int range_bottom, int range_top) {
2014-05-10 02:11:18 +00:00
if (range_top > MAX_COUNT) {
range_top = MAX_COUNT;
}
if (next_id >= range_bottom && next_id < range_top) {
range_bottom = next_id++;
}
for (int i = range_bottom; i < range_top; i++) {
if (!occupied[i]) {
occupied[i] = true;
pool[i] = obj;
pool[i]->handle = i + HANDLE_OFFSET;
2014-05-10 02:11:18 +00:00
return i + HANDLE_OFFSET;
}
}
LOG_ERROR(Kernel, "Unable to allocate kernel object, too many objects slots in use.");
2014-05-10 02:11:18 +00:00
return 0;
}
2014-12-13 23:16:13 +00:00
bool HandleTable::IsValid(Handle handle) const {
2014-05-10 02:11:18 +00:00
int index = handle - HANDLE_OFFSET;
if (index < 0)
return false;
if (index >= MAX_COUNT)
return false;
return occupied[index];
}
2014-12-13 23:16:13 +00:00
void HandleTable::Clear() {
for (int i = 0; i < MAX_COUNT; i++) {
2014-05-10 02:11:18 +00:00
//brutally clear everything, no validation
if (occupied[i])
delete pool[i];
occupied[i] = false;
}
pool.fill(nullptr);
2014-05-10 02:11:18 +00:00
next_id = INITIAL_NEXT_ID;
}
2014-12-13 23:16:13 +00:00
Object* &HandleTable::operator [](Handle handle)
2014-05-10 02:11:18 +00:00
{
_dbg_assert_msg_(Kernel, IsValid(handle), "GRABBING UNALLOCED KERNEL OBJ");
2014-05-10 02:11:18 +00:00
return pool[handle - HANDLE_OFFSET];
}
2014-12-13 23:16:13 +00:00
void HandleTable::List() {
2014-05-10 02:11:18 +00:00
for (int i = 0; i < MAX_COUNT; i++) {
if (occupied[i]) {
if (pool[i]) {
LOG_DEBUG(Kernel, "KO %i: %s \"%s\"", i + HANDLE_OFFSET, pool[i]->GetTypeName().c_str(),
pool[i]->GetName().c_str());
2014-05-10 02:11:18 +00:00
}
}
}
}
2014-12-13 23:16:13 +00:00
int HandleTable::GetCount() const {
2014-12-04 00:55:45 +00:00
return std::count(occupied.begin(), occupied.end(), true);
2014-05-10 02:11:18 +00:00
}
2014-12-13 23:16:13 +00:00
Object* HandleTable::CreateByIDType(int type) {
LOG_ERROR(Kernel, "Unimplemented: %d.", type);
return nullptr;
2014-05-10 02:11:18 +00:00
}
/// Initialize the kernel
void Init() {
Kernel::ThreadingInit();
}
/// Shutdown the kernel
void Shutdown() {
Kernel::ThreadingShutdown();
2014-12-13 23:16:13 +00:00
g_handle_table.Clear(); // Free all kernel objects
}
/**
* Loads executable stored at specified address
* @entry_point Entry point in memory of loaded executable
* @return True on success, otherwise false
*/
bool LoadExec(u32 entry_point) {
Core::g_app_core->SetPC(entry_point);
// 0x30 is the typical main thread priority I've seen used so far
g_main_thread = Kernel::SetupMainThread(0x30);
return true;
}
} // namespace