2023-02-20 04:27:35 +01:00
|
|
|
#include <thread>
|
|
|
|
#include <variant>
|
|
|
|
#include <set>
|
|
|
|
#include "blockingconcurrentqueue.h"
|
|
|
|
|
|
|
|
#include "ultra64.h"
|
2023-11-13 01:40:02 +01:00
|
|
|
#include "ultramodern.hpp"
|
2023-02-20 04:27:35 +01:00
|
|
|
|
|
|
|
// Start time for the program
|
2023-11-04 17:45:22 +01:00
|
|
|
static std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
|
2023-02-20 04:27:35 +01:00
|
|
|
// Game speed multiplier (1 means no speedup)
|
|
|
|
constexpr uint32_t speed_multiplier = 1;
|
|
|
|
// N64 CPU counter ticks per millisecond
|
|
|
|
constexpr uint32_t counter_per_ms = 46'875 * speed_multiplier;
|
|
|
|
|
|
|
|
struct OSTimer {
|
|
|
|
PTR(OSTimer) unused1;
|
|
|
|
PTR(OSTimer) unused2;
|
|
|
|
OSTime interval;
|
|
|
|
OSTime timestamp;
|
|
|
|
PTR(OSMesgQueue) mq;
|
|
|
|
OSMesg msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AddTimerAction {
|
2023-10-23 21:32:30 +02:00
|
|
|
PTR(OSTimer) timer;
|
2023-02-20 04:27:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RemoveTimerAction {
|
|
|
|
PTR(OSTimer) timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
using Action = std::variant<AddTimerAction, RemoveTimerAction>;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
std::thread thread;
|
|
|
|
moodycamel::BlockingConcurrentQueue<Action> action_queue{};
|
|
|
|
} timer_context;
|
|
|
|
|
|
|
|
uint64_t duration_to_ticks(std::chrono::system_clock::duration duration) {
|
|
|
|
uint64_t delta_micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
|
|
|
|
// More accurate than using a floating point timer, will only overflow after running for 12.47 years
|
|
|
|
// Units: (micros * (counts/millis)) / (micros/millis) = counts
|
|
|
|
uint64_t total_count = (delta_micros * counter_per_ms) / 1000;
|
|
|
|
|
|
|
|
return total_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::microseconds ticks_to_duration(uint64_t ticks) {
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
return ticks * 1000us / counter_per_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::system_clock::time_point ticks_to_timepoint(uint64_t ticks) {
|
2023-11-04 17:45:22 +01:00
|
|
|
return start_time + ticks_to_duration(ticks);
|
2023-02-20 04:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t time_now() {
|
2023-11-04 17:45:22 +01:00
|
|
|
return duration_to_ticks(std::chrono::system_clock::now() - start_time);
|
2023-02-20 04:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void timer_thread(RDRAM_ARG1) {
|
2023-11-13 01:40:02 +01:00
|
|
|
ultramodern::set_native_thread_name("Timer Thread");
|
|
|
|
ultramodern::set_native_thread_priority(ultramodern::ThreadPriority::VeryHigh);
|
2023-07-07 20:21:06 +02:00
|
|
|
|
2023-02-20 04:27:35 +01:00
|
|
|
// Lambda comparator function to keep the set ordered
|
|
|
|
auto timer_sort = [PASS_RDRAM1](PTR(OSTimer) a_, PTR(OSTimer) b_) {
|
|
|
|
OSTimer* a = TO_PTR(OSTimer, a_);
|
|
|
|
OSTimer* b = TO_PTR(OSTimer, b_);
|
|
|
|
|
|
|
|
// Order by timestamp if the timers have different timestamps
|
|
|
|
if (a->timestamp != b->timestamp) {
|
|
|
|
return a->timestamp < b->timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they have the exact same timestamp then order by address instead
|
|
|
|
return a < b;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ordered set of timers that are currently active
|
|
|
|
std::set<PTR(OSTimer), decltype(timer_sort)> active_timers{timer_sort};
|
|
|
|
|
|
|
|
// Lambda to process a timer action to handle adding and removing timers
|
|
|
|
auto process_timer_action = [&](const Action& action) {
|
|
|
|
// Determine the action type and act on it
|
|
|
|
if (const auto* add_action = std::get_if<AddTimerAction>(&action)) {
|
|
|
|
active_timers.insert(add_action->timer);
|
|
|
|
} else if (const auto* remove_action = std::get_if<RemoveTimerAction>(&action)) {
|
|
|
|
active_timers.erase(remove_action->timer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Empty the action queue
|
|
|
|
Action cur_action;
|
|
|
|
while (timer_context.action_queue.try_dequeue(cur_action)) {
|
|
|
|
process_timer_action(cur_action);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no timer to act on, wait for one to come in from the action queue
|
|
|
|
while (active_timers.empty()) {
|
|
|
|
timer_context.action_queue.wait_dequeue(cur_action);
|
|
|
|
process_timer_action(cur_action);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the timer that's closest to running out
|
|
|
|
PTR(OSTimer) cur_timer_ = *active_timers.begin();
|
|
|
|
OSTimer* cur_timer = TO_PTR(OSTimer, cur_timer_);
|
|
|
|
|
|
|
|
// Remove the timer from the queue (it may get readded if waiting is interrupted)
|
|
|
|
active_timers.erase(cur_timer_);
|
|
|
|
|
|
|
|
// Determine how long to wait to reach the timer's timestamp
|
|
|
|
auto wait_duration = ticks_to_timepoint(cur_timer->timestamp) - std::chrono::system_clock::now();
|
|
|
|
auto wait_us = std::chrono::duration_cast<std::chrono::microseconds>(wait_duration);
|
|
|
|
|
|
|
|
// Wait for either the duration to complete or a new action to come through
|
|
|
|
if (timer_context.action_queue.wait_dequeue_timed(cur_action, wait_duration)) {
|
|
|
|
// Timer was interrupted by a new action
|
|
|
|
// Add the current timer back to the queue (done first in case the action is to remove this timer)
|
|
|
|
active_timers.insert(cur_timer_);
|
|
|
|
// Process the new action
|
|
|
|
process_timer_action(cur_action);
|
2023-11-12 20:47:38 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-02-20 04:27:35 +01:00
|
|
|
// Waiting for the timer completed, so send the timer's message to its message queue
|
|
|
|
osSendMesg(PASS_RDRAM cur_timer->mq, cur_timer->msg, OS_MESG_NOBLOCK);
|
|
|
|
// If the timer has a specified interval then reload it with that value
|
|
|
|
if (cur_timer->interval != 0) {
|
|
|
|
cur_timer->timestamp = cur_timer->interval + time_now();
|
|
|
|
active_timers.insert(cur_timer_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 01:40:02 +01:00
|
|
|
void ultramodern::init_timers(RDRAM_ARG1) {
|
2023-02-20 04:27:35 +01:00
|
|
|
timer_context.thread = std::thread{ timer_thread, PASS_RDRAM1 };
|
2023-11-12 20:47:38 +01:00
|
|
|
timer_context.thread.detach();
|
2023-02-20 04:27:35 +01:00
|
|
|
}
|
|
|
|
|
2023-11-13 01:40:02 +01:00
|
|
|
uint32_t ultramodern::get_speed_multiplier() {
|
2023-02-20 04:27:35 +01:00
|
|
|
return speed_multiplier;
|
|
|
|
}
|
|
|
|
|
2023-11-13 01:40:02 +01:00
|
|
|
std::chrono::system_clock::time_point ultramodern::get_start() {
|
2023-11-04 17:45:22 +01:00
|
|
|
return start_time;
|
2023-02-20 04:27:35 +01:00
|
|
|
}
|
|
|
|
|
2023-11-13 01:40:02 +01:00
|
|
|
std::chrono::system_clock::duration ultramodern::time_since_start() {
|
2023-11-04 17:45:22 +01:00
|
|
|
return std::chrono::system_clock::now() - start_time;
|
2023-02-20 04:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" u32 osGetCount() {
|
|
|
|
uint64_t total_count = time_now();
|
|
|
|
|
|
|
|
// Allow for overflows, which is how osGetCount behaves
|
|
|
|
return (uint32_t)total_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" OSTime osGetTime() {
|
|
|
|
uint64_t total_count = time_now();
|
|
|
|
|
|
|
|
return total_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int osSetTimer(RDRAM_ARG PTR(OSTimer) t_, OSTime countdown, OSTime interval, PTR(OSMesgQueue) mq, OSMesg msg) {
|
|
|
|
OSTimer* t = TO_PTR(OSTimer, t_);
|
|
|
|
|
|
|
|
// Determine the time when this timer will trigger off
|
|
|
|
if (countdown == 0) {
|
|
|
|
// Set the timestamp based on the interval
|
|
|
|
t->timestamp = interval + time_now();
|
|
|
|
} else {
|
|
|
|
t->timestamp = countdown + time_now();
|
|
|
|
}
|
|
|
|
t->interval = interval;
|
|
|
|
t->mq = mq;
|
|
|
|
t->msg = msg;
|
|
|
|
|
|
|
|
timer_context.action_queue.enqueue(AddTimerAction{ t_ });
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int osStopTimer(RDRAM_ARG PTR(OSTimer) t_) {
|
|
|
|
timer_context.action_queue.enqueue(RemoveTimerAction{ t_ });
|
|
|
|
|
|
|
|
// TODO don't blindly return 0 here; requires some response from the timer thread to know what the returned value was
|
|
|
|
return 0;
|
|
|
|
}
|