2020-09-13 21:32:55 -04:00
|
|
|
#pragma once
|
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
#ifndef JAK_IOP_KERNEL_H
|
|
|
|
#define JAK_IOP_KERNEL_H
|
|
|
|
|
2022-06-22 23:37:46 -04:00
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2020-08-22 22:30:12 -04:00
|
|
|
#include <queue>
|
2022-06-22 23:37:46 -04:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2022-07-26 21:15:37 -04:00
|
|
|
#include <utility>
|
2020-08-22 22:30:12 -04:00
|
|
|
#include <vector>
|
2022-06-22 23:37:46 -04:00
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
#include "common/common_types.h"
|
2022-02-08 19:02:47 -05:00
|
|
|
#include "common/util/Assert.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
|
2022-05-19 16:54:36 -04:00
|
|
|
#include "game/sce/iop.h"
|
2020-08-22 22:30:12 -04:00
|
|
|
|
2022-07-22 11:54:27 -04:00
|
|
|
#include "third-party/libco/libco.h"
|
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
class IOP_Kernel;
|
|
|
|
namespace iop {
|
2020-08-26 01:21:33 -04:00
|
|
|
struct sceSifQueueData;
|
2020-08-22 22:30:12 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 21:15:37 -04:00
|
|
|
using time_stamp = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
|
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
struct SifRpcCommand {
|
|
|
|
bool started = true;
|
|
|
|
bool finished = true;
|
|
|
|
|
|
|
|
void* buff;
|
|
|
|
int fno;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
void* copy_back_buff;
|
|
|
|
int copy_back_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SifRecord {
|
|
|
|
iop::sceSifQueueData* qd;
|
|
|
|
SifRpcCommand cmd;
|
|
|
|
u32 thread_to_wake;
|
|
|
|
};
|
|
|
|
|
2022-07-22 11:54:27 -04:00
|
|
|
struct IopThread {
|
|
|
|
enum class State {
|
|
|
|
Run,
|
|
|
|
Ready,
|
|
|
|
Wait,
|
|
|
|
WaitSuspend,
|
|
|
|
Suspend,
|
|
|
|
Dormant,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class Wait {
|
|
|
|
None,
|
|
|
|
Semaphore,
|
|
|
|
Delay,
|
|
|
|
};
|
|
|
|
|
2022-07-26 21:15:37 -04:00
|
|
|
IopThread(std::string n, void (*f)(), s32 ID, u32 priority)
|
|
|
|
: name(std::move(n)), function(f), priority(priority), thID(ID) {
|
|
|
|
thread = co_create(0x300000, functionWrapper);
|
2020-08-22 22:30:12 -04:00
|
|
|
}
|
|
|
|
|
2022-07-22 11:54:27 -04:00
|
|
|
~IopThread() { co_delete(thread); }
|
2020-08-22 22:30:12 -04:00
|
|
|
|
2022-07-26 21:15:37 -04:00
|
|
|
static void functionWrapper();
|
2020-08-22 22:30:12 -04:00
|
|
|
std::string name;
|
2022-07-22 11:54:27 -04:00
|
|
|
void (*function)();
|
|
|
|
cothread_t thread;
|
|
|
|
State state = State::Dormant;
|
|
|
|
Wait waitType = Wait::None;
|
2022-07-26 21:15:37 -04:00
|
|
|
time_stamp resumeTime = {};
|
|
|
|
u32 priority = 0;
|
2020-08-22 22:30:12 -04:00
|
|
|
s32 thID = -1;
|
2022-07-22 11:54:27 -04:00
|
|
|
};
|
2020-08-22 22:30:12 -04:00
|
|
|
|
2022-07-22 11:54:27 -04:00
|
|
|
struct Semaphore {
|
|
|
|
u32 option;
|
|
|
|
u32 attr;
|
|
|
|
s32 count;
|
|
|
|
s32 maxCount;
|
|
|
|
s32 initCount;
|
2020-08-22 22:30:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class IOP_Kernel {
|
2020-08-26 01:21:33 -04:00
|
|
|
public:
|
2020-08-22 22:30:12 -04:00
|
|
|
IOP_Kernel() {
|
|
|
|
// this ugly hack
|
|
|
|
threads.reserve(16);
|
2022-07-26 21:15:37 -04:00
|
|
|
CreateThread("null-thread", nullptr, 0);
|
2020-08-22 22:30:12 -04:00
|
|
|
CreateMbx();
|
2022-07-22 11:54:27 -04:00
|
|
|
kernel_thread = co_active();
|
2020-08-22 22:30:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
~IOP_Kernel();
|
|
|
|
|
2022-07-26 21:15:37 -04:00
|
|
|
s32 CreateThread(std::string n, void (*f)(), u32 priority);
|
|
|
|
s32 ExitThread();
|
2020-08-22 22:30:12 -04:00
|
|
|
void StartThread(s32 id);
|
2022-07-26 21:15:37 -04:00
|
|
|
void DelayThread(u32 usec);
|
2020-08-22 22:30:12 -04:00
|
|
|
void SleepThread();
|
|
|
|
void WakeupThread(s32 id);
|
2022-07-26 21:15:37 -04:00
|
|
|
time_stamp dispatch();
|
2020-08-26 01:21:33 -04:00
|
|
|
void set_rpc_queue(iop::sceSifQueueData* qd, u32 thread);
|
2020-08-22 22:30:12 -04:00
|
|
|
void rpc_loop(iop::sceSifQueueData* qd);
|
|
|
|
void shutdown();
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Get current thread ID.
|
|
|
|
*/
|
2022-07-26 21:15:37 -04:00
|
|
|
s32 getCurrentThread() {
|
|
|
|
ASSERT(_currentThread);
|
|
|
|
return _currentThread->thID;
|
|
|
|
}
|
2020-08-22 22:30:12 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Create a message box
|
|
|
|
*/
|
|
|
|
s32 CreateMbx() {
|
|
|
|
s32 id = mbxs.size();
|
|
|
|
mbxs.emplace_back();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Set msg to thing if its there and pop it.
|
|
|
|
* Returns if it got something.
|
|
|
|
*/
|
|
|
|
s32 PollMbx(void** msg, s32 mbx) {
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(mbx < (s32)mbxs.size());
|
2020-08-26 01:21:33 -04:00
|
|
|
s32 gotSomething = mbxs[mbx].empty() ? 0 : 1;
|
|
|
|
if (gotSomething) {
|
2020-08-22 22:30:12 -04:00
|
|
|
void* thing = mbxs[mbx].front();
|
2020-12-28 18:37:05 -05:00
|
|
|
|
|
|
|
if (msg) {
|
2020-08-22 22:30:12 -04:00
|
|
|
*msg = thing;
|
2020-12-28 18:37:05 -05:00
|
|
|
}
|
|
|
|
|
2020-08-22 22:30:12 -04:00
|
|
|
mbxs[mbx].pop();
|
|
|
|
}
|
|
|
|
|
2022-05-19 16:54:36 -04:00
|
|
|
return gotSomething ? KE_OK : KE_MBOX_NOMSG;
|
2020-08-22 22:30:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Push something into a mbx
|
|
|
|
*/
|
|
|
|
s32 SendMbx(s32 mbx, void* value) {
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(mbx < (s32)mbxs.size());
|
2020-08-22 22:30:12 -04:00
|
|
|
mbxs[mbx].push(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-26 01:21:33 -04:00
|
|
|
s32 CreateSema() { return 1; }
|
2020-08-22 22:30:12 -04:00
|
|
|
|
|
|
|
void read_disc_sectors(u32 sector, u32 sectors, void* buffer);
|
|
|
|
bool sif_busy(u32 id);
|
|
|
|
|
2020-08-26 01:21:33 -04:00
|
|
|
void sif_rpc(s32 rpcChannel,
|
|
|
|
u32 fno,
|
|
|
|
bool async,
|
|
|
|
void* sendBuff,
|
|
|
|
s32 sendSize,
|
|
|
|
void* recvBuff,
|
|
|
|
s32 recvSize);
|
2020-08-22 22:30:12 -04:00
|
|
|
|
2020-08-26 01:21:33 -04:00
|
|
|
private:
|
2022-07-26 21:15:37 -04:00
|
|
|
void runThread(IopThread* thread);
|
2022-07-22 11:54:27 -04:00
|
|
|
void exitThread();
|
2022-07-26 21:15:37 -04:00
|
|
|
void updateDelay();
|
|
|
|
void processWakeups();
|
|
|
|
|
|
|
|
IopThread* schedNext();
|
|
|
|
time_stamp nextWakeup();
|
|
|
|
|
2022-07-22 11:54:27 -04:00
|
|
|
cothread_t kernel_thread;
|
2020-08-22 22:30:12 -04:00
|
|
|
s32 _nextThID = 0;
|
2022-07-26 21:15:37 -04:00
|
|
|
IopThread* _currentThread = nullptr;
|
2022-07-22 11:54:27 -04:00
|
|
|
std::vector<IopThread> threads;
|
2020-08-22 22:30:12 -04:00
|
|
|
std::vector<std::queue<void*>> mbxs;
|
|
|
|
std::vector<SifRecord> sif_records;
|
2022-07-22 11:54:27 -04:00
|
|
|
std::vector<Semaphore> semas;
|
2022-07-26 21:15:37 -04:00
|
|
|
std::queue<int> wakeup_queue;
|
2020-08-22 22:30:12 -04:00
|
|
|
bool mainThreadSleep = false;
|
|
|
|
FILE* iso_disc_file = nullptr;
|
2022-07-26 21:15:37 -04:00
|
|
|
std::mutex sif_mtx, wakeup_mtx;
|
2020-08-22 22:30:12 -04:00
|
|
|
};
|
|
|
|
|
2020-08-26 01:21:33 -04:00
|
|
|
#endif // JAK_IOP_KERNEL_H
|