From 3bb0c7caa454479ced73d41f20c936fbb9de09b1 Mon Sep 17 00:00:00 2001
From: Francisco Javier Trujillo Mata <fjtrujy@gmail.com>
Date: Sun, 6 Jan 2019 23:26:34 +0100
Subject: [PATCH 1/2] Disable the time function, after start core is not
 working fine.

---
 ps2/compat_files/time.c | 42 +++++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/ps2/compat_files/time.c b/ps2/compat_files/time.c
index 328fac7522..2a7e672892 100755
--- a/ps2/compat_files/time.c
+++ b/ps2/compat_files/time.c
@@ -70,24 +70,7 @@ static time_t _gmtotime_t (
    return seconds_from_1970;
 }
 
-/* Protected methods in libc */ 
-void _ps2sdk_time_init(void)
-{
-   SDL_Init(SDL_INIT_TIMER);
-}
-
-/* Protected methods in libc */
-void _ps2sdk_time_deinit(void)
-{
-   SDL_QuitSubSystem(SDL_INIT_TIMER);
-}
-
-clock_t clock(void)
-{
-   return SDL_GetTicks();
-}
-
-time_t time(time_t *t) {
+time_t ps2_time(time_t *t) {
    time_t tim;
    sceCdCLOCK clocktime; /* defined in libcdvd.h */
 
@@ -106,3 +89,26 @@ time_t time(time_t *t) {
 		
 	return tim;
 }
+
+/* Protected methods in libc */ 
+void _ps2sdk_time_init(void)
+{
+   SDL_Init(SDL_INIT_TIMER);
+}
+
+/* Protected methods in libc */
+void _ps2sdk_time_deinit(void)
+{
+   SDL_QuitSubSystem(SDL_INIT_TIMER);
+}
+
+clock_t clock(void)
+{
+   return SDL_GetTicks();
+}
+
+time_t time(time_t *t) {
+   time_t tim = -1;
+   /* TODO: This function need to be implemented again because the SDK one is not working fine */
+   return time;
+}

From f48ffb9aac479a816dbc8ded6aef36aa6a548c96 Mon Sep 17 00:00:00 2001
From: Francisco Javier Trujillo Mata <fjtrujy@gmail.com>
Date: Mon, 7 Jan 2019 00:25:45 +0100
Subject: [PATCH 2/2] Open RetroArch Menu Long Pressing Start button

---
 audio/drivers/ps2_audio.c         | 74 +++++++++++++-------------
 config.def.h                      |  2 +
 gfx/drivers/ps2_gfx.c             | 86 +++++++++++++++----------------
 input/drivers_joypad/ps2_joypad.c | 12 ++---
 retroarch.c                       |  2 +-
 5 files changed, 87 insertions(+), 89 deletions(-)

diff --git a/audio/drivers/ps2_audio.c b/audio/drivers/ps2_audio.c
index f47ef9fc9d..a74dd7b5e0 100644
--- a/audio/drivers/ps2_audio.c
+++ b/audio/drivers/ps2_audio.c
@@ -81,26 +81,27 @@ static void audioCreateThread(ps2_audio_t *ps2)
    thread.attr=thread.option=0;
 
    /*Backup the PS2 content to be used in the thread */
-   backup_ps2         = ps2;
+   backup_ps2 = ps2;
 
-   ps2->running       = true;
+   ps2->running = true;
    ps2->worker_thread = CreateThread(&thread);
 
-   if (ps2->worker_thread >= 0)
-   {
+   if (ps2->worker_thread >= 0) {
       ret = StartThread(ps2->worker_thread, NULL);
-      if (ret < 0)
+      if (ret < 0) {
          printf("sound_init: StartThread returned %d\n", ret);
-   }
-   else
+      }
+   } else {
       printf("CreateThread failed: %d\n", ps2->worker_thread);
+   }
 }
 
 static void audioStopNDeleteThread(ps2_audio_t *ps2)
 {
    ps2->running = false;
-   if (ps2->worker_thread)
+   if (ps2->worker_thread) {
       ps2->worker_thread = 0;
+   }
 }
 
 static void audioConfigure(ps2_audio_t *ps2, unsigned rate)
@@ -108,14 +109,13 @@ static void audioConfigure(ps2_audio_t *ps2, unsigned rate)
    int err;
    struct audsrv_fmt_t format;
 
-   format.bits     = AUDIO_BITS;
-   format.freq     = rate;
+   format.bits = AUDIO_BITS;
+   format.freq = rate;
    format.channels = AUDIO_CHANNELS;
 
-   err             = audsrv_set_format(&format);
+   err = audsrv_set_format(&format);
 
-   if (err)
-   {
+   if (err){
       printf("set format returned %d\n", err);
       printf("audsrv returned error string: %s\n", audsrv_get_error_string());
    }
@@ -128,16 +128,16 @@ static void audioCreateSemas(ps2_audio_t *ps2)
    ee_sema_t lock_info;
    ee_sema_t cond_lock_info;
 
-   lock_info.max_count       = 1;
-   lock_info.init_count      = 1;
-   lock_info.option          = 0;
-   ps2->lock                 = CreateSema(&lock_info);
+   lock_info.max_count = 1;
+   lock_info.init_count = 1;
+   lock_info.option = 0;
+   ps2->lock = CreateSema(&lock_info);
 
    cond_lock_info.init_count = 1;
    cond_lock_info.max_count  = 1;
-   cond_lock_info.option     = 0;
+   cond_lock_info.option = 0;
 
-   ps2->cond_lock            = CreateSema(&cond_lock_info);
+   ps2->cond_lock = CreateSema(&cond_lock_info);
 }
 
 static void *ps2_audio_init(const char *device,
@@ -164,18 +164,15 @@ static void ps2_audio_free(void *data)
    if(!ps2)
       return;
 
-   if(ps2->running)
-   {
+   if(ps2->running){
       audioStopNDeleteThread(ps2);
 
-      if (ps2->lock)
-      {
+      if (ps2->lock){
          iDeleteSema(ps2->lock);
          ps2->lock = 0;
       }
 
-      if (ps2->cond_lock)
-      {
+      if (ps2->cond_lock){
          iDeleteSema(ps2->cond_lock);
          ps2->cond_lock = 0;
       }
@@ -192,14 +189,14 @@ static ssize_t ps2_audio_write(void *data, const void *buf, size_t size)
    if (!ps2->running)
       return -1;
 
-   if (ps2->nonblocking)
-   {
+   if (ps2->nonblocking){
       if (fifo_write_avail(ps2->buffer) < size)
          return 0;
    }
 
-   while (fifo_write_avail(ps2->buffer) < size)
+   while (fifo_write_avail(ps2->buffer) < size) {
       WaitSema(ps2->cond_lock);
+   }
 
    WaitSema(ps2->lock);
    fifo_write(ps2->buffer, buf, size);
@@ -213,8 +210,9 @@ static bool ps2_audio_alive(void *data)
    bool alive = false;
 
    ps2_audio_t* ps2 = (ps2_audio_t*)data;
-   if (ps2)
+   if (ps2) {
       alive = ps2->running;
+   }
 
    return alive;
 }
@@ -224,8 +222,7 @@ static bool ps2_audio_stop(void *data)
    bool stop = true;
    ps2_audio_t* ps2 = (ps2_audio_t*)data;
 
-   if (ps2)
-   {
+   if (ps2) {
       audioStopNDeleteThread(ps2);
       audsrv_stop_audio();
    }
@@ -235,13 +232,13 @@ static bool ps2_audio_stop(void *data)
 
 static bool ps2_audio_start(void *data, bool is_shutdown)
 {
-   bool       start = true;
    ps2_audio_t* ps2 = (ps2_audio_t*)data;
+   bool start = true;
 
-   if(ps2)
-   {
-      if (!ps2->running && !ps2->worker_thread)
+   if (ps2) {
+      if (!ps2->running && !ps2->worker_thread) {
          audioCreateThread(ps2);
+      }
    }
 
    return start;
@@ -250,8 +247,10 @@ static bool ps2_audio_start(void *data, bool is_shutdown)
 static void ps2_audio_set_nonblock_state(void *data, bool toggle)
 {
    ps2_audio_t* ps2 = (ps2_audio_t*)data;
-   if (ps2)
+
+   if (ps2) {
       ps2->nonblocking = toggle;
+   }
 }
 
 static bool ps2_audio_use_float(void *data)
@@ -263,8 +262,7 @@ static size_t ps2_audio_write_avail(void *data)
 {
    ps2_audio_t* ps2 = (ps2_audio_t*)data;
 
-   if (ps2 && ps2->running)
-   {
+   if (ps2 && ps2->running) {
       size_t size;
       WaitSema(ps2->lock);
       size = AUDIO_BUFFER - fifo_read_avail(ps2->buffer); 
diff --git a/config.def.h b/config.def.h
index b5a3a63b47..c3011d1a24 100644
--- a/config.def.h
+++ b/config.def.h
@@ -401,6 +401,8 @@ static bool default_screenshots_in_content_dir = false;
 
 #if defined(__CELLOS_LV2__) || defined(_XBOX1) || defined(_XBOX360)
 static unsigned menu_toggle_gamepad_combo    = INPUT_TOGGLE_L3_R3;
+#elif defined(PS2)
+static unsigned menu_toggle_gamepad_combo    = INPUT_TOGGLE_HOLD_START;
 #elif defined(VITA)
 static unsigned menu_toggle_gamepad_combo    = INPUT_TOGGLE_L1_R1_START_SELECT;
 #elif defined(SWITCH)
diff --git a/gfx/drivers/ps2_gfx.c b/gfx/drivers/ps2_gfx.c
index ea4383adcf..fc3d29bf62 100644
--- a/gfx/drivers/ps2_gfx.c
+++ b/gfx/drivers/ps2_gfx.c
@@ -45,7 +45,7 @@ typedef struct ps2_video
 
 // PRIVATE METHODS
 static GSGLOBAL *init_GSGlobal(void) {
-	GSGLOBAL *gsGlobal = gsKit_init_global();
+   GSGLOBAL *gsGlobal = gsKit_init_global();
 
    gsGlobal->Mode = GS_MODE_NTSC;
    gsGlobal->Interlace = GS_INTERLACED;
@@ -54,28 +54,26 @@ static GSGLOBAL *init_GSGlobal(void) {
    gsGlobal->Height = NTSC_HEIGHT;
    
    gsGlobal->PSM = GS_PSM_CT16;
-	gsGlobal->PSMZ = GS_PSMZ_16;
-	gsGlobal->DoubleBuffering = GS_SETTING_OFF;
-	gsGlobal->ZBuffering = GS_SETTING_OFF;
+   gsGlobal->PSMZ = GS_PSMZ_16;
+   gsGlobal->DoubleBuffering = GS_SETTING_OFF;
+   gsGlobal->ZBuffering = GS_SETTING_OFF;
    gsGlobal->PrimAlphaEnable = GS_SETTING_OFF;
 
-	dmaKit_init(D_CTRL_RELE_OFF,D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC,
-		    D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF);
+   dmaKit_init(D_CTRL_RELE_OFF,D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC, 
+               D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF);
 
-	// Initialize the DMAC
-	dmaKit_chan_init(DMA_CHANNEL_GIF);
+   /* Initialize the DMAC */
+   dmaKit_chan_init(DMA_CHANNEL_GIF);
 
-	gsKit_init_screen(gsGlobal);
-      gsKit_mode_switch(gsGlobal, GS_ONESHOT);
+   gsKit_init_screen(gsGlobal);
+   gsKit_mode_switch(gsGlobal, GS_ONESHOT);
 
-	gsKit_clear(gsGlobal, GS_BLACK);
-
-      return gsGlobal;
+   return gsGlobal;
 }
 
 static GSTEXTURE * prepare_new_texture(void) {
-      GSTEXTURE *texture = calloc(1, sizeof(*texture));
-      return texture;
+   GSTEXTURE *texture = calloc(1, sizeof(*texture));
+   return texture;
 }
 
 static void init_ps2_video(ps2_video_t *ps2) {
@@ -93,22 +91,22 @@ static void deinitTexture(GSTEXTURE *texture) {
 
 static void color_correction32(uint32_t *buffer, uint32_t dimensions)
 {
-    uint32_t i;
-    uint32_t x32;
-    for (i = 0; i < dimensions; i++) {
+   uint32_t i;
+   uint32_t x32;
+   for (i = 0; i < dimensions; i++) {
       x32 = buffer[i];
       buffer[i] = ((x32 >> 16) & 0xFF) | ((x32 << 16) & 0xFF0000) | (x32 & 0xFF00FF00);
-    }
+   }
 }
 
 static void color_correction16(uint16_t *buffer, uint32_t dimensions)
 {
-    uint32_t i;
-    uint16_t x16;
-    for (i = 0; i < dimensions; i++) {
+   uint32_t i;
+   uint16_t x16;
+   for (i = 0; i < dimensions; i++) {
       x16 = buffer[i];
       buffer[i] = (x16 & 0x8000) | ((x16 << 10) & 0x7C00) | ((x16 >> 1) & 0x3E0) | ((x16 >> 11) & 0x1F);
-    }
+   }
 }
 
 static void transfer_texture(GSTEXTURE *texture, const void *frame, 
@@ -144,7 +142,7 @@ if (color_correction) {
 
 static void vram_alloc(GSGLOBAL *gsGlobal, GSTEXTURE *texture) {
    uint32_t size = gsKit_texture_size(texture->Width, texture->Height, texture->PSM);
-   texture->Vram=gsKit_vram_alloc(gsGlobal, size, GSKIT_ALLOC_USERBUFFER);
+   texture->Vram = gsKit_vram_alloc(gsGlobal, size, GSKIT_ALLOC_USERBUFFER);
    if(texture->Vram == GSKIT_ALLOC_ERROR) {
       printf("VRAM Allocation Failed. Will not upload texture.\n");
    }
@@ -152,24 +150,24 @@ static void vram_alloc(GSGLOBAL *gsGlobal, GSTEXTURE *texture) {
 
 static void prim_texture(GSGLOBAL *gsGlobal, GSTEXTURE *texture, int zPosition, bool force_aspect) {
       float x1, y1, x2, y2;
-      if (force_aspect) {
-         float width_proportion = (float)gsGlobal->Width / (float)texture->Width;
-         float height_proportion = (float)gsGlobal->Height / (float)texture->Height;
-         float delta = MIN(width_proportion, height_proportion);
-         float newWidth = texture->Width * delta;
-         float newHeight = texture->Height * delta;
-         
-         x1 = (gsGlobal->Width - newWidth) / 2.0f;
-         y1 = (gsGlobal->Height - newHeight) / 2.0f;
-         x2 = newWidth + x1;
-         y2 = newHeight + y1;
+   if (force_aspect) {
+      float width_proportion = (float)gsGlobal->Width / (float)texture->Width;
+      float height_proportion = (float)gsGlobal->Height / (float)texture->Height;
+      float delta = MIN(width_proportion, height_proportion);
+      float newWidth = texture->Width * delta;
+      float newHeight = texture->Height * delta;
+      
+      x1 = (gsGlobal->Width - newWidth) / 2.0f;
+      y1 = (gsGlobal->Height - newHeight) / 2.0f;
+      x2 = newWidth + x1;
+      y2 = newHeight + y1;
 
-      } else {
-         x1 = 0.0f;
-         y1 = 0.0f;
-         x2 = gsGlobal->Width;
-         y2 = gsGlobal->Height;
-      }
+   } else {
+      x1 = 0.0f;
+      y1 = 0.0f;
+      x2 = gsGlobal->Width;
+      y2 = gsGlobal->Height;
+   }
 
    gsKit_prim_sprite_texture( gsGlobal, texture,
                               x1, //X1
@@ -225,13 +223,15 @@ static bool ps2_gfx_frame(void *data, const void *frame,
    static float fps = 0.0;
 #endif
    ps2_video_t *ps2 = (ps2_video_t*)data;
+   bool texture_empty = true;
 
    if (!width || !height)
       return false;
 
    if (frame_count%120==0) {
-      printf("ps2_gfx_frame %d\n", frame_count);
+      printf("ps2_gfx_frame %lu\n", frame_count);
    }
+   gsKit_clear(ps2->gsGlobal, GS_BLACK);
    gsKit_vram_clear(ps2->gsGlobal);
 
    if (frame) {
@@ -241,7 +241,7 @@ static bool ps2_gfx_frame(void *data, const void *frame,
       prim_texture(ps2->gsGlobal, ps2->coreTexture, 1, ps2->force_aspect);
    }
 
-   bool texture_empty = !ps2->menuTexture->Width || !ps2->menuTexture->Height;
+   texture_empty = !ps2->menuTexture->Width || !ps2->menuTexture->Height;
    if (ps2->menuVisible && !texture_empty) {
       vram_alloc(ps2->gsGlobal, ps2->menuTexture);
       gsKit_texture_upload(ps2->gsGlobal, ps2->menuTexture);
diff --git a/input/drivers_joypad/ps2_joypad.c b/input/drivers_joypad/ps2_joypad.c
index 35d05cf3e9..937a78fa9e 100644
--- a/input/drivers_joypad/ps2_joypad.c
+++ b/input/drivers_joypad/ps2_joypad.c
@@ -32,6 +32,7 @@ static char padBuf[256] __attribute__((aligned(64)));
 
 static uint64_t pad_state[PS2_MAX_PADS];
 
+extern uint64_t lifecycle_state;
 
 static const char *ps2_joypad_name(unsigned pad)
 {
@@ -45,6 +46,8 @@ static bool ps2_joypad_init(void *data)
 
    for (i = 0; i < players_count; i++)
    {
+      int ret, port, slot;
+
       bool auto_configure = input_autoconfigure_connect( ps2_joypad_name(i),
                                                          NULL,
                                                          ps2_joypad.ident,
@@ -57,9 +60,6 @@ static bool ps2_joypad_init(void *data)
 
       padInit(i);
 
-      int ret;
-      int port, slot;
-
       port = 0; // 0 -> Connector 1, 1 -> Connector 2
       slot = 0; // Always zero if not using multitap
 
@@ -101,15 +101,13 @@ static void ps2_joypad_poll(void)
    unsigned players_count = PS2_MAX_PADS;
    struct padButtonStatus buttons;
 
-   for (player = 0; player < players_count; player++)
-   {
+   for (player = 0; player < players_count; player++) {
       unsigned j, k;
       unsigned i  = player;
       unsigned p  = player;
    
       int ret = padRead(player, player, &buttons); // port, slot, buttons
-      if (ret != 0)
-      {
+      if (ret != 0) {
          int32_t state_tmp = 0xffff ^ buttons.btns;
 
          pad_state[i] = 0;
diff --git a/retroarch.c b/retroarch.c
index a617032b1d..ca1940e3dd 100644
--- a/retroarch.c
+++ b/retroarch.c
@@ -3716,4 +3716,4 @@ struct retro_system_info *runloop_get_libretro_system_info(void)
 char *get_retroarch_launch_arguments(void)
 {
    return launch_arguments;
-}
\ No newline at end of file
+}