Skip to content

Commit

Permalink
Optimize touch screen mode switch code (outside of NO$GBA)
Browse files Browse the repository at this point in the history
ALSO: Fix sound/mic frequency writing
  • Loading branch information
RocketRobz committed Aug 29, 2024
1 parent 0634d23 commit 9d4c7b0
Show file tree
Hide file tree
Showing 6 changed files with 567 additions and 531 deletions.
3 changes: 2 additions & 1 deletion booter/arm7/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ int main() {
}
}

*(vu16*)(0x04004700) |= BIT(13); // Set 48khz sound/mic frequency
*(vu16*)0x04004700 &= ~BIT(15); // Runs before sound frequency change
*(vu16*)0x04004700 |= BIT(13); // Set 48khz sound/mic frequency

// clear sound registers
dmaFillWords(0, (void*)0x04000400, 0x100);
Expand Down
3 changes: 2 additions & 1 deletion booter_fc/arm7/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ int main() {
*(vu16*)(0x04004014) = 0x264C;
*(vu16*)(0x04004C02) = 0x4000; // enable powerbutton irq (Fix for Unlaunch 1.3)

*(vu16*)(0x04004700) |= BIT(13); // Set 48khz sound/mic frequency (DSi/3DS only)
*(vu16*)0x04004700 &= ~BIT(15); // Runs before sound frequency change
*(vu16*)0x04004700 |= BIT(13); // Set 48khz sound/mic frequency (DSi/3DS only)

// clear sound registers
dmaFillWords(0, (void*)0x04000400, 0x100);
Expand Down
278 changes: 145 additions & 133 deletions slot1launch/bootloader/source/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,146 +499,151 @@ static void NDSTouchscreenMode(void) {
volLevel = 0xA7;
}

const bool noSgba = (strncmp((const char*)0x04FFFA00, "no$gba", 6) == 0);

// Touchscreen
cdcReadReg (0x63, 0x00);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x00);
cdcReadReg (CDC_CONTROL, 0x51);
cdcReadReg (CDC_TOUCHCNT, 0x02);
cdcReadReg (CDC_CONTROL, 0x3F);
cdcReadReg (CDC_SOUND, 0x28);
cdcReadReg (CDC_SOUND, 0x2A);
cdcReadReg (CDC_SOUND, 0x2E);
cdcWriteReg(CDC_CONTROL, 0x52, 0x80);
cdcWriteReg(CDC_CONTROL, 0x40, 0x0C);
cdcWriteReg(CDC_SOUND, 0x24, 0xFF);
cdcWriteReg(CDC_SOUND, 0x25, 0xFF);
cdcWriteReg(CDC_SOUND, 0x26, 0x7F);
cdcWriteReg(CDC_SOUND, 0x27, 0x7F);
cdcWriteReg(CDC_SOUND, 0x28, 0x4A);
cdcWriteReg(CDC_SOUND, 0x29, 0x4A);
cdcWriteReg(CDC_SOUND, 0x2A, 0x10);
cdcWriteReg(CDC_SOUND, 0x2B, 0x10);
cdcWriteReg(CDC_CONTROL, 0x51, 0x00);
cdcReadReg (CDC_TOUCHCNT, 0x02);
cdcWriteReg(CDC_TOUCHCNT, 0x02, 0x98);
cdcWriteReg(CDC_SOUND, 0x23, 0x00);
cdcWriteReg(CDC_SOUND, 0x1F, 0x14);
cdcWriteReg(CDC_SOUND, 0x20, 0x14);
cdcWriteReg(CDC_CONTROL, 0x3F, 0x00);
cdcReadReg (CDC_CONTROL, 0x0B);
cdcWriteReg(CDC_CONTROL, 0x05, 0x00);
cdcWriteReg(CDC_CONTROL, 0x0B, 0x01);
cdcWriteReg(CDC_CONTROL, 0x0C, 0x02);
cdcWriteReg(CDC_CONTROL, 0x12, 0x01);
cdcWriteReg(CDC_CONTROL, 0x13, 0x02);
cdcWriteReg(CDC_SOUND, 0x2E, 0x00);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x60);
cdcWriteReg(CDC_CONTROL, 0x01, 0x01);
cdcWriteReg(CDC_CONTROL, 0x39, 0x66);
cdcReadReg (CDC_SOUND, 0x20);
cdcWriteReg(CDC_SOUND, 0x20, 0x10);
cdcWriteReg(CDC_CONTROL, 0x04, 0x00);
cdcWriteReg(CDC_CONTROL, 0x12, 0x81);
cdcWriteReg(CDC_CONTROL, 0x13, 0x82);
cdcWriteReg(CDC_CONTROL, 0x51, 0x82);
cdcWriteReg(CDC_CONTROL, 0x51, 0x00);
cdcWriteReg(CDC_CONTROL, 0x04, 0x03);
cdcWriteReg(CDC_CONTROL, 0x05, 0xA1);
cdcWriteReg(CDC_CONTROL, 0x06, 0x15);
cdcWriteReg(CDC_CONTROL, 0x0B, 0x87);
cdcWriteReg(CDC_CONTROL, 0x0C, 0x83);
cdcWriteReg(CDC_CONTROL, 0x12, 0x87);
cdcWriteReg(CDC_CONTROL, 0x13, 0x83);
cdcReadReg (CDC_TOUCHCNT, 0x10);
cdcWriteReg(CDC_TOUCHCNT, 0x10, 0x08);
cdcWriteReg(0x04, 0x08, 0x7F);
cdcWriteReg(0x04, 0x09, 0xE1);
cdcWriteReg(0x04, 0x0A, 0x80);
cdcWriteReg(0x04, 0x0B, 0x1F);
cdcWriteReg(0x04, 0x0C, 0x7F);
cdcWriteReg(0x04, 0x0D, 0xC1);
cdcWriteReg(CDC_CONTROL, 0x41, 0x08);
cdcWriteReg(CDC_CONTROL, 0x42, 0x08);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x00);
cdcWriteReg(0x04, 0x08, 0x7F);
cdcWriteReg(0x04, 0x09, 0xE1);
cdcWriteReg(0x04, 0x0A, 0x80);
cdcWriteReg(0x04, 0x0B, 0x1F);
cdcWriteReg(0x04, 0x0C, 0x7F);
cdcWriteReg(0x04, 0x0D, 0xC1);
cdcWriteReg(CDC_SOUND, 0x2F, 0x2B);
cdcWriteReg(CDC_SOUND, 0x30, 0x40);
cdcWriteReg(CDC_SOUND, 0x31, 0x40);
cdcWriteReg(CDC_SOUND, 0x32, 0x60);
cdcReadReg (CDC_CONTROL, 0x74);
cdcWriteReg(CDC_CONTROL, 0x74, 0x02);
cdcReadReg (CDC_CONTROL, 0x74);
cdcWriteReg(CDC_CONTROL, 0x74, 0x10);
cdcReadReg (CDC_CONTROL, 0x74);
cdcWriteReg(CDC_CONTROL, 0x74, 0x40);
cdcWriteReg(CDC_SOUND, 0x21, 0x20);
cdcWriteReg(CDC_SOUND, 0x22, 0xF0);
cdcReadReg (CDC_CONTROL, 0x51);
cdcReadReg (CDC_CONTROL, 0x3F);
cdcWriteReg(CDC_CONTROL, 0x3F, 0xD4);
cdcWriteReg(CDC_SOUND, 0x23, 0x44);
cdcWriteReg(CDC_SOUND, 0x1F, 0xD4);
cdcWriteReg(CDC_SOUND, 0x28, 0x4E);
cdcWriteReg(CDC_SOUND, 0x29, 0x4E);
cdcWriteReg(CDC_SOUND, 0x24, 0x9E);
cdcWriteReg(CDC_SOUND, 0x25, 0x9E);
cdcWriteReg(CDC_SOUND, 0x20, 0xD4);
cdcWriteReg(CDC_SOUND, 0x2A, 0x14);
cdcWriteReg(CDC_SOUND, 0x2B, 0x14);
cdcWriteReg(CDC_SOUND, 0x26, 0xA7);
cdcWriteReg(CDC_SOUND, 0x27, 0xA7);
cdcWriteReg(CDC_CONTROL, 0x40, 0x00);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x60);
if (noSgba) {
cdcReadReg (0x63, 0x00);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x00);
cdcReadReg (CDC_CONTROL, 0x51);
cdcReadReg (CDC_TOUCHCNT, 0x02);
cdcReadReg (CDC_CONTROL, 0x3F);
cdcReadReg (CDC_SOUND, 0x28);
cdcReadReg (CDC_SOUND, 0x2A);
cdcReadReg (CDC_SOUND, 0x2E);
cdcWriteReg(CDC_CONTROL, 0x52, 0x80);
cdcWriteReg(CDC_CONTROL, 0x40, 0x0C);
cdcWriteReg(CDC_SOUND, 0x24, 0xFF);
cdcWriteReg(CDC_SOUND, 0x25, 0xFF);
cdcWriteReg(CDC_SOUND, 0x26, 0x7F);
cdcWriteReg(CDC_SOUND, 0x27, 0x7F);
cdcWriteReg(CDC_SOUND, 0x28, 0x4A);
cdcWriteReg(CDC_SOUND, 0x29, 0x4A);
cdcWriteReg(CDC_SOUND, 0x2A, 0x10);
cdcWriteReg(CDC_SOUND, 0x2B, 0x10);
cdcWriteReg(CDC_CONTROL, 0x51, 0x00);
cdcReadReg (CDC_TOUCHCNT, 0x02);
cdcWriteReg(CDC_TOUCHCNT, 0x02, 0x98);
cdcWriteReg(CDC_SOUND, 0x23, 0x00);
cdcWriteReg(CDC_SOUND, 0x1F, 0x14);
cdcWriteReg(CDC_SOUND, 0x20, 0x14);
cdcWriteReg(CDC_CONTROL, 0x3F, 0x00);
cdcReadReg (CDC_CONTROL, 0x0B);
cdcWriteReg(CDC_CONTROL, 0x05, 0x00);
cdcWriteReg(CDC_CONTROL, 0x0B, 0x01);
cdcWriteReg(CDC_CONTROL, 0x0C, 0x02);
cdcWriteReg(CDC_CONTROL, 0x12, 0x01);
cdcWriteReg(CDC_CONTROL, 0x13, 0x02);
cdcWriteReg(CDC_SOUND, 0x2E, 0x00);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x60);
cdcWriteReg(CDC_CONTROL, 0x01, 0x01);
cdcWriteReg(CDC_CONTROL, 0x39, 0x66);
cdcReadReg (CDC_SOUND, 0x20);
cdcWriteReg(CDC_SOUND, 0x20, 0x10);
cdcWriteReg(CDC_CONTROL, 0x04, 0x00);
cdcWriteReg(CDC_CONTROL, 0x12, 0x81);
cdcWriteReg(CDC_CONTROL, 0x13, 0x82);
cdcWriteReg(CDC_CONTROL, 0x51, 0x82);
cdcWriteReg(CDC_CONTROL, 0x51, 0x00);
cdcWriteReg(CDC_CONTROL, 0x04, 0x03);
cdcWriteReg(CDC_CONTROL, 0x05, 0xA1);
cdcWriteReg(CDC_CONTROL, 0x06, 0x15);
cdcWriteReg(CDC_CONTROL, 0x0B, 0x87);
cdcWriteReg(CDC_CONTROL, 0x0C, 0x83);
cdcWriteReg(CDC_CONTROL, 0x12, 0x87);
cdcWriteReg(CDC_CONTROL, 0x13, 0x83);
cdcReadReg (CDC_TOUCHCNT, 0x10);
cdcWriteReg(CDC_TOUCHCNT, 0x10, 0x08);
cdcWriteReg(0x04, 0x08, 0x7F);
cdcWriteReg(0x04, 0x09, 0xE1);
cdcWriteReg(0x04, 0x0A, 0x80);
cdcWriteReg(0x04, 0x0B, 0x1F);
cdcWriteReg(0x04, 0x0C, 0x7F);
cdcWriteReg(0x04, 0x0D, 0xC1);
cdcWriteReg(CDC_CONTROL, 0x41, 0x08);
cdcWriteReg(CDC_CONTROL, 0x42, 0x08);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x00);
cdcWriteReg(0x04, 0x08, 0x7F);
cdcWriteReg(0x04, 0x09, 0xE1);
cdcWriteReg(0x04, 0x0A, 0x80);
cdcWriteReg(0x04, 0x0B, 0x1F);
cdcWriteReg(0x04, 0x0C, 0x7F);
cdcWriteReg(0x04, 0x0D, 0xC1);
cdcWriteReg(CDC_SOUND, 0x2F, 0x2B);
cdcWriteReg(CDC_SOUND, 0x30, 0x40);
cdcWriteReg(CDC_SOUND, 0x31, 0x40);
cdcWriteReg(CDC_SOUND, 0x32, 0x60);
cdcReadReg (CDC_CONTROL, 0x74);
cdcWriteReg(CDC_CONTROL, 0x74, 0x02);
cdcReadReg (CDC_CONTROL, 0x74);
cdcWriteReg(CDC_CONTROL, 0x74, 0x10);
cdcReadReg (CDC_CONTROL, 0x74);
cdcWriteReg(CDC_CONTROL, 0x74, 0x40);
cdcWriteReg(CDC_SOUND, 0x21, 0x20);
cdcWriteReg(CDC_SOUND, 0x22, 0xF0);
cdcReadReg (CDC_CONTROL, 0x51);
cdcReadReg (CDC_CONTROL, 0x3F);
cdcWriteReg(CDC_CONTROL, 0x3F, 0xD4);
cdcWriteReg(CDC_SOUND, 0x23, 0x44);
cdcWriteReg(CDC_SOUND, 0x1F, 0xD4);
cdcWriteReg(CDC_SOUND, 0x28, 0x4E);
cdcWriteReg(CDC_SOUND, 0x29, 0x4E);
cdcWriteReg(CDC_SOUND, 0x24, 0x9E);
cdcWriteReg(CDC_SOUND, 0x25, 0x9E);
cdcWriteReg(CDC_SOUND, 0x20, 0xD4);
cdcWriteReg(CDC_SOUND, 0x2A, 0x14);
cdcWriteReg(CDC_SOUND, 0x2B, 0x14);
cdcWriteReg(CDC_SOUND, 0x26, 0xA7);
cdcWriteReg(CDC_SOUND, 0x27, 0xA7);
cdcWriteReg(CDC_CONTROL, 0x40, 0x00);
cdcWriteReg(CDC_CONTROL, 0x3A, 0x60);
}
cdcWriteReg(CDC_SOUND, 0x26, volLevel);
cdcWriteReg(CDC_SOUND, 0x27, volLevel);
cdcWriteReg(CDC_SOUND, 0x2E, 0x03);
cdcWriteReg(CDC_TOUCHCNT, 0x03, 0x00);
cdcWriteReg(CDC_SOUND, 0x21, 0x20);
cdcWriteReg(CDC_SOUND, 0x22, 0xF0);
cdcReadReg (CDC_SOUND, 0x22);
cdcWriteReg(CDC_SOUND, 0x22, 0x00);
cdcWriteReg(CDC_SOUND, 0x22, 0x70);
cdcWriteReg(CDC_CONTROL, 0x52, 0x80);
cdcWriteReg(CDC_CONTROL, 0x51, 0x00);

