From 418d2ac8ce9e902c49d1a3164e3dfb4a1211992b Mon Sep 17 00:00:00 2001 From: Ced2911 Date: Tue, 3 Dec 2013 18:09:16 +0100 Subject: [PATCH] [xbox] 360 support for common --- Common/Atomic_Win32.h | 4 ++++ Common/Common.h | 2 +- Common/CommonFuncs.h | 5 ++++- Common/CommonWindows.h | 10 ++++++++++ Common/ConsoleListener.cpp | 30 +++++++++++++++------------- Common/ConsoleListener.h | 4 ++-- Common/FileUtil.cpp | 20 +++++++++++++++++++ Common/KeyMap.cpp | 4 ++-- Common/LogManager.cpp | 6 +++--- Common/MemArena.cpp | 41 +++++++++++++++++++++++++++++++++----- Common/MemoryUtil.cpp | 1 - Common/Misc.cpp | 5 ++++- Common/MsgHandler.cpp | 3 +-- Common/Timer.cpp | 10 +++++++--- Common/stdafx.h | 4 ---- 15 files changed, 110 insertions(+), 39 deletions(-) diff --git a/Common/Atomic_Win32.h b/Common/Atomic_Win32.h index 87ddc8a82d..ce2016b82c 100644 --- a/Common/Atomic_Win32.h +++ b/Common/Atomic_Win32.h @@ -19,7 +19,11 @@ #define _ATOMIC_WIN32_H_ #include "Common.h" +#ifndef _XBOX #include +#else +#include +#endif #include "CommonWindows.h" // Atomic operations are performed in a single step by the CPU. It is diff --git a/Common/Common.h b/Common/Common.h index c0ee1ca424..9fdf41080f 100644 --- a/Common/Common.h +++ b/Common/Common.h @@ -147,7 +147,7 @@ private: # elif defined __SSE2__ # define _M_SSE 0x200 # endif -#elif ((_MSC_VER >= 1500) || __INTEL_COMPILER) // Visual Studio 2008 +#elif ((_MSC_VER >= 1500) || __INTEL_COMPILER) && !defined(_XBOX) // Visual Studio 2008 # define _M_SSE 0x402 #endif diff --git a/Common/CommonFuncs.h b/Common/CommonFuncs.h index 73a0a3cfda..1214d5be89 100644 --- a/Common/CommonFuncs.h +++ b/Common/CommonFuncs.h @@ -84,7 +84,7 @@ inline u64 __rotr64(u64 x, unsigned int shift){ #define stat64 _stat64 #define fstat64 _fstat64 #define fileno _fileno - +#ifndef _XBOX #if _M_IX86 #define Crash() {__asm int 3} #else @@ -93,6 +93,9 @@ extern "C" { } #define Crash() {DebugBreak();} #endif // M_IX86 +#else + #define Crash() {DebugBreak();} +#endif // _XBOX ndef #endif // WIN32 ndef // Generic function to get last error message. diff --git a/Common/CommonWindows.h b/Common/CommonWindows.h index 367223bb8b..2780a8dfed 100644 --- a/Common/CommonWindows.h +++ b/Common/CommonWindows.h @@ -10,6 +10,16 @@ #ifdef _XBOX #include + +extern "C" void _ReadWriteBarrier(); +#pragma intrinsic(_ReadWriteBarrier) + +extern "C" void _WriteBarrier(); +#pragma intrinsic(_WriteBarrier) + +extern "C" void _ReadBarrier(); +#pragma intrinsic(_ReadBarrier) + #else #include #endif diff --git a/Common/ConsoleListener.cpp b/Common/ConsoleListener.cpp index ea394eb5db..069270606f 100644 --- a/Common/ConsoleListener.cpp +++ b/Common/ConsoleListener.cpp @@ -32,7 +32,7 @@ #include "ConsoleListener.h" // Common #include "Atomics.h" -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) const int LOG_PENDING_MAX = 120 * 10000; const int LOG_LATENCY_DELAY_MS = 20; const int LOG_SHUTDOWN_DELAY_MS = 250; @@ -50,7 +50,7 @@ volatile u32 ConsoleListener::logPendingWritePos = 0; ConsoleListener::ConsoleListener() : bHidden(true) { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) hConsole = NULL; bUseColor = true; @@ -61,6 +61,8 @@ ConsoleListener::ConsoleListener() : bHidden(true) logPending = new char[LOG_PENDING_MAX]; } ++refCount; +#elif defined(_XBOX) + bUseColor = false; #else bUseColor = isatty(fileno(stdout)); #endif @@ -101,7 +103,7 @@ bool WINAPI ConsoleHandler(DWORD msgType) // Name is the window title void ConsoleListener::Init(bool AutoOpen, int Width, int Height, const char *Title) { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) openWidth_ = Width; openHeight_ = Height; title_ = ConvertUTF8ToWString(Title); @@ -113,7 +115,7 @@ void ConsoleListener::Init(bool AutoOpen, int Width, int Height, const char *Tit void ConsoleListener::Open() { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) if (!GetConsoleWindow()) { // Open the console window and create the window handle for GetStdHandle() @@ -147,7 +149,7 @@ void ConsoleListener::Open() void ConsoleListener::Show(bool bShow) { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) if (bShow && bHidden) { if (!IsOpen()) @@ -166,7 +168,7 @@ void ConsoleListener::Show(bool bShow) void ConsoleListener::UpdateHandle() { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) hConsole = GetStdHandle(STD_OUTPUT_HANDLE); #endif } @@ -174,7 +176,7 @@ void ConsoleListener::UpdateHandle() // Close the console window and close the eventual file handle void ConsoleListener::Close() { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) if (hConsole == NULL) return; @@ -212,7 +214,7 @@ void ConsoleListener::Close() bool ConsoleListener::IsOpen() { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) return (hConsole != NULL); #else return true; @@ -226,7 +228,7 @@ bool ConsoleListener::IsOpen() void ConsoleListener::BufferWidthHeight(int BufferWidth, int BufferHeight, int ScreenWidth, int ScreenHeight, bool BufferFirst) { _dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console."); -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) BOOL SB, SW; if (BufferFirst) { @@ -251,7 +253,7 @@ void ConsoleListener::BufferWidthHeight(int BufferWidth, int BufferHeight, int S void ConsoleListener::LetterSpace(int Width, int Height) { _dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console."); -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) // Get console info CONSOLE_SCREEN_BUFFER_INFO ConInfo; GetConsoleScreenBufferInfo(hConsole, &ConInfo); @@ -277,7 +279,7 @@ void ConsoleListener::LetterSpace(int Width, int Height) #endif } -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) COORD ConsoleListener::GetCoordinates(int BytesRead, int BufferWidth) { COORD Ret = {0, 0}; @@ -502,7 +504,7 @@ void ConsoleListener::WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Tex void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool Resize) { _dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console."); -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) // Check size if (Width < 8 || Height < 12) return; @@ -589,7 +591,7 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text) { -#if defined(_WIN32) +#if defined(_WIN32) && !defined(_XBOX) if (hThread == NULL && IsOpen()) WriteToConsole(Level, Text, strlen(Text)); else @@ -623,7 +625,7 @@ void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text) void ConsoleListener::ClearScreen(bool Cursor) { _dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console."); -#if defined(_WIN32) +#if defined(_WIN32) && !defined(_XBOX) COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; diff --git a/Common/ConsoleListener.h b/Common/ConsoleListener.h index 4efcca9280..b2b5acd8d0 100644 --- a/Common/ConsoleListener.h +++ b/Common/ConsoleListener.h @@ -38,7 +38,7 @@ public: void LetterSpace(int Width, int Height); void BufferWidthHeight(int BufferWidth, int BufferHeight, int ScreenWidth, int ScreenHeight, bool BufferFirst); void PixelSpace(int Left, int Top, int Width, int Height, bool); -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) COORD GetCoordinates(int BytesRead, int BufferWidth); #endif void Log(LogTypes::LOG_LEVELS, const char *Text); @@ -47,7 +47,7 @@ public: void Show(bool bShow); bool Hidden() const { return bHidden; } private: -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) HWND GetHwnd(void); HANDLE hConsole; diff --git a/Common/FileUtil.cpp b/Common/FileUtil.cpp index 6756bbc653..b46d515874 100644 --- a/Common/FileUtil.cpp +++ b/Common/FileUtil.cpp @@ -20,9 +20,11 @@ #ifdef _WIN32 #include "CommonWindows.h" +#ifndef _XBOX #include // for SHGetFolderPath #include #include // for GetSaveFileName +#endif #include #include // getcwd #else @@ -638,6 +640,7 @@ void CopyDir(const std::string &source_path, const std::string &dest_path) std::string GetCurrentDir() { char *dir; +#ifndef _XBOX // Get the current working directory (getcwd uses malloc) if (!(dir = __getcwd(NULL, 0))) { @@ -648,12 +651,19 @@ std::string GetCurrentDir() std::string strDir = dir; free(dir); return strDir; +#else + return "game:\\"; +#endif } // Sets the current directory to the given directory bool SetCurrentDir(const std::string &directory) { +#ifndef _XBOX return __chdir(directory.c_str()) == 0; +#else + return false; +#endif } const std::string &GetExeDirectory() @@ -661,6 +671,7 @@ const std::string &GetExeDirectory() static std::string ExePath; if (ExePath.empty()) +#ifndef _XBOX { #ifdef _WIN32 TCHAR program_path[4096] = {0}; @@ -697,6 +708,10 @@ const std::string &GetExeDirectory() } return ExePath; +#else + static std::wstring ExePath = L"game:\\"; + return ExePath; +#endif } @@ -789,6 +804,7 @@ bool IOFile::Flush() bool IOFile::Resize(u64 size) { +#ifndef _XBOX if (!IsOpen() || 0 != #ifdef _WIN32 // ector: _chsize sucks, not 64-bit safe @@ -802,6 +818,10 @@ bool IOFile::Resize(u64 size) m_good = false; return m_good; +#else + // TODO: Implement. + return false; +#endif } } // namespace diff --git a/Common/KeyMap.cpp b/Common/KeyMap.cpp index 1200e49ea4..b6b261b444 100644 --- a/Common/KeyMap.cpp +++ b/Common/KeyMap.cpp @@ -15,7 +15,7 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) #include #endif @@ -336,7 +336,7 @@ void SetDefaultKeyMap(DefaultMaps dmap, bool replace) { { bool azerty = false; bool qwertz = false; -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) HKL localeId = GetKeyboardLayout(0); // TODO: Is this list complete enough? switch ((int)localeId & 0xFFFF) { diff --git a/Common/LogManager.cpp b/Common/LogManager.cpp index 89539f57c1..af78f57803 100644 --- a/Common/LogManager.cpp +++ b/Common/LogManager.cpp @@ -94,7 +94,7 @@ LogManager::LogManager() { } // Remove file logging on small devices -#if !defined(MOBILE_DEVICE) || defined(_DEBUG) +#if !(defined(MOBILE_DEVICE) || defined(_XBOX)) || defined(_DEBUG) fileLog_ = new FileLogListener(""); consoleLog_ = new ConsoleListener(); debuggerLog_ = new DebuggerLogListener(); @@ -106,10 +106,10 @@ LogManager::LogManager() { for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) { log_[i]->SetEnable(true); -#if !defined(MOBILE_DEVICE) || defined(_DEBUG) +#if !(defined(MOBILE_DEVICE) || defined(_XBOX)) || defined(_DEBUG) log_[i]->AddListener(fileLog_); log_[i]->AddListener(consoleLog_); -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(_XBOX) if (IsDebuggerPresent() && debuggerLog_ != NULL && LOG_MSC_OUTPUTDEBUG) log_[i]->AddListener(debuggerLog_); #endif diff --git a/Common/MemArena.cpp b/Common/MemArena.cpp index e9db9adf5c..cce900e430 100644 --- a/Common/MemArena.cpp +++ b/Common/MemArena.cpp @@ -108,7 +108,7 @@ std::string ram_temp_file = "/home/user/gc_mem.tmp"; #else std::string ram_temp_file = "/tmp/gc_mem.tmp"; #endif -#else +#elif !defined(_XBOX) SYSTEM_INFO sysInfo; #endif @@ -116,7 +116,11 @@ SYSTEM_INFO sysInfo; // Windows mappings need to be on 64K boundaries, due to Alpha legacy. #ifdef _WIN32 size_t roundup(size_t x) { +#ifndef _XBOX int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000; +#else + int gran = 0x10000; // 64k in 360 +#endif return (x + gran - 1) & ~(gran - 1); } #else @@ -129,8 +133,10 @@ size_t roundup(size_t x) { void MemArena::GrabLowMemSpace(size_t size) { #ifdef _WIN32 +#ifndef _XBOX hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)(size), NULL); GetSystemInfo(&sysInfo); +#endif #elif defined(ANDROID) // Use ashmem so we don't have to allocate a file on disk! fd = ashmem_create_region("PPSSPP_RAM", size); @@ -176,9 +182,16 @@ void MemArena::ReleaseSpace() void *MemArena::CreateView(s64 offset, size_t size, void *base) { #ifdef _WIN32 +#ifdef _XBOX + size = roundup(size); + // use 64kb pages + void * ptr = VirtualAlloc(NULL, size, MEM_COMMIT|MEM_LARGE_PAGES, PAGE_READWRITE); + return ptr; +#else size = roundup(size); void *ptr = MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base); return ptr; +#endif #else void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | // Do not sync memory to underlying file. Linux has this by default. @@ -202,7 +215,9 @@ void *MemArena::CreateView(s64 offset, size_t size, void *base) void MemArena::ReleaseView(void* view, size_t size) { #ifdef _WIN32 +#ifndef _XBOX UnmapViewOfFile(view); +#endif #elif defined(__SYMBIAN32__) memmap->Decommit(((int)view - (int)memmap->Base()) & 0x3FFFFFFF, size); #else @@ -276,6 +291,10 @@ static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32 size_t position = 0; size_t last_position = 0; +#if defined(_XBOX) + void *ptr; +#endif + // Zero all the pointers to be sure. for (int i = 0; i < num_views; i++) { @@ -300,6 +319,12 @@ static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32 arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size); } *(view.out_ptr) = (u8*)((int)arena->memmap->Base() + view.virtual_address & 0x3FFFFFFF); +#elif defined(_XBOX) + *(view.out_ptr_low) = (u8*)(base + view.virtual_address); + //arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size); + ptr = VirtualAlloc(base + (view.virtual_address & 0x3FFFFFFF), view.size, MEM_COMMIT, PAGE_READWRITE); + } + *(view.out_ptr) = (u8*)base + (view.virtual_address & 0x3FFFFFFF); #else *(view.out_ptr_low) = (u8*)arena->CreateView(position, view.size); if (!*view.out_ptr_low) @@ -383,8 +408,16 @@ u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena PanicAlert("MemoryMap_Setup: Failed finding a memory base."); return 0; } -#else -#ifdef _WIN32 +#elif defined(_XBOX) + // Reserve 256MB + u8 *base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE|MEM_LARGE_PAGES, PAGE_READWRITE); + if (!Memory_TryBase(base, views, num_views, flags, arena)) + { + PanicAlert("MemoryMap_Setup: Failed finding a memory base."); + exit(0); + return 0; + } +#elif defined(_WIN32) // Try a whole range of possible bases. Return once we got a valid one. u32 max_base_addr = 0x7FFF0000 - 0x10000000; u8 *base = NULL; @@ -418,8 +451,6 @@ u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena PanicAlert("MemoryMap_Setup: Failed finding a memory base."); return 0; } -#endif - #endif if (base_attempts) PanicAlert("No possible memory base pointer found!"); diff --git a/Common/MemoryUtil.cpp b/Common/MemoryUtil.cpp index 0ac3f07aad..a2f8f17f63 100644 --- a/Common/MemoryUtil.cpp +++ b/Common/MemoryUtil.cpp @@ -219,4 +219,3 @@ void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); #endif } - diff --git a/Common/Misc.cpp b/Common/Misc.cpp index 851e4b6a69..3750bf19a7 100644 --- a/Common/Misc.cpp +++ b/Common/Misc.cpp @@ -33,7 +33,7 @@ const char* GetLastErrorMsg() { static const size_t buff_size = 255; - +#ifndef _XBOX #ifdef _WIN32 static __declspec(thread) char err_str[buff_size] = {}; @@ -48,4 +48,7 @@ const char* GetLastErrorMsg() #endif return err_str; +#else + return "GetLastErrorMsg"; +#endif } diff --git a/Common/MsgHandler.cpp b/Common/MsgHandler.cpp index e554dc9beb..9ea581441a 100644 --- a/Common/MsgHandler.cpp +++ b/Common/MsgHandler.cpp @@ -67,7 +67,7 @@ bool MsgAlert(bool yes_no, int Style, const char* format, ...) // Default non library dependent panic alert bool MsgHandler(const char* caption, const char* text, bool yes_no, int Style) { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) int STYLE = MB_ICONINFORMATION; if (Style == QUESTION) STYLE = MB_ICONQUESTION; if (Style == WARNING) STYLE = MB_ICONWARNING; @@ -76,7 +76,6 @@ bool MsgHandler(const char* caption, const char* text, bool yes_no, int Style) std::wstring wcaption = ConvertUTF8ToWString(caption); return IDYES == MessageBox(0, wtext.c_str(), wcaption.c_str(), STYLE | (yes_no ? MB_YESNO : MB_OK)); - #else printf("%s\n", text); return true; diff --git a/Common/Timer.cpp b/Common/Timer.cpp index e4bb67d075..89f10db9e3 100644 --- a/Common/Timer.cpp +++ b/Common/Timer.cpp @@ -19,7 +19,9 @@ #ifdef _WIN32 #include "CommonWindows.h" +#ifndef _XBOX #include +#endif #include #else #include @@ -33,7 +35,9 @@ namespace Common u32 Timer::GetTimeMs() { -#ifdef _WIN32 +#ifdef _XBOX + return GetTickCount(); +#elif defined(_WIN32) return timeGetTime(); #elif defined(BLACKBERRY) struct timespec time; @@ -149,14 +153,14 @@ std::string Timer::GetTimeElapsedFormatted() const // Get current time void Timer::IncreaseResolution() { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) timeBeginPeriod(1); #endif } void Timer::RestoreResolution() { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(_XBOX) timeEndPeriod(1); #endif } diff --git a/Common/stdafx.h b/Common/stdafx.h index d8e0aca636..94ccaf4910 100644 --- a/Common/stdafx.h +++ b/Common/stdafx.h @@ -33,10 +33,6 @@ #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers -//#define _CRT_SECURE_NO_DEPRECATE 1 -//#define _CRT_NONSTDC_NO_DEPRECATE 1 - #include "CommonWindows.h" #include #include -