Clean up berry_fix_program

This commit is contained in:
GriffinR
2021-02-24 16:45:18 -05:00
parent f172ff84c9
commit 2dfa84c10f
+150 -128
View File
@@ -12,40 +12,42 @@
#include "text.h" #include "text.h"
#include "menu.h" #include "menu.h"
#include "m4a.h" #include "m4a.h"
#include "constants/rgb.h"
// Static type declarations struct {
typedef struct {
u8 state; u8 state;
u8 unk1; u8 curScene;
u16 unk2; u16 timer;
struct MultiBootParam mb; struct MultiBootParam mb;
} berryfix_t; } static * sBerryFix;
// Static RAM declarations static void BerryFix_Main(void);
static void BerryFix_GpuSet(void);
static berryfix_t *berry_fix_mb_manager; static int BerryFix_TrySetScene(int);
static void BerryFix_SetScene(int);
// Static ROM declarations static void BerryFix_HideScene(void);
static void berry_fix_main(void);
static void berry_fix_gpu_set(void);
static int berry_fix_text_update(int);
static void berry_fix_text_print(int);
static void berry_fix_bg_hide(void);
// .rodata
static const u8 sText_BerryProgramUpdate[] = _("Berry Program Update"); static const u8 sText_BerryProgramUpdate[] = _("Berry Program Update");
static const u8 sText_RubySapphire[] = _("Ruby/Sapphire"); static const u8 sText_RubySapphire[] = _("Ruby/Sapphire");
static const u8 sText_Emerald[] = _("Emerald"); static const u8 sText_Emerald[] = _("Emerald");
static const u8 sText_BerryProgramWillBeUpdatedPressA[] = _("The Berry Program on your POKéMON\n"
static const u8 sText_BerryProgramWillBeUpdatedPressA[] = _("The Berry Program on your POKéMON\nRuby/Sapphire Game Pak will be updated.\n{COLOR RED}{SHADOW LIGHT_RED}Press the A Button."); "Ruby/Sapphire Game Pak will be updated.\n"
static const u8 sText_EnsureGBAConnectionMatches[] = _("Please ensure the connection of your\nGame Boy Advance system matches this.\n{COLOR RED}{SHADOW LIGHT_RED}YES: Press the A Button.\nNO: Turn off the power and try again."); "{COLOR RED}{SHADOW LIGHT_RED}Press the A Button.");
static const u8 sText_TurnOffPowerHoldingStartSelect[] = _("Please turn on the power of POKéMON\nRuby/Sapphire while holding START and\nSELECT simultaneously. Then, ensure\nthe picture above appears."); static const u8 sText_EnsureGBAConnectionMatches[] = _("Please ensure the connection of your\n"
static const u8 sText_TransmittingPleaseWait[] = _("Transmitting. Please wait.\n{COLOR RED}{SHADOW LIGHT_RED}Please do not turn off the power or\nunplug the Game Boy Advance Game\nLink Cable."); "Game Boy Advance system matches this.\n"
static const u8 sText_PleaseFollowInstructionsOnScreen[] = _("Please follow the instructions on your\nPOKéMON Ruby/Sapphire screen."); "{COLOR RED}{SHADOW LIGHT_RED}YES: Press the A Button.\n"
static const u8 sText_TransmissionFailureTryAgain[] = _("Transmission failure.\n{COLOR RED}{SHADOW LIGHT_RED}Please try again."); "NO: Turn off the power and try again.");
static const u8 sText_TurnOffPowerHoldingStartSelect[] = _("Please turn on the power of POKéMON\n"
"Ruby/Sapphire while holding START and\n"
"SELECT simultaneously. Then, ensure\n"
"the picture above appears.");
static const u8 sText_TransmittingPleaseWait[] = _("Transmitting. Please wait.\n"
"{COLOR RED}{SHADOW LIGHT_RED}Please do not turn off the power or\n"
"unplug the Game Boy Advance Game\nLink Cable.");
static const u8 sText_PleaseFollowInstructionsOnScreen[] = _("Please follow the instructions on your\n"
"POKéMON Ruby/Sapphire screen.");
static const u8 sText_TransmissionFailureTryAgain[] = _("Transmission failure.\n"
"{COLOR RED}{SHADOW LIGHT_RED}Please try again.");
static const struct BgTemplate sBerryFixBgTemplates[] = { static const struct BgTemplate sBerryFixBgTemplates[] = {
{ {
@@ -108,52 +110,66 @@ static const struct WindowTemplate sBerryFixWindowTemplates[] = {
DUMMY_WIN_TEMPLATE DUMMY_WIN_TEMPLATE
}; };
static const u16 sUnknown_08618138[] = { static const u16 sBerryFixPalColors[] = {
0x7fff, 0x7fff, 0x318c, 0x675a, RGB_WHITE, RGB_WHITE, RGB(12, 12, 12), RGB(26, 26, 25),
0x043c, 0x3aff, 0x0664, 0x4bd2, RGB(28, 1, 1), RGB(31, 23, 14), RGB(4, 19, 1), RGB(18, 30, 18),
0x6546, 0x7b14, 0x7fff, 0x318c, RGB(6, 10, 25), RGB(20, 24, 30), RGB_WHITE, RGB(12, 12, 12),
0x675a, 0, 0, 0 RGB(26, 26, 25), RGB_BLACK, RGB_BLACK, RGB_BLACK
}; };
static const u8 sBerryProgramTextColors[] = {TEXT_DYNAMIC_COLOR_1, TEXT_DYNAMIC_COLOR_2, TEXT_DYNAMIC_COLOR_3}; static const u8 sBerryProgramTextColors[] = {TEXT_DYNAMIC_COLOR_1, TEXT_DYNAMIC_COLOR_2, TEXT_DYNAMIC_COLOR_3};
static const u8 sGameTitleTextColors[] = { TEXT_COLOR_TRANSPARENT, TEXT_DYNAMIC_COLOR_1, TEXT_DYNAMIC_COLOR_4}; static const u8 sGameTitleTextColors[] = { TEXT_COLOR_TRANSPARENT, TEXT_DYNAMIC_COLOR_1, TEXT_DYNAMIC_COLOR_4};
static const u8 *const sBerryProgramTexts[] = { enum {
sText_EnsureGBAConnectionMatches, SCENE_ENSURE_CONNECT,
sText_TurnOffPowerHoldingStartSelect, SCENE_TURN_OFF_POWER,
sText_TransmittingPleaseWait, SCENE_TRANSMITTING,
sText_PleaseFollowInstructionsOnScreen, SCENE_FOLLOW_INSTRUCT,
sText_TransmissionFailureTryAgain, SCENE_TRANSMIT_FAILED,
sText_BerryProgramWillBeUpdatedPressA SCENE_BEGIN,
SCENE_NONE
}; };
static const u8 *const sBerryProgramTexts[] = {
[SCENE_ENSURE_CONNECT] = sText_EnsureGBAConnectionMatches,
[SCENE_TURN_OFF_POWER] = sText_TurnOffPowerHoldingStartSelect,
[SCENE_TRANSMITTING] = sText_TransmittingPleaseWait,
[SCENE_FOLLOW_INSTRUCT] = sText_PleaseFollowInstructionsOnScreen,
[SCENE_TRANSMIT_FAILED] = sText_TransmissionFailureTryAgain,
[SCENE_BEGIN] = sText_BerryProgramWillBeUpdatedPressA
};
static const struct { static const struct {
const u32 *gfx; const u32 *gfx;
const u32 *tilemap; const u32 *tilemap;
const u16 *pltt; const u16 *palette;
} sBerryFixGraphics[] = { } sBerryFixGraphics[] = {
{ [SCENE_ENSURE_CONNECT] = {
gBerryFixGameboy_Gfx, gBerryFixGameboy_Gfx,
gBerryFixGameboy_Tilemap, gBerryFixGameboy_Tilemap,
gBerryFixGameboy_Pal gBerryFixGameboy_Pal
}, { },
[SCENE_TURN_OFF_POWER] = {
gBerryFixGameboyLogo_Gfx, gBerryFixGameboyLogo_Gfx,
gBerryFixGameboyLogo_Tilemap, gBerryFixGameboyLogo_Tilemap,
gBerryFixGameboyLogo_Pal gBerryFixGameboyLogo_Pal
}, { },
[SCENE_TRANSMITTING] = {
gBerryFixGbaTransfer_Gfx, gBerryFixGbaTransfer_Gfx,
gBerryFixGbaTransfer_Tilemap, gBerryFixGbaTransfer_Tilemap,
gBerryFixGbaTransfer_Pal gBerryFixGbaTransfer_Pal
}, { },
[SCENE_FOLLOW_INSTRUCT] = {
gBerryFixGbaTransferHighlight_Gfx, gBerryFixGbaTransferHighlight_Gfx,
gBerryFixGbaTransferHighlight_Tilemap, gBerryFixGbaTransferHighlight_Tilemap,
gBerryFixGbaTransferHighlight_Pal gBerryFixGbaTransferHighlight_Pal
}, { },
[SCENE_TRANSMIT_FAILED] = {
gBerryFixGbaTransferError_Gfx, gBerryFixGbaTransferError_Gfx,
gBerryFixGbaTransferError_Tilemap, gBerryFixGbaTransferError_Tilemap,
gBerryFixGbaTransferError_Pal gBerryFixGbaTransferError_Pal
}, { },
[SCENE_BEGIN] = {
gBerryFixWindow_Gfx, gBerryFixWindow_Gfx,
gBerryFixWindow_Tilemap, gBerryFixWindow_Tilemap,
gBerryFixWindow_Pal gBerryFixWindow_Pal
@@ -163,7 +179,16 @@ static const struct {
extern const u8 gMultiBootProgram_BerryGlitchFix_Start[0x3BF4]; extern const u8 gMultiBootProgram_BerryGlitchFix_Start[0x3BF4];
extern const u8 gMultiBootProgram_BerryGlitchFix_End[]; extern const u8 gMultiBootProgram_BerryGlitchFix_End[];
// .text enum {
MAINSTATE_INIT,
MAINSTATE_BEGIN,
MAINSTATE_CONNECT,
MAINSTATE_INIT_MULTIBOOT,
MAINSTATE_MULTIBOOT,
MAINSTATE_TRANSMIT,
MAINSTATE_EXIT,
MAINSTATE_FAILED,
};
void CB2_InitBerryFixProgram(void) void CB2_InitBerryFixProgram(void)
{ {
@@ -175,81 +200,79 @@ void CB2_InitBerryFixProgram(void)
ResetTasks(); ResetTasks();
ScanlineEffect_Stop(); ScanlineEffect_Stop();
SetGpuReg(REG_OFFSET_DISPCNT, 0); SetGpuReg(REG_OFFSET_DISPCNT, 0);
berry_fix_mb_manager = AllocZeroed(0x50); sBerryFix = AllocZeroed(sizeof(*sBerryFix));
berry_fix_mb_manager->state = 0; sBerryFix->state = MAINSTATE_INIT;
berry_fix_mb_manager->unk1 = 6; sBerryFix->curScene = SCENE_NONE;
SetMainCallback2(berry_fix_main); SetMainCallback2(BerryFix_Main);
} }
static void berry_fix_main(void) #define TryScene(sceneNum) BerryFix_TrySetScene(sceneNum) == (sceneNum)
static void BerryFix_Main(void)
{ {
switch (berry_fix_mb_manager->state) switch (sBerryFix->state)
{ {
case 0: case MAINSTATE_INIT:
berry_fix_gpu_set(); BerryFix_GpuSet();
berry_fix_mb_manager->state = 1; sBerryFix->state = MAINSTATE_BEGIN;
break; break;
case 1: case MAINSTATE_BEGIN:
if (berry_fix_text_update(5) == 5 && (JOY_NEW(A_BUTTON))) if (TryScene(SCENE_BEGIN) && (JOY_NEW(A_BUTTON)))
sBerryFix->state = MAINSTATE_CONNECT;
break;
case MAINSTATE_CONNECT:
if (TryScene(SCENE_ENSURE_CONNECT) && (JOY_NEW(A_BUTTON)))
sBerryFix->state = MAINSTATE_INIT_MULTIBOOT;
break;
case MAINSTATE_INIT_MULTIBOOT:
if (TryScene(SCENE_TURN_OFF_POWER))
{ {
berry_fix_mb_manager->state = 2; sBerryFix->mb.masterp = gMultiBootProgram_BerryGlitchFix_Start;
sBerryFix->mb.server_type = 0;
MultiBootInit(&sBerryFix->mb);
sBerryFix->timer = 0;
sBerryFix->state = MAINSTATE_MULTIBOOT;
} }
break; break;
case 2: case MAINSTATE_MULTIBOOT:
if (berry_fix_text_update(0) == 0 && (JOY_NEW(A_BUTTON))) MultiBootMain(&sBerryFix->mb);
if (sBerryFix->mb.probe_count != 0 || (!(sBerryFix->mb.response_bit & 2) || !(sBerryFix->mb.client_bit & 2)))
{ {
berry_fix_mb_manager->state = 3; sBerryFix->timer = 0;
}
else if (++sBerryFix->timer > 180)
{
MultiBootStartMaster(&sBerryFix->mb,
gMultiBootProgram_BerryGlitchFix_Start + ROM_HEADER_SIZE,
(u32)(gMultiBootProgram_BerryGlitchFix_End - (gMultiBootProgram_BerryGlitchFix_Start + ROM_HEADER_SIZE)),
4,
1);
sBerryFix->state = MAINSTATE_TRANSMIT;
} }
break; break;
case 3: case MAINSTATE_TRANSMIT:
if (berry_fix_text_update(1) == 1) if (TryScene(SCENE_TRANSMITTING))
{ {
berry_fix_mb_manager->mb.masterp = gMultiBootProgram_BerryGlitchFix_Start; MultiBootMain(&sBerryFix->mb);
berry_fix_mb_manager->mb.server_type = 0;
MultiBootInit(&berry_fix_mb_manager->mb); if (MultiBootCheckComplete(&sBerryFix->mb))
berry_fix_mb_manager->unk2 = 0; sBerryFix->state = MAINSTATE_EXIT;
berry_fix_mb_manager->state = 4; else if (!(sBerryFix->mb.client_bit & 2))
sBerryFix->state = MAINSTATE_FAILED;
} }
break; break;
case 4: case MAINSTATE_EXIT:
MultiBootMain(&berry_fix_mb_manager->mb); if (TryScene(SCENE_FOLLOW_INSTRUCT) && JOY_NEW(A_BUTTON))
if (berry_fix_mb_manager->mb.probe_count != 0 || (!(berry_fix_mb_manager->mb.response_bit & 2) || !(berry_fix_mb_manager->mb.client_bit & 2)))
{
berry_fix_mb_manager->unk2 = 0;
}
else if (++ berry_fix_mb_manager->unk2 > 180)
{
MultiBootStartMaster(&berry_fix_mb_manager->mb, gMultiBootProgram_BerryGlitchFix_Start + ROM_HEADER_SIZE, (u32)(gMultiBootProgram_BerryGlitchFix_End - (gMultiBootProgram_BerryGlitchFix_Start + ROM_HEADER_SIZE)), 4, 1);
berry_fix_mb_manager->state = 5;
}
break;
case 5:
if (berry_fix_text_update(2) == 2) {
MultiBootMain(&berry_fix_mb_manager->mb);
if (MultiBootCheckComplete(&berry_fix_mb_manager->mb)) {
berry_fix_mb_manager->state = 6;
}
else if (!(berry_fix_mb_manager->mb.client_bit & 2)) {
berry_fix_mb_manager->state = 7;
}
}
break;
case 6:
if (berry_fix_text_update(3) == 3 && JOY_NEW(A_BUTTON))
{
DoSoftReset(); DoSoftReset();
}
break; break;
case 7: case MAINSTATE_FAILED:
if (berry_fix_text_update(4) == 4 && JOY_NEW(A_BUTTON)) if (TryScene(SCENE_TRANSMIT_FAILED) && JOY_NEW(A_BUTTON))
{ sBerryFix->state = MAINSTATE_BEGIN;
berry_fix_mb_manager->state = 1;
}
break; break;
} }
} }
static void berry_fix_gpu_set(void) static void BerryFix_GpuSet(void)
{ {
s32 width, left; s32 width, left;
@@ -274,26 +297,26 @@ static void berry_fix_gpu_set(void)
InitWindows(sBerryFixWindowTemplates); InitWindows(sBerryFixWindowTemplates);
DeactivateAllTextPrinters(); DeactivateAllTextPrinters();
DmaCopy32(3, sUnknown_08618138, BG_PLTT + 0x1E0, 0x20); DmaCopy32(3, sBerryFixPalColors, BG_PLTT + 0x1E0, sizeof(sBerryFixPalColors));
SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_1D_MAP); SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_1D_MAP);
FillWindowPixelBuffer(2, PIXEL_FILL(0)); FillWindowPixelBuffer(2, PIXEL_FILL(0));
FillWindowPixelBuffer(3, PIXEL_FILL(0)); FillWindowPixelBuffer(3, PIXEL_FILL(0));
FillWindowPixelBuffer(0, PIXEL_FILL(0xA)); FillWindowPixelBuffer(0, PIXEL_FILL(10));
width = GetStringWidth(0, sText_Emerald, 0); width = GetStringWidth(0, sText_Emerald, 0);
left = (0x78 - width) / 2; left = (120 - width) / 2;
AddTextPrinterParameterized3(2, 0, left, 3, sGameTitleTextColors, TEXT_SPEED_FF, sText_Emerald); AddTextPrinterParameterized3(2, 0, left, 3, sGameTitleTextColors, TEXT_SPEED_FF, sText_Emerald);
width = GetStringWidth(0, sText_RubySapphire, 0); width = GetStringWidth(0, sText_RubySapphire, 0);
left = (0x78 - width) / 2 + 0x78; left = (120 - width) / 2 + 120;
AddTextPrinterParameterized3(2, 0, left, 3, sGameTitleTextColors, TEXT_SPEED_FF, sText_RubySapphire); AddTextPrinterParameterized3(2, 0, left, 3, sGameTitleTextColors, TEXT_SPEED_FF, sText_RubySapphire);
width = GetStringWidth(0, sText_RubySapphire, 0); width = GetStringWidth(0, sText_RubySapphire, 0);
left = (0x70 - width) / 2; left = (112 - width) / 2;
AddTextPrinterParameterized3(3, 0, left, 0, sGameTitleTextColors, TEXT_SPEED_FF, sText_RubySapphire); AddTextPrinterParameterized3(3, 0, left, 0, sGameTitleTextColors, TEXT_SPEED_FF, sText_RubySapphire);
width = GetStringWidth(1, sText_BerryProgramUpdate, 0); width = GetStringWidth(1, sText_BerryProgramUpdate, 0);
left = (0xD0 - width) / 2; left = (208 - width) / 2;
AddTextPrinterParameterized3(0, 1, left, 2, sBerryProgramTextColors, TEXT_SPEED_FF, sText_BerryProgramUpdate); AddTextPrinterParameterized3(0, 1, left, 2, sBerryProgramTextColors, TEXT_SPEED_FF, sText_BerryProgramUpdate);
CopyWindowToVram(2, 2); CopyWindowToVram(2, 2);
@@ -301,56 +324,55 @@ static void berry_fix_gpu_set(void)
CopyWindowToVram(0, 2); CopyWindowToVram(0, 2);
} }
static int berry_fix_text_update(int checkval) static int BerryFix_TrySetScene(int scene)
{ {
if (berry_fix_mb_manager->unk1 == checkval) if (sBerryFix->curScene == scene)
return scene;
if (sBerryFix->curScene == SCENE_NONE)
{ {
return checkval; BerryFix_SetScene(scene);
} sBerryFix->curScene = scene;
if (berry_fix_mb_manager->unk1 == 6)
{
berry_fix_text_print(checkval);
berry_fix_mb_manager->unk1 = checkval;
} }
else else
{ {
berry_fix_bg_hide(); BerryFix_HideScene();
berry_fix_mb_manager->unk1 = 6; sBerryFix->curScene = SCENE_NONE;
} }
return berry_fix_mb_manager->unk1; return sBerryFix->curScene;
} }
static void berry_fix_text_print(int scene) static void BerryFix_SetScene(int scene)
{ {
FillBgTilemapBufferRect_Palette0(0, 0, 0, 0, 32, 32); FillBgTilemapBufferRect_Palette0(0, 0, 0, 0, 32, 32);
FillWindowPixelBuffer(1, PIXEL_FILL(0xA)); FillWindowPixelBuffer(1, PIXEL_FILL(10));
AddTextPrinterParameterized3(1, 1, 0, 0, sBerryProgramTextColors, -1, sBerryProgramTexts[scene]); AddTextPrinterParameterized3(1, 1, 0, 0, sBerryProgramTextColors, -1, sBerryProgramTexts[scene]);
PutWindowTilemap(1); PutWindowTilemap(1);
CopyWindowToVram(1, 2); CopyWindowToVram(1, 2);
switch (scene) switch (scene)
{ {
case 0: case SCENE_ENSURE_CONNECT:
case 2: case SCENE_TRANSMITTING:
case 3: case SCENE_FOLLOW_INSTRUCT:
case 4: case SCENE_TRANSMIT_FAILED:
PutWindowTilemap(2); PutWindowTilemap(2);
break; break;
case 1: case SCENE_TURN_OFF_POWER:
PutWindowTilemap(3); PutWindowTilemap(3);
break; break;
case 5: case SCENE_BEGIN:
PutWindowTilemap(0); PutWindowTilemap(0);
break; break;
} }
CopyBgTilemapBufferToVram(0); CopyBgTilemapBufferToVram(0);
LZ77UnCompVram(sBerryFixGraphics[scene].gfx, (void *)BG_CHAR_ADDR(1)); LZ77UnCompVram(sBerryFixGraphics[scene].gfx, (void *)BG_CHAR_ADDR(1));
LZ77UnCompVram(sBerryFixGraphics[scene].tilemap, (void *)BG_SCREEN_ADDR(31)); LZ77UnCompVram(sBerryFixGraphics[scene].tilemap, (void *)BG_SCREEN_ADDR(31));
CpuCopy32(sBerryFixGraphics[scene].pltt, (void *)BG_PLTT, 0x100); CpuCopy32(sBerryFixGraphics[scene].palette, (void *)BG_PLTT, 0x100);
ShowBg(0); ShowBg(0);
ShowBg(1); ShowBg(1);
} }
static void berry_fix_bg_hide(void) static void BerryFix_HideScene(void)
{ {
HideBg(0); HideBg(0);
HideBg(1); HideBg(1);