From 16c3dc834f986357f7c2a32029a04e29c8b5aafe Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Wed, 30 Sep 2026 06:03:54 +0000 Subject: [PATCH] vi3in1 15/18, kbd_lib 12/21, iplMemoryCardManager (worker feba9540) --- README.md | 4 +- libs/RVL_SDK/src/kbd/kbd_lib.c | 947 ++++++++++++++++++ libs/RVL_SDK/src/vi/vi3in1.c | 719 +++++++++++++ src/scene/memoryCard/iplMemoryCardManager.cpp | 457 +++++++++ .../iplMemoryCardManager.attempts.md | 107 ++ tools/decomp-assist/kbd_lib.attempts.md | 66 ++ tools/decomp-assist/vi3in1.attempts.md | 21 + 7 files changed, 2319 insertions(+), 2 deletions(-) create mode 100644 libs/RVL_SDK/src/kbd/kbd_lib.c create mode 100644 libs/RVL_SDK/src/vi/vi3in1.c create mode 100644 src/scene/memoryCard/iplMemoryCardManager.cpp create mode 100644 tools/decomp-assist/iplMemoryCardManager.attempts.md create mode 100644 tools/decomp-assist/kbd_lib.attempts.md create mode 100644 tools/decomp-assist/vi3in1.attempts.md diff --git a/README.md b/README.md index 5daee4e1..585b5e32 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 90.99% | 80.50% | 56.95% | 88.40% | +| 91.56% | 80.70% | 56.95% | 88.70% | -units 758/1027 complete, functions 11561/12563 matched. +units 758/1027 complete, functions 11604/12563 matched. decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data diff --git a/libs/RVL_SDK/src/kbd/kbd_lib.c b/libs/RVL_SDK/src/kbd/kbd_lib.c new file mode 100644 index 00000000..5e43d176 --- /dev/null +++ b/libs/RVL_SDK/src/kbd/kbd_lib.c @@ -0,0 +1,947 @@ +#include +#include +#include +#include +#include + +typedef union { + u32 value; + struct { + u32 locks : 26; + u32 physical : 6; + } bits; +} KBDModifierState; + +typedef struct { + u8 codeCount; + u8 flags; + u16* keyMap; + u16* modifierMap; + u8 lockIndex; + u8 shiftIndex; + u8 altIndex; + u8 maxCode; + u8 keypadMap[8]; + u8 keypadCount; +} KBDKeyMap; + +typedef union { + KBDKeyMap maps[36]; + u8 storage[0x400]; +} KBDKeyMapTable; + +typedef struct { + u8 channel; + u8 code; + u32 state; + u32 modifiers; + u16 translated; +} KBDKeyEventData; + +typedef struct { + u8 channel; + s32 country; + u32 flags; + void* device; + u8 modifiers; + u8 reportReserved; + u8 keys[6]; + KBDKeyEventData events[32]; + u8 writeIndex; + u8 readIndex; + u32 lockProcessing; + u32 modState; + u8 lockCount[6]; + u16 repeatDelay; + u16 repeatInterval; + u8 repeatKey; + OSAlarm alarm; + u32 lockState; +} KBDChannel; + +typedef struct { + u32 callbackAddress; + void* callbackArg; +} KBDLEDCallbackData; + +const char* __KBDVersion = + "<< RVL_SDK - KBD \trelease build: Apr 20 2010 11:21:43 (0x4199_60831) >>"; + +static BOOL kbdInitialized; +static USBKBDAttachCallback kbdDevAttachCallback; +static USBKBDDetachCallback kbdDevDetachCallback; +static void (*kbdKeyCallback)(KBDKeyEventData* event); + +KBDKeyMapTable kbdKeyMaps; +typedef struct { + u32 device; + u8 leds; + u32 transfer[4]; + USBKBDCmdLEDCallback callback; + void* callbackArg; +} KBDLEDCommand; + +KBDLEDCommand kbdCmdBuf[12]; +KBDChannel kbdData[4]; +KBDLEDCallbackData kbdLCBuf[12]; + +u16 kbdKeyMapUS_International[500] = { + 0x00FF, 0x0061, 0x0041, 0x00E1, 0x00C1, 0x003F, 0x0062, 0x0042, 0x0000, 0x0000, + 0x003F, 0x0063, 0x0043, 0x00A9, 0x00A2, 0x00FF, 0x0064, 0x0044, 0x00F0, 0x00D0, + 0x00FF, 0x0065, 0x0045, 0x00E9, 0x00C9, 0x003F, 0x0066, 0x0046, 0x0000, 0x0000, + 0x003F, 0x0067, 0x0047, 0x0000, 0x0000, 0x003F, 0x0068, 0x0048, 0x0000, 0x0000, + 0x00FF, 0x0069, 0x0049, 0x00ED, 0x00CD, 0x003F, 0x006A, 0x004A, 0x0000, 0x0000, + 0x003F, 0x006B, 0x004B, 0x0000, 0x0000, 0x00FF, 0x006C, 0x004C, 0x00F8, 0x00D8, + 0x003F, 0x006D, 0x004D, 0x00B5, 0x0000, 0x00FF, 0x006E, 0x004E, 0x00F1, 0x00D1, + 0x00FF, 0x006F, 0x004F, 0x00F3, 0x00D3, 0x00FF, 0x0070, 0x0050, 0x00F6, 0x00D6, + 0x00FF, 0x0071, 0x0051, 0x00E4, 0x00C4, 0x003F, 0x0072, 0x0052, 0x00AE, 0x0000, + 0x003F, 0x0073, 0x0053, 0x00DF, 0x00A7, 0x00FF, 0x0074, 0x0054, 0x00FE, 0x00DE, + 0x00FF, 0x0075, 0x0055, 0x00FA, 0x00DA, 0x003F, 0x0076, 0x0056, 0x0000, 0x0000, + 0x00FF, 0x0077, 0x0057, 0x00E5, 0x00C5, 0x003F, 0x0078, 0x0058, 0x0000, 0x0000, + 0x00FF, 0x0079, 0x0059, 0x00FC, 0x00DC, 0x00FF, 0x007A, 0x005A, 0x00E6, 0x00C6, + 0x0000, 0x0031, 0x0021, 0x00A1, 0x00B9, 0x0000, 0x0032, 0x0040, 0x00B2, 0x0000, + 0x0000, 0x0033, 0x0023, 0x00B3, 0x0000, 0x0000, 0x0034, 0x0024, 0x00A4, 0x00A3, + 0x0000, 0x0035, 0x0025, 0x20AC, 0x0000, 0x0000, 0x0036, 0x0302, 0x00BC, 0x0000, + 0x0000, 0x0037, 0x0026, 0x00BD, 0x0000, 0x0000, 0x0038, 0x002A, 0x00BE, 0x0000, + 0x0000, 0x0039, 0x0028, 0x2018, 0x0000, 0x0000, 0x0030, 0x0029, 0x2019, 0x0000, + 0x0000, 0xF1CD, 0xF1CD, 0xF1CD, 0xF1CD, 0x0000, 0xF1DB, 0xF1DB, 0xF1DB, 0xF1DB, + 0x0000, 0xF1C8, 0xF1C8, 0xF1C8, 0xF1C8, 0x0000, 0xF1C9, 0xF1C9, 0xF1C9, 0xF1C9, + 0x0000, 0x0020, 0x0020, 0x0020, 0x0020, 0x0000, 0x002D, 0x005F, 0x00A5, 0x0000, + 0x0000, 0x003D, 0x002B, 0x00D7, 0x00F7, 0x0000, 0x005B, 0x007B, 0x00AB, 0x0000, + 0x0000, 0x005D, 0x007D, 0x00BB, 0x0000, 0x0000, 0x005C, 0x007C, 0x00AC, 0x00A6, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x003B, 0x003A, 0x00B6, 0x00B0, + 0x0000, 0x030D, 0x030E, 0x00B4, 0x00A8, 0x0000, 0x0300, 0x0303, 0x0000, 0x0000, + 0x00C0, 0x002C, 0x003C, 0x00E7, 0x00C7, 0x0000, 0x002E, 0x003E, 0x0000, 0x0000, + 0x0000, 0x002F, 0x003F, 0x00BF, 0x0000, 0xC000, 0xF008, 0xF008, 0xF008, 0xF008, + 0x0000, 0xF061, 0xF061, 0xF061, 0xF061, 0x0000, 0xF062, 0xF062, 0xF062, 0xF062, + 0x0000, 0xF063, 0xF063, 0xF063, 0xF063, 0x0000, 0xF064, 0xF064, 0xF064, 0xF064, + 0x0000, 0xF065, 0xF065, 0xF065, 0xF065, 0x0000, 0xF066, 0xF066, 0xF066, 0xF066, + 0x0000, 0xF067, 0xF067, 0xF067, 0xF067, 0x0000, 0xF068, 0xF068, 0xF068, 0xF068, + 0x0000, 0xF069, 0xF069, 0xF069, 0xF069, 0x0000, 0xF06A, 0xF06A, 0xF06A, 0xF06A, + 0x0000, 0xF06B, 0xF06B, 0xF06B, 0xF06B, 0x0000, 0xF06C, 0xF06C, 0xF06C, 0xF06C, + 0xC000, 0xF020, 0xF020, 0xF020, 0xF020, 0xC000, 0xF021, 0xF021, 0xF021, 0xF021, + 0xC000, 0xF022, 0xF022, 0xF022, 0xF022, 0x0000, 0xF1B0, 0xF1B0, 0xF1B0, 0xF1B0, + 0x0000, 0xF1B7, 0xF1B7, 0xF1B7, 0xF1B7, 0x0000, 0xF1B9, 0xF1B9, 0xF1B9, 0xF1B9, + 0x0000, 0xF1AE, 0xF1AE, 0xF1AE, 0xF1AE, 0x0000, 0xF1B1, 0xF1B1, 0xF1B1, 0xF1B1, + 0x0000, 0xF1B3, 0xF1B3, 0xF1B3, 0xF1B3, 0x0000, 0xF1B6, 0xF1B6, 0xF1B6, 0xF1B6, + 0x0000, 0xF1B4, 0xF1B4, 0xF1B4, 0xF1B4, 0x0000, 0xF1B2, 0xF1B2, 0xF1B2, 0xF1B2, + 0x0000, 0xF1B8, 0xF1B8, 0xF1B8, 0xF1B8, 0xC000, 0xF007, 0xF007, 0xF007, 0xF007, + 0x0000, 0xF12F, 0xF12F, 0xF12F, 0xF12F, 0x0000, 0xF12A, 0xF12A, 0xF12A, 0xF12A, + 0x0000, 0xF12D, 0xF12D, 0xF12D, 0xF12D, 0x0000, 0xF12B, 0xF12B, 0xF12B, 0xF12B, + 0x0000, 0xF10D, 0xF10D, 0xF10D, 0xF10D, 0x80FF, 0xF171, 0xF131, 0x0000, 0x0000, + 0x80FF, 0xF172, 0xF132, 0x0000, 0x0000, 0x80FF, 0xF173, 0xF133, 0x0000, 0x0000, + 0x80FF, 0xF174, 0xF134, 0x0000, 0x0000, 0x80FF, 0xF175, 0xF135, 0x0000, 0x0000, + 0x80FF, 0xF176, 0xF136, 0x0000, 0x0000, 0x80FF, 0xF177, 0xF137, 0x0000, 0x0000, + 0x80FF, 0xF178, 0xF138, 0x0000, 0x0000, 0x80FF, 0xF179, 0xF139, 0x0000, 0x0000, + 0x80FF, 0xF170, 0xF130, 0x0000, 0x0000, 0x80FF, 0xF16E, 0xF12E, 0x0000, 0x0000, + 0x0000, 0x005C, 0x007C, 0x0000, 0x0000, 0xC000, 0xF02F, 0xF02F, 0xF02F, 0xF02F, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xF13D, 0xF13D, 0xF13D, 0xF13D +}; + +u16 kbdModifierMapUS_International[40] = { + 0xC000, 0xF000, 0xF000, 0xF000, 0xF000, 0xC000, 0xF001, 0xF001, 0xF001, 0xF001, + 0xC000, 0xF002, 0xF002, 0xF002, 0xF002, 0xC000, 0xF003, 0xF003, 0xF003, 0xF003, + 0xC000, 0xF000, 0xF000, 0xF000, 0xF000, 0xC000, 0xF001, 0xF001, 0xF001, 0xF001, + 0xC000, 0xF005, 0xF005, 0xF005, 0xF005, 0xC000, 0xF003, 0xF003, 0xF003, 0xF003 +}; + +static u16 noRepeatKeys[7] = {0xf006, 0xf008, 0xf007, 0xf021, 0xf020, 0xf022, 0xf02f}; + +USBKBDErr KBDSetModState(u32 channel, u32 value); +USBKBDErr KBDResetChannel(u32 channel); +u16 KBDTranslateHidCode(u32 keyCode, u32 modifiers, s32 country); + +static void kbdAttachHandler(void* device); +static void kbdDetachHandler(void* device); +static void kbdEventHandler(void* device, char* report); +static void kbdRepeatHandler(OSAlarm* alarm, OSContext* context); +static void kbdProcKey(u32 key, u32 state, u32 channel); +static void kbdProcMod(u32 key, u32 state, u32 channel); +static void kbdSendKey(KBDKeyEventData* event); +static void kbd_led_handler(BOOL success, void* callbackArg); + +static void kbdAttachHandler(void* device) { + u32 channel; + channel = 0; + + for (channel = 0; channel < 4; channel++) { + if (kbdData[(u8)channel].device == NULL) { + break; + } + } + + if (channel != 4) { + u8 event[1]; + + kbdData[channel].device = device; + kbdData[channel].flags &= ~1; + KBDResetChannel(channel); + VIResetDimmingCount(); + + if (kbdDevAttachCallback != NULL) { + event[0] = (u8)channel; + kbdDevAttachCallback(event); + } + } +} + +static void kbdDetachHandler(void* device) { + u32 channel; + channel = 0; + + for (channel = 0; channel < 4; channel++) { + if (device == kbdData[(u8)channel].device) { + break; + } + } + + if (channel != 4) { + u8 event[1]; + + kbdData[channel].device = NULL; + kbdData[channel].flags |= 1; + + if (kbdDevDetachCallback != NULL) { + event[0] = (u8)channel; + kbdDevDetachCallback(event); + } + + KBDResetChannel(channel); + } +} + +static void kbdEventHandler(void* device, char* report) { + u32 channel; + channel = 0; + + for (channel = 0; channel < 4; channel++) { + if (device == kbdData[(u8)channel].device) { + break; + } + } + + if (channel != 4) { + KBDChannel* data; + u8* bytes; + u8 status; + data = &kbdData[channel]; + bytes = (u8*)report; + status = bytes[2]; + + if ((u8)(bytes[2] + 0xff) <= 2) { + OSCancelAlarm(&data->alarm); + + if (bytes[2] == 1) { + data->flags |= 2; + } else { + data->flags |= 4; + } + } else { + s32 i; + u32 mask; + data->flags = 0; + VIResetDimmingCount(); + + + for (i = 0, mask = 1; i < 8; i++, mask <<= 1) { + if ((data->modifiers & (u8)mask) != 0) { + if ((bytes[0] & (u8)mask) == 0) { + kbdProcKey((u8)(i + 0xe0), 0, channel); + } + } else if ((bytes[0] & (u8)mask) != 0) { + kbdProcKey((u8)(i + 0xe0), 1, channel); + } + } + + + for (i = 0; i < 6; i++) { + u8 key; + s32 j; + key = data->keys[(s8)i]; + j = 0; + + if (key != 0) { + for (j = 0; j < 6; j++) { + if (key == bytes[j + 2]) { + break; + } + } + + if (j == 6) { + kbdProcKey(key, 0, channel); + data->keys[(s8)i] = 0; + } + } + } + + + for (i = 0; i < 6; i++) { + u8 key; + s32 j; + key = bytes[(s8)i + 2]; + j = 0; + + if (key != 0) { + for (j = 0; j < 6; j++) { + if (key == data->keys[j]) { + break; + } + } + + if (j == 6) { + kbdProcKey(key, 1, channel); + } + } + } + + data->modifiers = bytes[0]; + data->keys[0] = bytes[2]; + data->keys[1] = bytes[3]; + data->keys[2] = bytes[4]; + data->keys[3] = bytes[5]; + data->keys[4] = bytes[6]; + data->keys[5] = bytes[7]; + } + } +} + +static void kbdRepeatHandler(OSAlarm* alarm, OSContext* context) { + KBDChannel* data; + data = (KBDChannel*)OSGetAlarmUserData(alarm); + + kbdProcKey(data->repeatKey, 3, data->channel); +} + +static inline USBKBDErr kbdGetModState(u32 channel, u32* modifiers) { + if (!kbdInitialized) { + return 2; + } + if (channel >= 4 || modifiers == NULL) { + return 4; + } + *modifiers = kbdData[channel].modState; + return 0; +} + +static void kbdProcKey(u32 key, u32 pressed, u32 channel) { + KBDChannel* data; + KBDKeyMap* map; + s32 keyCount; + s32 i; + u32 j; + u16 translated; + OSTime timeout; + KBDKeyEventData event; + data = &kbdData[channel]; + + event.channel = channel; + event.state = pressed; + event.modifiers = 0; + kbdGetModState(channel, &event.modifiers); + map = &kbdKeyMaps.maps[data->country]; + keyCount = map->keypadCount; + for (i = 0; i < keyCount; i++) { + if (key == map->keypadMap[i * 2]) { + key = map->keypadMap[i * 2 + 1]; + break; + } + } + event.code = key; + translated = KBDTranslateHidCode(key, event.modifiers, + data->country); + event.translated = translated; + if ((translated == 0xF000) || (translated == 0xF001) || + (translated == 0xF002) || (translated == 0xF003) || + (translated == 0xF004) || (translated == 0xF005) || + (translated == 0xF006) || (translated == 0xF007) || + (translated == 0xF008) || (translated == 0xF021)) { + kbdProcMod(translated, pressed, channel); + if ((translated != 0xF006) && (translated != 0xF007) && + (translated != 0xF008) && (translated != 0xF021)) { + kbdSendKey(&event); + return; + } + } + if ((pressed & 1) == 0) { + if (key == data->repeatKey) { + OSCancelAlarm(&data->alarm); + } + kbdSendKey(&event); + return; + } + OSCancelAlarm(&data->alarm); + for (j = 0; j < 7; j++) { + if (translated == noRepeatKeys[j]) { + break; + } + } + if (j == 7) { + data->repeatKey = key; + if (data->repeatDelay != 0) { + if ((pressed & 2) != 0) { + timeout = data->repeatInterval * (OS_TIMER_CLOCK / 1000); + } else { + timeout = data->repeatDelay * (OS_TIMER_CLOCK / 1000); + } + OSSetAlarm(&data->alarm, timeout, kbdRepeatHandler); + } + } + event.translated = translated; + kbdSendKey(&event); +} + +static void kbdProcMod(u32 key, u32 pressed, u32 channel) { + KBDChannel* data; + s8 delta; + u8 flags; + u32 state; + u32 finalState; + s8 value; + data = &kbdData[channel]; + delta = (pressed & 1) != 0 ? 1 : -1; + flags = kbdKeyMaps.maps[data->country].flags; + state = 0; + + kbdGetModState(channel, &state); + switch (key) { + case 0xF001: + if (data->lockState != 0) { + if ((pressed & 1) != 0) { + state ^= 2; + } + } else { + value = data->lockCount[1] + delta; + data->lockCount[1] = value; + state = (state & 0xFFFFFFFD) | ((-(u32)(u8)value | (u32)(u8)value) >> 0x1E & 2); + } + break; + case 0xF005: + if (data->lockState != 0) { + if ((pressed & 1) != 0) { + state ^= 0x20; + } + } else { + value = data->lockCount[4] + delta; + data->lockCount[4] = value; + state = (state & 0xFFFFFFDF) | ((-(u32)(u8)value | (u32)(u8)value) >> 0x1A & 0x20); + } + break; + case 0xF000: + if (data->lockState != 0) { + if ((pressed & 1) != 0) { + state ^= 1; + } + } else { + value = data->lockCount[0] + delta; + data->lockCount[0] = value; + state = (state & 0xFFFFFFFE) | ((-(u32)(u8)value | (u32)(u8)value) >> 0x1F); + } + break; + case 0xF008: + if (((pressed & 1) != 0) && (data->lockProcessing != 0)) { + state ^= 0x200; + } + break; + case 0xF007: + if (((pressed & 1) != 0) && (data->lockProcessing != 0)) { + state ^= 0x100; + } + break; + case 0xF006: + if (((pressed & 1) != 0) && (data->lockProcessing != 0)) { + u32 oldState; + u32 lockState; + oldState = state; + lockState = oldState & 0xC0; + switch (lockState) { + case 0: + if ((flags & 1) == 1) { + state = oldState | 0x40; + } + break; + case 0x40: + { + state = oldState & 0xFFFFFFBF; + if ((flags & 4) == 4) { + state = oldState & 0xFFFFFFBF | 0x80; + } + } + break; + case 0x80: + state = oldState & 0xFFFFFF7F; + break; + case 0xC0: + state = oldState & 0xFFFFFF3F; + break; + } + } + break; + case 0xF002: + if (data->lockState != 0) { + if ((pressed & 1) != 0) { + state ^= 4; + } + } else { + value = data->lockCount[2] + delta; + data->lockCount[2] = value; + state = (state & 0xFFFFFFFB) | ((-(u32)(u8)value | (u32)(u8)value) >> 0x1D & 4); + } + break; + case 0xF003: + if (data->lockState != 0) { + if ((pressed & 1) != 0) { + state ^= 8; + } + } else { + value = data->lockCount[3] + delta; + data->lockCount[3] = value; + state = (state & 0xFFFFFFF7) | ((-(u32)(u8)value | (u32)(u8)value) >> 0x1C & 8); + } + break; + case 0xF004: + if (data->lockState != 0) { + if ((pressed & 1) != 0) { + state ^= 0x10; + } + } else { + value = data->lockCount[5] + delta; + data->lockCount[5] = value; + state = (state & 0xFFFFFFEF) | ((-(u32)(u8)value | (u32)(u8)value) >> 0x1B & 0x10); + } + break; + case 0xF021: + if (((pressed & 1) != 0) && (data->lockProcessing != 0)) { + state ^= 0x400; + } + break; + } + finalState = state | 0x1000; + KBDSetModState(channel, finalState); +} + +static void kbdSendKey(KBDKeyEventData* event) { + KBDChannel* data; + u8 head; + u8 tail; + u8 next; + if (kbdKeyCallback != NULL) { + kbdKeyCallback(event); + } + data = &kbdData[event->channel]; + tail = data->readIndex; + head = data->writeIndex; + if (head != tail) { + next = (head + 1) % 0x20; + if (next == tail) { + data->events[head].channel = event->channel; + data->events[data->writeIndex].code = 0xff; + data->events[data->writeIndex].state = 0; + data->events[data->writeIndex].modifiers = 0; + data->events[data->writeIndex].translated = 0; + } else { + data->events[head] = *event; + if (data->readIndex == 0x20) { + data->readIndex = data->writeIndex; + } + } + data->writeIndex = next; + } +} + +static void kbd_led_handler(BOOL success, void* callbackArg) { + u32 index; + u32 result; + u32 callbackAddress; + index = (u32)callbackArg; + callbackAddress = kbdLCBuf[index].callbackAddress; + + kbdCmdBuf[index].device = 0; + if (callbackAddress == 0) { + return; + } + switch (success) { + case TRUE: + result = 0; + break; + default: + result = 7; + break; + } + ((USBKBDCmdLEDCallback)kbdLCBuf[index].callbackAddress)(result, kbdLCBuf[index].callbackArg); +} + +USBKBDErr KBDSetLedsAsync(u32 channel, u32 leds, USBKBDCmdLEDCallback callback, void* callbackArg) { + u32 index; + BOOL interrupts; + USBKBDErr result; + if (!kbdInitialized) { + return 2; + } + if (channel >= 4) { + return 4; + } + if ((s32)kbdData[channel].flags == 1 || (s32)kbdData[channel].flags == 4) { + return 5; + } + leds &= 0xff; + interrupts = OSDisableInterrupts(); + for (index = 0; index < 12; index++) { + if (kbdCmdBuf[index].device == 0) { + kbdCmdBuf[index].device = (u32)kbdData[channel].device; + break; + } + } + OSRestoreInterrupts(interrupts); + if (index == 12) { + return 7; + } + kbdLCBuf[index].callbackAddress = (u32)callback; + kbdLCBuf[index].callbackArg = callbackArg; + result = USBKBDSetLEDAsync((u32)kbdData[channel].device, leds, + (USBKBDCmdLEDAsync*)&kbdCmdBuf[index], + kbd_led_handler, (void*)index); + if (result != 0) { + return 7; + } + return 0; +} + +USBKBDErr KBDSetLeds(u32 channel, u32 leds) { + u32 index; + BOOL interrupts; + USBKBDErr result; + if (!kbdInitialized) { + return 2; + } + if (channel >= 4) { + return 4; + } + if ((s32)kbdData[channel].flags == 1 || (s32)kbdData[channel].flags == 4) { + return 5; + } + leds &= 0xff; + interrupts = OSDisableInterrupts(); + for (index = 0; index < 12; index++) { + if (kbdCmdBuf[index].device == 0) { + kbdCmdBuf[index].device = (u32)kbdData[channel].device; + break; + } + } + OSRestoreInterrupts(interrupts); + if (index == 12) { + return 7; + } + result = USBKBDSetLED((u32)kbdData[channel].device, leds, + (USBKBDCmdLED*)&kbdCmdBuf[index]); + kbdCmdBuf[index].device = 0; + if (result != 0) { + return 7; + } + return 0; +} + +void kbdInitMap(u32 country, u8 codeCount, u8 flags, u16* keyMap, u16* modifierMap, + u8 lockIndex, u8 shiftIndex, u8 altIndex, u8 maxCode, s32 keypadType) { + KBDKeyMap* map; + map = &kbdKeyMaps.maps[country]; + + map->codeCount = codeCount; + map->flags = flags; + map->keyMap = keyMap; + map->modifierMap = modifierMap; + map->lockIndex = lockIndex; + map->shiftIndex = shiftIndex; + map->altIndex = altIndex; + map->maxCode = maxCode; + + switch (keypadType) { + case 0: + map->keypadMap[0] = 0x32; + map->keypadMap[1] = 0x31; + map->keypadCount = 1; + break; + case 1: + map->keypadMap[0] = 0x31; + map->keypadMap[1] = 0x32; + map->keypadMap[2] = 0x94; + map->keypadMap[3] = 0x35; + map->keypadCount = 2; + break; + case 2: + map->keypadMap[0] = 0x31; + map->keypadMap[1] = 0x32; + map->keypadCount = 1; + break; + } +} + +void kbdInitMapIntl(void) { + kbdInitMap(0, 5, 0x30, kbdKeyMapUS_International, + kbdModifierMapUS_International, 0, 0, 3, 100, 0); +} + +USBKBDErr KBDInit(void) { + USBKBDErr result; + u32 i; + + if (kbdInitialized) { + return 3; + } + OSRegisterVersion(__KBDVersion); + kbdInitialized = TRUE; + + + for (i = 0; i < 4; i++) { + KBDChannel* data; + data = &kbdData[(u8)i]; + + data->channel = (u8)i; + data->country = 0x21; + data->flags = 1; + data->device = NULL; + data->lockProcessing = 1; + data->repeatDelay = 500; + data->repeatInterval = 0x21; + data->lockState = 0; + + OSCreateAlarm(&data->alarm); + OSSetAlarmUserData(&data->alarm, data); + KBDResetChannel((u8)i); + } + + if (USBKBDInitialize(kbdAttachHandler, kbdDetachHandler) != 0) { + result = 1; + } else { + result = USBKBDRegisterEventCallback(kbdEventHandler); + result = (USBKBDErr)((-(u32)result | (u32)result) >> 31); + } + + + return result; +} + +USBKBDAttachCallback KBDSetAttachCallback(USBKBDAttachCallback callback) { + USBKBDAttachCallback previous = kbdDevAttachCallback; + + kbdDevAttachCallback = callback; + return previous; +} + +USBKBDDetachCallback KBDSetDetachCallback(USBKBDDetachCallback callback) { + USBKBDDetachCallback previous = kbdDevDetachCallback; + + kbdDevDetachCallback = callback; + return previous; +} + +void (*KBDSetKeyCallback(void (*callback)(KBDKeyEventData* event)))(KBDKeyEventData* event) { + void (*previous)(KBDKeyEventData* event) = kbdKeyCallback; + + kbdKeyCallback = callback; + return previous; +} + +USBKBDErr KBDResetChannel(u32 channel) { + if (!kbdInitialized) { + return 2; + } + + if (channel >= 4) { + return 4; + } + { + KBDChannel* data; + data = &kbdData[channel]; + + data->modifiers = 0; + data->keys[0] = 0; + data->keys[1] = 0; + data->keys[2] = 0; + data->keys[3] = 0; + data->keys[4] = 0; + data->keys[5] = 0; + data->readIndex = 0x20; + KBDSetModState(channel, 0x1000); + data->lockCount[0] = 0; + data->lockCount[1] = 0; + data->lockCount[2] = 0; + data->lockCount[3] = 0; + data->lockCount[4] = 0; + data->lockCount[5] = 0; + data->repeatKey = 0; + OSCancelAlarm(&data->alarm); + return 0; + } +} + +USBKBDErr KBDSetCountry(u32 channel, s32 country) { + if (!kbdInitialized) { + return 2; + } + if (channel >= 4) { + return 4; + } + if (country > 0x23) { + return 4; + } + if (kbdKeyMaps.maps[country].codeCount == 0) { + return 4; + } + kbdData[channel].country = country; + KBDResetChannel(channel); + return 0; +} + +USBKBDErr KBDSetLockProcessing(u32 channel, u32 value) { + if (!kbdInitialized) { + return 2; + } + + if (channel >= 4) { + return 4; + } + + kbdData[channel].lockProcessing = value; + return 0; +} + +USBKBDErr KBDSetModState(u32 channel, u32 value) { + if (!kbdInitialized) { + return 2; + } + if (channel >= 4) { + return 4; + } + if ((value & 0x1000) != 0) { + kbdData[channel].modState = value & ~0x1000; + } else { + BOOL interrupts; + KBDModifierState* state; + KBDModifierState oldState; + KBDModifierState newState; + interrupts = OSDisableInterrupts(); + state = (KBDModifierState*)&kbdData[channel].modState; + newState.value = value & 0xfc0; + oldState = *state; + newState.bits.physical = oldState.bits.physical; + *state = newState; + OSRestoreInterrupts(interrupts); + } + return 0; +} + +u16 KBDTranslateHidCode(u32 keyCode, u32 modifiers, s32 country) { + KBDKeyMap* map; + u16* table; + u32 keyIndex; + u16 entry; + u32 mask; + u32 offset; + s32 group; + u32 shiftFlag; + u32 shift; + + if (kbdInitialized == FALSE) { + return 0xFFFF; + } + if (country >= 0x24) { + return 0xFFFF; + } + + map = &kbdKeyMaps.maps[country]; + + if (map->codeCount == 0) { + return 0; + } + + if (keyCode < 4) { + return 0xFFFF; + } + + + if ((s32)keyCode < (s32)map->maxCode + 4) { + keyIndex = (u8)(keyCode - 4); + table = map->keyMap; + } else { + if (keyCode >= 0xE0 && keyCode <= 0xE7) { + keyIndex = (u8)(keyCode - 0xe0); + table = map->modifierMap; + } else { + return 0; + } + } + + keyIndex = (keyIndex & 0xFF) * map->codeCount; + entry = table[keyIndex]; + mask = entry & 0xC000; + + if (mask == 0xC000) { + group = 1; + offset = 0; + } else { + if ((modifiers & 0x20) == 0 && (modifiers & 5) != 5) { + if ((modifiers & 0x40) == 0) { + if ((modifiers & 0x80) == 0) { + group = 1; + shiftFlag = 1; + } else { + group = map->shiftIndex; + shiftFlag = 0x10; + } + } else { + group = map->lockIndex; + shiftFlag = 4; + } + } else { + group = map->altIndex; + shiftFlag = 0x40; + } + + if ((modifiers & 0x800) == 0 && (modifiers & 0x1D) != 0) { + offset = 0; + } else { + shift = (modifiers >> 1) & 1; + offset = (shiftFlag << shift) & 0xFFFF; + + if (mask == 0 && (offset & entry) != 0) { + offset = shift ^ ((modifiers >> 9) & 1); + } else if (mask == 0x8000 && (offset & entry) != 0) { + offset = 0; + if ((modifiers & 0x100) != 0 && shift == 0) { + offset = 1; + } + } else { + offset = shift; + if (mask == 0x4000 && group == 1 && (modifiers & 0x200) != 0) { + group = map->lockIndex; + } + } + } + + if (offset == 0) { + if (group == map->altIndex && (map->flags & 0x10) == 0) { + return 0; + } + if (group == map->lockIndex && (map->flags & 1) == 0) { + return 0; + } + if (group == map->shiftIndex && (map->flags & 4) == 0) { + return 0; + } + } else { + if (group == map->altIndex && (map->flags & 0x20) == 0) { + return 0; + } + if (group == map->lockIndex && (map->flags & 2) == 0) { + return 0; + } + if (group == map->shiftIndex && (map->flags & 8) == 0) { + return 0; + } + } + + } + + return table[keyIndex + offset + group]; + +} diff --git a/libs/RVL_SDK/src/vi/vi3in1.c b/libs/RVL_SDK/src/vi/vi3in1.c new file mode 100644 index 00000000..1b56acf0 --- /dev/null +++ b/libs/RVL_SDK/src/vi/vi3in1.c @@ -0,0 +1,719 @@ +#include +#include +#include + +static u16 gammaSet[31][17] = { + {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, + {0x0000, 0x0000, 0x0000, 0x0030, 0x0397, 0x3b49, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1000, 0x1000, 0x1000, 0x1080, 0x1b80, 0xeb00}, + {0x0000, 0x0028, 0x005a, 0x02db, 0x0d8d, 0x3049, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1000, 0x1040, 0x1100, 0x1880, 0x4200, 0xeb00}, + {0x0000, 0x007a, 0x023c, 0x076d, 0x129c, 0x2724, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1000, 0x10c0, 0x1580, 0x2900, 0x6200, 0xeb00}, + {0x004e, 0x0199, 0x052d, 0x0b24, 0x1429, 0x20a4, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1040, 0x12c0, 0x1dc0, 0x3b00, 0x78c0, 0xeb00}, + {0x00ec, 0x03d7, 0x0800, 0x0d9e, 0x143e, 0x1bdb, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x10c0, 0x16c0, 0x27c0, 0x4b80, 0x8980, 0xeb00}, + {0x0276, 0x0666, 0x0a96, 0x0ef3, 0x13ac, 0x1849, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1200, 0x1c00, 0x3280, 0x59c0, 0x9600, 0xeb00}, + {0x04ec, 0x08f5, 0x0c96, 0x0fcf, 0x12c6, 0x1580, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1400, 0x2200, 0x3cc0, 0x6640, 0x9fc0, 0xeb00}, + {0x0800, 0x0bae, 0x0e00, 0x1030, 0x11cb, 0x1349, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1680, 0x28c0, 0x4680, 0x7100, 0xa780, 0xeb00}, + {0x0bb1, 0x0e14, 0x0f2d, 0x1018, 0x10e5, 0x1180, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x1980, 0x2f80, 0x4fc0, 0x7a00, 0xadc0, 0xeb00}, + {0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1020, 0x4060, 0x80a0, 0xeb00, 0x1000, 0x2000, 0x4000, 0x6000, 0x8000, 0xa000, 0xeb00}, + {0x14ec, 0x11c2, 0x1078, 0x0fb6, 0x0f2f, 0x0eb6, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x2100, 0x3cc0, 0x5fc0, 0x8900, 0xb780, 0xeb00}, + {0x19d8, 0x1333, 0x10d2, 0x0f6d, 0x0e5e, 0x0da4, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x2500, 0x4300, 0x66c0, 0x8f40, 0xbb40, 0xeb00}, + {0x1ec4, 0x147a, 0x110f, 0x0f0c, 0x0da1, 0x0cb6, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x2900, 0x4900, 0x6d40, 0x94c0, 0xbe80, 0xeb00}, + {0x2400, 0x1570, 0x110f, 0x0eaa, 0x0d0f, 0x0bdb, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x2d40, 0x4ec0, 0x7300, 0x9980, 0xc180, 0xeb00}, + {0x293b, 0x163d, 0x110f, 0x0e30, 0x0c7d, 0x0b24, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x3180, 0x5440, 0x7880, 0x9dc0, 0xc400, 0xeb00}, + {0x2e27, 0x170a, 0x10d2, 0x0de7, 0x0beb, 0x0a80, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x3580, 0x5980, 0x7d40, 0xa1c0, 0xc640, 0xeb00}, + {0x3362, 0x175c, 0x10d2, 0x0d6d, 0x0b6d, 0x09ed, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x39c0, 0x5e40, 0x8200, 0xa540, 0xc840, 0xeb00}, + {0x384e, 0x17ae, 0x10b4, 0x0d0c, 0x0af0, 0x096d, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x3dc0, 0x62c0, 0x8640, 0xa880, 0xca00, 0xeb00}, + {0x3d3b, 0x1800, 0x105a, 0x0cc3, 0x0a72, 0x0900, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x41c0, 0x6740, 0x8a00, 0xab80, 0xcb80, 0xeb00}, + {0x41d8, 0x1828, 0x103c, 0x0c49, 0x0a1f, 0x0892, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x4580, 0x6b40, 0x8dc0, 0xae00, 0xcd00, 0xeb00}, + {0x4676, 0x1851, 0x0fe1, 0x0c00, 0x09b6, 0x0836, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x4940, 0x6f40, 0x9100, 0xb080, 0xce40, 0xeb00}, + {0x4ac4, 0x187a, 0x0fa5, 0x0b9e, 0x0963, 0x07db, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x4cc0, 0x7300, 0x9440, 0xb2c0, 0xcf80, 0xeb00}, + {0x4f13, 0x1851, 0x0f69, 0x0b6d, 0x090f, 0x0780, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x5040, 0x7640, 0x9700, 0xb500, 0xd0c0, 0xeb00}, + {0x5313, 0x187a, 0x0f0f, 0x0b24, 0x08bc, 0x0736, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x5380, 0x79c0, 0x99c0, 0xb700, 0xd1c0, 0xeb00}, + {0x5713, 0x1851, 0x0ef0, 0x0ac3, 0x087d, 0x06ed, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x56c0, 0x7cc0, 0x9c80, 0xb8c0, 0xd2c0, 0xeb00}, + {0x5b13, 0x1828, 0x0e96, 0x0a92, 0x0829, 0x06b6, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x5a00, 0x7fc0, 0x9ec0, 0xba80, 0xd380, 0xeb00}, + {0x5ec4, 0x1800, 0x0e78, 0x0a30, 0x0800, 0x066d, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x5d00, 0x8280, 0xa140, 0xbc00, 0xd480, 0xeb00}, + {0x6276, 0x17d7, 0x0e1e, 0x0a00, 0x07c1, 0x0636, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x6000, 0x8540, 0xa340, 0xbd80, 0xd540, 0xeb00}, + {0x65d8, 0x17ae, 0x0de1, 0x09cf, 0x0782, 0x0600, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x62c0, 0x87c0, 0xa540, 0xbf00, 0xd600, 0xeb00}, + {0x693b, 0x1785, 0x0da5, 0x0986, 0x0743, 0x05db, 0x101d, 0x3658, 0x82b3, 0xeb00, 0x1000, 0x6580, 0x8a40, 0xa740, 0xc040, 0xd680, 0xeb00} +}; + +static u8 VINtscACPType1[0x1a] = { + 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x0f, 0x0f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +static u8 VINtscACPType2[0x1a] = { + 0x3e, 0x1d, 0x11, 0x25, 0x11, 0x01, 0x07, 0x00, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x0f, 0x0f, 0x60, 0x01, 0x0a, 0x00, 0x05, 0x04, 0x03, 0xff, 0x00 +}; + +static u8 VINtscACPType3[0x1a] = { + 0x3e, 0x17, 0x15, 0x21, 0x15, 0x05, 0x05, 0x02, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x0f, 0x0f, 0x60, 0x01, 0x0a, 0x00, 0x05, 0x04, 0x03, 0xff, 0x00 +}; + +static u8 VIPalACPType1[0x1a] = { + 0x36, 0x1a, 0x22, 0x2a, 0x22, 0x05, 0x02, 0x00, 0x1c, 0x3d, 0x14, 0x03, 0xfe, + 0x01, 0x54, 0xfe, 0x7e, 0x60, 0x00, 0x08, 0x00, 0x04, 0x07, 0x01, 0x55, 0x01 +}; + +static u8 VIPalACPType2[0x1a] = { + 0x36, 0x1a, 0x22, 0x2a, 0x22, 0x05, 0x02, 0x00, 0x1c, 0x3d, 0x14, 0x03, 0xfe, + 0x01, 0x54, 0xfe, 0x7e, 0x60, 0x00, 0x08, 0x00, 0x04, 0x07, 0x01, 0x55, 0x01 +}; + +static u8 VIPalACPType3[0x1a] = { + 0x36, 0x1a, 0x22, 0x2a, 0x22, 0x05, 0x02, 0x00, 0x1c, 0x3d, 0x14, 0x03, 0xfe, + 0x01, 0x54, 0xfe, 0x7e, 0x60, 0x00, 0x08, 0x00, 0x04, 0x07, 0x01, 0x55, 0x01 +}; + +static u8 VIEurgb60ACPType1[0x1a] = { + 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x1e, 0x1e, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 +}; + +static u8 VIEurgb60ACPType2[0x1a] = { + 0x36, 0x1d, 0x11, 0x25, 0x11, 0x01, 0x07, 0x00, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x1e, 0x1e, 0x60, 0x01, 0x0a, 0x00, 0x05, 0x04, 0x03, 0xff, 0x01 +}; + +static u8 VIEurgb60ACPType3[0x1a] = { + 0x36, 0x17, 0x15, 0x21, 0x15, 0x05, 0x05, 0x02, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x1e, 0x1e, 0x60, 0x01, 0x0a, 0x00, 0x05, 0x04, 0x03, 0xff, 0x01 +}; + +static u8 VIMpalACPType1[0x1a] = { + 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x0f, 0x0f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +static u8 VIMpalACPType2[0x1a] = { + 0x36, 0x1d, 0x11, 0x25, 0x11, 0x01, 0x07, 0x00, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x0f, 0x0f, 0x60, 0x01, 0x0a, 0x00, 0x05, 0x04, 0x03, 0xff, 0x00 +}; + +static u8 VIMpalACPType3[0x1a] = { + 0x36, 0x17, 0x15, 0x21, 0x15, 0x05, 0x05, 0x02, 0x1b, 0x1b, 0x24, 0x07, 0xf8, + 0x00, 0x00, 0x0f, 0x0f, 0x60, 0x01, 0x0a, 0x00, 0x05, 0x04, 0x03, 0xff, 0x00 +}; + +u8 VIProgressiveACPType[0x1a] = { + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + + +void __VISendI2CData(u8 dev, u8* data, u8 length); +void WaitMicroTime(u32 time); +void __VISetYUVSEL(u8 value); +void __VISetGammaImm(u16* gamma); +void __VISetRevolutionMode(void); +void __VISetRevolutionModeSimple(void); +void VISetMacrovision(s32 type); + +#define COPY_ACP(dst, src) \ + do { \ + u32 copyIndex; \ + u8* destination = (dst); \ + const u8* source = (src); \ + for (copyIndex = 0; copyIndex < 0x1a; copyIndex++) { \ + destination[copyIndex + 1] = source[copyIndex]; \ + } \ + destination[0] = 0x40; \ + } while (0) + +static u8 __wd0 = 0xff; +static u8 __wd1 = 0xff; +static u8 __wd2 = 0xff; +static u8 __gp1 = 0xff; +static u8 __gp2 = 0xff; +static u8 __gp3 = 0xff; +static u8 __gp4 = 0xff; +static u8 __cc1 = 0xff; +static u8 __cc2 = 0xff; +static u8 __cc3 = 0xff; +static u8 __cc4 = 0xff; +static u32 __tvType = 0xff; +static u8 __filter = 0xff; +u8 VIZeroACPType[0x1a]; +static s32 Vdac_Flag_Region; +static s32 __type; +static s32 __gamma; +static s32 __level; +u32 Vdac_Flag_Changed_816991E0; + +void __VISetVolume(u8 wd0, u8 wd1) { + u8 data[3]; + + data[0] = 0x71; + data[1] = wd0; + data[2] = wd1; + __VISendI2CData(0xe0, data, 3); + WaitMicroTime(2); +} + +void __VISetYUVSEL(u8 value) { + s32 region = *(s32*)0x800000cc; + u8 data[2]; + + if (region != 2) { + if (region < 2) { + if (region == 0) { + goto region0; + } + if (region >= 0) { + goto region2; + } + } else { + switch (region) { + case 5: + goto region2; + default: + goto regionDefault; + } + } + goto regionDefault; + } + goto region1; +region2: + Vdac_Flag_Region = 2; + goto send; +region1: + Vdac_Flag_Region = 1; + goto send; +region0: + Vdac_Flag_Region = 0; + goto send; +regionDefault: + Vdac_Flag_Region = 0; + goto send; +send: + data[0] = 1; + data[1] = (value << 5) | Vdac_Flag_Region; + __VISendI2CData(0xe0, data, 2); + WaitMicroTime(2); +} + +void __VISetFilter4EURGB60(u8 value) { + u8 data[2]; + + data[0] = 0x6e; + data[1] = value; + __VISendI2CData(0xe0, data, 2); + WaitMicroTime(2); +} + +void __VISetCGMS(void) { + u8 data[3]; + + data[0] = 5; + data[1] = (__wd1 & 0xf) << 2 | __wd0 & 3; + data[2] = __wd2; + __VISendI2CData(0xe0, data, 3); + WaitMicroTime(2); +} + +void __VISetWSS(void) { + u8 data[3]; + + data[0] = 8; + data[1] = (__gp2 & 0xf) << 4 | __gp1 & 0xf; + data[2] = (__gp4 & 7) << 3 | __gp3 & 7; + __VISendI2CData(0xe0, data, 3); + WaitMicroTime(2); +} + +void __VISetClosedCaption(void) { + u8 data[5]; + + data[0] = 0x7a; + data[1] = __cc1 & 0x7f; + data[2] = __cc2 & 0x7f; + data[3] = __cc3 & 0x7f; + data[4] = __cc4 & 0x7f; + __VISendI2CData(0xe0, data, 5); + WaitMicroTime(2); +} + +void __VISetMacrovision(void) { + switch (__type) { + case 2: + switch (__tvType) { + case 0: { + u8 data[0x1b]; + COPY_ACP(data, VINtscACPType1); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 1: { + u8 data[0x1b]; + COPY_ACP(data, VIPalACPType1); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 2: { + u8 data[0x1b]; + COPY_ACP(data, VIMpalACPType1); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 5: { + u8 data[0x1b]; + COPY_ACP(data, VIEurgb60ACPType1); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + } + break; + case 3: + switch (__tvType) { + case 0: { + u8 data[0x1b]; + COPY_ACP(data, VINtscACPType2); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 1: { + u8 data[0x1b]; + COPY_ACP(data, VIPalACPType2); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 2: { + u8 data[0x1b]; + COPY_ACP(data, VIMpalACPType2); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 5: { + u8 data[0x1b]; + COPY_ACP(data, VIEurgb60ACPType2); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + } + break; + case 4: + switch (__tvType) { + case 0: { + u8 data[0x1b]; + COPY_ACP(data, VINtscACPType3); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 1: { + u8 data[0x1b]; + COPY_ACP(data, VIPalACPType3); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 2: { + u8 data[0x1b]; + COPY_ACP(data, VIMpalACPType3); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + case 5: { + u8 data[0x1b]; + COPY_ACP(data, VIEurgb60ACPType3); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + } + break; + case 1: { + u8 data[0x1b]; + COPY_ACP(data, VIZeroACPType); + __VISendI2CData(0xe0, data, 0x1b); + WaitMicroTime(2); + break; + } + } +} + +void VISetMacrovision(s32 type) { + u8 oldType; + u8 oldWd2; + u8 oldWd0; + u8 mode; + u32 volume; + u8 newType; + u8 newWd2; + s32 tvFormat; + + mode = 0; + volume = 0; + oldWd0 = __wd0; + oldType = __wd1; + oldWd2 = __wd2; + newType = oldType; + if (type == 3) { + goto type3; + } + if (type < 3) { + if (type == 1) { + goto type1; + } else if (type >= 1) { + goto type2; + } + goto done; + } + if (type >= 5) { + goto done; + } + goto type5; +type1: + mode = 0; + volume = 0; + goto done; +type2: + mode = 2; + volume = 3; + newType = 0; + goto done; +type3: + mode = 1; + volume = 3; + newType = 0; + goto done; +type5: + mode = 3; + volume = 3; + newType = 0; +done: + volume |= oldWd2 & 0xf0; + newWd2 = volume | mode << 2; + if ((oldType != newType) || (oldWd2 != newWd2)) { + __wd0 = oldWd0; + __wd1 = newType; + __wd2 = newWd2; + Vdac_Flag_Changed_816991E0 |= 1; + } + tvFormat = VIGetTvFormat(); + if ((__type != type) || (__tvType != tvFormat)) { + __type = type; + __tvType = tvFormat; + Vdac_Flag_Changed_816991E0 |= 8; + } +} + +void __VISetGammaImm(u16* gamma) { + u8 data[0x23]; + + data[0] = 0x10; + data[1] = ((u32)gamma[0] << 16) >> 24; + data[2] = gamma[0]; + data[3] = ((u32)gamma[1] << 16) >> 24; + data[4] = gamma[1]; + data[5] = ((u32)gamma[2] << 16) >> 24; + data[6] = gamma[2]; + data[7] = ((u32)gamma[3] << 16) >> 24; + data[8] = gamma[3]; + data[9] = ((u32)gamma[4] << 16) >> 24; + data[10] = gamma[4]; + data[11] = ((u32)gamma[5] << 16) >> 24; + data[12] = gamma[5]; + data[13] = ((u8*)gamma)[12]; + data[14] = ((u8*)gamma)[13]; + data[15] = ((u8*)gamma)[14]; + data[16] = ((u8*)gamma)[15]; + data[17] = ((u8*)gamma)[16]; + data[18] = ((u8*)gamma)[17]; + data[19] = ((u8*)gamma)[18]; + data[20] = ((u32)gamma[10] << 16) >> 24; + data[21] = gamma[10] & 0xc0; + data[22] = ((u32)gamma[11] << 16) >> 24; + data[23] = gamma[11] & 0xc0; + data[24] = ((u32)gamma[12] << 16) >> 24; + data[25] = gamma[12] & 0xc0; + data[26] = ((u32)gamma[13] << 16) >> 24; + data[27] = gamma[13] & 0xc0; + data[28] = ((u32)gamma[14] << 16) >> 24; + data[29] = gamma[14] & 0xc0; + data[30] = ((u32)gamma[15] << 16) >> 24; + data[31] = gamma[15] & 0xc0; + data[32] = ((u32)gamma[16] << 16) >> 24; + data[33] = gamma[16] & 0xc0; + __VISendI2CData(0xe0, data, 0x22); + WaitMicroTime(2); +} +void __VISetGamma(void) { + __VISetGammaImm(gammaSet[__gamma]); +} + +void __VISetTrapFilter(void) { + u8 data[2]; + + data[0] = 3; + if (__filter == 1) { + data[1] = 0; + } else { + data[1] = 1; + } + __VISendI2CData(0xe0, data, 2); + WaitMicroTime(2); +} + +void VISetTrapFilter(BOOL filter) { + if (__filter == (u32)filter) { + return; + } + __filter = filter; + Vdac_Flag_Changed_816991E0 |= 0x20; +} + +void __VISetRGBOverDrive(void) { + u8 data[2]; + + if (Vdac_Flag_Region == 3) { + data[0] = 10; + data[1] = __level << 1 | 1; + __VISendI2CData(0xe0, data, 2); + WaitMicroTime(2); + } else { + data[0] = 10; + data[1] = 0; + __VISendI2CData(0xe0, data, 2); + WaitMicroTime(2); + } +} + +void VISetRGBModeImm(void) { + Vdac_Flag_Changed_816991E0 |= 0x80; +} + +void __VISetRGBModeImm(void) { + u8 data[2]; + + Vdac_Flag_Region = 3; + data[0] = 1; + data[1] = 3; + __VISendI2CData(0xe0, data, 2); + WaitMicroTime(2); +} + +void __VISetRevolutionMode(void) { + u8 resetCommand[2]; + u8 powerCommand[2]; + u8 clockCommand[2]; + u8 outputCommand[2]; + u8 volumeCommand[3]; + u8 syncCommand[2]; + u8 enableCommand[2]; + + resetCommand[0] = 4; + resetCommand[1] = 0; + __VISendI2CData(0xe0, resetCommand, 2); + WaitMicroTime(2); + powerCommand[0] = 0x6a; + powerCommand[1] = 1; + __VISendI2CData(0xe0, powerCommand, 2); + WaitMicroTime(2); + clockCommand[0] = 0x65; + clockCommand[1] = 1; + __VISendI2CData(0xe0, clockCommand, 2); + WaitMicroTime(2); + __VISetYUVSEL(VIGetDTVStatus()); + outputCommand[0] = 0; + outputCommand[1] = 0; + __VISendI2CData(0xe0, outputCommand, 2); + WaitMicroTime(2); + volumeCommand[0] = 0x71; + volumeCommand[1] = 0x8e; + volumeCommand[2] = 0x8e; + __VISendI2CData(0xe0, volumeCommand, 3); + WaitMicroTime(2); + syncCommand[0] = 2; + syncCommand[1] = 7; + __VISendI2CData(0xe0, syncCommand, 2); + WaitMicroTime(2); + if ((__wd0 != 0) || (__wd1 != 0) || (__wd2 != 0)) { + __wd0 = 0; + __wd1 = 0; + __wd2 = 0; + Vdac_Flag_Changed_816991E0 |= 1; + } + if ((__gp1 != 0) || (__gp2 != 0) || (__gp3 != 0) || (__gp4 != 0)) { + __gp1 = 0; + __gp2 = 0; + __gp3 = 0; + __gp4 = 0; + Vdac_Flag_Changed_816991E0 |= 2; + } + if ((__cc1 != 0) || (__cc2 != 0) || (__cc3 != 0) || (__cc4 != 0)) { + __cc1 = 0; + __cc2 = 0; + __cc3 = 0; + __cc4 = 0; + Vdac_Flag_Changed_816991E0 |= 4; + } + VISetMacrovision(1); + if (__filter != 0) { + __filter = 0; + Vdac_Flag_Changed_816991E0 |= 0x20; + } + if (__level != 0) { + __level = 0; + Vdac_Flag_Changed_816991E0 |= 0x40; + } + if (__gamma != 10) { + __gamma = 10; + Vdac_Flag_Changed_816991E0 |= 0x10; + } + VIFlush(); + VIWaitForRetrace(); + enableCommand[0] = 4; + enableCommand[1] = 1; + __VISendI2CData(0xe0, enableCommand, 2); + WaitMicroTime(2); +} + +void __VISetRevolutionModeSimple(void) { + u32 i; + u8 powerCommand[2]; + u8 clockCommand[2]; + u8 outputCommand[2]; + u8 volumeCommand[3]; + u8 syncCommand[2]; + u8 cgmsCommand[3]; + u8 wssCommand[3]; + u8 captionCommand[5]; + u8 acpCommand[0x1c]; + u8 driveCommand[2]; + u8 filterCommand[2]; + + powerCommand[0] = 0x6a; + powerCommand[1] = 1; + __VISendI2CData(0xe0, powerCommand, 2); + WaitMicroTime(2); + clockCommand[0] = 0x65; + clockCommand[1] = 1; + __VISendI2CData(0xe0, clockCommand, 2); + WaitMicroTime(2); + __VISetYUVSEL(VIGetDTVStatus()); + outputCommand[0] = 0; + outputCommand[1] = 0; + __VISendI2CData(0xe0, outputCommand, 2); + WaitMicroTime(2); + volumeCommand[0] = 0x71; + volumeCommand[1] = 0x8e; + volumeCommand[2] = 0x8e; + __VISendI2CData(0xe0, volumeCommand, 3); + WaitMicroTime(2); + syncCommand[0] = 2; + syncCommand[1] = 7; + __VISendI2CData(0xe0, syncCommand, 2); + WaitMicroTime(2); + __wd0 = 0; + __wd1 = 0; + __wd2 = 0; + cgmsCommand[0] = 5; + cgmsCommand[1] = 0; + cgmsCommand[2] = 0; + __VISendI2CData(0xe0, cgmsCommand, 3); + WaitMicroTime(2); + if ((__gp1 != 0) || (__gp2 != 0) || (__gp3 != 0) || (__gp4 != 0)) { + __gp1 = 0; + __gp2 = 0; + __gp3 = 0; + __gp4 = 0; + Vdac_Flag_Changed_816991E0 |= 2; + } + wssCommand[0] = 8; + wssCommand[1] = (__gp2 & 0xf) << 4 | __gp1 & 0xf; + wssCommand[2] = (__gp4 & 7) << 3 | __gp3 & 7; + __VISendI2CData(0xe0, wssCommand, 3); + WaitMicroTime(2); + if ((__cc1 != 0) || (__cc2 != 0) || (__cc3 != 0) || (__cc4 != 0)) { + __cc1 = 0; + __cc2 = 0; + __cc3 = 0; + __cc4 = 0; + Vdac_Flag_Changed_816991E0 |= 4; + } + captionCommand[0] = 0x7a; + captionCommand[1] = __cc1 & 0x7f; + captionCommand[2] = __cc2 & 0x7f; + captionCommand[3] = __cc3 & 0x7f; + captionCommand[4] = __cc4 & 0x7f; + __VISendI2CData(0xe0, captionCommand, 5); + WaitMicroTime(2); + for (i = 0; i < 0x1a; i++) { + acpCommand[i + 1] = VIZeroACPType[i]; + } + acpCommand[0] = 0x40; + __VISendI2CData(0xe0, acpCommand, 0x1b); + WaitMicroTime(2); + if (__level != 0) { + __level = 0; + Vdac_Flag_Changed_816991E0 |= 0x40; + } + if (Vdac_Flag_Region == 3) { + driveCommand[0] = 10; + driveCommand[1] = __level << 1 | 1; + __VISendI2CData(0xe0, driveCommand, 2); + WaitMicroTime(2); + } else { + driveCommand[0] = 10; + driveCommand[1] = 0; + __VISendI2CData(0xe0, driveCommand, 2); + WaitMicroTime(2); + } + filterCommand[0] = 3; + filterCommand[1] = 1; + __VISendI2CData(0xe0, filterCommand, 2); + WaitMicroTime(2); + __VISetGammaImm(gammaSet[10]); +} + +void __VIInit3in1(VITVMode mode) { + u8 data[2]; + s32 value = (u32)mode >> 2; + u32 flag; + + if (value == 2) { + goto region1; + } + if (value < 2) { + if (value == 0) { + goto region0; + } + if (value >= 0) { + goto region2; + } + goto regionDefault; + } + if (value == 5) { + goto region2; + } + goto regionDefault; +region0: + flag = 0; + goto send; +region1: + flag = 1; + goto send; +region2: + flag = 2; + goto send; +regionDefault: + flag = 0; +send: + Vdac_Flag_Region = flag; + data[0] = 1; + data[1] = flag; + __VISendI2CData(0xe0, data, 2); + WaitMicroTime(2); + __VISetRevolutionMode(); +} diff --git a/src/scene/memoryCard/iplMemoryCardManager.cpp b/src/scene/memoryCard/iplMemoryCardManager.cpp new file mode 100644 index 00000000..791872d2 --- /dev/null +++ b/src/scene/memoryCard/iplMemoryCardManager.cpp @@ -0,0 +1,457 @@ +#include "scene/memoryCard/iplMemoryCardManager.h" +#include "utility/iplCharacterCode.h" +#include +#include +#include +#include + +struct MemoryCardSortEntry { + u32 fileNo; + u32 flags; + s64 key; +}; + +extern "C" int iplMemoryCardManager_813CD034(const void* first, const void* second) { + const MemoryCardSortEntry* a = static_cast(first); + const MemoryCardSortEntry* b = static_cast(second); + s64 bKey = b->key; + s64 aKey = a->key; + if (bKey < aKey) { + return 1; + } + if (aKey < bKey) { + return -1; + } + u32 bIndex = b->fileNo; + u32 aIndex = a->fileNo; + if (aIndex > bIndex) { + return 1; + } + return -(int)(aIndex < bIndex); +} + +namespace ipl { +namespace scene { + +typedef memorycard::FileInfo CardDirectory[0x7f]; +typedef memorycard::IconState CardIcons[0x7f]; + +void MemoryCardManager::calc() { + memorycard::probeCard(); + update_change_cardstate(0); + update_change_cardstate(1); + update_icon_anm(); +} + +bool MemoryCardManager::isSlotReady(u8 slot) { + memorycard::CardState* states = memorycard::getCardSlotState(); + return states[slot].state == 4; +} + +bool MemoryCardManager::isSlotNoCard(u8 slot) { + memorycard::CardState* states = memorycard::getCardSlotState(); + return states[slot].state == 0; +} + +long MemoryCardManager::isSlotWrongDevice(u8 slot) { + memorycard::CardState* states = memorycard::getCardSlotState(); + int result = 1; + memorycard::CardState* state = &states[slot]; + bool isWrong = true; + bool isUnsupported = true; + bool isUnformatted = true; + bool isInvalid = true; + if (state->state != 5 && state->state != 6) { + isInvalid = false; + } + if (!isInvalid && state->state != 7) { + isUnformatted = false; + } + if (!isUnformatted && state->state != 8) { + isUnsupported = false; + } + if (!isUnsupported && state->state != 9) { + isWrong = false; + } + if (!isWrong && state->state != 10) { + result = 0; + } + return result; +} + +void MemoryCardManager::sort_file_array(u8 slot) { + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + memorycard::FileInfo* directory = &dirs[slot][0]; + for (long file = 0; file < 0x7f; file++) { + if (directory[file].fileNo != 0) { + mFile[slot][file].fileNo = file; + mFile[slot][file].sortKey = directory[file].key; + mFile[slot][file].unk_0x08 = 0; + } else { + mFile[slot][file].fileNo = file; + mFile[slot][file].sortKey = -1 - file; + mFile[slot][file].unk_0x08 = 0; + } + } + qsort(mFile[slot], 0x7f, sizeof(MCFile), iplMemoryCardManager_813CD034); +} + +void MemoryCardManager::sendCardCmdMove(u8 slot, s16 index) { + u32 file = mFile[slot][index].fileNo; + mLastCmd = 2; + if (isDistSlot(slot, NULL)) { + memorycard::sendCardMoveCmd(slot, file); + mLastResult = -0x15; + mLastCmd = 3; + } else { + mLastResult = -0x17; + } +} + +void MemoryCardManager::sendCardCmdCopy(u8 slot, s16 index) { + u32 file = mFile[slot][index].fileNo; + if (isDistSlot(slot, NULL)) { + memorycard::sendCardCopyCmd(slot, file); + mLastResult = -0x15; + mLastCmd = 2; + } else { + mLastResult = -0x17; + } +} + +void MemoryCardManager::sendCardCmdDelete(u8 slot, s16 index) { + memorycard::sendCardDeleteCmd(slot, mFile[slot][index].fileNo); + mLastResult = -0x15; + mLastCmd = 4; +} + +void MemoryCardManager::sendCardCmdFormat(u8 slot) { + memorycard::sendCardFormatCmd(slot); + mLastResult = -0x15; + mLastCmd = 1; +} + +bool MemoryCardManager::isDistSlot(u8 slot, long* state) { + u32 other = slot ^ 1; + memorycard::CardState* states = memorycard::getCardSlotState(); + if (state != NULL) { + *state = states[other].state; + } + if (states[other].state == 4) { + return true; + } + return false; +} + +bool MemoryCardManager::isMoveEnable(u8 slot, u32 index, long* code) { + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + memorycard::CardState* states = memorycard::getCardSlotState(); + bool enabled = false; + int file = mFile[slot][index].fileNo; + memorycard::FileInfo* dir = NULL; + memorycard::CardState* destination = NULL; + long result = file; + if (isDistSlot(slot, NULL) && + ((dir = &dirs[slot][file])->canMove != 0) && + states[slot].key == (destination = &states[slot ^ 1])->key && + destination->freeBlocks >= dir->size && destination->unk_0x0E != 0 && + dir->unk_0x06 == 0) { + result = -0x15; + enabled = true; + } else { + long destinationState = 0; + if (!isDistSlot(slot, &destinationState)) { + if (destinationState == 0) { + result = -0x17; + } else if (destinationState == 5 || destinationState == 7 || destinationState == 9) { + result = -0x16; + } else { + result = -0x1c; + } + } else if ((dir = &dirs[slot][file])->canMove == 0) { + result = -0x1a; + } else if (states[slot].key != (destination = &states[slot ^ 1])->key) { + result = -0x1b; + } else if (destination->freeBlocks < dir->size || destination->unk_0x0E == 0) { + result = -0x19; + } else if (dir->unk_0x06 != 0) { + result = -0x18; + } + } + if (code != NULL) { + *code = result; + } + return enabled; +} + +bool MemoryCardManager::isCopyEnable(u8 slot, u32 index, long* code) { + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + memorycard::CardState* states = memorycard::getCardSlotState(); + bool enabled = false; + int file = mFile[slot][index].fileNo; + memorycard::FileInfo* dir = NULL; + memorycard::CardState* destination = NULL; + long result = file; + if (isDistSlot(slot, NULL) && + ((dir = &dirs[slot][file])->canCopy != 0) && + states[slot].key == (destination = &states[slot ^ 1])->key && + destination->freeBlocks >= dir->size && destination->unk_0x0E != 0 && + dir->unk_0x06 == 0) { + result = -0x15; + enabled = true; + } else { + long destinationState = 0; + if (!isDistSlot(slot, &destinationState)) { + if (destinationState == 0) { + result = -0x17; + } else if (destinationState == 5 || destinationState == 7 || destinationState == 9) { + result = -0x16; + } else { + result = -0x1c; + } + } else if ((dir = &dirs[slot][file])->canCopy == 0) { + result = -0x1a; + } else if (states[slot].key != (destination = &states[slot ^ 1])->key) { + result = -0x1b; + } else if (destination->freeBlocks < dir->size || destination->unk_0x0E == 0) { + result = -0x19; + } else if (dir->unk_0x06 != 0) { + result = -0x18; + } + } + if (code != NULL) { + *code = result; + } + return enabled; +} + +bool MemoryCardManager::isIconValidate(u8 slot, s16 index) { + u32 cardSlot = slot; + if (cardSlot > 1 || index < 0 || index >= 0x7f) { + return false; + } + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + if (mFile[cardSlot][index].fileNo >= 0x7f) { + return false; + } + return dirs[cardSlot][mFile[cardSlot][index].fileNo].fileNo != 0; +} + +bool MemoryCardManager::isBannerEnable(u8 slot, s16 index) { + CardIcons* icons = reinterpret_cast(memorycard::getIconStateArray()); + u32 file = mFile[slot][index].fileNo; + memorycard::IconState* icon = &icons[slot][file]; + u8 enabled = icon->bannerEnable; + return enabled != 0; +} + +void MemoryCardManager::update_icon_anm() { + CardIcons* icons = reinterpret_cast(memorycard::getIconStateArray()); + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + int slot = 0; + do { + int index = 0; + for (int remaining = 0x7f; remaining > 0; remaining--, index++) { + u32 file = mFile[slot][index].fileNo; + if (file < 0x7f && dirs[slot][file].fileNo != 0) { + memorycard::IconState* icon = &icons[slot][file]; + s16 frame = mFileCell[slot][file].iconAnmCounter + icon->anmDelta; + mFileCell[slot][file].iconAnmCounter = frame; + if (frame >= icon->anmMax) { + if (icon->anmType == 4) { + mFileCell[slot][file].iconAnmCounter = icon->anmMax - icon->unk_0x07 - 1; + icon->anmDelta = -1; + } else { + mFileCell[slot][file].iconAnmCounter = 0; + icon->anmDelta = 1; + } + } + if (mFileCell[slot][file].iconAnmCounter < 0) { + if (icon->anmType == 4) { + mFileCell[slot][file].iconAnmCounter = icon->unk_0x06; + } else { + mFileCell[slot][file].iconAnmCounter = 0; + } + icon->anmDelta = 1; + } + } + } + slot++; + } while (slot < 2); +} + +void MemoryCardManager::update_file_array(u8 slot) { + memorycard::CardState* states = memorycard::getCardSlotState(); + states[slot].changed = 0; + sort_file_array(slot); + long command = mLastCmd; + if ((u32)(command - 1) <= 3) { + if (mLastResult == -0x15) { + mLastResult = memorycard::getCardLastCMDFCmdResult(); + } + if (mLastResult == 0) { + switch (mLastCmd) { + case 3: + mpEventHandler->onMemEvent(2, slot); + mLastCmd = 6; + break; + case 2: + mpEventHandler->onMemEvent(1, slot); + mLastCmd = 5; + break; + case 4: + mpEventHandler->onMemEvent(3, slot); + mLastCmd = 7; + break; + case 1: + mpEventHandler->onMemEvent(4, slot); + mLastCmd = 8; + break; + } + } + } +} + +void MemCardEventHandler::onMemEvent(long event, u8 slot) { +} + +void MemoryCardManager::update_change_cardstate(u8 slot) { + memorycard::CardState* states = memorycard::getCardSlotState(); + if (states[slot].changed == 1) { + if (states[slot].state == 4) { + update_file_array(slot); + } else if (states[slot].state == 8 || states[slot].state == 6) { + mpEventHandler->onMemEvent(0, slot); + states[slot].changed = 0; + } + if (mLastResult == 0 && memorycard::getCardLastSendCmd() == 9 && + slot == memorycard::getCardLastSrcSlot()) { + mpEventHandler->onMemEvent(5, slot); + states[slot].changed = 0; + for (int file = 0; file < 0x7f; file++) { + mFile[slot][file].fileNo = -1; + mFile[slot][file].sortKey = -1; + mFile[slot][file].unk_0x08 = 0; + mFileCell[slot][file].iconAnmCounter = 0; + wmemset(mFileCell[slot][file].comment[0], 0, 0x40); + } + } + } + if (states[slot].state != 4 && states[slot].state != 6 && states[slot].state != 8) { + mpEventHandler->onMemEvent(6, slot); + } +} + +GXTexObj* MemoryCardManager::create_icon(u8 slot, s16 index) { + CardIcons* icons = reinterpret_cast(memorycard::getIconStateArray()); + int total = 0; + int file = mFile[slot][index].fileNo; + s16 frame = 0; + do { + u32 shift = frame; + frame++; + total += ((icons[slot][file].anmFrameBits >> ((shift & 0x1f) << 1)) & 3) * 4; + if (mFileCell[slot][file].iconAnmCounter <= total) { + break; + } + } while (frame < 9); + return _create_icon(slot, file, frame - 1); +} + +GXTexObj* MemoryCardManager::create_icon(u8 slot, s16 index, long start) { + memorycard::getIconStateArray(); + int file = mFile[slot][index].fileNo; + return _create_icon(slot, file, 0); +} + +GXTexObj* MemoryCardManager::_create_icon(u8 slot, s16 file, long start) { + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + if (dirs[slot][file].fileNo == 0) { + return NULL; + } + CardIcons* icons = reinterpret_cast(memorycard::getIconStateArray()); + memorycard::IconState* icon = &icons[slot][file]; + if ((int)icon->iconFmt[start] == GX_TF_RGB5A3) { + GXInitTexObj(&mFileCell[slot][file].icon, reinterpret_cast(icon) + icon->iconOffset[start], 0x20, 0x20, GX_TF_RGB5A3, GX_CLAMP, GX_CLAMP, GX_FALSE); + GXLoadTexObj(&mFileCell[slot][file].icon, GX_TEXMAP0); + } else if ((int)icon->iconFmt[start] == GX_TF_C8) { + GXInitTexObjCI(&mFileCell[slot][file].icon, reinterpret_cast(icon) + icon->iconOffset[start], 0x20, 0x20, GX_TF_C8, GX_CLAMP, GX_CLAMP, GX_FALSE, GX_TLUT0); + GXInitTlutObj(&mFileCell[slot][file].iconTlut, reinterpret_cast(icon) + icon->iconTlutOffset, GX_TL_RGB5A3, 0x100); + GXLoadTlut(&mFileCell[slot][file].iconTlut, GX_TLUT0); + GXLoadTexObj(&mFileCell[slot][file].icon, GX_TEXMAP0); + } else { + return NULL; + } + return &mFileCell[slot][file].icon; +} + +const wchar_t* MemoryCardManager::getComment(u8 slot, s16 index, int which) { + int file = mFile[slot][index].fileNo; + wchar_t* dest = mFileCell[slot][file].comment[which]; + wmemset(dest, 0, 0x40); + char comment[33] = {0}; + const char* comments = memorycard::getIconComment(slot, file); + memcpy(comment, comments + which * 0x20, 0x20); + char* end = comment + 0x1f; + char* tail = end; + for (; *end == ' ' || *end == '\n' || *end == 0;) { + *tail = 0; + end--; + tail--; + } + if (SCGetLanguage() == 0) { + utility::CharacterCode::shiftJISToUTF16(dest, reinterpret_cast(comment), 0x20); + for (int pos = 0x1f; dest[pos] == 0x20 || dest[pos] == 0x3000 || dest[pos] == 10 || dest[pos] == 0; pos--) { + dest[pos] = 0; + } + } else { + utility::CharacterCode::ANSIToUTF16(dest, reinterpret_cast(comment), 0x20); + } + bool found = false; + int count = 0; + while (dest[count] != 0 && count < 0x20) { + if (found) { + dest[count] = 0; + } else if (dest[count] == 10 || dest[count] == 0xd0a) { + dest[count] = 0; + found = true; + } + count++; + } + return dest; +} + +GXTexObj* MemoryCardManager::create_banner(u8 slot, s16 index) { + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + CardIcons* icons = reinterpret_cast(memorycard::getIconStateArray()); + int file = mFile[slot][index].fileNo; + memorycard::IconState* icon = &icons[slot][file]; + if (dirs[slot][file].fileNo == 0 || !icon->bannerEnable) { + return NULL; + } + if ((int)icon->bannerType == GX_TF_RGB5A3) { + GXInitTexObj(&mFileCell[slot][file].banner, reinterpret_cast(icon) + icon->bannerOffset, + 0x60, 0x20, GX_TF_RGB5A3, GX_CLAMP, GX_CLAMP, GX_FALSE); + GXLoadTexObj(&mFileCell[slot][file].banner, GX_TEXMAP0); + } else if ((int)icon->bannerType == GX_TF_C8) { + GXInitTexObjCI(&mFileCell[slot][file].banner, reinterpret_cast(icon) + icon->bannerOffset, + 0x60, 0x20, GX_TF_C8, GX_CLAMP, GX_CLAMP, GX_FALSE, GX_TLUT0); + GXInitTlutObj(&mFileCell[slot][file].bannerTlut, reinterpret_cast(icon) + icon->bannerTlutOffset, GX_TL_RGB5A3, 0x100); + GXLoadTlut(&mFileCell[slot][file].bannerTlut, GX_TLUT0); + GXLoadTexObj(&mFileCell[slot][file].banner, GX_TEXMAP0); + } + return &mFileCell[slot][file].banner; +} + +u16 MemoryCardManager::getBlocks(u8 slot, s16 index) { + CardDirectory* dirs = reinterpret_cast(memorycard::getCardDirState()); + return dirs[slot][mFile[slot][index].fileNo].size; +} + +u16 MemoryCardManager::getFreeBlocks(u8 slot) { + memorycard::CardState* states = memorycard::getCardSlotState(); + return states[slot].freeBlocks; +} + +} +} diff --git a/tools/decomp-assist/iplMemoryCardManager.attempts.md b/tools/decomp-assist/iplMemoryCardManager.attempts.md new file mode 100644 index 00000000..7020ae30 --- /dev/null +++ b/tools/decomp-assist/iplMemoryCardManager.attempts.md @@ -0,0 +1,107 @@ +# iplMemoryCardManager attempts + +All 26 functions implemented in target object order. Shared headers unchanged. +Target and source have no .data string pool. All destination buffers and state locals are initialized. +Final retained variants combine the best readable implementations from the measured waves. + +## sort_file_array +1. Cached file-array pointer and flat directory lookup. +2. Typed directory arrays and pointer indexing. +3. Direct member indexing, removing cached file-array pointer. +4. Signed long loop counter; identical register allocation. +Measured waves 1-4, objdiff percent: 72.745094, 72.745094, 98.13725, 98.13725. +Final instruction evidence: +`src 0xcc base 0xcc insns 51/51; diffs 15: [12, 14, 17, 19, 20, 23, 24, 25, 26, 27, 29, 31, 32, 33, 38]` + + +## isMoveEnable +1. Flat directory lookup and early rejection. +2. Typed directory array and lookup inside the ready guard. +3. Success-first permission branch and initialized destination-state local. +Measured waves 1-4, objdiff percent: 71.08475, 87.78814, 91.39831, 91.39831. +Final instruction evidence: +`src 0x1e0 base 0x1d8 insns 120/118; --- insert mine 13:13 base 13:14` + + +## isCopyEnable +1. Flat directory lookup and early rejection. +2. Typed directory array and lookup inside the ready guard. +3. Success-first permission branch and initialized destination-state local. +Measured waves 1-4, objdiff percent: 71.08475, 87.78814, 91.39831, 91.39831. +Final instruction evidence: +`src 0x1e0 base 0x1d8 insns 120/118; --- insert mine 13:13 base 13:14` + + +## isBannerEnable +1. Flat icon lookup. +2. Typed slot/file icon array. +3. Named result local; identical epilogue schedule. +Measured waves 1-4, objdiff percent: 91.30769, 92.30769, 92.30769, 92.30769. +Final instruction evidence: +`src 0x68 base 0x68 insns 26/26; diffs 8: [11, 12, 13, 14, 15, 16, 17, 18]` + + +## update_icon_anm +1. Cached animation-counter pointer and ascending frame loop. +2. Direct cell-member indexing. +3. Descending frame-count loop and separately named delta. +Measured waves 1-4, objdiff percent: 87.53571, 95.15476, 96.78571, 96.78571. +Final instruction evidence: +`src 0x14c base 0x150 insns 83/84; --- replace mine 10:11 base 10:11` + + +## update_file_array +1. Direct unsigned subtraction expression compared with three. +2. Named signed command local before the unsigned subtraction; identical allocation. +3. Named unsigned command after subtraction, compared with four; worse register allocation. +Retained attempt 2. +Measured waves 1-4, objdiff percent: 99.72222, 99.72222, 99.655556, 99.72222. +Final instruction evidence: +`src 0x168 base 0x168 insns 90/90; diffs 4: [15, 16, 17, 20]` + + +## create_icon +1. Unsigned masked frame shift. +2. Signed frame counter and less-than-nine loop bound. +3. Signed unmasked shift and less-than-or-equal-eight bound. +Measured waves 1-4, objdiff percent: 82.23529, 86.84314, 86.17647, 86.84314. +Final instruction evidence: +`src 0xc8 base 0xcc insns 50/51; --- replace mine 16:18 base 16:18` + + +## _create_icon +1. Cached cell and texture pointers. +2. Direct member expressions for each texture call. +3. Branch-local texture pointer. +4. Direct member expressions and signed format comparisons. +Measured waves 1-4, objdiff percent: 46.9, 83.95, 79.15, 85.15. +Final instruction evidence: +`src 0x184 base 0x190 insns 97/100; --- replace mine 5:9 base 5:9` + + +## getComment +1. Indexed byte clear loop. +2. Pointer-increment byte clear loop. +3. Initialized local character array; closer initialization sequence. +Measured waves 1-4, objdiff percent: 77.93496, 78.097565, 79.42277, 79.42277. +Final instruction evidence: +`src 0x1c0 base 0x1ec insns 112/123; --- replace mine 0:1 base 0:1` + + +## create_banner +1. Cached cell pointer and early return. +2. Direct member texture expressions and compound rejection condition. +3. Nested success guards and named texture pointer; worse schedule. +4. Direct member expressions and signed format comparisons. +Measured waves 1-4, objdiff percent: 50.603775, 76.31132, 55.679245, 78.669815. +Final instruction evidence: +`src 0x19c base 0x1a8 insns 103/106; --- replace mine 0:1 base 0:1` + + +## getBlocks +1. Flat directory lookup and cached file number. +2. Direct typed slot/file directory lookup. +3. Named directory pointer and result local; worse epilogue schedule. +Measured waves 1-4, objdiff percent: 74.96, 92.0, 91.0, 92.0. +Final instruction evidence: +`src 0x64 base 0x64 insns 25/25; diffs 9: [11, 12, 13, 14, 15, 16, 17, 18, 19]` diff --git a/tools/decomp-assist/kbd_lib.attempts.md b/tools/decomp-assist/kbd_lib.attempts.md new file mode 100644 index 00000000..a77f98dc --- /dev/null +++ b/tools/decomp-assist/kbd_lib.attempts.md @@ -0,0 +1,66 @@ +# kbd_lib attempts + +All 21 functions implemented in object order. Shared headers unchanged. +All modifier destinations are initialized; no volatile or register declarations were added. +Pools identical. Command reservation preserves the device marker until completion. + +## kbdEventHandler +1. Compound search and unsigned report loops. +2. Byte channel search and for/break key scans. +3. Signed scan indices and byte mask. +4. Status reload after alarm; branch inversion; word mask. +Measured waves 1-6, objdiff percent: 63.822582, 65.145164, 73.053764, 73.268814, 98.81183, 99.10215. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## kbdProcKey +1. Range predicate; mapped byte local; repeat loop. +2. Explicit modifier keys; signed map loop; initialized getter destination. +3. Getter result handling with preloaded destination. +Measured waves 1-6, objdiff percent: 52.11628, 62.15116, 62.15116, 97.5, 97.5, 89.91279. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## kbdProcMod +1. Ascending cases and delta counters. +2. Object-order cases; getter/setter calls; unsigned count fields. +3. Boolean count expressions instead of word-sign masks. +Measured waves 1-6, objdiff percent: 29.371094, 29.371094, 27.867188, 81.50391, 81.50391, 75.78125. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## kbd_led_handler +1. Ternary result and callback pointer. +2. Switch result and numeric callback table. +3. Inverted if/else result. +Measured waves 1-6, objdiff percent: 22.16, 22.16, 70.0, 70.0, 70.0, 43.76. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## KBDSetLedsAsync +1. Compound free-command scan. +2. For/break scan and device reservation. +3. Guard returns, delayed channel lookup and explicit result checks. +Measured waves 1-6, objdiff percent: 60.4321, 60.4321, 60.444443, 66.03704, 66.03704, 73.02469. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## KBDSetLeds +1. Compound free-command scan. +2. For/break scan and device reservation. +3. Guard returns, delayed channel lookup and explicit result checks. +Measured waves 1-6, objdiff percent: 48.49367, 48.49367, 48.49367, 61.481014, 61.481014, 70.91139. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## KBDSetModState +1. Negative flag branch; direct scalar masks. +2. Positive flag branch; named masked value. +3. Bitfield state copy through a field pointer. +Measured waves 1-6, objdiff percent: 50.690475, 94.04762, 94.04762, 94.04762, 94.04762, 98.690475. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## KBDTranslateHidCode +1. Unsigned groups and final stateIndex. +2. Signed group and separate table selection. +3. Word mask, byte cast within each table branch. +Measured waves 1-6, objdiff percent: 79.152435, 79.152435, 82.54878, 82.54878, 82.54878, 86.29878. +Instruction diffs retained in /tmp/sol-low-kbd-final-ctx.txt. + +## Final gate discrepancy +KBDResetChannel has objdiff 100.0% and identical 176-byte instruction contents. +The gate reports 12/21 instead of 13/21 because odiff.dis treats the first CR1 conditional-branch operand as an immediate and subtracts the different symbol offsets. Both disputed branch instruction words are identical. Tools were left unchanged. diff --git a/tools/decomp-assist/vi3in1.attempts.md b/tools/decomp-assist/vi3in1.attempts.md new file mode 100644 index 00000000..056c21fc --- /dev/null +++ b/tools/decomp-assist/vi3in1.attempts.md @@ -0,0 +1,21 @@ +# vi3in1 attempts + +All 18 functions implemented in object order; first build matched 15 exactly. +String pools identical. Data 1512/1512 bytes in objdiff. + +## __VISetMacrovision +1. Indexed 26-byte copy loop, literal command byte after copy: 864/866 instructions, 94.26905%. Missing saved r28; copy pointer and bytes allocated to different registers. +2. Pointer increment loop: same 864/866 instruction diff. +3. Explicit byte assignments: 889/866 instructions, 37.678986%; excessive live byte values increased the stack frame. Rejected. +4. Structured switches and branch-local command arrays: same 864/866 diff. Retained for readability. + +## VISetMacrovision +1. Literal byte loads and separate volume update: 68/68 instructions, 7 differences, 99.48529%. Load and intermediate registers differ. +2. Reordered old-byte loads and combined OR expression: 68/68, 22 differences. Rejected. +3. Reordered declarations: 68/68, 10 differences. Rejected. +4. Separate old-byte declaration order and reversed OR operands: 68/68, 24 differences. Rejected. Retained attempt 1. + +## __VISetRevolutionModeSimple +1. Indexed ACP copy, original assignment order: 265/267 instructions, 96.82022%; saved constant register and byte-copy allocation differ. +2. Masked WSS operands and command-first caption assignment: 265/267; WSS and caption differences removed. Retained. +3. Explicit ACP byte copies: 266/267, 78.92135%; enlarged frame and save set. Rejected. -- 2.49.1