Skip to content
20 changes: 16 additions & 4 deletions Core/Intrinsics.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <intrin.h>

// TODO Eliminate this
#include <memory.h>

Expand All @@ -8,6 +6,9 @@ GLOBAL char CPU_BRAND_STRING[0x40] = { "N/A (__cpuid intrinsic not yet supported

#ifdef RAGLITE_COMPILER_MSVC

#include <intrin.h>

// TODO: Should look into whether (and how much) dllimport improves performance?
#define EXPORT extern "C" __declspec(dllexport)

INTERNAL void IntrinsicsReadCPUID() {
Expand All @@ -25,14 +26,25 @@ INTERNAL void IntrinsicsReadCPUID() {
__cpuid((int*)CPU_INFO_MASK, 0x80000004);
memcpy(CPU_BRAND_STRING + 32, CPU_INFO_MASK, sizeof(CPU_INFO_MASK));
}
}

#define DebugTrap() __debugbreak();

#else
// TODO Support for the other toolchains
#endif

#include <cpuid.h>

// TODO: Only relevant if the build script (that doesn't currently exist) uses -fvisibility=hidden
#define EXPORT extern "C" __attribute__((visibility("default")))

INTERNAL void IntrinsicsReadCPUID() {
// NYI: Probably need to rewrite this anyway, so for now just leave it as a placeholder
}

#define DebugTrap() __builtin_trap();

#endif

// TODO: typeof(x) could simplify this - look into toolchain support/extensions?
#define Swap(first, second, type) \
do { \
Expand Down
51 changes: 51 additions & 0 deletions Core/Platforms/Linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ABOUT: This is a placeholder program that has nothing in common with the Win32 platform runtime (delete when porting)

#include <cpuid.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>

void DebugPrintASCII(unsigned int value) {
for(int i = 0; i < 4; i++) {
char byte = (value >> (i * 8)) & 0xFF;
printf("%c", byte);
}
}

// See https://en.wikipedia.org/wiki/CPUID and https://github.com/gcc-mirror/gcc/blob/master/gcc/config/i386/cpuid.h
INTERNAL void DebugDumpCPUID() {
unsigned int eax, ebx, ecx, edx;
// EAX=0: Highest Function Parameter and Manufacturer ID
uint EAX_input = 0;
unsigned int ret = __get_cpuid(EAX_input, &eax, &ebx, &ecx, &edx);
if(ret != 1) {
errx(EXIT_FAILURE, "Failed to call CPUID with EAX=%d", EAX_input);
}

printf("CPU Manufacturer ID: ");
DebugPrintASCII(ebx);
DebugPrintASCII(edx);
DebugPrintASCII(ecx);
printf("\n");
}

INTERNAL void PlatformRuntimeMain() {
printf("There is no platform runtime for Linux yet. Instead, behold this placeholder program :3\n");

DebugDumpCPUID();
// TODO: Maybe dump some of the other CPU details also? Not really useful right now...

unsigned eax = 0;
unsigned ebx = 0;
unsigned ecx = 0;
unsigned edx = 0;

int r = __get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx);
if(!r) {
fprintf(stderr, "cpuid leaf 7 unsupported ...\n");
return;
}
printf("cpuid.07h.0: eax=0x%x ebx=0x%x ecx=0x%x edx=0x%x\n",
eax, ebx, ecx, edx);
}
6 changes: 6 additions & 0 deletions Core/Platforms/MacOS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ABOUT: This is a placeholder program that has nothing in common with the Win32 platform runtime (delete when porting)

#include <stdio.h>
INTERNAL void PlatformRuntimeMain() {
printf("This is a placeholder program; there's no OSX platform runtime yet...\n");
}
8 changes: 3 additions & 5 deletions Core/Platforms/Win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ LRESULT CALLBACK MainWindowProcessIncomingMessage(HWND window, UINT message, WPA
return result;
}

int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR,
int) {
INTERNAL void PlatformRuntimeMain() {
HINSTANCE instance = GetModuleHandle(NULL);
hardware_tick_t applicationStartTime = PerformanceMetricsNow();

IntrinsicsReadCPUID();
Expand Down Expand Up @@ -326,7 +326,7 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR,
GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);

ATOM classGUID = RegisterClassEx(&windowClass);
ASSUME(classGUID != NULL, "Failed to register window class (...really?)");
ASSUME(classGUID, "Failed to register window class (...really?)");

// TODO: On modern Windows systems, MAX_PATH is insufficient and Unicode paths should ideally be supported (later?)
TCHAR executableFileSystemPath[MAX_PATH];
Expand Down Expand Up @@ -385,6 +385,4 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR,
}

