diff --git a/src/sm_82.c b/src/sm_82.c index 3e0e60b..18aef8d 100644 --- a/src/sm_82.c +++ b/src/sm_82.c @@ -2548,7 +2548,7 @@ static Func_V *const kEquipmentScreenCategories[4] = { // 0x82AC4F EquipmentScreenCategory_Boots, }; void EquipmentScreenMain(void) { - kEquipmentScreenCategories[(uint16)(2 * (uint8)pausemenu_equipment_category_item) >> 1](); + kEquipmentScreenCategories[(uint8)pausemenu_equipment_category_item](); EquipmentScreenDrawItemSelector(); EquipmentScreenDisplayReserveTankAmount(); WriteSamusWireframeTilemapAndQueue(); @@ -2558,7 +2558,7 @@ static Func_V *const kEquipmentScreenCategory_TanksFuncs[2] = { // 0x82AC70 EquipmentScreenCategory_Tanks_1, }; void EquipmentScreenCategory_Tanks(void) { - kEquipmentScreenCategory_TanksFuncs[(uint16)(2 * HIBYTE(pausemenu_equipment_category_item)) >> 1](); + kEquipmentScreenCategory_TanksFuncs[HIBYTE(pausemenu_equipment_category_item)](); EquipmentScreenHandleDpad(); EquipmentScreenGlowingArrow(); } @@ -2770,8 +2770,11 @@ void EquipmentScreenCategory_Weapons(void) { // 0x82AFBE R36 = equipped_beams; if (collected_beams) { R24_ = 10; - EquipmentScreenCategory_ButtonResponse(); - EquipmentScreenCategory_Weapons_PlazmaSpazerCheck(); + // Fixed var bug + if ((uint8)pausemenu_equipment_category_item == 1) { + EquipmentScreenCategory_ButtonResponse(); + EquipmentScreenCategory_Weapons_PlazmaSpazerCheck(); + } } } @@ -2805,16 +2808,16 @@ void EquipmentScreenCategory_Weapons_MoveButtons(void) { // 0x82AFDB #define kEquipmentTilemaps_Weapons ((uint16*)RomPtr(0x82c08c)) #define kEquipmentTilemaps_Suits ((uint16*)RomPtr(0x82c096)) #define kEquipmentTilemaps_Boots ((uint16*)RomPtr(0x82c0a2)) + void EquipmentScreenCategory_Weapons_PlazmaSpazerCheck(void) { // 0x82B068 - if (((uint8)equipped_beams & (uint8)~(uint8)R36 & 4) != 0) { + int t = equipped_beams & ~R36; + if ((t & 4) != 0) { if ((R36 & 4) == 0 && (equipped_beams & 8) != 0) { equipped_beams &= ~8; R0_.addr = kEquipmentTilemapOffs_Weapons[4]; goto LABEL_9; } - } else if (((uint8)equipped_beams & (uint8)~(uint8)R36 & 8) != 0 - && (R36 & 8) == 0 - && (equipped_beams & 4) != 0) { + } else if ((t & 8) != 0 && (R36 & 8) == 0 && (equipped_beams & 4) != 0) { equipped_beams &= ~4; R0_.addr = kEquipmentTilemapOffs_Weapons[3]; LABEL_9: @@ -2827,7 +2830,9 @@ LABEL_9: void EquipmentScreenCategory_Suit(void) { // 0x82B0C2 EquipmentScreenCategory_Suit_MoveResponse(); R24_ = 18; - EquipmentScreenCategory_ButtonResponse(); + // Fixed var bug + if ((uint8)pausemenu_equipment_category_item == 2) + EquipmentScreenCategory_ButtonResponse(); } void EquipmentScreenCategory_Suit_MoveResponse(void) { // 0x82B0D2 @@ -2856,7 +2861,9 @@ void EquipmentScreenCategory_Suit_MoveResponse(void) { // 0x82B0D2 void EquipmentScreenCategory_Boots(void) { // 0x82B150 EquipmentScreenCategory_Boots_MoveResponse(); R24_ = 18; - EquipmentScreenCategory_ButtonResponse(); + // Fixed var bug + if ((uint8)pausemenu_equipment_category_item == 3) + EquipmentScreenCategory_ButtonResponse(); } void EquipmentScreenCategory_Boots_MoveResponse(void) { // 0x82B160 @@ -3137,24 +3144,20 @@ void EquipmentScreenCategory_ButtonResponse(void) { // 0x82B568 QueueSfx1_Max6(0x38u); R26_ = 2 * (uint8)pausemenu_equipment_category_item; R18_ = 2 * HIBYTE(pausemenu_equipment_category_item); - int v0 = R26_ >> 1; - R0_.addr = *(uint16 *)RomPtr_82(R18_ + kEquipmentPtrsToRamTilemapOffsets[v0]); - uint16 v1 = kEquipmentPtrsToBitsets[v0]; - uint16 v2 = R18_ + kEquipmentPtrsToBitmasks[v0]; - uint8 *v3 = RomPtr_RAM(v1); - uint8 *v4 = RomPtr_82(v2); - if ((*(uint16 *)v4 & *(uint16 *)v3) != 0) { - R18_ = ~*(uint16 *)RomPtr_82(v2); - uint8 *v6 = RomPtr_RAM(v1); - *(uint16 *)v6 &= R18_; - R18_ = 3072; + int item = HIBYTE(pausemenu_equipment_category_item); + int category = (uint8)pausemenu_equipment_category_item; + R0_.addr = *(uint16 *)RomPtr_82(kEquipmentPtrsToRamTilemapOffsets[category] + item * 2); + uint16 *var = (uint16 *)RomPtr_RAM(kEquipmentPtrsToBitsets[category]); + uint16 mask = *(uint16 *)RomPtr_82(kEquipmentPtrsToBitmasks[category] + item * 2); + if ((*var & mask) != 0) { + *var &= ~mask; + R18_ = 0xC00; R22_ = R24_; SetPaletteOfR22TilemapBytesToR18(); } else { - *(uint16 *)v3 |= *(uint16 *)v4; + *var |= mask; R22_ = R24_; - uint16 v5 = *(uint16 *)RomPtr_82(R18_ + kEquipmentPtrsToEquipmentTilemaps[R26_ >> 1]); - Copy_R22_Bytes(v5); + Copy_R22_Bytes(*(uint16 *)RomPtr_82(kEquipmentPtrsToEquipmentTilemaps[category] + item * 2)); } } } diff --git a/src/sm_90.c b/src/sm_90.c index cbe9262..0c62edb 100644 --- a/src/sm_90.c +++ b/src/sm_90.c @@ -2718,7 +2718,7 @@ void WriteBeamPalette_A(uint16 a) { // 0x90ACC2 } void WriteBeamPalette_Y(uint16 j) { // 0x90ACCD - *(VoidP *)((char *)&R0_.addr + 1) = -28672; + R0_.bank = 0x90; R0_.addr = kBeamPalettePtrs[j >> 1]; uint16 v1 = 0; uint16 v2 = 0; @@ -2729,12 +2729,8 @@ void WriteBeamPalette_Y(uint16 j) { // 0x90ACCD } while ((int16)(v1 - 32) < 0); } -void LoadProjectilePalette(uint16 a) { // 0x90ACF0 - LoadProjectilePalette_(a); -} - -void LoadProjectilePalette_(uint16 a) { // 0x90ACFC - *(VoidP *)((char *)&R0_.addr + 1) = -28672; +void LoadProjectilePalette(uint16 a) { // 0x90ACFC + R0_.bank = 0x90; R0_.addr = kBeamPalettePtrs[a & 0xFFF]; uint16 v1 = 0; uint16 v2 = 0; diff --git a/src/sm_cpu_infra.c b/src/sm_cpu_infra.c index fbefe7a..188f7de 100644 --- a/src/sm_cpu_infra.c +++ b/src/sm_cpu_infra.c @@ -228,7 +228,7 @@ static const uint32 kPatchedCarrys[] = { }; static uint8 kPatchedCarrysOrg[arraysize(kPatchedCarrys)]; -void PatchBugs(uint32 mode, uint32 addr) { +uint32 PatchBugs(uint32 mode, uint32 addr) { hookmode = mode, hookadr = addr, hookcnt = 0; if (FixBugHook(0x86EF35)) { g_cpu->x = g_cpu->y; @@ -286,12 +286,36 @@ void PatchBugs(uint32 mode, uint32 addr) { // Xray_SetupStage4_Func2 passes a bad value to Xray_HandleXrayedBlock if (g_cpu->x == 0) g_cpu->pc = 0xCD52; + + // Fix VAR BEAM etc. + // Prevent EquipmentScreenCategory_ButtonResponse from getting called when category changed + } else if (FixBugHook(0x82AFD3)) { + if ((uint8)pausemenu_equipment_category_item != 1) + return 0x82AFD9; + } else if (FixBugHook(0x82B0CD)) { + if ((uint8)pausemenu_equipment_category_item != 2) + return 0x82AFD9; + } else if (FixBugHook(0x82B15B)) { + if ((uint8)pausemenu_equipment_category_item != 3) + return 0x82AFD9; } + + return 0; } int RunPatchBugHook(uint32 addr) { - PatchBugs(2, addr); - return hookmode == 3 ? hook_fixbug_orgbyte[hookcnt] : -1; + uint32 new_pc = PatchBugs(2, addr); + if (hookmode == 3) { + if (new_pc == 0) { + return hook_fixbug_orgbyte[hookcnt]; + } else { + g_cpu->k = new_pc >> 16; + g_cpu->pc = (new_pc & 0xffff) + 1; + return *RomPtr(new_pc); + } + } + + return -1; } int CpuOpcodeHook(uint32 addr) {