Adding code to call sendLoader in LinkCard example

This commit is contained in:
Rodrigo Alfonso 2025-02-15 03:44:31 -03:00
parent 5372106db5
commit 3d0fa72083
6 changed files with 55 additions and 25 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ node_modules/
# Files
examples/multiboot
loader.gbfs
.DS_Store
*.elf
*.gba

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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;

View File

@ -244,6 +244,13 @@ static inline void intToStr5(char* buf, int num) {
buf[j] = '\0';
}
template <typename Func>
struct ScopeGuard {
Func f;
ScopeGuard(Func f) : f(f) {}
~ScopeGuard() { f(); }
};
// Interfaces
class AsyncMultiboot {