Add compression for the text data, output stack usage .su files and rework script_array

Add a binary table format and convert the text entries into this format in text_helper/main.py. It then gets compressed with zx0.

The new text_data_table and streamed_data_table classes exist to read the various entries from this binary table. streamed_data_table specifically
exists to use a decompression buffer that is smaller than the actual binary table. But it requires a decompression buffer that is
still larger than ZX0_DEFAULT_WINDOW_SIZE (default: 2048 bytes) and will only be able to decompress in
chunks of (<decompression_buffer_size> - <ZX0_DEFAULT_WINDOW_SIZE>) bytes

Try to keep the binary text tables sufficiently small though, because since zx0 doesn't actually support random access,
getting to the last entry is significantly more expensive than reading the first one. And unless you use streamed_data_table,
it also requires <uncompressed_size> bytes of stack space, therefore IWRAM to decompress them.

I also had to rework script_array because it can no longer reference the strings directly. Instead we now reference the DIA_* "enum" values.
We also no longer store an array of script_obj instances, because these were getting stored in IWRAM since they're non-const global variables
originally. Instead we now have const arrays of script_obj_params structs, which should end up in .rodata -> therefore EWRAM.

Right now, script_obj only supports the PTGB text table (originally the dialogue array). But if the need arises to support other tables as well,
I'd consider adding a separate enum to script_obj_params to indicate the specific table.

The compilation process will also output .su files in the build folder from now on. These files indicate the stack frame size for every function in
every compilation unit, so be sure to check them from time to time. Note that they will only show the stack consumption for that specific function.
So to get the worst case stack consumption, you need to manually add all the functions in a certain stack flow.
This commit is contained in:
Philippe Symons
2025-05-21 12:21:06 +02:00
parent 12d1d10fc7
commit 26fd1e2dd3
29 changed files with 1628 additions and 534 deletions

View File

