diff --git a/arm9/source/common/touchcal.c b/arm9/source/common/touchcal.c new file mode 100644 index 0000000..9bd5067 --- /dev/null +++ b/arm9/source/common/touchcal.c @@ -0,0 +1,78 @@ +#include "touchcal.h" +#include "ui.h" +#include "hid.h" + + +bool ShowCalibrationDialog(void) +{ + static const u32 dot_positions[][2] = { + {16, 16}, + {320 - 16, 240 - 16}, + {16, 240 - 16}, + {320 - 16, 16}, + }; + + HID_CalibrationData calibrations[countof(dot_positions)]; + for (u32 i = 0; i < countof(dot_positions); i++) { + calibrations[i].screen_x = dot_positions[i][0]; + calibrations[i].screen_y = dot_positions[i][1]; + } + + // clear screen, draw instructions + ClearScreen(BOT_SCREEN, COLOR_STD_BG); + DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, + "Touch the red crosshairs to\ncalibrate your touchscreen.\n \nUse the stylus for best\nresults!"); + + // actual calibration + for (u32 current_dot = 0; current_dot < countof(dot_positions); current_dot++) { + // draw four crosshairs + for (u32 i = 0; i < countof(dot_positions); i++) { + int color_cross = (i < current_dot) ? COLOR_BRIGHTGREEN : (i == current_dot) ? COLOR_RED : COLOR_STD_FONT; + for (u32 r = 2; r < 8; r++) { + DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] + r, color_cross); + DrawPixel(BOT_SCREEN, dot_positions[i][0] + r, dot_positions[i][1] + 0, color_cross); + DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] - r, color_cross); + DrawPixel(BOT_SCREEN, dot_positions[i][0] - r, dot_positions[i][1] + 0, color_cross); + } + } + + // wait until touchscreen released + while (HID_ReadState() & (BUTTON_B | BUTTON_TOUCH)); + + // wait for input, store calibration data + while (1) { + u32 pressed = HID_ReadState(); + if (pressed & BUTTON_B) { + return false; + } else if (pressed & BUTTON_TOUCH) { + calibrations[current_dot].ts_raw = HID_ReadRawTouchState(); + break; + } + } + } + + return HID_SetCalibrationData(calibrations, countof(dot_positions), 320, 240); +} + +void ShowTouchPlayground(void) +{ + ClearScreen(BOT_SCREEN, COLOR_STD_BG); + + while(1) { + u16 tx, ty; + u32 pressed = HID_ReadState(); + + if (pressed & BUTTON_TOUCH) { + HID_ReadTouchState(&tx, &ty); + if (tx < 320 && ty < 240) + DrawPixel(BOT_SCREEN, tx, ty, COLOR_BRIGHTYELLOW); + } else { + tx = ty = 0; + } + + DrawStringF(BOT_SCREEN, 16, 16, COLOR_STD_FONT, COLOR_STD_BG, "Current touchscreen coordinates: %3.3d, %3.3d", tx, ty); + if (pressed & BUTTON_B) + return; + } + +} diff --git a/arm9/source/common/touchcal.h b/arm9/source/common/touchcal.h new file mode 100644 index 0000000..0bf6485 --- /dev/null +++ b/arm9/source/common/touchcal.h @@ -0,0 +1,6 @@ +#pragma once + +#include "common.h" + +bool ShowCalibrationDialog(void); +void ShowTouchPlayground(void); diff --git a/arm9/source/common/ui.c b/arm9/source/common/ui.c index e064222..fd6788c 100644 --- a/arm9/source/common/ui.c +++ b/arm9/source/common/ui.c @@ -1072,77 +1072,3 @@ bool ShowProgress(u64 current, u64 total, const char* opstr) return !CheckButton(BUTTON_B); } - -bool ShowCalibrationDialog(void) -{ - static const u32 dot_positions[][2] = { - {16, 16}, - {320 - 16, 240 - 16}, - {16, 240 - 16}, - {320 - 16, 16}, - }; - - HID_CalibrationData calibrations[countof(dot_positions)]; - for (u32 i = 0; i < countof(dot_positions); i++) { - calibrations[i].screen_x = dot_positions[i][0]; - calibrations[i].screen_y = dot_positions[i][1]; - } - - // clear screen, draw instructions - ClearScreen(BOT_SCREEN, COLOR_STD_BG); - DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, - "Touch the red crosshairs to\ncalibrate your touchscreen.\n \nUse the stylus for best\nresults!"); - - // actual calibration - for (u32 current_dot = 0; current_dot < countof(dot_positions); current_dot++) { - // draw four crosshairs - for (u32 i = 0; i < countof(dot_positions); i++) { - int color_cross = (i < current_dot) ? COLOR_BRIGHTGREEN : (i == current_dot) ? COLOR_RED : COLOR_STD_FONT; - for (u32 r = 2; r < 8; r++) { - DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] + r, color_cross); - DrawPixel(BOT_SCREEN, dot_positions[i][0] + r, dot_positions[i][1] + 0, color_cross); - DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] - r, color_cross); - DrawPixel(BOT_SCREEN, dot_positions[i][0] - r, dot_positions[i][1] + 0, color_cross); - } - } - - // wait until touchscreen released - while (HID_ReadState() & (BUTTON_B | BUTTON_TOUCH)); - - // wait for input, store calibration data - while (1) { - u32 pressed = HID_ReadState(); - if (pressed & BUTTON_B) { - return false; - } else if (pressed & BUTTON_TOUCH) { - calibrations[current_dot].ts_raw = HID_ReadRawTouchState(); - break; - } - } - } - - return HID_SetCalibrationData(calibrations, countof(dot_positions), 320, 240); -} - -void ShowTouchPlayground(void) -{ - ClearScreen(BOT_SCREEN, COLOR_STD_BG); - - while(1) { - u16 tx, ty; - u32 pressed = HID_ReadState(); - - if (pressed & BUTTON_TOUCH) { - HID_ReadTouchState(&tx, &ty); - if (tx < 320 && ty < 240) - DrawPixel(BOT_SCREEN, tx, ty, COLOR_BRIGHTYELLOW); - } else { - tx = ty = 0; - } - - DrawStringF(BOT_SCREEN, 16, 16, COLOR_STD_FONT, COLOR_STD_BG, "Current touchscreen coordinates: %3.3d, %3.3d", tx, ty); - if (pressed & BUTTON_B) - return; - } - -} diff --git a/arm9/source/common/ui.h b/arm9/source/common/ui.h index 5ca8bc6..a12f4f1 100644 --- a/arm9/source/common/ui.h +++ b/arm9/source/common/ui.h @@ -50,6 +50,7 @@ bool SetFontFromPbm(const void* pbm, const u32 pbm_size); void ClearScreen(unsigned char *screen, int color); void ClearScreenF(bool clear_main, bool clear_alt, int color); +void DrawPixel(u8* screen, int x, int y, int color); void DrawRectangle(u8* screen, int x, int y, int width, int height, int color); void DrawBitmap(u8* screen, int x, int y, int w, int h, u8* bitmap); void DrawQrCode(u8* screen, u8* qrcode); @@ -82,6 +83,3 @@ u64 ShowNumberPrompt(u64 start_val, const char *format, ...); bool ShowDataPrompt(u8* data, u32* size, const char *format, ...); bool ShowRtcSetterPrompt(void* time, const char *format, ...); bool ShowProgress(u64 current, u64 total, const char* opstr); - -bool ShowCalibrationDialog(void); -void ShowTouchPlayground(void); diff --git a/arm9/source/crypto/crc16.c b/arm9/source/crypto/crc16.c new file mode 100644 index 0000000..75e1a5d --- /dev/null +++ b/arm9/source/crypto/crc16.c @@ -0,0 +1,26 @@ +#include "crc16.h" + +#define CRC16_TABVAL \ + 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, \ + 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 + + +// see: https://github.com/TASVideos/desmume/blob/master/desmume/src/bios.cpp#L1070tions +u16 crc16_quick(const void* src, u32 len) { + const u16 tabval[] = { CRC16_TABVAL }; + u16* data = (u16*) src; + u16 crc = 0xFFFF; + + for (len >>= 1; len; len--) { + u16 curr = *(data++); + for (u32 i = 0; i < 4; i++) { + u16 tval = tabval[crc&0xF]; + crc >>= 4; + crc ^= tval; + tval = tabval[(curr >> (4*i))&0xF]; + crc ^= tval; + } + } + + return crc; +} diff --git a/arm9/source/crypto/crc16.h b/arm9/source/crypto/crc16.h new file mode 100644 index 0000000..41e8367 --- /dev/null +++ b/arm9/source/crypto/crc16.h @@ -0,0 +1,5 @@ +#pragma once + +#include "common.h" + +u16 crc16_quick(const void* src, u32 len); diff --git a/arm9/source/system/bps.c b/arm9/source/game/bps.c similarity index 100% rename from arm9/source/system/bps.c rename to arm9/source/game/bps.c diff --git a/arm9/source/system/bps.h b/arm9/source/game/bps.h similarity index 100% rename from arm9/source/system/bps.h rename to arm9/source/game/bps.h diff --git a/arm9/source/system/ips.c b/arm9/source/game/ips.c similarity index 100% rename from arm9/source/system/ips.c rename to arm9/source/game/ips.c diff --git a/arm9/source/system/ips.h b/arm9/source/game/ips.h similarity index 100% rename from arm9/source/system/ips.h rename to arm9/source/game/ips.h diff --git a/arm9/source/game/nds.c b/arm9/source/game/nds.c index 1f5aa3b..ec1d966 100644 --- a/arm9/source/game/nds.c +++ b/arm9/source/game/nds.c @@ -1,9 +1,8 @@ #include "nds.h" #include "vff.h" +#include "crc16.h" #include "utf.h" -#define CRC16_TABVAL 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 - #define FNT_ENTRY_ISDIR(e) ((bool)((*(u8*)(e))&0x80)) #define FNT_ENTRY_FNLEN(e) ((*(u8*)(e))&~0x80) #define FNT_ENTRY_LEN(e) (1 + FNT_ENTRY_FNLEN(e) + (FNT_ENTRY_ISDIR(e)?2:0)) @@ -22,26 +21,6 @@ typedef struct { } __attribute__((packed)) NitroFatEntry; -// see: https://github.com/TASVideos/desmume/blob/master/desmume/src/bios.cpp#L1070tions -u16 crc16_quick(const void* src, u32 len) { - const u16 tabval[] = { CRC16_TABVAL }; - u16* data = (u16*) src; - u16 crc = 0xFFFF; - - for (len >>= 1; len; len--) { - u16 curr = *(data++); - for (u32 i = 0; i < 4; i++) { - u16 tval = tabval[crc&0xF]; - crc >>= 4; - crc ^= tval; - tval = tabval[(curr >> (4*i))&0xF]; - crc ^= tval; - } - } - - return crc; -} - u32 ValidateTwlHeader(TwlHeader* twl) { if (twl->logo_crc != NDS_LOGO_CRC16) return 1; return (crc16_quick(twl->logo, sizeof(twl->logo)) == NDS_LOGO_CRC16) ? 0 : 1; diff --git a/arm9/source/godmode.c b/arm9/source/godmode.c index b8da7c7..7b7c79f 100644 --- a/arm9/source/godmode.c +++ b/arm9/source/godmode.c @@ -3,6 +3,7 @@ #include "support.h" #include "ui.h" #include "hid.h" +#include "touchcal.h" #include "fs.h" #include "utils.h" #include "nand.h"