diff --git a/include/link_handler.h b/include/link_handler.h index 3ca82fb..10fb381 100644 --- a/include/link_handler.h +++ b/include/link_handler.h @@ -69,13 +69,23 @@ struct LinkState bool test_packet_fail = false; // ??? byte data_packet[PACKET_SIZE]; + + // This is info that was passed in via handleIncomingByte + byte in; + byte *box_data_storage; + byte *curr_payload; + GB_ROM *curr_gb_rom; + PokeBox *box; + const u16 *debug_charset; + bool cancel_connection; }; void setup(const u16 *debug_charset); -byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBox *box, const u16 *debug_charset, bool cancel_connection); +byte handleIncomingByte(); int loop(byte *box_data_storage, byte *curr_payload, GB_ROM *curr_rom, PokeBox *box, const u16 *debug_charset, bool cancel_connection); byte exchange_parties(byte curr_in, byte *curr_payload); byte exchange_boxes(byte curr_in, byte *party_data, GB_ROM *curr_gb_rom, const u16 *debug_charset); byte exchange_remove_array(byte curr_in, PokeBox *box, bool cancel_connection); +void handshake(); #endif /* LINK_HANDLER_H_ */ diff --git a/source/link_handler.cpp b/source/link_handler.cpp index 9f39c02..8ec5791 100644 --- a/source/link_handler.cpp +++ b/source/link_handler.cpp @@ -70,9 +70,9 @@ void print(const char *format, ...) void setup(const u16 *debug_charset) { - interrupt_init(); - interrupt_set_handler(INTR_SERIAL, LINK_SPI_ISR_SERIAL); - interrupt_enable(INTR_SERIAL); + REG_TM3D = -0x4000; + REG_TM3CNT = TM_FREQ_1024 | TM_ENABLE; + irq_enable(II_TIMER3); linkSPI->activate(LinkSPI::Mode::MASTER_256KBPS); linkSPI->setWaitModeActive(false); @@ -89,18 +89,18 @@ void setup(const u16 *debug_charset) } } -byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBox *box, const u16 *debug_charset, bool cancel_connection) +byte handleIncomingByte() { switch (currLinkState->conState) { case HS: currLinkState->mosi_delay = 4; - if (curr_gb_rom->generation == 2) + if (currLinkState->curr_gb_rom->generation == 2) { currLinkState->conState = ACK; return 0x00; } - if (in == 0x00) + if (currLinkState->in == 0x00) { currLinkState->conState = ACK; return 0x01; @@ -108,9 +108,9 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ break; case ACK: - if (curr_gb_rom->generation == 2) + if (currLinkState->curr_gb_rom->generation == 2) { - if (in == 0x61) + if (currLinkState->in == 0x61) { currLinkState->conState = MENU; return 0x61; @@ -119,12 +119,12 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ } else { - if (in == 0x00) + if (currLinkState->in == 0x00) { currLinkState->conState = MENU; return 0x00; } - else if (in == 0x02) + else if (currLinkState->in == 0x02) { currLinkState->conState = HS; return 0x02; @@ -133,7 +133,7 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ break; case MENU: - if (in == 0x60 || in == 0x61) + if (currLinkState->in == 0x60 || currLinkState->in == 0x61) { { u8 general_text_table_buffer[2048]; @@ -147,16 +147,16 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ link_animation_state(STATE_NO_ANIM); currLinkState->conState = PRETRADE; currLinkState->data_counter = 0; - return in; + return currLinkState->in; } - else if (in == 0x02) + else if (currLinkState->in == 0x02) { currLinkState->conState = HS; return 0x02; } else { - return in; + return currLinkState->in; } break; @@ -167,11 +167,11 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ currLinkState->conState = TRADE; } currLinkState->data_counter++; - if (curr_gb_rom->generation == 2) + if (currLinkState->curr_gb_rom->generation == 2) { return 0x61; } - else if (curr_gb_rom->generation == 1 && curr_gb_rom->version == YELLOW_ID) + else if (currLinkState->curr_gb_rom->generation == 1 && currLinkState->curr_gb_rom->version == YELLOW_ID) { return 0xD5; } @@ -181,7 +181,7 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ } break; case TRADE: - if (in == 0xfd) + if (currLinkState->in == 0xfd) { { u8 general_text_table_buffer[2048]; @@ -196,20 +196,20 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ currLinkState->mosi_delay = 1; currLinkState->conState = PARTY_PREAMBLE; } - return in; + return currLinkState->in; break; case PARTY_PREAMBLE: - if (in != 0xfd) + if (currLinkState->in != 0xfd) { currLinkState->conState = TRADE_DATA; - return exchange_parties(in, curr_payload); + return exchange_parties(currLinkState->in, currLinkState->curr_payload); } - return in; + return currLinkState->in; break; case TRADE_DATA: - if (currLinkState->data_counter >= curr_gb_rom->payload_size) + if (currLinkState->data_counter >= currLinkState->curr_gb_rom->payload_size) { - if (in == 0xFD) + if (currLinkState->in == 0xFD) { currLinkState->conState = BOX_PREAMBLE; currLinkState->init_packet = true; @@ -219,18 +219,18 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ return 0x00; } } - return exchange_parties(in, curr_payload); + return exchange_parties(currLinkState->in, currLinkState->curr_payload); break; case BOX_PREAMBLE: - if (in != 0xFD) + if (currLinkState->in != 0xFD) { currLinkState->conState = BOX_DATA; - return exchange_boxes(in, box_data_storage, curr_gb_rom, debug_charset); + return exchange_boxes(currLinkState->in, currLinkState->box_data_storage, currLinkState->curr_gb_rom, currLinkState->debug_charset); } - return in; + return currLinkState->in; break; case BOX_DATA: - return exchange_boxes(in, box_data_storage, curr_gb_rom, debug_charset); + return exchange_boxes(currLinkState->in, currLinkState->box_data_storage, currLinkState->curr_gb_rom, currLinkState->debug_charset); break; case REBOOT: currLinkState->data_counter = 0; @@ -239,12 +239,12 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ break; case REMOVE_ARRAY_PREAMBLE: - if (in != 0xFD) + if (currLinkState->in != 0xFD) { currLinkState->conState = SEND_REMOVE_ARRAY; - return exchange_remove_array(in, box, cancel_connection); + return exchange_remove_array(currLinkState->in, currLinkState->box, currLinkState->cancel_connection); } - return in; + return currLinkState->in; break; case SEND_REMOVE_ARRAY: @@ -253,15 +253,15 @@ byte handleIncomingByte(byte in, byte *box_data_storage, byte *curr_payload, GB_ currLinkState->conState = END2; } currLinkState->data_counter++; - return exchange_remove_array(in, box, cancel_connection); + return exchange_remove_array(currLinkState->in, currLinkState->box, currLinkState->cancel_connection); break; default: - return in; + return currLinkState->in; break; } - return 0; // This should never hit + return 0; // This should never hit but the compiler likes this being here } int loop(byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBox *box, const u16 *debug_charset, bool cancel_connection) @@ -270,6 +270,14 @@ int loop(byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBo #define NUM_LINES 8 int counter = 0; char stuff[NUM_LINES][LINE_WIDTH]; + + currLinkState->box_data_storage = box_data_storage; + currLinkState->curr_payload = curr_payload; + currLinkState->curr_gb_rom = curr_gb_rom; + currLinkState->box = box; + currLinkState->debug_charset = debug_charset; + currLinkState->cancel_connection = cancel_connection; + while (true) { if (g_debug_options.print_link_data && key_held(KEY_L)) @@ -321,7 +329,7 @@ int loop(byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBo } } - currLinkState->out_data = handleIncomingByte(currLinkState->in_data, box_data_storage, curr_payload, curr_gb_rom, box, debug_charset, cancel_connection); + currLinkState->out_data = handleIncomingByte(); if (currLinkState->FF_count > (15 * 60)) { @@ -374,6 +382,11 @@ int loop(byte *box_data_storage, byte *curr_payload, GB_ROM *curr_gb_rom, PokeBo } }; +void handshake() +{ + currLinkState->out_data = handleIncomingByte(); +} + byte exchange_parties(byte curr_in, byte *curr_payload) { int ret = curr_payload[currLinkState->data_counter]; diff --git a/source/main.cpp b/source/main.cpp index 9cf3da4..7bd7041 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -89,7 +89,7 @@ void initialization_script(void) // sound_init(); // Link Cable init - //irq_add(II_TIMER0, handleIncomingByte); + irq_add(II_TIMER3, handshake); // Graphics init irq_add(II_VBLANK, global_next_frame); diff --git a/source/pokemon_party.cpp b/source/pokemon_party.cpp index 278b0a0..11665c7 100644 --- a/source/pokemon_party.cpp +++ b/source/pokemon_party.cpp @@ -183,7 +183,10 @@ void Pokemon_Party::start_link() // This used to clear out the box data, probably isn't needed anymore // memset(box_data_array, 0, curr_gb_rom.box_data_size); - last_error = loop(&box_data_array[0], current_payload, &curr_gb_rom, &box, debug_charset, false); + //last_error = loop(&box_data_array[0], current_payload, &curr_gb_rom, &box, debug_charset, false); + while(true){ + VBlankIntrWait(); + }; box.loadData(curr_gb_rom.generation, (Language)curr_gb_rom.language, box_data_array); if (g_debug_options.write_cable_data_to_save)