Skip to content

Commit

Permalink
add in Runtime files
Browse files Browse the repository at this point in the history
  • Loading branch information
shibbo committed Nov 21, 2024
1 parent 6918e29 commit 826199e
Show file tree
Hide file tree
Showing 13 changed files with 1,327 additions and 11 deletions.
17 changes: 8 additions & 9 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@
# Use for any additional files that should cause a re-configure when modified
config.reconfig_deps = []

# Optional numeric ID for decomp.me preset
# Can be overridden in libraries or objects
config.scratch_preset_id = None

# Base flags, common to most GC/Wii games.
# Generally leave untouched, with overrides added below.
cflags_base = [
Expand All @@ -181,7 +185,7 @@
"-enum int",
"-fp hardware",
"-Cpp_exceptions off",
"-O4,s",
"-O4,p",
"-inline auto",
'-pragma "cats off"',
'-pragma "warn_notinlined off"',
Expand All @@ -190,7 +194,7 @@
"-RTTI off",
"-fp_contract on",
"-str reuse",
"-multibyte", # For Wii compilers, replace with `-enc SJIS`
"-enc SJIS",
"-i include",
f"-i build/{config.version}/include",
f"-DVERSION={version_num}"
Expand Down Expand Up @@ -358,13 +362,8 @@
"-gccinc",
"-common off",
"-inline auto",
]

# REL flags
cflags_rel = [
*cflags_base,
"-sdata 0",
"-sdata2 0",
"-i libs/Runtime/include",
"-i libs/MSL_C/include"
]

