#pragma once #include "Utilities/BitField.h" // Return Codes enum CellSyncError : u32 { CELL_SYNC_ERROR_AGAIN = 0x80410101, CELL_SYNC_ERROR_INVAL = 0x80410102, CELL_SYNC_ERROR_NOSYS = 0x80410103, CELL_SYNC_ERROR_NOMEM = 0x80410104, CELL_SYNC_ERROR_SRCH = 0x80410105, CELL_SYNC_ERROR_NOENT = 0x80410106, CELL_SYNC_ERROR_NOEXEC = 0x80410107, CELL_SYNC_ERROR_DEADLK = 0x80410108, CELL_SYNC_ERROR_PERM = 0x80410109, CELL_SYNC_ERROR_BUSY = 0x8041010A, CELL_SYNC_ERROR_ABORT = 0x8041010C, CELL_SYNC_ERROR_FAULT = 0x8041010D, CELL_SYNC_ERROR_CHILD = 0x8041010E, CELL_SYNC_ERROR_STAT = 0x8041010F, CELL_SYNC_ERROR_ALIGN = 0x80410110, CELL_SYNC_ERROR_NULL_POINTER = 0x80410111, CELL_SYNC_ERROR_NOT_SUPPORTED_THREAD = 0x80410112, CELL_SYNC_ERROR_NO_NOTIFIER = 0x80410113, CELL_SYNC_ERROR_NO_SPU_CONTEXT_STORAGE = 0x80410114, }; enum CellSyncError1 : u32 { CELL_SYNC_ERROR_SHOTAGE = 0x80410112, CELL_SYNC_ERROR_UNKNOWNKEY = 0x80410113, }; struct CellSyncMutex { struct alignas(4) Counter { be_t rel; be_t acq; auto lock_begin() { return acq++; } bool try_lock() { if (UNLIKELY(rel != acq)) { return false; } acq++; return true; } void unlock() { rel++; } }; atomic_t ctrl; }; CHECK_SIZE_ALIGN(CellSyncMutex, 4, 4); struct CellSyncBarrier { struct alignas(4) ctrl_t { be_t value; be_t count; }; atomic_t ctrl; static inline bool try_notify(ctrl_t& ctrl) { if (ctrl.value & 0x8000) { return false; } if (++ctrl.value == ctrl.count) { ctrl.value |= 0x8000; } return true; } static inline bool try_wait(ctrl_t& ctrl) { if ((ctrl.value & 0x8000) == 0) { return false; } if (--ctrl.value == -0x8000) { ctrl.value = 0; } return true; } }; CHECK_SIZE_ALIGN(CellSyncBarrier, 4, 4); struct alignas(16) CellSyncRwm { struct alignas(4) ctrl_t { be_t readers; be_t writers; }; atomic_t ctrl; be_t size; vm::bptr buffer; static inline bool try_read_begin(ctrl_t& ctrl) { if (ctrl.writers) { return false; } ctrl.readers++; return true; } static inline bool try_read_end(ctrl_t& ctrl) { if (ctrl.readers == 0) { return false; } ctrl.readers--; return true; } static inline bool try_write_begin(ctrl_t& ctrl) { if (ctrl.writers) { return false; } ctrl.writers = 1; return true; } }; CHECK_SIZE_ALIGN(CellSyncRwm, 16, 16); struct alignas(32) CellSyncQueue { struct alignas(8) ctrl_t { union { be_t x0; bf_t, 0, 24> next; bf_t, 24, 8> _pop; }; union { be_t x4; bf_t, 0, 24> count; bf_t, 24, 8> _push; }; }; atomic_t ctrl; be_t size; be_t depth; vm::bptr buffer; be_t reserved; u32 check_depth() const { const auto data = ctrl.load(); if (data.next > depth || data.count > depth) { fmt::throw_exception("Invalid queue pointers" HERE); } return depth; } static inline bool try_push_begin(ctrl_t& ctrl, u32 depth, u32* position) { const u32 count = ctrl.count; if (ctrl._push || count + ctrl._pop >= depth) { return false; } *position = ctrl.next; ctrl.next = *position + 1 != depth ? *position + 1 : 0; ctrl.count = count + 1; ctrl._push = 1; return true; } static inline void push_end(ctrl_t& ctrl) { ctrl._push = 0; } static inline bool try_pop_begin(ctrl_t& ctrl, u32 depth, u32* position) { const u32 count = ctrl.count; if (ctrl._pop || count <= ctrl._push) { return false; } ctrl._pop = 1; *position = ctrl.next + depth - count; ctrl.count = count - 1; return true; } static inline bool try_peek_begin(ctrl_t& ctrl, u32 depth, u32* position) { const u32 count = ctrl.count; if (ctrl._pop || count <= ctrl._push) { return false; } ctrl._pop = 1; *position = ctrl.next + depth - count; return true; } static inline void pop_end(ctrl_t& ctrl) { ctrl._pop = 0; } static inline bool try_clear_begin_1(ctrl_t& ctrl) { if (ctrl._pop) { return false; } ctrl._pop = 1; return true; } static inline bool try_clear_begin_2(ctrl_t& ctrl) { if (ctrl._push) { return false; } ctrl._push = 1; return true; } }; CHECK_SIZE_ALIGN(CellSyncQueue, 32, 32); enum CellSyncQueueDirection : u32 // CellSyncLFQueueDirection { CELL_SYNC_QUEUE_SPU2SPU = 0, // SPU to SPU CELL_SYNC_QUEUE_SPU2PPU = 1, // SPU to PPU CELL_SYNC_QUEUE_PPU2SPU = 2, // PPU to SPU CELL_SYNC_QUEUE_ANY2ANY = 3, // SPU/PPU to SPU/PPU }; struct alignas(128) CellSyncLFQueue { struct alignas(8) pop1_t { be_t m_h1; be_t m_h2; be_t m_h3; be_t m_h4; }; struct pop2_t { be_t pack; }; struct alignas(4) pop3_t { be_t m_h1; be_t m_h2; }; struct alignas(8) push1_t { be_t m_h5; be_t m_h6; be_t m_h7; be_t m_h8; }; struct push2_t { be_t pack; }; struct alignas(4) push3_t { be_t m_h5; be_t m_h6; }; union // 0x0 { atomic_t pop1; atomic_t pop3; }; union // 0x8 { atomic_t push1; atomic_t push3; }; be_t m_size; // 0x10 be_t m_depth; // 0x14 vm::bcptr m_buffer; // 0x18 u8 m_bs[4]; // 0x20 be_t m_direction; // 0x24 CellSyncQueueDirection be_t m_v1; // 0x28 atomic_be_t init; // 0x2C atomic_t push2; // 0x30 be_t m_hs1[15]; // 0x32 atomic_t pop2; // 0x50 be_t m_hs2[15]; // 0x52 vm::bptr m_eaSignal; // 0x70 be_t m_v2; // 0x78 be_t m_eq_id; // 0x7C }; CHECK_SIZE_ALIGN(CellSyncLFQueue, 128, 128); // Prototypes error_code cellSyncLFQueueInitialize(vm::ptr queue, vm::cptr buffer, u32 size, u32 depth, u32 direction, vm::ptr eaSignal);