diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bbc54e..aafa263 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -236,7 +236,10 @@ src/agent/push_endpoint.c if(WOLFRAM_BUILD_WII) target_sources(wolfram PRIVATE src/platform/wii_platform.c src/openssl_compat.c) target_compile_definitions(wolfram PUBLIC WOLFRAM_WII) - target_include_directories(wolfram PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) + target_include_directories(wolfram PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src + ${LIBOGC}/include) + target_link_directories(wolfram PUBLIC ${LIBOGC}/lib/wii) + target_link_libraries(wolfram PUBLIC ogc) message(STATUS "Building for Nintendo Wii (WOLFRAM_BUILD_WII=ON)") elseif(WOLFRAM_BUILD_WIIU) target_sources(wolfram PRIVATE src/platform/wiiu_platform.c src/openssl_compat.c) diff --git a/include/wolfram/platform.h b/include/wolfram/platform.h index 07de800..77f1fcc 100644 --- a/include/wolfram/platform.h +++ b/include/wolfram/platform.h @@ -54,7 +54,8 @@ extern "C" { * One-time platform initialisation. * * Desktop: no-op (returns WF_OK). - * Wii: will initialise net_init(), VIDEO_Init(), etc. + * Wii: initialises libogc networking. Video and filesystems remain owned by + * the application. */ wf_status wf_platform_init(void); diff --git a/src/platform/wii_platform.c b/src/platform/wii_platform.c index 37c43da..5f2efa5 100644 --- a/src/platform/wii_platform.c +++ b/src/platform/wii_platform.c @@ -1,111 +1,67 @@ /** * wii_platform.c — Nintendo Wii platform implementation (libogc stubs). * - * Maps platform abstractions to libogc primitives. Currently contains - * stub implementations; TODO comments indicate what needs to be filled - * in when the Wii integration is built out. + * Maps platform abstractions to libogc primitives. Video and filesystem + * setup remain application concerns; Wolfram owns only networking, locking, + * and its monotonic clock. * * Build with devkitPPC: powerpc-eabi-gcc, linked against libogc. */ #include "wolfram/platform.h" +#include +#include +#include #include -#include - -/* TODO: #include — for LWP mutex */ -/* TODO: #include */ -/* TODO: #include — for VIDEO_Init */ -/* TODO: #include — for net_init */ /* ── Init / shutdown ────────────────────────────────────────────────── */ +static int wf_network_initialized = 0; + wf_status wf_platform_init(void) { - /* - * TODO: Implement when building the Wii integration: - * 1. VIDEO_Init() - * 2. net_init() — lwIP TCP/IP stack - * 3. Initialise SSL/TLS context (mbedTLS) - * 4. Mount SD card (fatMountSimple) - */ - return WF_ERR_NOT_IMPLEMENTED; + if (wf_network_initialized) return WF_OK; + if (net_init() < 0) return WF_ERR_NETWORK; + wf_network_initialized = 1; + return WF_OK; } void wf_platform_shutdown(void) { - /* - * TODO: Implement when building the Wii integration: - * 1. net_shutdown() - * 2. fatUnmount("sd:") - */ + if (!wf_network_initialized) return; + net_deinit(); + wf_network_initialized = 0; } /* ── Mutex ──────────────────────────────────────────────────────────── */ -/* - * TODO: Replace with lwp_mutex from libogc: - * #include - * - * struct wf_platform_mutex { mutex_t mtx; }; - * - * wf_platform_mutex *wf_platform_mutex_new(void) { - * wf_platform_mutex *m = calloc(1, sizeof(*m)); - * if (!m) return NULL; - * LWP_MutexInit(&m->mtx, false); - * return m; - * } - * - * void wf_platform_mutex_lock(wf_platform_mutex *m) { - * if (m) LWP_MutexLock(m->mtx); - * } - * - * void wf_platform_mutex_unlock(wf_platform_mutex *m) { - * if (m) LWP_MutexUnlock(m->mtx); - * } - * - * void wf_platform_mutex_free(wf_platform_mutex *m) { - * if (!m) return; - * LWP_MutexDestroy(m->mtx); - * free(m); - * } - */ - -struct wf_platform_mutex { int dummy; }; +struct wf_platform_mutex { mutex_t handle; }; wf_platform_mutex *wf_platform_mutex_new(void) { - /* Stub: no real backend yet — signal failure honestly. */ - return NULL; + wf_platform_mutex *mutex = calloc(1, sizeof(*mutex)); + if (!mutex) return NULL; + if (LWP_MutexInit(&mutex->handle, false) < 0) { + free(mutex); + return NULL; + } + return mutex; } void wf_platform_mutex_lock(wf_platform_mutex *m) { - (void)m; + if (m) LWP_MutexLock(m->handle); } void wf_platform_mutex_unlock(wf_platform_mutex *m) { - (void)m; + if (m) LWP_MutexUnlock(m->handle); } void wf_platform_mutex_free(wf_platform_mutex *m) { + if (!m) return; + LWP_MutexDestroy(m->handle); free(m); } /* ── Time ───────────────────────────────────────────────────────────── */ -/* - * TODO: Replace with libogc timer: - * #include - * - * uint64_t wf_platform_time_micros(void) { - * // gettime() returns TB ticks; ticks_to_microsecs() converts - * return ticks_to_microsecs(gettime()); - * } - * - * The Wii's Timebase (TB) counter runs at 162.5 MHz on NTSC (144 MHz PAL) - * and provides monotonically increasing ticks. ticks_to_microsecs() - * from libogc handles the conversion. - */ - uint64_t wf_platform_time_micros(void) { - /* Stub: return 0. TID generation will not produce unique values - * until this is implemented. */ - return 0; + return ticks_to_microsecs(gettime()); } diff --git a/src/platform/wii_tid.c b/src/platform/wii_tid.c index 4eb4426..32144a8 100644 --- a/src/platform/wii_tid.c +++ b/src/platform/wii_tid.c @@ -58,12 +58,6 @@ static uint64_t wf_tid_last_ts = 0; static uint32_t wf_tid_clock = 0; static int wf_tid_initialized = 0; -/* - * TODO: When wii_platform.c implements wf_platform_time_micros() with - * libogc's gettime()/ticks_to_microsecs(), this will produce real - * microsecond timestamps. Currently returns 0 (stub), so TIDs will - * sort by clockid only. - */ static uint64_t wf_tid_now_micros(void) { return wf_platform_time_micros(); }