From 842f5bc25953aafc528ec6196283f7813f2160cd Mon Sep 17 00:00:00 2001 From: GearsProgress Date: Sun, 31 May 2026 19:52:16 -0400 Subject: [PATCH] Fixing specific payload upload process --- include/link_handler.h | 16 +-- source/dbg/debug_mode.cpp | 2 +- source/link_handler.cpp | 91 +++++++++++++----- source/script_array.cpp | 4 +- .../src/payloads/outfile.bin | Bin 199 -> 0 bytes .../payloads/specificPayloadGen1_RB_EN.asm | 15 ++- 6 files changed, 85 insertions(+), 43 deletions(-) delete mode 100644 tools/gb-payload-generator/src/payloads/outfile.bin diff --git a/include/link_handler.h b/include/link_handler.h index 78099b8..f90838d 100644 --- a/include/link_handler.h +++ b/include/link_handler.h @@ -4,6 +4,7 @@ #include #include "libraries/gba-link-connection/LinkSPI.hpp" #include "pokemon_party.h" +#include "GB_Payloads.h" #define DATA_PER_PACKET 8 #define PACKET_DATA_START 2 @@ -129,7 +130,7 @@ enum CompositeState { NO_COMPOSITE_STATE, INITIAL_CONNECTION, - SEND_PAYLOAD, + GET_BOX_LIST, }; enum SubstateState @@ -145,10 +146,13 @@ enum SubstateState TRADE_PREAMBLE, TRADE, MAIL, - WAIT_FOR_PAYLOAD, + WAIT_FOR_CHECKSUM_PAYLOAD, GET_CHECKSUM, + WAIT_FOR_SECOND_PAYLOAD, + SEND_SPECIFIC_PAYLOAD, + SOFT_RESET, - // SEND_PAYLOAD + // GET_BOX_LIST END, }; @@ -164,11 +168,11 @@ public: LinkConnection *globalPtr; CompositeState compState = NO_COMPOSITE_STATE; - CompositeState prevCompState = NO_COMPOSITE_STATE; + CompositeState nextCompState = NO_COMPOSITE_STATE; bool compStateChanged = false; SubstateState subState = NO_SUBSTATE; - SubstateState prevSubState = NO_SUBSTATE; + SubstateState nextSubState = NO_SUBSTATE; bool subStateChanged = false; LinkConnectionError lastError = NO_ERROR; @@ -212,7 +216,7 @@ public: void handleStateLogic(); private: - void load_universal_payload(); + void load_payload(GB_PayloadsFiles payload); void LoadCurrGameFromChecksum(); void logicState_initConnection(); diff --git a/source/dbg/debug_mode.cpp b/source/dbg/debug_mode.cpp index a9314eb..e4826c0 100644 --- a/source/dbg/debug_mode.cpp +++ b/source/dbg/debug_mode.cpp @@ -2,7 +2,7 @@ debug_options g_debug_options = { - .print_link_data = (false && DEBUG_MODE), + .print_link_data = (true && DEBUG_MODE), .instant_text_speed = (true && DEBUG_MODE), .ignore_game_pak = (true && DEBUG_MODE), .ignore_game_pak_sprites = (false && DEBUG_MODE), diff --git a/source/link_handler.cpp b/source/link_handler.cpp index 00539bf..25c0137 100644 --- a/source/link_handler.cpp +++ b/source/link_handler.cpp @@ -68,11 +68,11 @@ void LinkConnection::setup(const u16 *debug_charset) void LinkConnection::startConnection(CompositeState startState) { - compState = startState; + nextCompState = startState; switch (startState) { case INITIAL_CONNECTION: - subState = CLOCK; + nextSubState = CLOCK; REG_TM3D = -0x4000 / 60; REG_TM3CNT = TM_FREQ_1024 | TM_ENABLE; irq_enable(II_TIMER3); @@ -118,11 +118,17 @@ void LinkConnection::printData() void LinkConnection::writeData() { - global_memory_buffer[link_cable_array_index] = inData; - link_cable_array_index++; - global_memory_buffer[link_cable_array_index] = outData; - link_cable_array_index++; - if (link_cable_array_index >= 0x1000) + global_memory_buffer[link_cable_array_index + 0] = compState; + global_memory_buffer[link_cable_array_index + 1] = (compStateCounter >> 8) & 0xFF; + global_memory_buffer[link_cable_array_index + 2] = (compStateCounter >> 0) & 0xFF; + global_memory_buffer[link_cable_array_index + 3] = subState; + global_memory_buffer[link_cable_array_index + 4] = (subStateCounter >> 8) & 0xFF; + global_memory_buffer[link_cable_array_index + 5] = (subStateCounter >> 0) & 0xFF; + global_memory_buffer[link_cable_array_index + 6] = inData; + global_memory_buffer[link_cable_array_index + 7] = outData; + link_cable_array_index += 8; + + if (link_cable_array_index >= 0x1000 || nextSubState == END) { copy_ram_to_save(&global_memory_buffer[0], 0x1000 * link_cable_memory_section_index, 0x1000); link_cable_memory_section_index++; @@ -132,8 +138,6 @@ void LinkConnection::writeData() void LinkConnection::handleStateLogic() { - prevCompState = compState; - prevSubState = subState; switch (compState) { case INITIAL_CONNECTION: @@ -143,7 +147,7 @@ void LinkConnection::handleStateLogic() break; } - if (prevSubState != subState) + if (nextSubState != subState) { subStateCounter = 0; subStateChanged = true; @@ -154,7 +158,7 @@ void LinkConnection::handleStateLogic() subStateChanged = false; } - if (prevCompState != compState) + if (nextCompState != compState) { compStateCounter = 0; compStateChanged = true; @@ -164,8 +168,18 @@ void LinkConnection::handleStateLogic() compStateCounter++; compStateChanged = false; } + + subState = nextSubState; + compState = nextCompState; } +int test_packet[] = { + /* 1 preamble byte */ 0xFD, + /* 1 command byte */ 0x02, + /* 2 argument bytes */ 0x00, 0x00, + /* 1 16-bit pointer */ 0x00, 0x00, + /* 6 filler bytes */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + bool LinkConnection::earlyExit() { if (g_debug_options.print_link_data && key_hit(KEY_LEFT)) @@ -194,7 +208,7 @@ void LinkConnection::logicState_initConnection() case CLOCK: if (inData == 0xFE) { - subState = SAVE_SUCCESS; + nextSubState = SAVE_SUCCESS; outData = 0x00; } else @@ -206,7 +220,7 @@ void LinkConnection::logicState_initConnection() case SAVE_SUCCESS: if (inData == 0x60 || inData == 0x61) { - subState = MENU_OPEN; + nextSubState = MENU_OPEN; outData = inData; } else @@ -221,22 +235,23 @@ void LinkConnection::logicState_initConnection() if (inData == 0xD0) { gen = 1; + load_payload(GB_PayloadsFiles::UNIVERSALPAYLOADGEN1); outData = 0xD4; } else if (inData == 0x61) { gen = 2; + load_payload(GB_PayloadsFiles::UNIVERSALPAYLOADGEN2); outData = 0x61; } - load_universal_payload(); - subState = MENU_SUCCESS; + nextSubState = MENU_SUCCESS; } break; case MENU_SUCCESS: if (inData == 0xFE) { - subState = WAIT_FOR_TRADE; + nextSubState = WAIT_FOR_TRADE; } outData = inData; break; @@ -245,7 +260,7 @@ void LinkConnection::logicState_initConnection() if (inData == 0xFD) { REG_TM3D = -0x0040; - subState = TRADE_PREAMBLE; + nextSubState = TRADE_PREAMBLE; outData = 0x00; } else @@ -265,7 +280,7 @@ void LinkConnection::logicState_initConnection() } else { - subState = TRADE; + nextSubState = TRADE; outData = 0xFD; }; break; @@ -275,11 +290,11 @@ void LinkConnection::logicState_initConnection() { if (this->gen == 2) { - subState = MAIL; + nextSubState = MAIL; } else { - subState = WAIT_FOR_PAYLOAD; + nextSubState = WAIT_FOR_CHECKSUM_PAYLOAD; } } outData = curr_payload[subStateCounter]; @@ -288,14 +303,14 @@ void LinkConnection::logicState_initConnection() case MAIL: if (subStateCounter >= 0x186) { - subState = WAIT_FOR_PAYLOAD; + nextSubState = WAIT_FOR_CHECKSUM_PAYLOAD; } break; - case WAIT_FOR_PAYLOAD: + case WAIT_FOR_CHECKSUM_PAYLOAD: if (inData == 0xFD) { - subState = GET_CHECKSUM; + nextSubState = GET_CHECKSUM; } outData = 0x00; break; @@ -311,7 +326,8 @@ void LinkConnection::logicState_initConnection() else if (inData == 0xFD && dataOutBufferCurrIndex > 0) { LoadCurrGameFromChecksum(); - subState = END; + load_payload(GB_PayloadsFiles::SPECIFICPAYLOADGEN1_RB_EN); + nextSubState = SEND_SPECIFIC_PAYLOAD; } else { @@ -319,6 +335,29 @@ void LinkConnection::logicState_initConnection() } break; + case WAIT_FOR_SECOND_PAYLOAD: + if (inData == 0xFD) + { + nextSubState = SEND_SPECIFIC_PAYLOAD; + } + outData = 0x00; + break; + + case SEND_SPECIFIC_PAYLOAD: + if (subStateCounter > 200) // The 200 comes from the Universal Payload + { + nextSubState = END; + } + if (subStateCounter < curr_payload_size) + { + outData = curr_payload[subStateCounter]; + } + else + { + outData = 0x01; + } + break; + case END: irq_delete(II_TIMER3); break; @@ -329,13 +368,13 @@ void LinkConnection::logicState_initConnection() } } -void LinkConnection::load_universal_payload() +void LinkConnection::load_payload(GB_PayloadsFiles payload) { u32 fileSize; u8 decompressionBuffer[0x1000]; const u8 *chunkList[] = {(const u8 *)GB_Payloads_chunk0_lz10_bin}; FileContainerReader reader(chunkList, 1); - const u32 fileIndex = (this->gen == 1) ? (u32)GB_PayloadsFiles::UNIVERSALPAYLOADGEN1 : (u32)GB_PayloadsFiles::UNIVERSALPAYLOADGEN2; + const u32 fileIndex = (u32)payload; reader.init(decompressionBuffer, sizeof(decompressionBuffer)); fileSize = reader.getFileSize(fileIndex); diff --git a/source/script_array.cpp b/source/script_array.cpp index c644e0f..08ef400 100644 --- a/source/script_array.cpp +++ b/source/script_array.cpp @@ -762,7 +762,7 @@ bool run_conditional(int index) globalLinkCable.startConnection(INITIAL_CONNECTION); while (globalLinkCable.subState != END) { - if (globalLinkCable.subStateChanged) + if (globalLinkCable.subStateChanged && !g_debug_options.print_link_data) { switch (globalLinkCable.subState) { @@ -794,7 +794,7 @@ bool run_conditional(int index) obj_unhide(flag, 0); obj_set_pos(flag, 1.5 * 8, 14 * 8); - globalLinkCable.startConnection(INITIAL_CONNECTION); + //globalLinkCable.startConnection(INITIAL_CONNECTION); while (true) { diff --git a/tools/gb-payload-generator/src/payloads/outfile.bin b/tools/gb-payload-generator/src/payloads/outfile.bin deleted file mode 100644 index f5356a730297fcbc64a0eb35aa8c18ee16b9d026..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 199 zcmV;&0672sA>78xzAFBGDEUSsP9xGCCH!=YX0L^b97`BAPjKu%}zs1MJ%f(0r01*(zA<)Iu z&2J&$H{r=4kah+f!`3x<>L$#^J`3RgAXOgYXnWxp?IGfK7|13HbS6M>r^&Bpcgf9P#pyZ> BV;%qi diff --git a/tools/gb-payload-generator/src/payloads/specificPayloadGen1_RB_EN.asm b/tools/gb-payload-generator/src/payloads/specificPayloadGen1_RB_EN.asm index 76689ca..8a71e5e 100644 --- a/tools/gb-payload-generator/src/payloads/specificPayloadGen1_RB_EN.asm +++ b/tools/gb-payload-generator/src/payloads/specificPayloadGen1_RB_EN.asm @@ -26,11 +26,11 @@ DEF wCurrentMenuItem = 0xCC26 SECTION "Main", ROM0 Main: + db 0xFD LOAD "Payload", WRAM0[0xC508] Payload: - db 0xFD .loopTransfer - ld hl, 0xC6DC ; perfect place to store incoming packets. TODO: on first pass, this will interpret the generic payload as part of an incoming packet. Check if this interferes with anything. + ld hl, 0xC5DC ; perfect place to store incoming packets. TODO: on first pass, this will interpret the generic payload as part of an incoming packet. Check if this interferes with anything. .skipPreamble res 7, [hl] ; if current pointer == 0xFD, change so that rra with carry flag doesn't create 0xFE. If command byte, this functionally does nothing. ld a, [hli] @@ -48,11 +48,9 @@ Payload: ld a, [hli] ld h, [hl] ld l, a - ld de, 0xC6D2 + ld de, 0xC5D2 push de ld bc, 1 - ld a, c - ldh [hSerialConnectionStatus], a ; is this needed? .loop ld a, [hli] rra ; c flag is always 0 here, except if a failure state was detected in a command we ran this cycle. Failure state means 1st data byte > 0x80 @@ -71,16 +69,17 @@ Payload: add a, h add a, l res 7, a ; ensure that the checksum is never equal to 0xFE + inc de ld [de], a ; load in the checksum - inc de ; de now points to 0xC6DC, where the next packet will arrive. - + inc de ; de now points to 0xC5DC, where the next packet will arrive. pop hl ld a, c and 0x0F ; extract the lower nybble. ld [hld], a ; 4 LSB get stored here. xor c ; I love xor magic, this stores the higher nybble in a. swap a - ld [hld], a ; 4 MSB get stored here, hl now points to 0xC6D0, where the next packet will be sent from. + ld [hld], a ; 4 MSB get stored here, hl now points to 0xC5D0, where the next packet will be sent from. + ld [hl], SERIAL_PREAMBLE_BYTE ld bc, PACKET_SIZE call Serial_ExchangeBytes