diff --git a/.gitignore b/.gitignore index 4f97c25..f98726d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ node_modules/ # Files examples/multiboot +loader.gbfs .DS_Store *.elf *.gba diff --git a/examples/LinkCard_demo/Makefile b/examples/LinkCard_demo/Makefile index 3885fb1..e59975c 100644 --- a/examples/LinkCard_demo/Makefile +++ b/examples/LinkCard_demo/Makefile @@ -134,7 +134,7 @@ TITLE := $(PROJ) LIBS := -ltonc -lugba BUILD := build -SRCDIRS := src ../_lib ../../lib ../../lib/iwram_code +SRCDIRS := src ../_lib ../../lib ../../lib/iwram_code ../_lib/libgbfs DATADIRS := data INCDIRS := src LIBDIRS := $(TONCLIB) $(PWD)/../_lib/libugba @@ -279,10 +279,14 @@ endif # End BUILD switch .PHONY: clean rebuild start -rebuild: clean $(BUILD) +rebuild: clean $(BUILD) package -start: - start "$(TARGET).gba" +package: $(BUILD) + cd gbfs_files/ && gbfs loader.gbfs * && mv loader.gbfs .. && cd .. + ../gbfs.sh "$(TARGET).gba" loader.gbfs "$(TARGET).out.gba" + +start: package + start "$(TARGET).out.gba" restart: rebuild start diff --git a/examples/LinkCard_demo/src/main.cpp b/examples/LinkCard_demo/src/main.cpp index 9a3d199..57076b0 100644 --- a/examples/LinkCard_demo/src/main.cpp +++ b/examples/LinkCard_demo/src/main.cpp @@ -4,6 +4,12 @@ #include "../../_lib/common.h" #include "../../_lib/interrupt.h" +extern "C" { +#include "../../_lib/libgbfs/gbfs.h" +} + +static const GBFS_FILE* fs = find_first_gbfs_file(0); + // (1) Create a LinkCard instance LinkCard* linkCard = new LinkCard(); @@ -17,7 +23,18 @@ void init() { int main() { init(); - // bool a = true; + // Ensure there are GBFS files + if (fs == NULL) { + Common::log("! GBFS file not found"); + while (true) + ; + } else if (gbfs_get_nth_obj(fs, 0, NULL, NULL) == NULL) { + Common::log("! No files found (GBFS)"); + while (true) + ; + } + + bool a = true; while (true) { std::string output = "LinkCard_demo (v8.0.0)\n\n"; @@ -26,11 +43,21 @@ int main() { auto device = linkCard->getConnectedDevice(); switch (device) { case LinkCard::ConnectedDevice::E_READER_LOADER_NEEDED: { - output += "e-Reader"; + output += "e-Reader\n\nPress A to send the loader."; break; } case LinkCard::ConnectedDevice::DLC_LOADER: { output += "DLC Loader"; + + if (Common::didPress(KEY_A, a)) { + u32 loaderSize; + const u8* loader = + (const u8*)gbfs_get_nth_obj(fs, 0, NULL, &loaderSize); + auto res = linkCard->sendLoader(loader, loaderSize); + Common::log(std::to_string((int)res)); + Common::waitForKey(KEY_DOWN); + } + break; } default: { @@ -42,17 +69,6 @@ int main() { } } - // output += "Press A to send the loader"; - - // if (Common::didPress(KEY_A, a)) { - // LinkRawCable lrc; - // lrc.activate(); - // auto bb = lrc.transfer(0); - // Common::log(std::to_string(bb.data[1])); - // Common::waitForKey(KEY_DOWN); - // // linkCard->sendLoader() - // } - VBlankIntrWait(); Common::log(output); } diff --git a/examples/compile.sh b/examples/compile.sh index 0de4812..23b348d 100755 --- a/examples/compile.sh +++ b/examples/compile.sh @@ -34,7 +34,7 @@ compile() { # LinkCard_demo cd LinkCard_demo/ make rebuild $args - cp LinkCard_demo$suffix.gba ../$folder/ + cp LinkCard_demo$suffix.out.gba ../$folder/LinkCard_demo$suffix.gba cd .. # LinkCube_demo diff --git a/lib/LinkCard.hpp b/lib/LinkCard.hpp index c17af97..2465d5a 100644 --- a/lib/LinkCard.hpp +++ b/lib/LinkCard.hpp @@ -9,9 +9,9 @@ // LinkCard* linkCard = new LinkCard(); // - 2) Probe the connected device: // auto device = getConnectedDevice(); -// - 2) Send the loader program: +// - 2) Send the DLC loader program: // if (device == LinkCard::ConnectedDevice::E_READER_LOADER_NEEDED) -// linkCard->sendLoader(loader); +// LinkCard::SendResult result = linkCard->sendLoader(loader); // - 3) Receive scanned cards: // if (device == LinkCard::ConnectedDevice::CARD_SCANNER) { // u8 card[2076] @@ -68,10 +68,13 @@ class LinkCard { * protocol. */ ConnectedDevice getConnectedDevice() { + LINK_READ_TAG(LINK_CARD_VERSION); + linkRawCable.activate(); + auto guard = Link::ScopeGuard([&]() { linkRawCable.deactivate(); }); + if (linkRawCable.transfer(0).playerId != 0) return ConnectedDevice::WRONG_CONNECTION; - u16 remoteValues[3]; for (u32 i = 0; i < 3; i++) { remoteValues[i] = linkRawCable.transfer(0).data[1]; @@ -87,12 +90,11 @@ class LinkCard { /** * Sends the loader card. - * @param loader A pointer to a 2112-byte e-Reader program that sends + * @param loader A pointer to a e-Reader program that sends * the scanned cards to the game. Must be 4-byte aligned. + * @param loaderSize Size of the loader program in bytes. */ - SendResult sendLoader(const u8* loader) { - LINK_READ_TAG(LINK_CARD_VERSION); - + SendResult sendLoader(const u8* loader, u32 loaderSize) { if ((u32)loader % 4 != 0) return SendResult::UNALIGNED; diff --git a/lib/_link_common.hpp b/lib/_link_common.hpp index 926a01b..49358e9 100644 --- a/lib/_link_common.hpp +++ b/lib/_link_common.hpp @@ -244,6 +244,13 @@ static inline void intToStr5(char* buf, int num) { buf[j] = '\0'; } +template +struct ScopeGuard { + Func f; + ScopeGuard(Func f) : f(f) {} + ~ScopeGuard() { f(); } +}; + // Interfaces class AsyncMultiboot {