diff --git a/include/gameboy_colour.h b/include/gameboy_colour.h index f6a8ac5..ff22169 100644 --- a/include/gameboy_colour.h +++ b/include/gameboy_colour.h @@ -6,6 +6,7 @@ #include #include #include "LinkGPIO.h" + /* #define LINK_SPI_NO_DATA 0xffffffff #define LINK_SPI_SIOCNT_NORMAL 0 diff --git a/include/pokemon_party.h b/include/pokemon_party.h index 8449115..8baf851 100644 --- a/include/pokemon_party.h +++ b/include/pokemon_party.h @@ -4,14 +4,17 @@ #include #include "pokemon.h" -class Pokemon_Party{ - public: +class Pokemon_Party +{ +public: Pokemon_Party(); - int start_link(); + void start_link(); + int get_last_error(); - private: +private: Pokemon party[6]; - byte party_data[444]; + byte party_data_array[444]; + int last_error; }; #endif \ No newline at end of file diff --git a/include/script_array.h b/include/script_array.h index 2f0d4bb..c24dc7f 100644 --- a/include/script_array.h +++ b/include/script_array.h @@ -4,8 +4,9 @@ #include "script_obj.h" #include "pokemon_party.h" -extern script_obj script[11]; +extern script_obj script[13]; void add_script_party_var(Pokemon_Party var); +bool run_conditional(int index); #endif \ No newline at end of file diff --git a/include/script_obj.h b/include/script_obj.h index 9f54a5c..95e92e9 100644 --- a/include/script_obj.h +++ b/include/script_obj.h @@ -6,10 +6,10 @@ #define CHECK_GAME 1 #define START_LINK 2 - -#define ERROR_TIMEOUT 10 -#define ERROR_DISCONNECT 11 -#define ERROR_COM_ENDED 12 +#define ERROR_TIMEOUT_ONE 3 +#define ERROR_DISCONNECT 4 +#define ERROR_COM_ENDED 5 +#define ERROR_TIMEOUT_TWO 6 class script_obj { @@ -18,21 +18,17 @@ public: script_obj(std::string nText, int nNext_index); script_obj(std::string nText, int nNext_index, int nCond_index); script_obj(std::string nText, int nNext_index, int nCond_index, int nFalse_index); - void link_party_var(Pokemon_Party *var); std::string get_text(); int get_true_index(); int get_false_index(); int get_cond_id(); - int get_next_obj_id(); private: std::string text; int next_index; int conditional_index; int next_false_index; - bool run_conditional(int index); - Pokemon_Party *party_data; //ME }; #endif \ No newline at end of file diff --git a/include/text_engine.h b/include/text_engine.h index 661c91b..f65e36b 100644 --- a/include/text_engine.h +++ b/include/text_engine.h @@ -5,17 +5,18 @@ #include #include "script_obj.h" +#include "script_array.h" class text_engine { public: text_engine(); void next_frame(); + int get_next_obj_id(script_obj current_line); private: script_obj curr_line; int frame_count; - Pokemon_Party *party_data; void set_next_line(std::string next); }; diff --git a/source/gameboy_colour.cpp b/source/gameboy_colour.cpp index 9ab6531..755bb98 100644 --- a/source/gameboy_colour.cpp +++ b/source/gameboy_colour.cpp @@ -9,7 +9,8 @@ #define BYTE_INCOMPLETE 0 #define BYTE_COMPLETE 1 #define TIMEOUT 2 -#define TIMEOUT_LENGTH 1000000 +#define TIMEOUT_ONE_LENGTH 1000000 // Maybe keep a 10:1 ratio between ONE and TWO? +#define TIMEOUT_TWO_LENGTH 100000 const int MODE = 1; // mode=0 will transfer pokemon data from pokemon.h // mode=1 will copy pokemon party data being received @@ -361,29 +362,34 @@ int transferBit(byte *party_data) } in_data = 0; } - while (!((connection_state >= 2) ? SC_state : linkGPIO->readPin(LinkGPIO::Pin::SC))) { updateFrames(); timeout++; - if (timeout > TIMEOUT_LENGTH) + if (timeout > TIMEOUT_TWO_LENGTH) { timeout = 0; return TIMEOUT; } + /* + if (timeout % 100 == 0) + { + tte_set_pos(0, 0); + tte_erase_line(); + tte_write(std::to_string(timeout).c_str()); + }*/ } // no activity when clock is low //!digitalRead(SCLK_) // digitalWrite(MOSI_, out_data & 0x80 ? HIGH : LOW); linkGPIO->writePin(LinkGPIO::Pin::SO, (out_data >> 7)); out_data <<= 1; - return flag; } int loop(byte *party_data) { - byte small_array[12]; int counter = 0; + int timeout; int transfer_state = BYTE_INCOMPLETE; while (true) { @@ -391,19 +397,23 @@ int loop(byte *party_data) while (((connection_state >= 2) ? SC_state : linkGPIO->readPin(LinkGPIO::Pin::SC))) /*digitalRead(SCLK_)*/ { updateFrames(); + timeout++; + if (timeout > TIMEOUT_ONE_LENGTH){ + return ERROR_TIMEOUT_ONE; + } } + timeout = 0; // updateFrames(); - - transfer_state = transferBit(&party_data[counter]); + transfer_state = transferBit(&party_data[counter]); // This statement will cause a loop. If that loop takes too long, then the transfer state will be TIMEOUT if (transfer_state == BYTE_COMPLETE) { counter++; } else if ( - transfer_state == TIMEOUT || FF_count > 25 || zero_count > 25) + transfer_state == TIMEOUT) { - return ERROR_TIMEOUT; + return ERROR_TIMEOUT_TWO; } if (FF_count > 25) { diff --git a/source/main.cpp b/source/main.cpp index 08887a9..c4ba37a 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -175,8 +175,7 @@ int main(void) initalize_memory_locations(); back_main(); - //add_script_party_var(party); - + add_script_party_var(party); // load_sprite(); // MAIN LOOP @@ -186,8 +185,6 @@ int main(void) poke_rand.next_frame(); back_frame(); main_text.next_frame(); - - } } diff --git a/source/pokemon_party.cpp b/source/pokemon_party.cpp index ae0df35..4526401 100644 --- a/source/pokemon_party.cpp +++ b/source/pokemon_party.cpp @@ -2,8 +2,11 @@ #include "pokemon_party.h" Pokemon_Party::Pokemon_Party(){}; -int Pokemon_Party::start_link() +void Pokemon_Party::start_link() { setup(); - loop(&party_data[0]); + last_error = loop(&party_data_array[0]); +} +int Pokemon_Party::get_last_error(){ + return last_error; } diff --git a/source/script_array.cpp b/source/script_array.cpp index 5326df3..9b2aa1a 100644 --- a/source/script_array.cpp +++ b/source/script_array.cpp @@ -1,28 +1,66 @@ #include "script_array.h" -#include "script_obj.h" -#include "pokemon_party.h" #include -script_obj script[11] = -{ - script_obj("", 2, CHECK_GAME, 1), // 0 - script_obj("Game not detected. Please turn off your system and upload the program again.", 2), // 1 - script_obj("Welcome to Pokemon Mirror!", 3), // 2 - script_obj("Please connect your GBA to a GB in the trading room, and press A", 4, START_LINK), // 3 - script_obj("", 6, ERROR_COM_ENDED, 5), // 4 - script_obj("Error: Communication ended.", 3), // 5 - script_obj("", 8, ERROR_DISCONNECT, 7), // 6 - script_obj("Error: Cable disconnected.", 3), // 7 - script_obj("", 10, ERROR_TIMEOUT, 9), // 8 - script_obj("Error: Timed out.", 3), // 9 - script_obj("Connection successful! Transfering in Pokemon now", 5), // 10 +int last_error; +Pokemon_Party party_data; + +script_obj script[13] = + { + script_obj("", 2, CHECK_GAME, 1), // 0 + script_obj("Game not detected. Please turn off your system and upload the program again.", 2), // 1 + script_obj("Welcome to Pokemon Mirror!", 3), // 2 + script_obj("Please connect your GBA to a GB in the trading room, and press A", 4, START_LINK), // 3 + script_obj("", 6, ERROR_COM_ENDED, 5), // 4 + script_obj("Error: Communication ended.", 3), // 5 + script_obj("", 8, ERROR_DISCONNECT, 7), // 6 + script_obj("Error: Cable disconnected.", 3), // 7 + script_obj("", 10, ERROR_TIMEOUT_ONE, 9), // 8 + script_obj("Error: Timed out (1).", 3), // 9 + script_obj("", 12, ERROR_TIMEOUT_TWO, 11), // 10 + script_obj("Error: Timed out (2)", 3), // 11 + script_obj("Connection successful! Transfering in Pokemon now", 5), // 12 }; -void add_script_party_var(Pokemon_Party *var){ - for (int i = 0; i < 11; i++){ - script[i].link_party_var(var); - } +void add_script_party_var(Pokemon_Party var) +{ + party_data = var; } +bool run_conditional(int index) +{ + // Here is most of the logic that drives what lines show up where. It's probably not the best way to code it, but it works + switch (index) + { + case CHECK_GAME: + { + u32 game_code = (*(vu32 *)(0x80000AC)) & 0xFFFFFF; + return (game_code == 0x565841 || // Ruby + game_code == 0x505841 || // Sapphire + game_code == 0x525042 || // FireRed + game_code == 0x475042 || // LeafGreen + game_code == 0x455042 // Emerald + ); + } + case START_LINK: + party_data.start_link(); + return true; + case ERROR_COM_ENDED: + return party_data.get_last_error() != ERROR_COM_ENDED; + + case ERROR_DISCONNECT: + return party_data.get_last_error() != ERROR_DISCONNECT; + + case ERROR_TIMEOUT_ONE: + return party_data.get_last_error() != ERROR_TIMEOUT_ONE; + + case ERROR_TIMEOUT_TWO: + return party_data.get_last_error() != ERROR_TIMEOUT_TWO; + + default: + tte_set_pos(0, 0); + tte_write("ERROR! No conditional found."); + return false; + } +} diff --git a/source/script_obj.cpp b/source/script_obj.cpp index abfc176..f3abb5f 100644 --- a/source/script_obj.cpp +++ b/source/script_obj.cpp @@ -37,9 +37,6 @@ script_obj::script_obj(std::string nText, int nNext_index, int nCond_index, int next_false_index = nFalse_index; } -void script_obj::link_party_var(Pokemon_Party *var){ - party_data = var; -} std::string script_obj::get_text() { @@ -59,47 +56,4 @@ int script_obj::get_false_index() int script_obj::get_cond_id() { return conditional_index; -} - -int script_obj::get_next_obj_id() -{ - if (conditional_index == 0) - { - return next_index; - } - else - { - if (run_conditional(conditional_index)) - { - return next_index; - } - return next_false_index; - } -} - -bool script_obj::run_conditional(int index) -{ - // Here is most of the logic that drives what lines show up where. It's probably not the best way to code it, but it works - switch (index) - { - case CHECK_GAME: - { - u32 game_code = (*(vu32 *)(0x80000AC)) & 0xFFFFFF; - return (game_code == 0x565841 || // Ruby - game_code == 0x505841 || // Sapphire - game_code == 0x525042 || // FireRed - game_code == 0x475042 || // LeafGreen - game_code == 0x455042 // Emerald - ); - } - case START_LINK: - party_data->start_link(); - return true; - - //case : - default: - tte_set_pos(0, 0); - tte_write("ERROR! No conditional found."); - return false; - } -} +} \ No newline at end of file diff --git a/source/text_engine.cpp b/source/text_engine.cpp index c207fba..634eb1e 100644 --- a/source/text_engine.cpp +++ b/source/text_engine.cpp @@ -2,7 +2,6 @@ #include #include "text_engine.h" -#include "script_array.h" const int H_MAX = 240; const int V_MAX = 160; @@ -12,7 +11,6 @@ const int TOP = 122; const int BOTTOM = V_MAX; uint char_count = 0; -Pokemon_Party party_data; text_engine::text_engine() { @@ -40,12 +38,29 @@ void text_engine::next_frame() } else { - if (key_hit(KEY_A) || frame_count <= 1) + if (key_hit(KEY_A) || char_count == 0) { - curr_line = script[curr_line.get_next_obj_id()]; + curr_line = script[get_next_obj_id(curr_line)]; char_count = 0; } } frame_count++; } + +int text_engine::get_next_obj_id(script_obj current_line) +{ + if (current_line.get_cond_id() == 0) + { + return current_line.get_true_index(); + } + else + { + if (run_conditional(current_line.get_cond_id())) + { + return current_line.get_true_index(); + } + return current_line.get_false_index(); + } +} +