// Set remaining values
cdcWriteReg(CDC_CONTROL, 0x03, 0x44);
cdcWriteReg(CDC_CONTROL, 0x0D, 0x00);
cdcWriteReg(CDC_CONTROL, 0x0E, 0x80);
cdcWriteReg(CDC_CONTROL, 0x0F, 0x80);
cdcWriteReg(CDC_CONTROL, 0x10, 0x08);
cdcWriteReg(CDC_CONTROL, 0x14, 0x80);
cdcWriteReg(CDC_CONTROL, 0x15, 0x80);
cdcWriteReg(CDC_CONTROL, 0x16, 0x04);
cdcWriteReg(CDC_CONTROL, 0x1A, 0x01);
cdcWriteReg(CDC_CONTROL, 0x1E, 0x01);
cdcWriteReg(CDC_CONTROL, 0x24, 0x80);
cdcWriteReg(CDC_CONTROL, 0x33, 0x34);
cdcWriteReg(CDC_CONTROL, 0x34, 0x32);
cdcWriteReg(CDC_CONTROL, 0x35, 0x12);
cdcWriteReg(CDC_CONTROL, 0x36, 0x03);
cdcWriteReg(CDC_CONTROL, 0x37, 0x02);
cdcWriteReg(CDC_CONTROL, 0x38, 0x03);
cdcWriteReg(CDC_CONTROL, 0x3C, 0x19);
cdcWriteReg(CDC_CONTROL, 0x3D, 0x05);
cdcWriteReg(CDC_CONTROL, 0x44, 0x0F);
cdcWriteReg(CDC_CONTROL, 0x45, 0x38);
cdcWriteReg(CDC_CONTROL, 0x49, 0x00);
cdcWriteReg(CDC_CONTROL, 0x4A, 0x00);
cdcWriteReg(CDC_CONTROL, 0x4B, 0xEE);
cdcWriteReg(CDC_CONTROL, 0x4C, 0x10);
cdcWriteReg(CDC_CONTROL, 0x4D, 0xD8);
cdcWriteReg(CDC_CONTROL, 0x4E, 0x7E);
cdcWriteReg(CDC_CONTROL, 0x4F, 0xE3);
cdcWriteReg(CDC_CONTROL, 0x58, 0x7F);
cdcWriteReg(CDC_CONTROL, 0x74, 0xD2);
cdcWriteReg(CDC_CONTROL, 0x75, 0x2C);
cdcWriteReg(CDC_SOUND, 0x22, 0x70);
cdcWriteReg(CDC_SOUND, 0x2C, 0x20);

