From 252e2911383a5267673f00c08419e2afeac35a31 Mon Sep 17 00:00:00 2001
From: Kelly Rauchenberger <fefferburbia@gmail.com>
Date: Thu, 13 Jul 2017 15:22:55 -0400
Subject: App now waits for confirmation to import, and reads pokedex
 seen&caught

---
 gba/source/link.c | 22 +++++++++++++++++++++
 gba/source/link.h |  2 ++
 gba/source/main.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 source/main.c     | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/gba/source/link.c b/gba/source/link.c
index acadf3b..26443c8 100644
--- a/gba/source/link.c
+++ b/gba/source/link.c
@@ -27,6 +27,21 @@ void waitForAck()
   REG_HS_CTRL |= JOY_RW;
 }
 
+u32 waitForResponse()
+{
+  u32 val;
+
+  REG_JOYTR = 1;
+  while ((REG_HS_CTRL & JOY_WRITE) == 0);
+  val = REG_JOYRE;
+  REG_HS_CTRL |= JOY_RW;
+  REG_JOYTR = 0;
+  while ((REG_HS_CTRL & JOY_WRITE) == 0);
+  REG_HS_CTRL |= JOY_RW;
+
+  return val;
+}
+
 void sendS32(s32 val)
 {
   REG_JOYTR = val;
@@ -36,3 +51,10 @@ void sendU32(u32 val)
 {
   REG_JOYTR = val;
 }
+
+void directSendU32(u32 val)
+{
+  REG_JOYTR = val;
+  while ((REG_HS_CTRL & JOY_READ) == 0);
+  REG_HS_CTRL |= JOY_RW;
+}
diff --git a/gba/source/link.h b/gba/source/link.h
index 08fd998..38d1c39 100644
--- a/gba/source/link.h
+++ b/gba/source/link.h
@@ -11,7 +11,9 @@
 
 void initializeLink();
 void waitForAck();
+u32 waitForResponse();
 void sendS32(s32 val);
 void sendU32(u32 val);
+void directSendU32(u32 val);
 
 #endif
diff --git a/gba/source/main.c b/gba/source/main.c
index 6207685..14d2f1d 100644
--- a/gba/source/main.c
+++ b/gba/source/main.c
@@ -10,6 +10,10 @@
 
 int main(void)
 {
+  // This possibly increases stability, I don't rightly know, this is all black
+  // magic, will test more later.
+  REG_IME = 0;
+
   initializeLink();
 
   // Identify the host game.
@@ -105,4 +109,57 @@ int main(void)
 
   sendU32(tti);
   waitForAck();
+
+  // Does the player want to import this game?
+  if (waitForResponse() == 0)
+  {
+    return 0;
+  }
+
+  // Send Pokédex data
+  u8* pokedexSeen = 0;
+  if (GAME_RS)
+  {
+    pokedexSeen = SaveBlock2->rs.pokedex.seen;
+  } else if (GAME_FRLG)
+  {
+    pokedexSeen = SaveBlock2->frlg.pokedex.seen;
+  } else if (GAME_EM)
+  {
+    pokedexSeen = SaveBlock2->e.pokedex.seen;
+  }
+
+  for (int i=0; i<13; i++)
+  {
+    u32 psi =
+        (pokedexSeen[i*4])
+      | (pokedexSeen[i*4+1] << 8)
+      | (pokedexSeen[i*4+2] << 16)
+      | (pokedexSeen[i*4+3] << 24);
+
+    directSendU32(psi);
+  }
+
+  u8* pokedexCaught = 0;
+  if (GAME_RS)
+  {
+    pokedexCaught = SaveBlock2->rs.pokedex.owned;
+  } else if (GAME_FRLG)
+  {
+    pokedexCaught = SaveBlock2->frlg.pokedex.owned;
+  } else if (GAME_EM)
+  {
+    pokedexCaught = SaveBlock2->e.pokedex.owned;
+  }
+
+  for (int i=0; i<13; i++)
+  {
+    u32 psi =
+        (pokedexCaught[i*4])
+      | (pokedexCaught[i*4+1] << 8)
+      | (pokedexCaught[i*4+2] << 16)
+      | (pokedexCaught[i*4+3] << 24);
+
+    directSendU32(psi);
+  }
 }
diff --git a/source/main.c b/source/main.c
index 128feb9..dd252b5 100644
--- a/source/main.c
+++ b/source/main.c
@@ -240,12 +240,29 @@ u32 getMsg()
   }
 
   send(0);
-  while (recv()!=0) {sleep(1);};
+  while (recv()!=0) {sleep(1);}
   send(0);
 
   return val;
 }
 
+void getMsgArr(u32* arr, int len)
+{
+  for (int i=0; i<len; i++)
+  {
+    *(vu32*)(arr+i) = __builtin_bswap32(recv());
+    usleep(500000);
+  }
+}
+
+void sendMsg(u32 msg)
+{
+  while (recv()==0) {sleep(1);}
+  send(msg);
+  while (recv()!=0) {sleep(1);}
+  send(0);
+}
+
 int main(int argc, char *argv[])
 {
   void *xfb = NULL;
@@ -520,8 +537,44 @@ int main(int argc, char *argv[])
 
       if (waitForButtons(PAD_BUTTON_A | PAD_BUTTON_B) & PAD_BUTTON_B)
       {
+        printf("Cancelling...\n");
+        VIDEO_WaitVSync();
+
+        sendMsg(0);
+
         continue;
       }
+
+      printf("Importing...\n");
+      VIDEO_WaitVSync();
+
+      sendMsg(1);
+
+      // Get Pokédex data
+      u32 pokedexSeen[13];
+      u32 pokedexCaught[13];
+
+      getMsgArr(pokedexSeen, 13);
+      getMsgArr(pokedexCaught, 13);
+      int numCaught = 0;
+      int numSeen = 0;
+      for (int i=0; i<(13*32); i++)
+      {
+        if (pokedexCaught[i >> 5] >> (i & 31) & 1)
+        {
+          //printf("Caught #%d\n", i);
+          numCaught++;
+          numSeen++;
+        } else if (pokedexSeen[i >> 5] >> (i & 31) & 1)
+        {
+          //printf("Saw #%d\n", i);
+          numSeen++;
+        }
+      }
+
+      printf("Caught: %d\nSeen: %d\n", numCaught, numSeen);
+
+      waitForButtons(PAD_BUTTON_START);
     }
   }
 
-- 
cgit 1.4.1