Poke_Transporter_GB/include
Philippe Symons eef173b0d2 Fix crash + unrelated buffer overflow + some optimizations
There was a crash happening with ptgb::vector when you'd press A on the CONFIRM button of the box screen. It only occurred on actual gba hardware and
was a real heisenbug: as soon as you'd add code to display logs on screen, the problem would disappear. So it was very difficult to figure this one
out. We're not even entirely sure why, but it looks like the malloc/realloc/free use in ptgb::vector would cause issues.

Maybe it was alignment, but after messing with the code we also saw a warning appear in the terminal telling us that realloc wouldn't properly
deal with non-POD types. It complained about this very thing while referring to the add_track() function, which stores ptgb::vectors inside another
ptgb::vector. We also didn't have a custom copy constructor yet to actually copy the buffer instead of its pointer.
All of these could potentially have led to the crash. But debugging during the link cable flow was difficult, so we were never able to confirm it in
a debugger, log or dump.

Because I suspected the high IWRAM consumption (especially now with ZX0 decompression) for a while, I also did an optimization in mystery_gift_builder
to pass global_memory_buffer as its section_30_data buffer instead. This reduces IWRAM consumption by 4 KB.

There was another problem I discovered during my crash hunt: the out_array (now payload_buffer) was allocated as a 672 byte array, but the payloads
were actually 707 bytes. Therefore writing this to the buffer caused a buffer overflow, thereby corrupting the global variables appearing after it in
IWRAM. It turned out eventually that none of these variables were really critical, but it could explain some minor bugs GearsProgress has seen.

I also did a few performance optimizations:

- At various stages in the code, for loops were used to copy data from one buffer into another byte-by-byte. This was far from optimal because the gba
cpu can load/copy 4 bytes at a time if you ask it to. So I replaced those with memcpy(), which is a hand-optimized assembly function to copy data
using this principle.

- generate_payload was being called twice: once at start_link and once at continue_link, giving the exact same result, even though it was already
being stored in a global buffer allocated in IWRAM. This was also a fairly heavy function. So I optimized the code to only initialize it once in
the script chain and then just retrieve the buffer.

- generate_payload was constructing the eventual payload twice even within the same call. That's because it first merged z80_rng_seed, z80_payload
and z80_patchlist into a full_data ptgb::vector, after which it then copied the data again to out_array (now called payload_buffer). I eliminated the
full_data vector now.
2025-06-18 10:23:03 +02:00
..
gb_rom_values Applying patch for graphics issues 2025-05-27 15:13:42 -04:00
gba_rom_values Continuing box updating 2025-03-27 14:37:47 -04:00
libraries Reduce binary size by eliminating libstdc++ 2025-04-09 20:04:08 +02:00
background_engine.h Modifying the remote multiboot loader 2025-04-06 17:09:52 -04:00
box_menu.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
button_handler.h Updating DreamDex 2024-06-28 16:24:01 -04:00
button_menu.h Reduce binary size by eliminating libstdc++ 2025-04-09 20:04:08 +02:00
debug_mode.h Fixing post-transfer text box 2025-05-25 15:41:09 -04:00
dex_handler.h Updating buttons and dialogue 2024-03-29 11:55:54 -05:00
flash_mem.h Pre-Save issue fixing 2023-12-02 21:10:51 -06:00
gameboy_colour.h Fix ptgb_write_debug() after merge 2025-05-27 22:39:16 +02:00
gb_link.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
global_frame_controller.h Updating Dream Dex 2025-04-06 11:57:37 -04:00
interrupt.h Back to TONC 2023-06-09 18:31:19 -05:00
japanese_small.h Updating text rendering 2025-03-19 20:55:09 -04:00
latin_normal.h continuing work on updated text engine 2025-03-04 11:33:02 -05:00
latin_short.h Updating text rendering 2025-03-19 20:55:09 -04:00
libstd_replacements.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
multiboot_upload.h Add functionality to upload Poke Transporter GB to another gba after it has started on the current one [EZ Flash Omega] 2025-04-03 23:31:24 +02:00
mystery_gift_builder.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
mystery_gift_injector.h Modifying save data flags 2024-03-04 16:56:14 -06:00
payload_builder.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
pkmn_font.h Compress the graphics (mostly), fix potential vram memcpy issue 2025-01-17 19:32:48 -05:00
pokedex.h Updating Dex Graphics 2024-08-08 16:07:43 -04:00
pokemon_data.h Merge branch 'master' into optimizations/implement-zx0-compression 2025-05-21 13:16:45 +02:00
pokemon_party.h Implement zx0 compression 2025-04-24 21:14:48 +02:00
pokemon_trade.h Updating Link Cable Implementation 2024-02-12 13:28:06 -06:00
pokemon.h Fix merge compile error 2025-05-25 22:40:53 +02:00
random.h Disabling new conversion 2025-05-21 11:43:16 -04:00
rom_data.h Continuing box updating 2025-03-27 14:37:47 -04:00
save_data_manager.h Prep for bug testing 2024-07-03 17:44:33 -04:00
script_array.h Add compression for the text data, output stack usage .su files and rework script_array 2025-05-21 12:21:06 +02:00
script_obj.h Add compression for the text data, output stack usage .su files and rework script_array 2025-05-21 12:21:06 +02:00
script_var.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
select_menu.h Add compression for the text data, output stack usage .su files and rework script_array 2025-05-21 12:21:06 +02:00
sprite_data.h Implement zx0 compression 2025-04-24 21:14:48 +02:00
text_data_table.h Move reading dialogue text out of the text_loop() to conserve IWRAM 2025-05-21 17:39:52 +02:00
text_engine.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
translated_text.h Modifying the remote multiboot loader 2025-04-06 17:09:52 -04:00
z80_asm.h Fix crash + unrelated buffer overflow + some optimizations 2025-06-18 10:23:03 +02:00
zx0_decompressor.h Add compression for the text data, output stack usage .su files and rework script_array 2025-05-21 12:21:06 +02:00