Use posix functions for PSP

This commit is contained in:
Francisco Javier Trujillo Mata 2023-11-15 19:36:26 +01:00 committed by irixxxx
parent f2554438f8
commit 4b2223986c
2 changed files with 43 additions and 115 deletions

View file

@ -7,6 +7,9 @@
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <malloc.h>
#include <errno.h>
#include <unistd.h>
#include "../common/emu.h"
#include "../libpicofe/menu.h"
@ -137,42 +140,61 @@ int plat_get_data_dir(char *dst, int len)
/* check if path is a directory */
int plat_is_dir(const char *path)
{
SceIoStat st;
int ret = sceIoGetstat(path, &st);
return (ret >= 0 && (st.st_mode & FIO_S_IFDIR));
DIR *dir;
if ((dir = opendir(path))) {
closedir(dir);
return 1;
}
return 0;
}
/* current time in ms */
unsigned int plat_get_ticks_ms(void)
{
struct timeval tv;
unsigned int ret;
gettimeofday(&tv, NULL);
ret = (unsigned)tv.tv_sec * 1000;
/* approximate /= 1000 */
unsigned long long v64;
v64 = (unsigned long long)plat_get_ticks_us() * 4294968;
return v64 >> 32;
ret += ((unsigned)tv.tv_usec * 4195) >> 22;
return ret;
}
/* current time in us */
unsigned int plat_get_ticks_us(void)
{
return sceKernelGetSystemTimeLow();
struct timeval tv;
unsigned int ret;
gettimeofday(&tv, NULL);
ret = (unsigned)tv.tv_sec * 1000000;
ret += (unsigned)tv.tv_usec;
return ret;
}
/* sleep for some time in ms */
void plat_sleep_ms(int ms)
{
psp_msleep(ms);
usleep(ms * 1000);
}
/* sleep for some time in us */
void plat_wait_till_us(unsigned int us_to)
{
unsigned int tval;
int diff;
unsigned int now;
tval = sceKernelGetSystemTimeLow();
diff = (int)us_to - (int)tval;
if (diff >= 512 && diff < 100*1024)
sceKernelDelayThread(diff);
now = plat_get_ticks_us();
while ((signed int)(us_to - now) > 512)
{
usleep(1024);
now = plat_get_ticks_us();
}
}
/* wait until some event occurs, or timeout */
@ -237,106 +259,6 @@ struct plat_target plat_target = {
// .hwfilters = plat_hwfilters,
};
#ifndef DT_DIR
/* replacement libc stuff */
int alphasort(const struct dirent **a, const struct dirent **b)
{
return strcoll ((*a)->d_name, (*b)->d_name);
}
int scandir(const char *dir, struct dirent ***namelist_out,
int(*filter)(const struct dirent *),
int(*compar)(const struct dirent **, const struct dirent **))
{
int ret = -1, dir_uid = -1, name_alloc = 4, name_count = 0;
struct dirent **namelist = NULL, *ent;
SceIoDirent sce_ent;
namelist = malloc(sizeof(*namelist) * name_alloc);
if (namelist == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
// try to read first..
dir_uid = sceIoDopen(dir);
if (dir_uid >= 0)
{
/* it is very important to clear SceIoDirent to be passed to sceIoDread(), */
/* or else it may crash, probably misinterpreting something in it. */
memset(&sce_ent, 0, sizeof(sce_ent));
ret = sceIoDread(dir_uid, &sce_ent);
if (ret < 0)
{
lprintf("sceIoDread(\"%s\") failed with %i\n", dir, ret);
goto fail;
}
}
else
lprintf("sceIoDopen(\"%s\") failed with %i\n", dir, dir_uid);
while (ret > 0)
{
ent = malloc(sizeof(*ent));
if (ent == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
ent->d_stat = sce_ent.d_stat;
ent->d_stat.st_attr &= FIO_SO_IFMT; // serves as d_type
strncpy(ent->d_name, sce_ent.d_name, sizeof(ent->d_name));
ent->d_name[sizeof(ent->d_name)-1] = 0;
if (filter == NULL || filter(ent))
namelist[name_count++] = ent;
else free(ent);
if (name_count >= name_alloc)
{
void *tmp;
name_alloc *= 2;
tmp = realloc(namelist, sizeof(*namelist) * name_alloc);
if (tmp == NULL) { lprintf("%s:%i: OOM\n", __FILE__, __LINE__); goto fail; }
namelist = tmp;
}
memset(&sce_ent, 0, sizeof(sce_ent));
ret = sceIoDread(dir_uid, &sce_ent);
}
// sort
if (compar != NULL && name_count > 3)
qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), (int (*)()) compar);
// all done.
ret = name_count;
*namelist_out = namelist;
goto end;
fail:
if (namelist != NULL)
{
while (name_count--)
free(namelist[name_count]);
free(namelist);
}
end:
if (dir_uid >= 0) sceIoDclose(dir_uid);
return ret;
}
/* stubs for libflac (embedded in libchdr) */
#include <utime.h>
int chown(const char *pathname, uid_t owner, gid_t group) { return -1; }
int chmod(const char *pathname, mode_t mode) { return -1; }
int utime(const char *filename, const struct utimbuf *times) { return -1; }
#endif
#include <malloc.h>
#include <errno.h>
int posix_memalign(void **p, size_t align, size_t size)
{
if (p)
*p = memalign(align, size);
return (p ? *p ? 0 : ENOMEM : EINVAL);
}
int _flush_cache (char *addr, const int size, const int op)
{
//sceKernelDcacheWritebackAll();
@ -344,3 +266,10 @@ int _flush_cache (char *addr, const int size, const int op)
sceKernelIcacheInvalidateRange(addr, size);
return 0;
}
int posix_memalign(void **p, size_t align, size_t size)
{
if (p)
*p = memalign(align, size);
return (p ? *p ? 0 : ENOMEM : EINVAL);
}

View file

@ -31,7 +31,6 @@ extern int pico_main(int argc, char *argv[]);
#ifndef FW15
PSP_MODULE_INFO("PicoDrive", 0, 1, 97);
PSP_HEAP_SIZE_MAX();
int main(int argc, char *argv[]) { return pico_main(argc, argv); } /* just a wrapper */