if (noSgba) {
// Set remaining values
cdcWriteReg(CDC_CONTROL, 0x03, 0x44);
cdcWriteReg(CDC_CONTROL, 0x0D, 0x00);
cdcWriteReg(CDC_CONTROL, 0x0E, 0x80);
cdcWriteReg(CDC_CONTROL, 0x0F, 0x80);
cdcWriteReg(CDC_CONTROL, 0x10, 0x08);
cdcWriteReg(CDC_CONTROL, 0x14, 0x80);
cdcWriteReg(CDC_CONTROL, 0x15, 0x80);
cdcWriteReg(CDC_CONTROL, 0x16, 0x04);
cdcWriteReg(CDC_CONTROL, 0x1A, 0x01);
cdcWriteReg(CDC_CONTROL, 0x1E, 0x01);
cdcWriteReg(CDC_CONTROL, 0x24, 0x80);
cdcWriteReg(CDC_CONTROL, 0x33, 0x34);
cdcWriteReg(CDC_CONTROL, 0x34, 0x32);
cdcWriteReg(CDC_CONTROL, 0x35, 0x12);
cdcWriteReg(CDC_CONTROL, 0x36, 0x03);
cdcWriteReg(CDC_CONTROL, 0x37, 0x02);
cdcWriteReg(CDC_CONTROL, 0x38, 0x03);
cdcWriteReg(CDC_CONTROL, 0x3C, 0x19);
cdcWriteReg(CDC_CONTROL, 0x3D, 0x05);
cdcWriteReg(CDC_CONTROL, 0x44, 0x0F);
cdcWriteReg(CDC_CONTROL, 0x45, 0x38);
cdcWriteReg(CDC_CONTROL, 0x49, 0x00);
cdcWriteReg(CDC_CONTROL, 0x4A, 0x00);
cdcWriteReg(CDC_CONTROL, 0x4B, 0xEE);
cdcWriteReg(CDC_CONTROL, 0x4C, 0x10);
cdcWriteReg(CDC_CONTROL, 0x4D, 0xD8);
cdcWriteReg(CDC_CONTROL, 0x4E, 0x7E);
cdcWriteReg(CDC_CONTROL, 0x4F, 0xE3);
cdcWriteReg(CDC_CONTROL, 0x58, 0x7F);
cdcWriteReg(CDC_CONTROL, 0x74, 0xD2);
cdcWriteReg(CDC_CONTROL, 0x75, 0x2C);
cdcWriteReg(CDC_SOUND, 0x22, 0x70);
cdcWriteReg(CDC_SOUND, 0x2C, 0x20);
}

