about summary refs log tree commit diff stats
path: root/source/main.c
blob: 81f30a383cb9e218f8080f6fbf6aa1c99b32d69c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (C) 2017 hatkirby
 * Copyright (C) 2017 slipstream/RoL
 * Copyright (C) 2016 FIX94
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "link.h"
#include "encoding.h"
#include "multiboot.h"

void printmain()
{
  printf("\x1b[2J");
  printf("\x1b[37m");
  printf("Pokemon Gen III Data Extractor by hatkirby\n");
  printf("Based on gba-gen3multiboot by slipstream/RoL\n");
  printf("Based on GBA Link Cable Dumper v1.6 by FIX94\n");
}

void endproc()
{
  printf("Start pressed, exit\n");
  VIDEO_WaitVSync();
  VIDEO_WaitVSync();
  exit(0);
}

u32 waitForButtons(u32 mask)
{
  for (;;)
  {
    PAD_ScanPads();
    VIDEO_WaitVSync();

    u32 btns = PAD_ButtonsDown(0);
    if (btns & mask)
    {
      return btns;
    }
  }
}

void warnError(char *msg)
{
  puts(msg);
  VIDEO_WaitVSync();
  VIDEO_WaitVSync();
  sleep(2);
}

void fatalError(char *msg)
{
  puts(msg);
  VIDEO_WaitVSync();
  VIDEO_WaitVSync();
  sleep(5);
  exit(0);
}

void* extractor(void* userdata)
{
  for (;;)
  {
    printmain();

    printf("Press A when GBA is plugged in and turned off.\n");
    waitForButtons(PAD_BUTTON_A);

    printf("Turn on your GBA.\n");
    printf("Waiting for a GBA in port 2...\n");
    waitForGBA();

    printf("GBA Found! Sending multiboot image...\n");
    if (!sendMultibootImage())
    {
      warnError("Failed sending multiboot image.\n");

      continue;
    }

    printf("Waiting for GBA...\n");
    waitForAck();

    VIDEO_WaitVSync();

    // Get game
    // -1 - unsupported game
    //  1 - Ruby
    //  2 - Sapphire
    //  3 - FireRed
    //  4 - LeafGreen
    //  5 - Emerald
    u32 gameId = getMsg();
    if (gameId == -1)
    {
      warnError("ERROR: Unsupported GBA game inserted!\n");

      continue;
    }

    printf("\nPokemon ");
    switch (gameId)
    {
      case 1: printf("Ruby"); break;
      case 2: printf("Sapphire"); break;
      case 3: printf("FireRed"); break;
      case 4: printf("LeafGreen"); break;
      case 5: printf("Emerald"); break;
    }

    printf("\n");
    VIDEO_WaitVSync();

    u32 isValid = getMsg();
    if (isValid == -1)
    {
      warnError("ERROR: Unsupported game version inserted!\n");

      continue;
    }

    // Get trainer name
    u8 trainerName[8];

    u32 tnd = getMsg();
    trainerName[0] = (tnd & 0xFF000000) >> 24;
    trainerName[1] = (tnd & 0x00FF0000) >> 16;
    trainerName[2] = (tnd & 0x0000FF00) >> 8;
    trainerName[3] = (tnd & 0x000000FF);

    tnd = getMsg();
    trainerName[4] = (tnd & 0xFF000000) >> 24;
    trainerName[5] = (tnd & 0x00FF0000) >> 16;
    trainerName[6] = (tnd & 0x0000FF00) >> 8;
    trainerName[7] = (tnd & 0x000000FF);

    // Get trainer ID
    u32 trainerId = getMsg();

    printf("Trainer: ");

    for (int i = 0; i < 8; i++)
    {
      if (trainerName[i] == 0xFF)
      {
        break;
      } else {
        printf("%c", debugGen3Decode(trainerName[i]));
      }
    }

    printf(" (%ld)\n", trainerId);

    // Wait for confirmation.
    printf("Press A to import the data from this game.\n");
    printf("Press B to cancel.\n");
    VIDEO_WaitVSync();

    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);

    printf("Saw: ");
    for (int i=0; i<(13*32); i++)
    {
      if (pokedexSeen[i >> 5] >> (i & 31) & 1)
      {
        printf("#%d, ", i+1);
      }
    }

    printf("\nCaught: ");
    for (int i=0; i<(13*32); i++)
    {
      if (pokedexCaught[i >> 5] >> (i & 31) & 1)
      {
        printf("#%d, ", i+1);
      }
    }

    printf("\n");

    waitForButtons(PAD_BUTTON_START);
  }

  return NULL;
}

int main(int argc, char *argv[])
{
  void *xfb = NULL;
  GXRModeObj *rmode = NULL;
  VIDEO_Init();
  rmode = VIDEO_GetPreferredMode(NULL);
  xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  VIDEO_Configure(rmode);
  VIDEO_SetNextFramebuffer(xfb);
  VIDEO_SetBlack(FALSE);
  VIDEO_Flush();
  VIDEO_WaitVSync();
  if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  int x = 24, y = 32, w, h;
  w = rmode->fbWidth - (32);
  h = rmode->xfbHeight - (48);
  CON_InitEx(rmode, x, y, w, h);
  VIDEO_ClearFrameBuffer(rmode, xfb, COLOR_BLACK);
  PAD_Init();

  initLink();

  lwp_t extractorHandle = (lwp_t)NULL;

  LWP_CreateThread(
    &extractorHandle,   // thread handle
    extractor,          // code
    NULL,               // userdata
    NULL,               // stack base
    16*1024,            // stack size
    LWP_PRIO_HIGHEST);  // thread priority

  for (;;)
  {
    VIDEO_WaitVSync();
    PAD_ScanPads();

    if (PAD_ButtonsDown(0) & PAD_BUTTON_START)
    {
      endproc();
    }
  }

  return 0;
}