Skip to content
This repository has been archived by the owner on Dec 24, 2018. It is now read-only.

Commit

Permalink
Add Games menu now works again, and now caches banners at correct size
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Feb 24, 2018
1 parent c23412e commit 518ba74
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 101 deletions.
8 changes: 4 additions & 4 deletions flashcard-side/arm7/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ int main() {
//---------------------------------------------------------------------------------
nocashMessage("ARM7 main.c main");

REG_SCFG_ROM = 0x703;
REG_SCFG_CLK = 0x0187;
//REG_SCFG_ROM = 0x703;
//REG_SCFG_CLK = 0x0187;
// REG_SCFG_EXT = 0x93A40000;

// SCFG_CLK
Expand All @@ -65,7 +65,7 @@ int main() {
// REG_SCFG_CLK = 0x0181;

readUserSettings();
ledBlink(0);
//ledBlink(0);

irqInit();
// Start the RTC tracking IRQ
Expand All @@ -80,7 +80,7 @@ int main() {
irqSet(IRQ_VCOUNT, VcountHandler);
irqSet(IRQ_VBLANK, VblankHandler);

irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK);
irqEnable( IRQ_VBLANK | IRQ_VCOUNT );

setPowerButtonCB(powerButtonCB);

Expand Down
2 changes: 1 addition & 1 deletion flashcard-side/arm9/source/iconTitle.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void iconTitleInit (void) {

// everything's ready :)
writeRow (0,"...initializing...");
writeRow (1,"===>>> HBMenu+ <<<===");
writeRow (1,"===>>> Add Games <<<===");
writeRow (2,"(this text should disappear...");
writeRow (3,"...otherwise, trouble!)");
}
Expand Down
2 changes: 1 addition & 1 deletion flashcard-side/arm9/source/log.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>
#include "log.h"

bool LogCreated = true; // It's already done by the GUI
bool LogCreated = false;

int createLog(void) {
if (!LogCreated){
Expand Down
115 changes: 61 additions & 54 deletions flashcard-side/arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#include "inifile.h"
#include "log.h"

sNDSHeaderExt ndsHeader;
sNDSBannerExt ndsBanner;

using namespace std;

bool logEnabled = false;
Expand Down Expand Up @@ -126,38 +129,52 @@ void RemoveTrailingSlashes(string& path)
}
}


//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------
// REG_SCFG_CLK = 0x80;
REG_SCFG_CLK = 0x85;

bool TriggerExit = false;
std::string fcromfolder;
char fcfolder_path[256];

iconTitleInit();

// Subscreen as a console
videoSetModeSub(MODE_0_2D);
vramSetBankH(VRAM_H_SUB_BG);
consoleInit(NULL, 0, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true);

/* Log file is dissabled by default. If _nds/twloader/log exist, we turn log file on, else, log is dissabled */
struct stat logBuf;
logEnabled = stat("sd:/_nds/twloader/log", &logBuf) == 0;
//struct stat logBuf;
//logEnabled = stat("sd:/_nds/twloader/log", &logBuf) == 0;
/* Log configuration file end */

if (fatInitDefault()) {
if(fatInitDefault()) {
printf ("Reading TWLoader settings\n");
CIniFile twloaderini( "sd:/_nds/twloader/settings.ini" );
if (logEnabled) LogFM("Flashcard.Main", "Fat inited");
printf ("TWLoader settings read\n");
// if (logEnabled) LogFM("Flashcard.Main", "Fat inited");

// overwrite reboot stub identifier
// extern u64 *fake_heap_end;
// *fake_heap_end = 0;

// defaultExceptionHandler();


printf ("Reading flashcard ini folder\n");
fcromfolder = twloaderini.GetString( "FRONTEND", "FCROM_FOLDER", "");
printf ("Removing trailing slashes\n");
RemoveTrailingSlashes(fcromfolder);
printf ("Done!\n");
if (fcromfolder.empty()) {
fcromfolder = "roms/flashcard/nds";
}
snprintf(fcfolder_path, sizeof(fcfolder_path), "sd:/%s", fcromfolder.c_str());
} else {
iprintf ("fatInitDefault failed!\n");

doPause();

TriggerExit = true;
}

int pathLen;
Expand All @@ -167,21 +184,6 @@ int main(int argc, char **argv) {
char inifilepath[256];
std::string inifilepathfixed;

iconTitleInit();

// Subscreen as a console
videoSetModeSub(MODE_0_2D);
vramSetBankH(VRAM_H_SUB_BG);
consoleInit(NULL, 0, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true);

if (!fatInitDefault()) {
iprintf ("fatinitDefault failed!\n");

doPause();

TriggerExit = true;
}

chdir ("fat:/"); // Change directory to flashcard root

keysSetRepeat(25,5);
Expand Down Expand Up @@ -217,50 +219,55 @@ int main(int argc, char **argv) {
iprintf (filePath);
iprintf (" \n");

TWLDsNDSHeader NDSHeader;

if (logEnabled) LogFMA("Flashcard.Main", "Reading .NDS file:", filePath);
// if (logEnabled) LogFMA("Flashcard.Main", "Reading .NDS file:", filePath);
fseek ( ndsFile , 0 , SEEK_SET );
fread(&NDSHeader,1,sizeof(NDSHeader),ndsFile);
if (logEnabled) LogFMA("Flashcard.Main", ".NDS file read:", filePath);
fread(&ndsHeader,1,sizeof(ndsHeader),ndsFile);
// if (logEnabled) LogFMA("Flashcard.Main", ".NDS file read:", filePath);

// Set banner path
free(bannerfilepath);
snprintf(bannerfilepath, sizeof(bannerfilepath), "sd:/_nds/twloader/bnricons/flashcard/%s.bin", filename.c_str());
bannerfilepathfixed = ReplaceAll(bannerfilepath, ".nds", ".ini");

// Set .ini path
free(inifilepath);
snprintf(inifilepath, sizeof(inifilepath), "%s/%s", fcfolder_path, filename.c_str());
inifilepathfixed = ReplaceAll(inifilepath, ".nds", ".ini");

if( access( bannerfilepathfixed.c_str(), F_OK ) == -1 ) {
FILE* filetosave = fopen(bannerfilepathfixed.c_str(), "wb");

TWLDsNDSBannersize1 myBannersize1;
TWLDsNDSBannersize2 myBannersize2;
TWLDsNDSBannersize3 myBannersize3;
TWLDsNDSBannersize4 myBanner;

if (NDSHeader.bannerOffset != 0x00000000) {
fseek ( ndsFile , NDSHeader.bannerOffset , SEEK_SET );

fread(&myBanner,1,sizeof(myBanner),ndsFile);


if (ndsHeader.bannerOffset != 0x00000000) {
int ret;

ret = fseek (ndsFile, ndsHeader.bannerOffset, SEEK_SET);
if (ret == 0)
ret = fread(&ndsBanner, 1, sizeof(ndsBanner), ndsFile); // read if seek succeed
else
ret = 0; // if seek fails set to !=1

if (ret != 1)
{
// try again, but using regular banner size
ret = fseek (ndsFile, ndsHeader.bannerOffset, SEEK_SET);
if (ret == 0)
ret = fread(&ndsBanner, NDS_BANNER_SIZE_ORIGINAL, 1, ndsFile); // read if seek succeed
else
ret = 0; // if seek fails set to !=1
}

iprintf ("Now caching banner data.\n");
if (logEnabled) LogFMA("Flashcard.Main", "Caching banner data:", NDSHeader.gameCode);
if (myBanner.version == 0x0103) {
fwrite(&myBanner,1,sizeof(myBanner),filetosave);
} else if (myBanner.version == 0x0003) {
fwrite(&myBanner,1,sizeof(myBannersize3),filetosave);
} else if (myBanner.version == 0x0002) {
fwrite(&myBanner,1,sizeof(myBannersize2),filetosave);
// if (logEnabled) LogFMA("Flashcard.Main", "Caching banner data:", NDSHeader.gameCode);
if (ndsBanner.version == 0x0103) {
fwrite(&ndsBanner,1,NDS_BANNER_SIZE_DSi,filetosave);
} else if (ndsBanner.version == 0x0003) {
fwrite(&ndsBanner,1,NDS_BANNER_SIZE_ZH_KO,filetosave);
} else if (ndsBanner.version == 0x0002) {
fwrite(&ndsBanner,1,NDS_BANNER_SIZE_ZH,filetosave);
} else {
fwrite(&myBanner,1,sizeof(myBannersize1),filetosave);
fwrite(&ndsBanner,1,NDS_BANNER_SIZE_ORIGINAL,filetosave);
}

if (logEnabled) iprintf ("Banner data cached.\n");
if (logEnabled) LogFMA("Flashcard.Main", "Banner data cached:", NDSHeader.gameCode);
// if (logEnabled) iprintf ("Banner data cached.\n");
// if (logEnabled) LogFMA("Flashcard.Main", "Banner data cached:", NDSHeader.gameCode);
for (int i = 0; i < 60; i++) { swiWaitForVBlank(); }
} else {
iprintf ("Banner data doesn't exist.\n");
Expand All @@ -275,9 +282,9 @@ int main(int argc, char **argv) {
if( access( inifilepathfixed.c_str(), F_OK ) == -1 ) {
CIniFile rominini( inifilepathfixed );
rominini.SetString("FLASHCARD-ROM", "NDS_PATH", filePath+5);
rominini.SetString("FLASHCARD-ROM", "TID", NDSHeader.gameCode);
rominini.SetString("FLASHCARD-ROM", "TID", ndsHeader.gameCode);
iprintf (".ini file created.\n");
if (logEnabled) LogFMA("Flashcard.Main", ".ini file created:", NDSHeader.gameCode);
// if (logEnabled) LogFMA("Flashcard.Main", ".ini file created:", NDSHeader.gameCode);
rominini.SaveIniFile( inifilepathfixed );
for (int i = 0; i < 60; i++) { swiWaitForVBlank(); }
} else {
Expand Down
129 changes: 93 additions & 36 deletions flashcard-side/arm9/source/ndsheaderbanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,46 @@
#ifndef NDS_HEADER2
#define NDS_HEADER2

#include <nds.h>

typedef struct
{
u32 auto_load_list_offset;
u32 auto_load_list_end;
u32 auto_load_start;
u32 static_bss_start;
u32 static_bss_end;
u32 compressed_static_end;
u32 sdk_version;
u32 nitro_code_be;
u32 nitro_code_le;
} module_params_t;

/*!
\brief the NDS banner format.
See gbatek for more information.
\brief the GBA file header format.
See gbatek for more info.
*/
typedef struct {
u32 entryPoint; //!< 32 bits arm opcode to jump to executable code.
u8 logo[156]; //!< nintendo logo needed for booting the game.
char title[0xC]; //!< 12 characters for the game title.
char gamecode[0x4]; //!< 4 characters for the game code. first character is usally A or B, next 2 characters is a short title
//!< and last character is for destination/language.
u16 makercode; //!< identifies the (commercial) developer.
u8 is96h; //!< fixed value that is always 96h.
u8 unitcode; //!< identifies the required hardware.
u8 devicecode; //!< used by nintedo's hardware debuggers. normally 0.
u8 unused[7];
u8 version; //!< the version of the game.
u8 complement; //!< complement checksum of the gba header.
u16 checksum; //!< a 16 bit checksum? (gbatek says its unused/reserved).
} sGBAHeader2;

//#define GBA_HEADER (*(tGBAHeader *)0x08000000)

/*!
\brief the NDS file header format
See gbatek for more info.
*/
typedef struct {
char gameTitle[12]; //!< 12 characters for the game title.
Expand Down Expand Up @@ -94,43 +131,25 @@ typedef struct {
u32 offset_0x16C; //reserved?

u8 zero[0x90];
} TWLDsNDSHeader;

typedef struct {
u16 version; //!< version of the banner.
u16 crc[4]; //!< CRC-16s of the banner.
u8 reserved[22];
u8 icon[512]; //!< 32*32 icon of the game with 4 bit per pixel.
u16 palette[16]; //!< the pallete of the icon.
u16 titles[8][128]; //!< title of the game in 8 different languages.
} TWLDsNDSBanner;
// 0x200
// TODO: More DSi-specific fields.
u8 dsi1[0x30];
u32 dsi_tid;
u8 dsi2[0x180];
} sNDSHeaderExt;

typedef struct {
u16 version; //!< version of the banner.
u16 crc[4]; //!< CRC-16s of the banner.
u8 reserved[22];
u8 icon[512]; //!< 32*32 icon of the game with 4 bit per pixel.
u16 palette[16]; //!< the palette of the icon.
u16 titles[6][128]; //!< title of the game in 6 different languages.
} TWLDsNDSBannersize1;
char gameTitle[12]; //!< 12 characters for the game title.
char gameCode[4]; //!< 4 characters for the game code.
} sNDSHeadertitlecodeonly;

typedef struct {
u16 version; //!< version of the banner.
u16 crc[4]; //!< CRC-16s of the banner.
u8 reserved[22];
u8 icon[512]; //!< 32*32 icon of the game with 4 bit per pixel.
u16 palette[16]; //!< the palette of the icon.
u16 titles[7][128]; //!< title of the game in 7 different languages.
} TWLDsNDSBannersize2;

typedef struct {
u16 version; //!< version of the banner.
u16 crc[4]; //!< CRC-16s of the banner.
u8 reserved[22];
u8 icon[512]; //!< 32*32 icon of the game with 4 bit per pixel.
u16 palette[16]; //!< the palette of the icon.
u16 titles[8][128]; //!< title of the game in 8 different languages.
} TWLDsNDSBannersize3;
//#define __NDSHeader ((tNDSHeader *)0x02FFFE00)

// Make sure the banner size is correct.
static_assert(sizeof(sNDSHeaderExt) == 0x3B4, "sizeof(sNDSHeaderExt) is not 0x3B4 bytes");


/*!
\brief the NDS banner format.
Expand All @@ -151,7 +170,45 @@ typedef struct {
u8 dsi_icon[8][512]; //!< DSi animated icon frame data.
u16 dsi_palette[8][16]; //!< Palette for each DSi icon frame.
u16 dsi_seq[64]; //!< DSi animated icon sequence.
} TWLDsNDSBannersize4;

} sNDSBannerExt;

// sNDSBanner version.
typedef enum {
NDS_BANNER_VER_ORIGINAL = 0x0001,
NDS_BANNER_VER_ZH = 0x0002,
NDS_BANNER_VER_ZH_KO = 0x0003,
NDS_BANNER_VER_DSi = 0x0103,
} sNDSBannerVersion;

// sNDSBanner sizes.
typedef enum {
NDS_BANNER_SIZE_ORIGINAL = 0x0840,
NDS_BANNER_SIZE_ZH = 0x0940,
NDS_BANNER_SIZE_ZH_KO = 0x0A40,
NDS_BANNER_SIZE_DSi = 0x23C0,
} sNDSBannerSize;

// Make sure the banner size is correct.
static_assert(sizeof(sNDSBannerExt) == NDS_BANNER_SIZE_DSi, "sizeof(sNDSBannerExt) is not 0x23C0 bytes");

// Language indexes.
typedef enum {
// DS and 3DS
NDS_LANG_JAPANESE = 0,
NDS_LANG_ENGLISH = 1,
NDS_LANG_FRENCH = 2,
NDS_LANG_GERMAN = 3,
NDS_LANG_ITALIAN = 4,
NDS_LANG_SPANISH = 5,
NDS_LANG_CHINESE = 6,
NDS_LANG_KOREAN = 7,

// 3DS only
N3DS_LANG_CHINESE_SIMPLIFIED = 6,
N3DS_LANG_DUTCH = 8,
N3DS_LANG_PORTUGUESE = 9,
N3DS_LANG_RUSSIAN = 10,
N3DS_LANG_CHINESE_TRADITIONAL = 11,
} sNDSLanguage;

#endif // NDS_HEADER2
6 changes: 6 additions & 0 deletions flashcard-side/clean and compile.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@echo off
make clean
make
copy "flashcard-side.nds" "../7zfile/flashcardsdroot (all)/_nds/twloader.nds"
copy "flashcard-side.nds" "../TWLoader-update/twloader.nds"
pause
Loading

0 comments on commit 518ba74

Please sign in to comment.