2023-02-20 04:27:35 +01:00
|
|
|
#ifndef __MULTILIBULTRA_HPP__
|
|
|
|
#define __MULTILIBULTRA_HPP__
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "ultra64.h"
|
|
|
|
|
2023-10-23 21:32:30 +02:00
|
|
|
#if defined(_WIN32)
|
2023-10-23 23:51:21 +02:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# define NOMINMAX
|
2023-10-23 21:32:30 +02:00
|
|
|
# include <Windows.h>
|
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
# include "android/native_window.h"
|
|
|
|
#elif defined(__linux__)
|
|
|
|
# include "X11/Xlib.h"
|
|
|
|
# undef None
|
|
|
|
# undef Status
|
|
|
|
# undef LockMask
|
|
|
|
#endif
|
|
|
|
|
2023-02-20 04:27:35 +01:00
|
|
|
struct UltraThreadContext {
|
|
|
|
std::thread host_thread;
|
2023-10-23 21:32:30 +02:00
|
|
|
std::atomic_bool scheduled;
|
|
|
|
std::atomic_bool descheduled;
|
2023-02-20 04:27:35 +01:00
|
|
|
std::atomic_bool initialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace Multilibultra {
|
|
|
|
|
2023-10-23 21:32:30 +02:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// Native HWND handle to the target window.
|
|
|
|
using WindowHandle = HWND;
|
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
using WindowHandle = ANativeWindow*;
|
|
|
|
#elif defined(__linux__)
|
|
|
|
struct WindowHandle {
|
|
|
|
Display* display;
|
|
|
|
Window window;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2023-02-20 04:27:35 +01:00
|
|
|
// We need a place in rdram to hold the PI handles, so pick an address in extended rdram
|
|
|
|
constexpr int32_t cart_handle = 0x80800000;
|
|
|
|
constexpr int32_t flash_handle = (int32_t)(cart_handle + sizeof(OSPiHandle));
|
|
|
|
constexpr uint32_t save_size = 1024 * 1024 / 8; // Maximum save size, 1Mbit for flash
|
|
|
|
|
2023-10-23 21:32:30 +02:00
|
|
|
void preinit(uint8_t* rdram, uint8_t* rom, WindowHandle window_handle);
|
2023-02-20 04:27:35 +01:00
|
|
|
void save_init();
|
|
|
|
void init_scheduler();
|
2023-10-23 21:32:30 +02:00
|
|
|
void init_events(uint8_t* rdram, uint8_t* rom, WindowHandle window_handle);
|
2023-02-20 04:27:35 +01:00
|
|
|
void init_timers(RDRAM_ARG1);
|
2023-10-23 21:32:30 +02:00
|
|
|
void yield_self(RDRAM_ARG1);
|
|
|
|
void block_self(RDRAM_ARG1);
|
|
|
|
void unblock_thread(OSThread* t);
|
2023-02-20 04:27:35 +01:00
|
|
|
void wait_for_resumed(RDRAM_ARG1);
|
|
|
|
void swap_to_thread(RDRAM_ARG OSThread *to);
|
2023-10-23 21:32:30 +02:00
|
|
|
void resume_thread_impl(OSThread* t);
|
2023-02-20 04:27:35 +01:00
|
|
|
void schedule_running_thread(OSThread *t);
|
2023-10-23 21:32:30 +02:00
|
|
|
void halt_self(RDRAM_ARG1);
|
|
|
|
void stop_thread(OSThread *t);
|
2023-02-20 04:27:35 +01:00
|
|
|
void cleanup_thread(OSThread *t);
|
2023-07-07 20:21:06 +02:00
|
|
|
|
|
|
|
enum class ThreadPriority {
|
|
|
|
Low,
|
|
|
|
Normal,
|
|
|
|
High,
|
|
|
|
VeryHigh,
|
|
|
|
Critical
|
|
|
|
};
|
|
|
|
|
|
|
|
void set_native_thread_name(const std::string& name);
|
|
|
|
void set_native_thread_priority(ThreadPriority pri);
|
2023-02-20 04:27:35 +01:00
|
|
|
PTR(OSThread) this_thread();
|
|
|
|
void notify_scheduler();
|
|
|
|
void reprioritize_thread(OSThread *t, OSPri pri);
|
|
|
|
void set_main_thread();
|
|
|
|
bool is_game_thread();
|
|
|
|
void submit_rsp_task(RDRAM_ARG PTR(OSTask) task);
|
|
|
|
void send_si_message();
|
|
|
|
uint32_t get_speed_multiplier();
|
|
|
|
std::chrono::system_clock::time_point get_start();
|
|
|
|
std::chrono::system_clock::duration time_since_start();
|
2023-06-23 00:29:55 +02:00
|
|
|
|
|
|
|
// Audio
|
2023-02-20 04:27:35 +01:00
|
|
|
void init_audio();
|
|
|
|
void set_audio_frequency(uint32_t freq);
|
|
|
|
void queue_audio_buffer(RDRAM_ARG PTR(s16) audio_data, uint32_t byte_count);
|
|
|
|
uint32_t get_remaining_audio_bytes();
|
|
|
|
|
2023-06-23 00:29:55 +02:00
|
|
|
struct audio_callbacks_t {
|
|
|
|
using queue_samples_t = void(int16_t*, size_t);
|
|
|
|
using get_samples_remaining_t = size_t();
|
|
|
|
using set_frequency_t = void(uint32_t);
|
|
|
|
queue_samples_t* queue_samples;
|
2023-10-23 21:32:30 +02:00
|
|
|
get_samples_remaining_t* get_frames_remaining;
|
2023-06-23 00:29:55 +02:00
|
|
|
set_frequency_t* set_frequency;
|
|
|
|
};
|
|
|
|
void set_audio_callbacks(const audio_callbacks_t* callbacks);
|
|
|
|
|
|
|
|
// Input
|
|
|
|
struct input_callbacks_t {
|
|
|
|
using get_input_t = void(uint16_t*, float*, float*);
|
|
|
|
get_input_t* get_input;
|
|
|
|
};
|
|
|
|
void set_input_callbacks(const input_callbacks_t* callback);
|
|
|
|
|
2023-10-23 21:32:30 +02:00
|
|
|
struct gfx_callbacks_t {
|
|
|
|
using gfx_data_t = void*;
|
|
|
|
using create_gfx_t = gfx_data_t();
|
|
|
|
using create_window_t = WindowHandle(gfx_data_t);
|
|
|
|
using update_gfx_t = void(gfx_data_t);
|
|
|
|
create_gfx_t* create_gfx;
|
|
|
|
create_window_t* create_window;
|
|
|
|
update_gfx_t* update_gfx;
|
2023-02-20 04:27:35 +01:00
|
|
|
};
|
2023-10-23 21:32:30 +02:00
|
|
|
void set_gfx_callbacks(const gfx_callbacks_t* callbacks);
|
2023-02-20 04:27:35 +01:00
|
|
|
|
|
|
|
} // namespace Multilibultra
|
|
|
|
|
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
|
2023-10-23 21:32:30 +02:00
|
|
|
//#define debug_printf(...)
|
|
|
|
#define debug_printf(...) printf(__VA_ARGS__);
|
2023-02-20 04:27:35 +01:00
|
|
|
|
|
|
|
#endif
|