config.linker_version = "GC/3.0a5"
Expand Down
2 changes: 1 addition & 1 deletion include/Game/LiveActor/LiveActor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LiveActor : public NameObj {
/// @param pName The new name assigned to the LiveActor.
LiveActor(const char *pName);

virtual inline ~LiveActor() {}
virtual inline ~LiveActor();

virtual void init(JMapInfoIter const &);
virtual void movement();
Expand Down
2 changes: 1 addition & 1 deletion src/Game/MapObj/BigFan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ namespace NrvBigFan {

BigFan::~BigFan() {

}
}
42 changes: 42 additions & 0 deletions src/Runtime/GCN_mem_alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extern void OSReport(const char *, ...);
extern void* OSGetArenaLo();
extern void* OSGetArenaHi();
extern void* OSInitAlloc(void *, void *, int);
extern volatile int __OSCurrHeap;

#define OSRoundUp32B(x) (((unsigned int)(x) + 32 - 1) & ~(32 - 1))
#define OSRoundDown32B(x) (((unsigned int)(x)) & ~(32 - 1))

static void InitDefaultHeap()
{
void* arenaLo;
void* arenaHi;

OSReport("GCN_Mem_Alloc.c : InitDefaultHeap. No Heap Available\n");
OSReport("Metrowerks CW runtime library initializing default heap\n");

arenaLo = OSGetArenaLo();
arenaHi = OSGetArenaHi();

arenaLo = OSInitAlloc(arenaLo, arenaHi, 1);
OSSetArenaLo(arenaLo);

arenaLo = (void*)OSRoundUp32B(arenaLo);
arenaHi = (void*)OSRoundDown32B(arenaHi);

OSSetCurrentHeap(OSCreateHeap(arenaLo, arenaHi));
OSSetArenaLo(arenaLo = arenaHi);
}

// unused
void __sys_alloc()
{
}

__declspec(weak) extern void __sys_free(void* ptr)
{
if (__OSCurrHeap == -1) {
InitDefaultHeap();
}
OSFreeToHeap(__OSCurrHeap, ptr);
}
37 changes: 37 additions & 0 deletions src/Runtime/Gecko_ExceptionPPC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <NMWException.h>

typedef struct ProcessInfo {
__eti_init_info* exception_info;
char* TOC;
int active;
} ProcessInfo;

static ProcessInfo fragmentinfo[1];

int __register_fragment(struct __eti_init_info *pInfo, char *pTOC) {
ProcessInfo* info;
int i;

for (i = 0, info = fragmentinfo; i < 1; ++i, ++info) {
if (info->active == 0) {
info->exception_info = pInfo;
info->TOC = pTOC;
info->active = 1;
return i;
}
}


return -1;
}

void __unregister_fragment(int id) {
ProcessInfo* info;

if (id >= 0 && id < 1) {
info = &fragmentinfo[id];
info->exception_info = 0;
info->TOC = 0;
info->active = 0;
}
}
92 changes: 92 additions & 0 deletions src/Runtime/NMWException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <NMWException.h>
#include <new>

class __partial_array_destructor {
public:
void* mArrayStart; // 0x0
size_t mElemSize; // 0x4
size_t mArraySize; // 0x8
ctor_dtor_ptr mDtorPtr; // 0xC
size_t mCurElement; // 0x10

__partial_array_destructor(void *arr, unsigned int size, unsigned int count, ctor_dtor_ptr ptr) {
mArrayStart = arr;
mElemSize = size;
mArraySize = count;
mDtorPtr = ptr;
mCurElement = mArraySize;
}

~__partial_array_destructor() {
if (mCurElement < mArraySize && mDtorPtr) {
for (char* cur = (char*)mArrayStart + (mElemSize * mCurElement); mCurElement > 0; mCurElement--) {
cur -= mElemSize;
((void (*)(void *, short))mDtorPtr)(cur,-1);
}
}
}
};

extern "C" {

void __construct_array(void *pBlock, ctor_dtor_ptr ctor, ctor_dtor_ptr dtor, size_t size, size_t n) {
__partial_array_destructor pad(pBlock, size, n, dtor);

char* p = (char*)pBlock;
for (pad.mCurElement = 0; pad.mCurElement < n; p += size, pad.mCurElement++) {
((void (*)(void *, short))ctor)(p,1);
}
}

void *__construct_new_array(void *block, ctor_dtor_ptr ctor, ctor_dtor_ptr dtor, size_t size, size_t n) {
char *ptr = (char *)block;

if (ptr != 0){
*(size_t *)ptr = size;
*(size_t *)(ptr + 4) = n;
ptr += 0x10;

if (ctor) {

__partial_array_destructor pad(ptr, size, n, dtor);

char* p;
for (pad.mCurElement = 0, p = ptr; pad.mCurElement < n; pad.mCurElement++, p += size) {
((void (*)(void *, short))ctor)(p,1);
}
}
}
return ptr;
}

void __destroy_arr(void *pArraySource, ctor_dtor_ptr dtor, size_t size, size_t num) {
char* cur;

for (cur = (char*)pArraySource + (size * num); num > 0; num--) {
cur -= size;
((void (*)(void *, short))dtor)(cur,-1);
}
}

void __destroy_new_array(void *pArraySource, ctor_dtor_ptr dtor) {
if (pArraySource != 0) {
if (dtor != 0) {
size_t i, objs, obj_size;
char* cur;

// why are the 8 and 12 important to match?
obj_size = *(size_t*)((char*)pArraySource - 2 * 8);
objs = *(size_t*)((char *)pArraySource - 12);
cur = (char*)pArraySource + obj_size * objs;

for (i = 0; i < objs; i++) {
cur -= obj_size;
((void (*)(void *, short))dtor)(cur,-1);
}
}

::operator delete[] ((char *)pArraySource-2*8);
}
}

}
36 changes: 36 additions & 0 deletions src/Runtime/__init_cpp_exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <__ppc_eabi_linker.h>
#include <NMWException.h>

#ifdef __cplusplus
extern "C" {
#endif

extern void __init_cpp_exceptions(void);
extern void __fini_cpp_exceptions(void);

#ifdef __cplusplus
}
#endif

static int fragmentID = -2;

void __init_cpp_exceptions(void) {
if (fragmentID == -2) {
register char *temp;
asm {
mr temp,r2
}

fragmentID = __register_fragment(_eti_init_info, (char*)temp);
}
}

void __fini_cpp_exceptions(void) {
if (fragmentID != -2) {
__unregister_fragment(fragmentID);
fragmentID = -2;
}
}

__declspec(section ".ctors")
extern void * const __init_cpp_exceptions_reference = __init_cpp_exceptions;
86 changes: 86 additions & 0 deletions src/Runtime/__mem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <__mem.h>

void* memcpy(void *pDest, const void *pSrc, size_t len) {
const char *p;
char* q;
int rev = pDest < pSrc;

if (!rev) {
for (p = (const char*)pSrc - 1, q = (char*)pDest - 1, len++; --len;) {
*++q = *++p;
}
}
else {
for (p = (const char*)pSrc + len, q = (char*)pDest + len, len++; --len;) {
*--q = *--p;
}
}

return pDest;
}

// I really wish I could tell you what's going on here but I can't
void __fill_mem(void *pDest, int val, unsigned long n) {
unsigned long v = (unsigned char) val;
unsigned long i;

((unsigned char*)pDest) = ((unsigned char*)pDest) - 1;

if (n >= 32) {
i = (~(unsigned long)pDest) & 3;

if (i) {
n -= i;

do {
*++(((unsigned char*)pDest)) = v;
}
while (--i);
}

if (v != 0) {
v |= v << 24 | v << 16 | v << 8;
}

((unsigned long*)pDest) = ((unsigned long*) (((unsigned char*)pDest) + 1)) - 1;
i = n >> 5;

if (i != 0) {
do {
*++(((unsigned long*)pDest)) = v;
*++(((unsigned long*)pDest)) = v;
*++(((unsigned long*)pDest)) = v;
*++(((unsigned long*)pDest)) = v;
*++(((unsigned long*)pDest)) = v;
*++(((unsigned long*)pDest)) = v;
*++(((unsigned long*)pDest)) = v;
*++(((unsigned long*)pDest)) = v;
}
while (--i);
}

i = (n & 31) >> 2;

if (i) {
do {
*++(((unsigned long*)pDest)) = v;
}
while (--i);
}

((unsigned char*)pDest) = ((unsigned char*) (((unsigned long*)pDest) + 1)) - 1;
n &= 3;
}

if (n) {
do {
*++(((unsigned char*)pDest)) = v;
}
while (--n);
}
}

void* memset(void *pDest, int val, size_t num) {
__fill_mem(pDest, val, num);
return pDest;
}
Loading

0 comments on commit 826199e

Please sign in to comment.