@@ -29,6 +29,7 @@
#include "multiboot_upload.h"
#include "rom_data.h"
#include "libraries/Pokemon-Gen3-to-Gen-X/include/save.h"
#include "text_data_table.h"
/*
@@ -134,8 +135,7 @@ void initalization_script(void)
oam_init(obj_buffer, 128);
load_graphics();
// Prepare dialouge
populate_script();
// Prepare text engine for dialogue
init_text_engine();
// Set the random seed
@@ -153,7 +153,15 @@ void game_load_error(void)
REG_BG2CNT = (REG_BG2CNT & ~BG_PRIO_MASK) | BG_PRIO(1);
create_textbox(4, 1, 152, 100, true);
ptgb_write(cart_load_error, true);
{
u8 general_text_table_buffer[2048];
text_data_table general_text(general_text_table_buffer);
general_text.decompress(get_compressed_general_table());
ptgb_write(general_text.get_text_entry(GENERAL_cart_load_error), true);
}
key_poll();
do
{
@@ -187,7 +195,15 @@ void first_load_message(void)
tte_set_margins(8, 8, H_MAX - 8, V_MAX);
tte_set_pos(8, 8);
tte_set_ink(INK_ROM_COLOR);
ptgb_write(intro_first, true);
{
u8 general_text_table_buffer[2048];
text_data_table general_text(general_text_table_buffer);
general_text.decompress(get_compressed_general_table());
ptgb_write(general_text.get_text_entry(GENERAL_intro_first), true);
}
while (!key_hit(KEY_A))
{
global_next_frame();
@@ -195,34 +211,64 @@ void first_load_message(void)
tte_erase_rect(0, 0, H_MAX, V_MAX);
}
#define TIMER_ENABLE 0x80
#define TIMER_CASCADE 0x4
#define TIMER_FREQ_1 0x0 // 16.78 MHz
#define TIMER_FREQ_64 0x1 // 262,144 Hz
#define TIMER_FREQ_256 0x2 // 65,536 Hz
#define TIMER_FREQ_1024 0x3 // 16,384 Hz
int test_decompress()
{
uint16_t charset[256];
// Reset both timers
REG_TM0CNT = 0;
REG_TM1CNT = 0;
REG_TM0D = 0;
REG_TM1D = 0;
// Set up TIMER0: count with no prescaler
REG_TM0CNT = TIMER_ENABLE | TIMER_FREQ_1;
// Set up TIMER1: cascade mode (increment when TIMER0 overflows)
REG_TM1CNT = TIMER_ENABLE | TIMER_CASCADE;
load_localized_charset(charset, 3, ENG_ID);
// Read combined 32-bit timer value
const u32 ticks = ((u32)REG_TM1D << 16) | REG_TM0D;
// Stop timers
REG_TM0CNT = 0;
REG_TM1CNT = 0;
create_textbox(4, 1, 160, 80, true);
ptgb_write_debug(charset, "Test results:\n\nDecompress: ", true);
ptgb_write_debug(charset, ptgb::to_string(ticks * 1000 / 16777), true);
ptgb_write_debug(charset, " usec\n", true);
while (true)
{
if (key_hit(KEY_B))
{
hide_text_box();
reset_textbox();
return 0;
}
global_next_frame();
}
return 0;
}
int credits()
{
char hexBuffer[16];
#define CREDITS_ARRAY_SIZE 19
u8 text_decompression_buffer[2048];
text_data_table credits_text_table(text_decompression_buffer);
int curr_credits_num = 0;
const byte *credits_array[CREDITS_ARRAY_SIZE] = {
credits_page_1,
credits_page_2,
credits_page_3,
credits_page_4,
credits_page_5,
credits_page_6,
credits_page_7,
credits_page_8,
credits_page_9,
credits_page_10,
credits_page_11,
credits_page_12,
credits_page_13,
credits_page_14,
credits_page_15,
credits_page_16,
credits_page_17,
credits_page_18,
credits_page_19,
// Add translators
};
credits_text_table.decompress(get_compressed_credits_table());
bool update = true;
global_next_frame();
@@ -232,7 +278,7 @@ int credits()
{
create_textbox(4, 1, 160, 80, true);
show_text_box();
ptgb_write(credits_array[curr_credits_num], true);
ptgb_write(credits_text_table.get_text_entry(curr_credits_num), true);
update = false;
}
@@ -247,13 +293,19 @@ int credits()
curr_credits_num--;
update = true;
}
if (key_hit(KEY_RIGHT) && curr_credits_num < (CREDITS_ARRAY_SIZE - 1))
if (key_hit(KEY_RIGHT) && curr_credits_num < (credits_text_table.get_number_of_text_entries() - 1))
{
curr_credits_num++;
update = true;
}
if(key_hit(KEY_SELECT))
{
return test_decompress();
}
#if 0
if (ENABLE_DEBUG_SCREEN && key_hit(KEY_SELECT))
{
char hexBuffer[16];
uint16_t charset[256];
load_localized_charset(charset, 3, ENG_ID);
if (key_held(KEY_UP) && key_held(KEY_L) && key_held(KEY_R))
@@ -327,6 +379,7 @@ int credits()
global_next_frame();
}
}
#endif
global_next_frame();
}
};
@@ -335,10 +388,16 @@ int credits()
int main_menu_loop()
{
uint8_t general_text_table_buffer[2048];
text_data_table general_text(general_text_table_buffer);
bool update = true;
const byte *menu_options[NUM_MENU_OPTIONS] = {option_transfer, option_dreamdex, option_credits};
const uint8_t menu_options[NUM_MENU_OPTIONS] = {GENERAL_option_transfer, GENERAL_option_dreamdex, GENERAL_option_credits};
const uint8_t *text_entry;
int return_values[NUM_MENU_OPTIONS] = {BTN_TRANSFER, BTN_POKEDEX, BTN_CREDITS};
u16 test = 0;
general_text.decompress(get_compressed_general_table());
while (true)
{
if (update)
@@ -346,7 +405,8 @@ int main_menu_loop()
tte_erase_rect(0, 80, 240, 160);
for (int i = 0; i < NUM_MENU_OPTIONS; i++)
{
int size = get_string_length(menu_options[i]);
text_entry = general_text.get_text_entry(menu_options[i]);
int size = get_string_length(text_entry);
int char_width = (PTGB_BUILD_LANGUAGE == JPN_ID ? 8 : 6);
int x = ((240 - (size * char_width)) / 2);
tte_set_pos(x, ((i * 17) + 80));
@@ -358,7 +418,7 @@ int main_menu_loop()
{
tte_set_ink(INK_ROM_COLOR);
}
ptgb_write(menu_options[i], true);
ptgb_write(text_entry, true);
test++;
}
}
@@ -387,6 +447,93 @@ int main_menu_loop()
}
}
// Legal mumbo jumbo
static void show_legal_text(const u8* intro_text)
{
tte_set_margins(8, 8, H_MAX - 8, V_MAX - 8);
tte_set_pos(8, 8);
tte_set_ink(INK_ROM_COLOR);
ptgb_write(intro_text, true);
bool wait = true;
while (wait)
{
global_next_frame();
if (key_hit(KEY_A))
{
wait = false;
}
}
}
// Gears of Progress
static void show_gears_of_progress()
{
tte_erase_rect(0, 0, 240, 160);
REG_BG1VOFS = 0;
delay_counter = 0;
while (delay_counter < (15 * 60))
{
global_next_frame();
delay_counter++;
if (key_hit(KEY_A))
{
delay_counter = (15 * 60);
}
}
}
// split off from the main function in order to keep the scope of the variables limited to the execution of this function
// otherwise they stick around for the entire runtime of the program
// the attribute noinline is used to prevent the compiler from inlining this function back into the main function
// this decision was based on the output of build/main.su after adding the -fstack-usage compile flag
static void __attribute__((noinline)) show_intro()
{
bool start_pressed = false;
u8 general_text_table_buffer[2048];
u8 press_start_text[32];
u8 press_start_text_length;
text_data_table general_text(general_text_table_buffer);
const u8 *text_entry;
general_text.decompress(get_compressed_general_table());
text_entry = general_text.get_text_entry(GENERAL_press_start);
press_start_text_length = get_string_length(text_entry);
memcpy(press_start_text, text_entry, press_start_text_length + 1);
text_entry = general_text.get_text_entry(GENERAL_intro_legal);
show_legal_text(text_entry);
show_gears_of_progress();
REG_BG1CNT = REG_BG1CNT | BG_PRIO(3);
key_poll(); // Reset the keys
curr_rom.load_rom();
obj_set_pos(ptgb_logo_l, 56, 12);
obj_set_pos(ptgb_logo_r, 56 + 64, 12);
obj_unhide_multi(ptgb_logo_l, 1, 2);
REG_BLDCNT = BLD_BUILD(BLD_BG3, BLD_BG0, 1);
int char_width = (PTGB_BUILD_LANGUAGE == JPN_ID ? 8 : 6);
int x = ((240 - (press_start_text_length * char_width)) / 2);
tte_set_pos(x, 12 * 8);
tte_set_ink(INK_DARK_GREY);
ptgb_write(press_start_text, true);
int fade = 0;
while (!start_pressed)
{
fade = abs(((get_frame_count() / 6) % 24) - 12);
global_next_frame();
start_pressed = key_hit(KEY_START) | key_hit(KEY_A);
REG_BLDALPHA = BLDA_BUILD(0b10000, fade);
};
}
int main(void)
{
initalization_script();
@@ -400,60 +547,8 @@ int main(void)
first_load_message();
}*/
// Legal mumbo jumbo
tte_set_margins(8, 8, H_MAX - 8, V_MAX - 8);
tte_set_pos(8, 8);
tte_set_ink(INK_ROM_COLOR);
ptgb_write(intro_legal, true);
bool wait = true;
while (wait)
{
global_next_frame();
if (key_hit(KEY_A))
{
wait = false;
}
}
show_intro();
// Gears of Progress
tte_erase_rect(0, 0, 240, 160);
REG_BG1VOFS = 0;
delay_counter = 0;
while (delay_counter < (15 * 60))
{
global_next_frame();
delay_counter++;
if (key_hit(KEY_A))
{
delay_counter = (15 * 60);
}
}
REG_BG1CNT = REG_BG1CNT | BG_PRIO(3);
key_poll(); // Reset the keys
curr_rom.load_rom();
obj_set_pos(ptgb_logo_l, 56, 12);
obj_set_pos(ptgb_logo_r, 56 + 64, 12);
obj_unhide_multi(ptgb_logo_l, 1, 2);
bool start_pressed = false;
REG_BLDCNT = BLD_BUILD(BLD_BG3, BLD_BG0, 1);
int size = get_string_length(press_start);
int char_width = (PTGB_BUILD_LANGUAGE == JPN_ID ? 8 : 6);
int x = ((240 - (size * char_width)) / 2);
tte_set_pos(x, 12 * 8);
tte_set_ink(INK_DARK_GREY);
ptgb_write(press_start, true);
int fade = 0;
while (!start_pressed)
{
fade = abs(((get_frame_count() / 6) % 24) - 12);
global_next_frame();
start_pressed = key_hit(KEY_START) | key_hit(KEY_A);
REG_BLDALPHA = BLDA_BUILD(0b10000, fade);
};
key_poll();
tte_erase_rect(0, 0, H_MAX, V_MAX);
REG_BLDALPHA = BLDA_BUILD(0b10000, 0); // Reset fade