aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/libc
diff options
context:
space:
mode:
authorJohn Wilbert M. Villamor <lameguy64@gmail.com>2020-09-19 20:43:05 +0800
committerJohn Wilbert M. Villamor <lameguy64@gmail.com>2020-09-19 20:43:05 +0800
commit9f4891f95070c66ea9f1aba99d72724d4ab24e5a (patch)
tree723e3ef2118a3d1a9e6dafa811ed1b8b1bc9196e /libpsn00b/libc
parent6762c39551ded059450d17d8bb0cb80642c8aaab (diff)
downloadpsn00bsdk-9f4891f95070c66ea9f1aba99d72724d4ab24e5a.tar.gz
Revised makefiles, added strtok(), command line arguments, SetHeapSize(), moved ISR and callback system to psxetc, moved debug font to psxgpu, fixed CD-ROM library crashing on PSIO, fixed interrupt callback setup to fix crashing on ResetGraph()
Diffstat (limited to 'libpsn00b/libc')
-rw-r--r--libpsn00b/libc/makefile39
-rw-r--r--libpsn00b/libc/malloc.s11
-rw-r--r--libpsn00b/libc/start.c59
-rw-r--r--libpsn00b/libc/string.c44
4 files changed, 135 insertions, 18 deletions
diff --git a/libpsn00b/libc/makefile b/libpsn00b/libc/makefile
index c925b53..f639823 100644
--- a/libpsn00b/libc/makefile
+++ b/libpsn00b/libc/makefile
@@ -1,24 +1,29 @@
# Run using make (Linux) or gmake (BSD)
# Part of the PSn00bSDK Project
-# 2019 Lameguy64 / Meido-Tek Productions
+# 2019 - 2020 Lameguy64 / Meido-Tek Productions
-include ../common.mk
+include ../../psn00bsdk-setup.mk
-TARGET = ../libc.a
+TARGET = libc.a
-CFILES = $(notdir $(wildcard ./*.c))
-CXXFILES = $(notdir $(wildcard ./*.cxx))
-AFILES = $(notdir $(wildcard ./*.s))
-OFILES = $(addprefix build/,$(CFILES:.c=.o) $(CXXFILES:.cxx=.o) $(AFILES:.s=.o))
+INCLUDE = -I../include
-CFLAGS = -g -O2 -msoft-float -fno-builtin -fdata-sections -ffunction-sections -Wa,--strip-local-absolute
-AFLAGS = -g -msoft-float -Wa,-strip-local-absolute
+CFLAGS = -g -O2 -msoft-float -fno-builtin -fdata-sections \
+ -ffunction-sections -Wa,--strip-local-absolute
+AFLAGS = -g -msoft-float -Wa,-strip-local-absolute
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-AR = $(PREFIX)ar
-RANLIB = $(PREFIX)ranlib
+CFILES = $(notdir $(wildcard ./*.c))
+CXXFILES = $(notdir $(wildcard ./*.cxx))
+AFILES = $(notdir $(wildcard ./*.s))
+
+OFILES = $(addprefix build/,$(CFILES:.c=.o) $(CXXFILES:.cxx=.o) \
+ $(AFILES:.s=.o))
+
+ifndef PSN00BSDK_LIBS
+
+PSN00BSDK_LIBS = ..
+
+endif
all: $(TARGET)
@@ -39,5 +44,11 @@ build/%.o: %.s
@mkdir -p $(dir $@)
$(CC) $(AFLAGS) $(INCLUDE) -c $< -o $@
+install:
+ifneq ($(PSN00BSDK_LIBS), "..")
+ @mkdir -p $(PSN00BSDK_LIBS)
+endif
+ cp $(TARGET) $(PSN00BSDK_LIBS)/$(TARGET)
+
clean:
rm -Rf build $(TARGET)
diff --git a/libpsn00b/libc/malloc.s b/libpsn00b/libc/malloc.s
index fdb196d..90f9bd4 100644
--- a/libpsn00b/libc/malloc.s
+++ b/libpsn00b/libc/malloc.s
@@ -38,6 +38,17 @@ InitHeap:
jr $ra
sw $0 , ND_SIZE($a0)
+
+# Changes the heap size without clearing or relocating the heap
+# a0 - Size of memory heap in bytes
+.global SetHeapSize
+.type SetHeapSize, @function
+SetHeapSize:
+ la $v1, _malloc_size
+ lw $v0, 0($v1)
+ jr $ra
+ sw $a1, 0($v1)
+
# Allocates a block of memory in the heap
# a0 - Size of memory block to allocate.
diff --git a/libpsn00b/libc/start.c b/libpsn00b/libc/start.c
index c234e03..354ebb9 100644
--- a/libpsn00b/libc/start.c
+++ b/libpsn00b/libc/start.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <string.h>
#include <malloc.h>
#define load_gp() __asm__ volatile ( \
@@ -9,6 +10,11 @@ extern int main(int argc, const char* argv[]);
void _mem_init(void);
+int __argc;
+const char **__argv;
+
+static const char *_arg_ptrs_int[8];
+static char _arg_buff[132];
static void _call_global_ctors(void)
{
@@ -36,8 +42,50 @@ static void _call_global_dtors(void)
}
}
-void _start(void) {
+void _parse_args( int argc, const char *args[] )
+{
+ int i;
+ char *c,*s;
+
+ memset( _arg_buff, 0, 132 );
+
+ if( !args )
+ {
+ // Use arguments from kernel if args is NULL
+ strncpy( _arg_buff, (char*)0x180, 128 );
+
+ // Clean-up args froom stray line-ends
+ while( ( c = strrchr( _arg_buff, '\r' ) ) ||
+ ( c = strrchr( _arg_buff, '\n' ) ) )
+ *c = 0;
+ }
+ else
+ {
+ __argc = argc;
+ __argv = args;
+ return;
+ }
+
+ __argc = 0;
+ for( i=0; i<8; i++ )
+ _arg_ptrs_int[i] = 0;
+ s = _arg_buff;
+ while( c = strtok( s, " " ) )
+ {
+ _arg_ptrs_int[__argc] = c;
+ __argc++;
+ s = NULL;
+ if( __argc >= 8 )
+ break;
+ }
+
+ __argv = _arg_ptrs_int;
+
+} /* parse_args */
+
+void _start( int argc, const char *args[] )
+{
// Load GP address
load_gp();
@@ -48,10 +96,15 @@ void _start(void) {
// stupid gp relative addressing
_mem_init();
+ // process command line arguments
+ _parse_args( argc, args );
+
_call_global_ctors();
- main(0, NULL);
+ *((int*)0x8000DFFC) = main( __argc, __argv );
_call_global_dtors();
-} \ No newline at end of file
+ // Set return value to kernel return value area
+
+} /* _start */ \ No newline at end of file
diff --git a/libpsn00b/libc/string.c b/libpsn00b/libc/string.c
index 0b7307d..e11ed67 100644
--- a/libpsn00b/libc/string.c
+++ b/libpsn00b/libc/string.c
@@ -302,6 +302,48 @@ double strtod(const char *nptr, char **endptr)
return (i + d)*s;
}
+/* implementation by Lameguy64, behaves like OpenWatcom's strtok() */
+/* BIOS strtok seemed either bugged, or designed for wide chars */
+
+static char *_strtok_curpos;
+static char *_strtok_endpos;
+
+char *strtok( char *s1, char *s2 )
+{
+ char *c,*t;
+
+ if( s1 )
+ {
+ _strtok_curpos = s1;
+ _strtok_endpos = s1+strlen( s1 );
+ }
+ else
+ {
+ if( _strtok_curpos >= _strtok_endpos )
+ return( NULL );
+ }
+
+ if( !*_strtok_curpos )
+ return( NULL );
+
+ if( c = strstr( _strtok_curpos, s2 ) )
+ {
+ *c = 0;
+ t = _strtok_curpos;
+ _strtok_curpos = c+1;
+ return( t );
+ }
+ else
+ {
+ t = _strtok_curpos;
+ _strtok_curpos += strlen( t );
+ return( t );
+ }
+
+ return( NULL );
+
+} /* strtok */
+
long double strtold(const char *nptr, char **endptr)
{
return (long double)strtod(nptr, endptr);
@@ -310,4 +352,4 @@ long double strtold(const char *nptr, char **endptr)
float strtof(const char *nptr, char **endptr)
{
return (float)strtod(nptr, endptr);
-}
+} \ No newline at end of file