Problem porting SDL2 to Playstation2


I am trying to port SDL2 to PlayStation2, using ps2dev toolchain. The system is limited, but there’s already a SDL1.2 port, so i believe its possible.

Following the /docs and a insightful post, I managed to build a Makefile, and compile it using dummy drivers. Then i changed GetPlatform(), and started to code the joystick driver…

But i found a problem… I linked a test program (just SDL_VERSION macro, SDL_GetPlatform(), SDL_Init() and SDL_Quit()) against the generated library without problems, and tried to run it.

I used a real PS2, and two emulators (PCSX2 and Play!). i got the output of SDL_VERSION and SDL_GetPlatform() correctly, but it hanged on SDL_Init().

As the PS2 have very limited debugging, i inserted printf()'s in several places, to trace execution.

In src/SDL.c seems that SDL_Init() call SDL_InitSubSystem(), that call SDL_LogInit(). In src/SDL_log.c, SDL_LogInit() calls SDL_CreateMutex(), that never returns.

So I inserted several printf()'s in src/thread/generic/SDL_sysmutex.c at SDL_CreateMutex(), after each line (13 in total).

The first printf(), at top of SDL_CreateMutex(), is called 196909 (!) times in a row… Then the debug log show SDL_malloc() finally fail, code follow till SDL_free() (coz SDL_CreateSemaphore() dont returned 0). This happens 3 times till it finally hangs.

The debug log looks like:

  • SDL version 2.23.0 (PlayStation 2)
  • #SDL_Init
  • #SDL_InitSubSystem
  • #1
  • *SDL_LogInit
  • *1
  • @SDL_CreateMutex
  • @SDL_CreateMutex … 196909 times!

and then

  • @SDL_CreateMutex
  • @SDL_CreateMutex
  • @1
  • @10
  • @11
  • @12
  • @1
  • @2
  • @3
  • @4
  • @5
  • @6
  • @SDL_CreateMutex

3 times. And it hangs.

So, seems the problem is that SDL_CreateMutex() call SDL_malloc(), that call again SDL_CreateMutex() again, in a circle… Till I run out of stack space thanks to this recursion.

I tried to check in SDL_malloc.c how i could make that SDL_malloc() just point to system’s malloc() (and SDL_free() just call free(), etc), but i couldn’t figure out.


Can somebody point me in the right direction?