diff options
| author | John "Lameguy" Wilbert Villamor <lameguy64@gmail.com> | 2021-11-22 14:40:59 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-22 14:40:59 +0800 |
| commit | 45123e1b968d1883fed9b8526157ce2c4bffc4a7 (patch) | |
| tree | d20c80fbd4f5a5d1d3972669625972cea6b3684d /libpsn00b/libc | |
| parent | 538f28cfbbbb8163ab8a96de77d6887123856c81 (diff) | |
| parent | 9b00e5f7ff163a8fc6f341dbf237d90c61dadddc (diff) | |
| download | psn00bsdk-45123e1b968d1883fed9b8526157ce2c4bffc4a7.tar.gz | |
Merge pull request #43 from spicyjpeg/cmake
Even more CMake fixes, submodules, pads example
Diffstat (limited to 'libpsn00b/libc')
| -rw-r--r-- | libpsn00b/libc/c++-support.cxx | 3 | ||||
| -rw-r--r-- | libpsn00b/libc/start.c | 42 | ||||
| -rw-r--r-- | libpsn00b/libc/string.c | 2 | ||||
| -rw-r--r-- | libpsn00b/libc/vsprintf.c | 14 |
4 files changed, 37 insertions, 24 deletions
diff --git a/libpsn00b/libc/c++-support.cxx b/libpsn00b/libc/c++-support.cxx index fcf7cfc..d169fdb 100644 --- a/libpsn00b/libc/c++-support.cxx +++ b/libpsn00b/libc/c++-support.cxx @@ -1,7 +1,6 @@ #include <assert.h> -#include <sys/types.h> +#include <stdint.h> #include <stdlib.h> -#include <malloc.h> extern "C" diff --git a/libpsn00b/libc/start.c b/libpsn00b/libc/start.c index c5872df..f190794 100644 --- a/libpsn00b/libc/start.c +++ b/libpsn00b/libc/start.c @@ -3,9 +3,9 @@ * (C) 2021 Lameguy64, spicyjpeg - MPL licensed */ -#include <sys/types.h> +#include <stdint.h> #include <string.h> -#include <malloc.h> +#include <stdlib.h> #define KERNEL_ARG_STRING ((const char *) 0x80000180) #define KERNEL_RETURN_VALUE ((volatile int *) 0x8000dffc) @@ -47,13 +47,7 @@ static void _parse_kernel_args() { } } -/* Main */ - -// How much space at the end of RAM to leave for the stack (instead of using it -// as heap). By default 128 KB are reserved for the stack, but this constant -// can be overridden in main.c (or anywhere else) simply by redeclaring it -// without the weak attribute. -const int32_t __attribute__((weak)) STACK_MAX_SIZE = 0x20000; +/* Heap initialization */ // These are defined by the linker script. Note that these are *NOT* pointers, // they are virtual symbols whose location matches their value. The simplest @@ -63,6 +57,20 @@ extern uint8_t __bss_start[]; extern uint8_t _end[]; //extern uint8_t _gp[]; +// This function should not be called manually in most cases. It might be +// useful though to change the stack size and/or reinitialize the heap on +// systems that have more than 2 MB of RAM (e.g. emulators, devkits, PS1-based +// arcade boards). +void _mem_init(size_t ram_size, size_t stack_max_size) { + void *exe_end = _end + 4; + size_t exe_size = (size_t) exe_end - (size_t) __text_start; + size_t ram_used = (0x10000 + exe_size + stack_max_size) & 0xfffffffc; + + InitHeap(exe_end, ram_size - ram_used); +} + +/* Main */ + extern void (*__CTOR_LIST__[])(void); extern void (*__DTOR_LIST__[])(void); @@ -73,24 +81,16 @@ extern int32_t main(int32_t argc, const char* argv[]); // to overwrite the arg strings in kernel RAM. void _start(int32_t override_argc, const char **override_argv) { __asm__ volatile("la $gp, _gp;"); - - // Mem init assembly function (clears BSS and InitHeap to _end which is - // not possible to do purely in C because the linker complains about - // relocation truncated to fit: R_MIPS_GPREL16 against `_end' - // Workaround is to do it in assembly because la pseudo-op doesn't use - // stupid gp relative addressing - //_mem_init(); // Clear BSS 4 bytes at a time. BSS is always aligned to 4 bytes by the // linker script. for (uint32_t *i = (uint32_t *) __bss_start; i < (uint32_t *) _end; i++) *i = 0; - // Calculate how much RAM is available after the loaded executable and - // initialize heap accordingly. - void *exe_end = _end + 4; - unsigned int exe_size = (unsigned int) exe_end - (unsigned int) __text_start; - InitHeap(exe_end, 0x200000 - (exe_size + STACK_MAX_SIZE)); + // Initialize the heap, assuming 2 MB of RAM and reserving 128 KB for the + // stack. Note that _mem_init() can be called again in main() to change + // these values. + _mem_init(0x200000, 0x20000); if (override_argv) { __argc = override_argc; diff --git a/libpsn00b/libc/string.c b/libpsn00b/libc/string.c index e11ed67..445b227 100644 --- a/libpsn00b/libc/string.c +++ b/libpsn00b/libc/string.c @@ -6,7 +6,7 @@ #include <stdio.h> #include <string.h> -#include <malloc.h> +#include <stdlib.h> int tolower(int chr) { diff --git a/libpsn00b/libc/vsprintf.c b/libpsn00b/libc/vsprintf.c index 361b24e..0a99dcc 100644 --- a/libpsn00b/libc/vsprintf.c +++ b/libpsn00b/libc/vsprintf.c @@ -58,6 +58,15 @@ pad_quantity = (pad_quantity - 1) - last; \ if(pad_quantity < 0) pad_quantity = 0; +#define calculate_real_padding_bin() \ + last = 0; \ + for (x = 0; x < 32; x++) \ + if((arg >> x) & 1) \ + last = x; \ + \ + pad_quantity = (pad_quantity - 1) - last; \ + if(pad_quantity < 0) pad_quantity = 0; + #define write_padding() \ if(!(flags & SPRINTF_NEGFIELD_FLAG)) \ for(x = 0; x < pad_quantity; x++) \ @@ -703,6 +712,9 @@ int vsnprintf(char *string, unsigned int size, const char *fmt, va_list ap) //else // arg = va_arg(ap, unsigned long long); + calculate_real_padding_bin(); + write_padding(); + for(x=31;x>=0;x--) { y = (arg >> x); @@ -715,6 +727,8 @@ int vsnprintf(char *string, unsigned int size, const char *fmt, va_list ap) put_in_string(string, ssz, y + '0', string_pos++); } + write_neg_padding(); + directive_coming = 0; break; |
