From b3ef4c5874f579c968f2d26843d0db420e9ed40e Mon Sep 17 00:00:00 2001 From: spicyjpeg <88942473+spicyjpeg@users.noreply.github.com> Date: Sun, 7 Nov 2021 18:57:48 +0100 Subject: Added io/pads example, psxpad.h definitions, bugfixes --- libpsn00b/include/psxpad.h | 203 +++++++++++++++++++++++++++++++++------------ 1 file changed, 150 insertions(+), 53 deletions(-) (limited to 'libpsn00b/include') diff --git a/libpsn00b/include/psxpad.h b/libpsn00b/include/psxpad.h index 01aff06..d152896 100644 --- a/libpsn00b/include/psxpad.h +++ b/libpsn00b/include/psxpad.h @@ -3,10 +3,12 @@ * 2019 Lameguy64 / Meido-Tek Productions * * Currently only provides a bunch of definitions and a few structs but no - * handling functions yet. Use the code in pad.s in one of the sample - * programs for the meantime instead. + * handling functions yet. See the io/pads example for a simple manual pollling + * implementation (with support for DualShock 2 controllers). * * Work in progress, subject to change significantly in future releases. + * + * Reference: https://gist.github.com/scanlime/5042071 */ #ifndef _PSXPAD_H @@ -50,58 +52,153 @@ #define GCON_TRIGGER 8192 #define GCON_B 16384 -// Struct for digital, joystick, dual analog and Dualshock controllers -typedef struct { - unsigned char stat; // Status - unsigned char len:4; // Data length (in halfwords) - unsigned char type:4; // Device type: - // 0x4 - digital pad - // 0x5 - analog joystick - // 0x7 - dual analog & Dualshock - unsigned short btn; // Button states - unsigned char rs_x,rs_y; // Right stick coordinates - unsigned char ls_x,ls_y; // Left stick coordinates +typedef enum { + PAD_ID_MOUSE = 0x1, // Sony PS1 mouse + PAD_ID_NEGCON = 0x2, // Namco neGcon + PAD_ID_IRQ10_GUN = 0x3, // "Konami" lightgun without composite video passthrough + PAD_ID_DIGITAL = 0x4, // Digital pad or Dual Analog/DualShock in digital mode + PAD_ID_ANALOG_STICK = 0x5, // Flight stick or Dual Analog in green LED mode + PAD_ID_GUNCON = 0x6, // Namco Guncon (lightgun with composite video passthrough) + PAD_ID_ANALOG = 0x7, // Dual Analog/DualShock in analog (red LED) mode + PAD_ID_MULTITAP = 0x8, // Multitap adapter (when tap_mode == 1) + PAD_ID_JOGCON = 0xe, // Namco Jogcon + PAD_ID_CONFIG_MODE = 0xf, // Dual Analog/DualShock in config mode (if len == 0x3) + PAD_ID_NONE = 0xf // No pad connected (if len == 0xf) +} PAD_TYPEID; + +// Controller command definitions +typedef enum { + PAD_CMD_INIT_PRESSURE = '@', // Initialize DS2 button pressure sensors (in config mode) + PAD_CMD_READ = 'B', // Read pad state and set rumble + PAD_CMD_CONFIG_MODE = 'C', // Toggle DualShock configuration mode + PAD_CMD_SET_ANALOG = 'D', // Set analog mode/LED state (in config mode) + PAD_CMD_GET_ANALOG = 'E', // Get analog mode/LED state (in config mode) + PAD_CMD_REQUEST_CONFIG = 'M', // Configure request/unlock vibration (in config mode) + PAD_CMD_RESPONSE_CONFIG = 'O' // Configure response/unlock DS2 pressure (in config mode) +} PAD_COMMAND; + +// Memory card command/response definitions +typedef enum { + MCD_CMD_READ = 'R', // Read sector + MCD_CMD_IDENTIFY = 'S', // Retrieve ID and card size information + MCD_CMD_WRITE = 'W' // Write sector +} MCD_COMMAND; + +typedef enum { + MCD_STAT_OK = 'G', + MCD_STAT_BAD_CHECKSUM = 'N', + MCD_STAT_BAD_SECTOR = 0xff +} MCD_STATUS; + +#define MCD_CMD_READ_LEN 139 +#define MCD_CMD_IDENTIFY_LEN 9 +#define MCD_CMD_WRITE_LEN 137 + +// Memory card status flags +#define MCD_FLAG_WRITE_ERROR 4 // Last write command failed +#define MCD_FLAG_NOT_WRITTEN 8 // No writes have been issued yet +#define MCD_FLAG_UNKNOWN 16 // Might be set on third-party cards + +// Struct for data returned by controllers +typedef struct _PADTYPE { + union { // Header: + struct __attribute__((packed)) { // When parsing data returned by BIOS: + unsigned char stat; // Status + unsigned char len:4; // Payload length / 2, 0 for multitap + unsigned char type:4; // Device type (PAD_TYPEID) + }; + struct __attribute__((packed)) { // When parsing raw controller response: + unsigned char len:4; // Payload length / 2, 0 for multitap + unsigned char type:4; // Device type (PAD_TYPEID) + unsigned char prefix; // Must be 0x5a + } raw; + }; + struct { // Payload: + unsigned short btn; // Button states + union { + struct { // Analog controller: + unsigned char rs_x,rs_y; // Right stick coordinates + unsigned char ls_x,ls_y; // Left stick coordinates + unsigned char press[12]; // Button pressure (DualShock 2 only) + }; + struct { // Mouse: + char x_mov; // X movement of mouse + char y_mov; // Y movement of mouse + }; + struct { // neGcon: + unsigned char twist; // Controller twist + unsigned char btn_i; // I button value + unsigned char btn_ii; // II button value + unsigned char trg_l; // L trigger value + }; + struct { // Jogcon: + unsigned short jog_rot; // Jog rotation + }; + struct { // Guncon: + unsigned short gun_x; // Gun X position in dotclocks + unsigned short gun_y; // Gun Y position in scanlines + }; + }; + }; } PADTYPE; -// Struct for a mouse controller -typedef struct { - unsigned char stat; - unsigned char len:4; - unsigned char type:4; // Device type (0x1) - unsigned short btn; - char x_mov; // X movement of mouse - char y_mov; // Y movement of mouse -} MOUSETYPE; - -// Struct for a neGcon controller (for Namco neGcon) -typedef struct { - unsigned char stat; - unsigned char len:4; - unsigned char type:4; // (0x2) - unsigned short btn; - unsigned char twist; // Controller twist - unsigned char btn_i; // I button value - unsigned char btn_ii; // II button value - unsigned char trg_l; // L trigger value -} NCONTYPE; - -// Struct for a Jogcon controller (for Namco Jogcon) -typedef struct { - unsigned char stat; - unsigned char len:4; - unsigned char type:4; // (0xE) - unsigned short btn; - unsigned short jog_rot; // Jog rotation -} JCONTYPE; - -// Struct for a Gun-Con controller (for Namco Gun-Con) -typedef struct { - unsigned char status; - unsigned char len:4; - unsigned char type:4; // (0x6) - unsigned short btn; - unsigned short gun_x; // Gun X position in dotclocks - unsigned short gun_y; // Gun Y position in scanlines -} GCONTYPE; +typedef struct _MCDRESPONSE { + unsigned char flags; // Status flags + unsigned char type1; // Must be 0x5a + unsigned char type2; // Must be 0x5d + union { + struct { // MCD_CMD_READ response: + unsigned char dummy[2]; + unsigned char ack1; // Must be 0x5c + unsigned char ack2; // Must be 0x5d + unsigned char lba_h; + unsigned char lba_l; + unsigned char data[128]; + unsigned char checksum; // = lba_h ^ lba_l ^ data + unsigned char stat; // Status (MCD_STATUS) + } read; + struct { // MCD_CMD_IDENTIFY response: + unsigned char ack1; // Must be 0x5c + unsigned char ack2; // Must be 0x5d + unsigned char size_h; // Card capacity bits 8-15 (0x04 = 128KB) + unsigned char size_l; // Card capacity bits 0-7 (0x00 = 128KB) + unsigned char blksize_h; // Sector size bits 8-15 (must be 0x00) + unsigned char blksize_l; // Sector size bits 0-7 (must be 0x80) + } identify; + struct { // MCD_CMD_WRITE response: + unsigned char dummy[131]; + unsigned char ack1; // Must be 0x5c + unsigned char ack2; // Must be 0x5d + unsigned char stat; // Status (MCD_STATUS) + } write; + }; +} MCDRESPONSE; + +//typedef PADTYPE MOUSETYPE; +//typedef PADTYPE NCONTYPE; +//typedef PADTYPE JCONTYPE; +//typedef PADTYPE GCONTYPE; + +// Structs for raw controller request +typedef struct _PADREQUEST { + unsigned char addr; // Must be 0x01 (or 02/03/04 for multitap pads) + unsigned char cmd; // Command (PAD_COMMAND) + unsigned char tap_mode; // 0x01 to enable multitap response + unsigned char motor_r; // Right motor control (on/off) + unsigned char motor_l; // Left motor control (PWM) + unsigned char dummy[4]; +} PADREQUEST; + +// Structs for raw memory card request +typedef struct _MCDREQUEST { + unsigned char addr; // Must be 0x81 (or 02/03/04 for multitap cards) + unsigned char cmd; // Command (MCD_COMMAND) + unsigned char dummy[2]; + unsigned char lba_h; // Sector address bits 8-15 (dummy for CMD_IDENTIFY) + unsigned char lba_l; // Sector address bits 0-7 (dummy for CMD_IDENTIFY) + unsigned char data[128]; // Sector payload (dummy for CMD_READ/CMD_IDENTIFY) + unsigned char checksum; // = lba_h ^ lba_l ^ data (CMD_WRITE only) + unsigned char dummy2[3]; +} MCDREQUEST; #endif \ No newline at end of file -- cgit v1.2.3 From 619fa016bbc4ddd8d4a670cf3f8aa63617473b2f Mon Sep 17 00:00:00 2001 From: spicyjpeg <88942473+spicyjpeg@users.noreply.github.com> Date: Thu, 18 Nov 2021 17:23:25 +0100 Subject: Deprecated malloc.h, moved int*_t types to stdint.h --- examples/beginner/cppdemo/main.cpp | 2 +- examples/cdrom/cdbrowse/main.c | 1 - examples/cdrom/cdxa/main.c | 1 - examples/demos/n00bdemo/logo.c | 1 - examples/demos/n00bdemo/main.c | 1 - examples/io/pads/main.c | 2 +- examples/io/pads/spi.c | 4 ++-- examples/io/pads/spi.h | 1 + examples/system/dynlink/library/balls.c | 2 +- examples/system/dynlink/library/cube.c | 2 +- examples/system/dynlink/library/dll_common.h | 1 + examples/system/dynlink/main.c | 3 +-- indev/psxmdec/main.c | 2 +- indev/psxpad/main.c | 1 - libpsn00b/include/malloc.h | 14 ++------------ libpsn00b/include/stdint.h | 16 ++++++++++++++++ libpsn00b/include/stdlib.h | 28 +++++++++------------------- libpsn00b/include/sys/types.h | 16 ++++------------ libpsn00b/libc/c++-support.cxx | 3 +-- libpsn00b/libc/string.c | 2 +- libpsn00b/lzp/compress.c | 1 - libpsn00b/psxcd/isofs.c | 2 +- libpsn00b/psxetc/dl.c | 4 ++-- libpsn00b/psxgpu/font.c | 2 +- tools/util/elf2cpe.c | 2 +- 25 files changed, 48 insertions(+), 66 deletions(-) create mode 100644 libpsn00b/include/stdint.h (limited to 'libpsn00b/include') diff --git a/examples/beginner/cppdemo/main.cpp b/examples/beginner/cppdemo/main.cpp index 58bfcda..fd2e3a8 100644 --- a/examples/beginner/cppdemo/main.cpp +++ b/examples/beginner/cppdemo/main.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include diff --git a/examples/cdrom/cdbrowse/main.c b/examples/cdrom/cdbrowse/main.c index ead2df0..9a1dbd0 100644 --- a/examples/cdrom/cdbrowse/main.c +++ b/examples/cdrom/cdbrowse/main.c @@ -67,7 +67,6 @@ #include #include -#include #include "ball16c.h" diff --git a/examples/cdrom/cdxa/main.c b/examples/cdrom/cdxa/main.c index 16f1c82..5f11d8d 100644 --- a/examples/cdrom/cdxa/main.c +++ b/examples/cdrom/cdxa/main.c @@ -129,7 +129,6 @@ #include #include -#include #include "ball16c.h" diff --git a/examples/demos/n00bdemo/logo.c b/examples/demos/n00bdemo/logo.c index d10b5b4..40160e7 100644 --- a/examples/demos/n00bdemo/logo.c +++ b/examples/demos/n00bdemo/logo.c @@ -5,7 +5,6 @@ #include #include #include -#include "malloc.h" #include "smd.h" #include diff --git a/examples/demos/n00bdemo/main.c b/examples/demos/n00bdemo/main.c index e9dd336..d2fe317 100644 --- a/examples/demos/n00bdemo/main.c +++ b/examples/demos/n00bdemo/main.c @@ -32,7 +32,6 @@ #include #include -#include "malloc.h" #include "smd.h" #include "data.h" #include "disp.h" diff --git a/examples/io/pads/main.c b/examples/io/pads/main.c index ea7219b..92beb1c 100644 --- a/examples/io/pads/main.c +++ b/examples/io/pads/main.c @@ -23,7 +23,7 @@ * zerofilled responses, slow replies) for maximum compatibility. */ -#include +#include #include #include #include diff --git a/examples/io/pads/spi.c b/examples/io/pads/spi.c index 5fc2648..e01b3f6 100644 --- a/examples/io/pads/spi.c +++ b/examples/io/pads/spi.c @@ -26,9 +26,9 @@ * card APIs) alongside it. */ -#include +#include #include -#include +#include #include #include #include diff --git a/examples/io/pads/spi.h b/examples/io/pads/spi.h index 09223e2..1c473cd 100644 --- a/examples/io/pads/spi.h +++ b/examples/io/pads/spi.h @@ -6,6 +6,7 @@ #ifndef __SPI_H #define __SPI_H +#include #include //#define SPI_BUFF_LEN 34 diff --git a/examples/system/dynlink/library/balls.c b/examples/system/dynlink/library/balls.c index 2a4d9f4..ef6993e 100644 --- a/examples/system/dynlink/library/balls.c +++ b/examples/system/dynlink/library/balls.c @@ -3,7 +3,7 @@ * (C) 2021 spicyjpeg - MPL licensed */ -#include +#include #include #include #include diff --git a/examples/system/dynlink/library/cube.c b/examples/system/dynlink/library/cube.c index 57f3e56..84fe552 100644 --- a/examples/system/dynlink/library/cube.c +++ b/examples/system/dynlink/library/cube.c @@ -3,7 +3,7 @@ * (C) 2021 spicyjpeg - MPL licensed */ -#include +#include #include #include #include diff --git a/examples/system/dynlink/library/dll_common.h b/examples/system/dynlink/library/dll_common.h index 4f9314b..315a993 100644 --- a/examples/system/dynlink/library/dll_common.h +++ b/examples/system/dynlink/library/dll_common.h @@ -6,6 +6,7 @@ #ifndef __DLL_COMMON_H #define __DLL_COMMON_H +#include #include /* Common structures shared by the main executable and DLLs */ diff --git a/examples/system/dynlink/main.c b/examples/system/dynlink/main.c index 33f6f44..6d93e71 100644 --- a/examples/system/dynlink/main.c +++ b/examples/system/dynlink/main.c @@ -36,12 +36,11 @@ * as plugins/mods/patches stored on a memory card. */ -#include +#include #include #include #include #include -#include #include #include #include diff --git a/indev/psxmdec/main.c b/indev/psxmdec/main.c index c9fd678..f4f0f51 100644 --- a/indev/psxmdec/main.c +++ b/indev/psxmdec/main.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include diff --git a/indev/psxpad/main.c b/indev/psxpad/main.c index e8a6181..afd801e 100644 --- a/indev/psxpad/main.c +++ b/indev/psxpad/main.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/libpsn00b/include/malloc.h b/libpsn00b/include/malloc.h index d94823f..75c3711 100644 --- a/libpsn00b/include/malloc.h +++ b/libpsn00b/include/malloc.h @@ -1,18 +1,8 @@ #ifndef _MALLOC_H #define _MALLOC_H -#ifdef __cplusplus -extern "C" { -#endif +#warning " is deprecated, include instead" -unsigned int *GetBSSend(); -void InitHeap(unsigned int *addr, int size); -int SetHeapSize(int size); -void *malloc(int size); -void free(void *ptr); - -#ifdef __cplusplus -} -#endif +#include #endif // _MALLOC_H \ No newline at end of file diff --git a/libpsn00b/include/stdint.h b/libpsn00b/include/stdint.h new file mode 100644 index 0000000..83acb00 --- /dev/null +++ b/libpsn00b/include/stdint.h @@ -0,0 +1,16 @@ +#ifndef _STDINT_H +#define _STDINT_H + +typedef unsigned int size_t; + +typedef char int8_t; +typedef short int16_t; +typedef int int32_t; +typedef long long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; + +#endif // _STDINT_H \ No newline at end of file diff --git a/libpsn00b/include/stdlib.h b/libpsn00b/include/stdlib.h index 474eba6..de3ab47 100644 --- a/libpsn00b/include/stdlib.h +++ b/libpsn00b/include/stdlib.h @@ -19,34 +19,16 @@ extern long atol(char *s); extern char atob(char *s); // Is this right? */ -// Random number functions (not yet implemented) - -/* -int rand(); -void srand(unsigned int seed); -*/ - // Quick sort (not yet implemented) //void qsort(void *base , int nel , int width , int (*cmp)(const void *,const void *)); -// Memory allocation functions (not yet implemented, avoid using BIOS as they are reportedly buggy) - -/* -#warning "malloc() family of functions NEEDS MORE TESTING" - -void *malloc(int size); -void free(void *buf); -void *calloc(int number, int size); -void *realloc(void *buf , int n); -*/ - #ifdef __cplusplus extern "C" { #endif extern int __argc; -extern char __argv[]; +extern const char **__argv; int rand(); void srand(unsigned long seed); @@ -64,6 +46,14 @@ long atol(const char *s); double strtod(const char *nptr, char **endptr); float strtof(const char *nptr, char **endptr); +// Memory allocation functions +unsigned int *GetBSSend(); +void InitHeap(unsigned int *addr, int size); +int SetHeapSize(int size); +void *malloc(int size); +void *calloc(int number, int size); +void free(void *ptr); + #ifdef __cplusplus } #endif diff --git a/libpsn00b/include/sys/types.h b/libpsn00b/include/sys/types.h index aee197e..da43590 100644 --- a/libpsn00b/include/sys/types.h +++ b/libpsn00b/include/sys/types.h @@ -1,21 +1,13 @@ #ifndef _TYPES_H #define _TYPES_H +//#warning " and u_* types are deprecated, include instead" + +//#include + typedef unsigned char u_char; typedef unsigned short u_short; typedef unsigned int u_int; typedef unsigned long u_long; -typedef unsigned int size_t; - -typedef char int8_t; -typedef short int16_t; -typedef int int32_t; -typedef long long int64_t; - -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; -typedef unsigned long long uint64_t; - #endif // _TYPES_H \ No newline at end of file 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 -#include +#include #include -#include extern "C" 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 #include -#include +#include int tolower(int chr) { diff --git a/libpsn00b/lzp/compress.c b/libpsn00b/lzp/compress.c index 5969dd6..9cfc64d 100644 --- a/libpsn00b/lzp/compress.c +++ b/libpsn00b/lzp/compress.c @@ -3,7 +3,6 @@ #include #if LZP_USE_MALLOC == TRUE #include -#include #endif #include "lzconfig.h" diff --git a/libpsn00b/psxcd/isofs.c b/libpsn00b/psxcd/isofs.c index dc0c5ca..40a40af 100644 --- a/libpsn00b/psxcd/isofs.c +++ b/libpsn00b/psxcd/isofs.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/libpsn00b/psxetc/dl.c b/libpsn00b/psxetc/dl.c index 586e7e1..e43374f 100644 --- a/libpsn00b/psxetc/dl.c +++ b/libpsn00b/psxetc/dl.c @@ -23,9 +23,9 @@ * of entries */ -#include +#include #include -#include +#include #include #include #include diff --git a/libpsn00b/psxgpu/font.c b/libpsn00b/psxgpu/font.c index 7d0dd89..4c715a9 100644 --- a/libpsn00b/psxgpu/font.c +++ b/libpsn00b/psxgpu/font.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include diff --git a/tools/util/elf2cpe.c b/tools/util/elf2cpe.c index 46b0a37..210f25b 100644 --- a/tools/util/elf2cpe.c +++ b/tools/util/elf2cpe.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include "elf.h" #ifdef WIN32 -- cgit v1.2.3 From 853fa4eed241cdd87b8c2d2e60cf755509d9a184 Mon Sep 17 00:00:00 2001 From: spicyjpeg <88942473+spicyjpeg@users.noreply.github.com> Date: Thu, 18 Nov 2021 17:25:58 +0100 Subject: sprintf improvements, added new _mem_init, GetSystemInfo --- libpsn00b/include/psxapi.h | 32 +++++++++++++++++++++++++++ libpsn00b/libc/start.c | 42 ++++++++++++++++++------------------ libpsn00b/libc/vsprintf.c | 14 ++++++++++++ libpsn00b/psxapi/sys/getsysteminfo.s | 10 +++++++++ 4 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 libpsn00b/psxapi/sys/getsysteminfo.s (limited to 'libpsn00b/include') diff --git a/libpsn00b/include/psxapi.h b/libpsn00b/include/psxapi.h index 9e92568..ec0dfea 100644 --- a/libpsn00b/include/psxapi.h +++ b/libpsn00b/include/psxapi.h @@ -32,6 +32,35 @@ #define RCntMdFR 0x0000 #define RCntMdGATE 0x0010 +typedef struct { // Thread control block + int status; + int mode; + union { + unsigned int reg[37]; + struct { + unsigned int zero, at; + unsigned int v0, v1; + unsigned int a0, a1, a2, a3; + unsigned int t0, t1, t2, t3, t4, t5, t6, t7; + unsigned int s0, s1, s2, s3, s4, s5, s6, s7; + unsigned int t8, t9; + unsigned int k0, k1; + unsigned int gp, sp, fp, ra; + + unsigned int cop0r14; + unsigned int hi; + unsigned int lo; + unsigned int cop0r12; + unsigned int cop0r13; + }; + }; + int _reserved[9]; +} TCB; + +typedef struct { // Process control block + TCB *thread; +} PCB; + typedef struct { // Device control block char *name; int flags; @@ -182,6 +211,9 @@ void ChangeClearRCnt(int t, int m); int Exec(struct EXEC *exec, int argc, char **argv); void FlushCache(void); +// Misc functions +int GetSystemInfo(int index); + void _boot(void); #ifdef __cplusplus diff --git a/libpsn00b/libc/start.c b/libpsn00b/libc/start.c index c7dbfe5..f190794 100644 --- a/libpsn00b/libc/start.c +++ b/libpsn00b/libc/start.c @@ -3,9 +3,9 @@ * (C) 2021 Lameguy64, spicyjpeg - MPL licensed */ -#include +#include #include -#include +#include #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; - size_t exe_size = (size_t) exe_end - (size_t) __text_start; - InitHeap(exe_end, 0x1f0000 - (exe_size + STACK_MAX_SIZE) & 0xfffffffc); + // 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/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; diff --git a/libpsn00b/psxapi/sys/getsysteminfo.s b/libpsn00b/psxapi/sys/getsysteminfo.s new file mode 100644 index 0000000..60c1d43 --- /dev/null +++ b/libpsn00b/psxapi/sys/getsysteminfo.s @@ -0,0 +1,10 @@ +.set noreorder + +.section .text + +.global GetSystemInfo +.type GetSystemInfo, @function +GetSystemInfo: + addiu $t2, $0, 0xa0 + jr $t2 + addiu $t1, $0, 0xb4 \ No newline at end of file -- cgit v1.2.3