Match and link __mem

This commit is contained in:
Phillip Stephens 2022-10-04 22:17:40 -07:00
parent 693fe93eab
commit fb1a503cce
3 changed files with 92 additions and 2 deletions

View File

@ -13,6 +13,8 @@ parser.add_argument('--map', dest='map', action=argparse.BooleanOptionalAction,
default=False, help='generate map file')
parser.add_argument('--check', dest='check', action=argparse.BooleanOptionalAction,
default=True, help='check hash of resulting dol')
parser.add_argument('--static-libs', dest='static_libs', action=argparse.BooleanOptionalAction,
default=False, help='build and use static libs')
args = parser.parse_args()
# Completed c/cpp files to link
@ -66,6 +68,7 @@ COMPLETE_OBJECTS = [
"Dolphin/os/__start",
"Dolphin/os/OSAudioSystem",
"Dolphin/os/__ppc_eabi_init",
"Runtime/__mem",
"Runtime/abort_exit",
"Runtime/ctype",
"Runtime/locale",
@ -1113,7 +1116,7 @@ LIBS = [
# Create & link static libraries
# Disabled by default for now until we can get it working on windows/macOS
ENABLE_STATIC_LIBS = False
ENABLE_STATIC_LIBS = args.static_libs
# On Windows, we need this to use && in commands
ALLOW_CHAIN = "cmd /c " if os.name == "nt" else ""

View File

@ -10,7 +10,7 @@ extern "C" {
#pragma section code_type ".init"
void* memcpy(void* dst, const void* src, size_t n);
void* memset(void* dst, int val, size_t n);
void __fill_mem(void* dst, int val, unsigned long n);
void __fill_mem(void* dst, int val, size_t n);
#pragma section code_type
size_t strlen(const char* s);

87
src/Runtime/__mem.c Normal file
View File

@ -0,0 +1,87 @@
#include "string.h"
void* memcpy(void* dst, const void* src, size_t n) {
const char* p;
char* q;
int rev = ((unsigned long)src < (unsigned long)dst);
if (!rev) {
for (p = (const char*)src - 1, q = (char*)dst - 1, n++; --n;)
*++q = *++p;
} else {
for (p = (const char*)src + n, q = (char*)dst + n, n++; --n;)
*--q = *--p;
}
return (dst);
}
#define cps ((unsigned char*)src)
#define cpd ((unsigned char*)dst)
#define lps ((unsigned long*)src)
#define lpd ((unsigned long*)dst)
#define deref_auto_inc(p) *++(p)
void __fill_mem(void* dst, int val, size_t n) {
unsigned long v = (unsigned char)val;
unsigned long i;
cpd = ((unsigned char*)dst) - 1;
if (n >= 32) {
i = (~(unsigned long)dst) & 3;
if (i) {
n -= i;
do
deref_auto_inc(cpd) = v;
while (--i);
}
if (v)
v |= v << 24 | v << 16 | v << 8;
lpd = ((unsigned long*)(cpd + 1)) - 1;
i = n >> 5;
if (i)
do {
deref_auto_inc(lpd) = v;
deref_auto_inc(lpd) = v;
deref_auto_inc(lpd) = v;
deref_auto_inc(lpd) = v;
deref_auto_inc(lpd) = v;
deref_auto_inc(lpd) = v;
deref_auto_inc(lpd) = v;
deref_auto_inc(lpd) = v;
} while (--i);
i = (n & 31) >> 2;
if (i)
do
deref_auto_inc(lpd) = v;
while (--i);
cpd = ((unsigned char*)(lpd + 1)) - 1;
n &= 3;
}
if (n)
do
deref_auto_inc(cpd) = v;
while (--n);
return;
}
void* memset(void* str, int c, size_t n) {
__fill_mem(str, c, n);
return str;
}