// Finish up!
cdcReadReg (CDC_TOUCHCNT, 0x02);
Expand All @@ -651,6 +656,10 @@ static void NDSTouchscreenMode(void) {
}

static void DSiTouchscreenMode(void) {
if (strncmp((const char*)0x04FFFA00, "no$gba", 6) != 0) {
return;
}

// Touchscreen
cdcWriteReg(0, 0x01, 0x01);
cdcWriteReg(0, 0x39, 0x66);
Expand Down Expand Up @@ -1007,18 +1016,21 @@ void arm7_main (void) {
my_readUserSettings(ndsHeader); // Header has to be loaded first

if (my_isDSiMode()) {
*(vu16*)0x4004700 &= ~BIT(15); // Runs before sound frequency change
*(vu16*)0x4004700 = (soundFreq ? 0xC00F : 0x800F);

if (dsiModeConfirmed) {
*(u32*)0x3FFFFC8 = 0x7884; // Fix sound pitch table for DSi mode (works with SDK5 binaries)

if (ndsHeader->unitCode == 0 || (ndsHeader->unitCode > 0 && !(*(u8*)0x02FFE1BF & BIT(0)))) {
twlTouch ? DSiTouchscreenMode() : NDSTouchscreenMode();
*(u16*)0x4000500 = 0x807F;
*(vu16*)0x4000500 = 0x807F;
}
} else {
REG_SCFG_ROM = 0x703;

twlTouch ? DSiTouchscreenMode() : NDSTouchscreenMode();
*(u16*)0x4000500 = 0x807F;
*(vu16*)0x4000500 = 0x807F;

REG_GPIO_WIFI |= BIT(8); // Old NDS-Wifi mode

Expand Down
Loading

0 comments on commit 9d4c7b0

Please sign in to comment.