timeEndPeriod(requestedSchedulerGranularityInMilliseconds);

return 0;
}
16 changes: 15 additions & 1 deletion Core/RagLite2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,18 @@ GLOBAL memory_arena_t TRANSIENT_MEMORY = {};

#ifdef RAGLITE_PLATFORM_WINDOWS
#include "Platforms/Win32.cpp"
#endif
#elifdef RAGLITE_PLATFORM_MACOS
#include "Platforms/MacOS.cpp"
#elifdef RAGLITE_PLATFORM_LINUX
#include "Platforms/Linux.cpp"
#endif

#ifdef RAGLITE_PLATFORM_WINDOWS
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
#else
int main()
#endif
{
PlatformRuntimeMain();
return 0;
}
18 changes: 9 additions & 9 deletions Core/RagLite2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
#error "Unsupported Platform: OS-specific code paths have yet to be ported"
#endif

#define RAGLITE_COMPILER_GCC 0
#define RAGLITE_COMPILER_LLVM 0
#define RAGLITE_COMPILER_MSVC 0

#ifdef _MSC_VER
#undef RAGLITE_COMPILER_MSVC
#define RAGLITE_COMPILER_MSVC 1
// NOTE: Should probably use feature detection macros, but for now assume the latest (tested) version will work
#if defined(__clang__)
#define RAGLITE_COMPILER_CLANG
#elif defined(_MSC_VER)
#define RAGLITE_COMPILER_MSVC
#elif defined(__GNUC__)
#define RAGLITE_COMPILER_GCC 1
#else
#define RAGLITE_UNSUPPORTED_COMPILER
#endif
Expand All @@ -44,10 +44,10 @@
#define EXPAND_AS_STRING(x) #x
#define TOSTRING(x) EXPAND_AS_STRING(x)

constexpr size_t BITS_PER_BYTE = 8ULL;
constexpr int BITS_PER_BYTE = 8;
#define Bits(bits) ((bits) / BITS_PER_BYTE)

constexpr size_t PLATFORM_POINTER_SIZE = sizeof(void*);
constexpr int PLATFORM_POINTER_SIZE = sizeof(void*);
static_assert(PLATFORM_POINTER_SIZE == Bits(64), "Only 64-bit platforms are currently supported");

#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
Expand Down
6 changes: 6 additions & 0 deletions msys2build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# NOTE: This is not a full port of the MSVC build script, but rather a placeholder for local testing.
# NOTE: Eventually, a proper (more portable) solution will be required. But not today... so this is all there is

mkdir -p BuildArtifacts
RUNTIME_LIBS="-l gdi32 -l shlwapi -l user32 -l xinput -l winmm -l imagehlp -l ws2_32"
gcc Core/RagLite2.cpp -o BuildArtifacts/RagLiteMSYS2.exe $RUNTIME_LIBS
6 changes: 6 additions & 0 deletions unixbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# NOTE: This is not a full port of the MSVC build script, but rather a placeholder for local testing.
# NOTE: Eventually, a proper (more portable) solution will be required. But not today... so this is all there is

mkdir -p BuildArtifacts
RUNTIME_LIBS=""
gcc Core/RagLite2.cpp -o BuildArtifacts/RagLite2 $RUNTIME_LIBS -lm -fvisibility=hidden
Loading