diff --git a/README.md b/README.md index f7bd6703..f2e3fc9b 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ Progress | Decompiled | Matched | Linked | Data | |:---:|:---:|:---:|:---:| -| 84.90% | 75.26% | 56.76% | 86.70% | +| 85.10% | 75.39% | 56.76% | 86.72% | -units 748/1027 complete, functions 10978/12563 matched. +units 748/1027 complete, functions 10999/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/include/keyboard/tiHKBManager.h b/include/keyboard/tiHKBManager.h index 2ac12374..1f5504a1 100644 --- a/include/keyboard/tiHKBManager.h +++ b/include/keyboard/tiHKBManager.h @@ -6,9 +6,135 @@ #include #include "keyboard/tiCpData.h" +#ifdef TIHKBMANAGER_IMPLEMENTATION +#include +enum _KBDEc { KBD_EC_BUSY = 7 }; +#endif namespace textinput { namespace input { +#ifdef TIHKBMANAGER_IMPLEMENTATION + class HKBManager { + public: + class KeyState_ { + public: + void NotifyEvent(u8 down, u8 code); + void Update(); + void UpdateModState_(); + void Clear() { + held = 0; + triggered = 0; + released = 0; + repeated = 0; + current = 0; + pending = 0; + modifiers &= 0x700; + forceMask = 0; + forceState = 0; + retryLeds = 0; + } + void Initialize(u8 channel) { + held = 0; + triggered = 0; + released = 0; + repeated = 0; + current = 0; + pending = 0; + forceMask = 0; + forceState = 0; + retryLeds = 0; + device = channel; + country = 0xf; + modifiers = 0; + } + u8 device; + u32 held, triggered, released, repeated; + u32 previous, current, pending; + u32 modifiers, forceState, forceMask; + s32 country; + u8 retryLeds; + u8 previousKeys[8], currentKeys[8], pendingKeys[8]; + u32 repeatDelay[8]; + }; + class KeySet { + public: + KeySet() {} + KeySet(const HKBManager* owner, u8 kind) + : manager(owner), type(kind), index(-1), device(0), vcode(0) {} + u8 GetKey() const; + u32 GetWChar() const; + u32 GetVCode() const; + u32 IsValid() const; + bool CheckValidity() const { + if (!manager) return false; + s8 keyIndex = index; + if (keyIndex < 0 || keyIndex >= 8) return false; + u8 keyDevice = device; + if (keyDevice >= 2) return false; + switch (type) { + case 0: return (manager->states[keyDevice].held & (1 << keyIndex)) != 0; + case 1: return (manager->states[keyDevice].triggered & (1 << keyIndex)) != 0; + case 2: return (manager->states[keyDevice].released & (1 << keyIndex)) != 0; + case 3: return (manager->states[keyDevice].repeated & (1 << keyIndex)) != 0; + } + return false; + } + KeySet GetNext() const; + const HKBManager* manager; + u8 type; + s8 index; + u8 device; + mutable u16 vcode; + }; + class KBDListenerOwn; + class KBDListenerLinks { + public: + KBDListenerLinks() : previous(0), next(0) {} + virtual ~KBDListenerLinks() {} + virtual void OnAttach(KBDDevEvent*) = 0; + virtual void OnDetach(KBDDevEvent*) = 0; + virtual void OnKeyEvent(KBDKeyEvent*) = 0; + KBDListenerOwn* previous; + KBDListenerOwn* next; + }; + class KBDListenerOwn : public KBDListenerLinks { + public: + KBDListenerOwn(HKBManager* owner) : manager(owner) {} + virtual ~KBDListenerOwn(); + virtual void OnAttach(KBDDevEvent* event); + virtual void OnDetach(KBDDevEvent* event); + virtual void OnKeyEvent(KBDKeyEvent* event); + HKBManager* manager; + }; + HKBManager() __attribute__((never_inline)); + ~HKBManager(); + static HKBManager& getInstance() { return sInstance; } + void Initialize(); + void ClearState(); + u32 GetModifierState() const; + void SetCountry(u8 country); + void SetModifierState(u32 state, u32 mask); + void SetForceModifierState(u32 state, u32 mask); + void Update(); + KeySet GetTriggeredKeySet() const; + KeySet GetReleasedKeySet() const; + KeySet GetRepeatedKeySet() const; + static void SetLedCB(_KBDEc result, void* userData); + static void AttachCB(KBDDevEvent* event); + static void DetachCB(KBDDevEvent* event); + static void KeyEventCB(KBDKeyEvent* event); + private: + u8 initialized; + u8 attached[2]; + u32 lockState; + u32 retryDevices; + const wchar_t* allowedCharacters; + u32 allowedCharacterCount; + KeyState_ states[2]; + KBDListenerOwn listener; + static HKBManager sInstance; + }; +#else class HKBManager { public: #ifdef TIMANAGER_IMPLEMENTATION @@ -56,6 +182,7 @@ namespace textinput { static HKBManager sInstance; }; +#endif } // namespace input } // namespace textinput diff --git a/libs/RVL_SDK/include/revolution/kbd.h b/libs/RVL_SDK/include/revolution/kbd.h index 41aae23e..047b045c 100644 --- a/libs/RVL_SDK/include/revolution/kbd.h +++ b/libs/RVL_SDK/include/revolution/kbd.h @@ -30,11 +30,21 @@ typedef enum _KBDCountryCode { } KBDCountryCode; typedef struct _KBDKeyEvent { +#ifdef TIHKBMANAGER_IMPLEMENTATION + u8 device; + u8 key; + u32 flags; +#else u8 unk_0x00[32]; +#endif } KBDKeyEvent; typedef struct _KBDDevEvent { +#ifdef TIHKBMANAGER_IMPLEMENTATION + u8 device; +#else u8 unk_0x00[32]; +#endif } KBDDevEvent; #ifdef __cplusplus diff --git a/src/keyboard/tiHKBManager.cpp b/src/keyboard/tiHKBManager.cpp new file mode 100644 index 00000000..e858331f --- /dev/null +++ b/src/keyboard/tiHKBManager.cpp @@ -0,0 +1,549 @@ +#define TIHKBMANAGER_IMPLEMENTATION +#include "keyboard/tiHKBManager.h" +#include + +extern "C" { +_KBDEc KBDSetLedsAsync(u8, u8, void (*)(_KBDEc, void*), void*); +void KBDSetLockProcessing(u8, int); +int KBDSetCountry(u8, u32); +int KBDSetModState(u8, u32); +_KBDEc KBDSetLeds(u8, u8); +u16 KBDTranslateHidCode(u8, u32, u32); +void KBDInitRegionUS(); +void KBDInit(); +void KBDSetAttachCallback(void (*)(KBDDevEvent*)); +void KBDSetDetachCallback(void (*)(KBDDevEvent*)); +void KBDSetKeyCallback(void (*)(KBDKeyEvent*)); +} + +namespace textinput { +namespace input { + +HKBManager HKBManager::sInstance; + +// KBD callbacks encode the device in the first byte of the opaque pointer value. +union LedCallbackData { + void* pointer; + u8 device; + struct { + u8 device; + u32 leds; + } request; +}; + +void HKBManager::SetLedCB(_KBDEc result, void* userData) { + if (result == KBD_EC_BUSY) { + LedCallbackData received; + received.pointer = userData; + LedCallbackData callback; + callback.request.device = received.device; + callback.request.leds = 0; + const u8 leds = callback.request.leds; + u32 interrupts = OSDisableInterrupts(); + u32 mask = 1 << received.device; + sInstance.retryDevices &= ~mask; + OSRestoreInterrupts(interrupts); + if (KBDSetLedsAsync(received.device, leds, SetLedCB, callback.pointer) == KBD_EC_BUSY) { + interrupts = OSDisableInterrupts(); + sInstance.retryDevices |= mask; + OSRestoreInterrupts(interrupts); + } + } +} + +void HKBManager::KBDListenerOwn::OnAttach(KBDDevEvent* event) { + HKBManager* owner; + u8 device; + if (event->device >= 2) { + KBDSetLockProcessing(event->device, 0); + device = event->device; + owner = manager; + LedCallbackData callback; + callback.request.device = device; + callback.request.leds = 0; + const u8 leds = callback.request.leds; + u32 interrupts = OSDisableInterrupts(); + u32 mask = 1 << device; + owner->retryDevices &= ~mask; + OSRestoreInterrupts(interrupts); + if (KBDSetLedsAsync(device, leds, SetLedCB, callback.pointer) == KBD_EC_BUSY) { + interrupts = OSDisableInterrupts(); + owner->retryDevices |= mask; + OSRestoreInterrupts(interrupts); + } + } else { + manager->attached[event->device] = 1; + KBDSetLockProcessing(event->device, 0); + manager->states[event->device].retryLeds = 1; + } +} + +void HKBManager::KBDListenerOwn::OnDetach(KBDDevEvent* event) { + u8 device = event->device; + if (device >= 2) return; + HKBManager* owner = manager; + owner->attached[device] = 0; + owner->states[device].Clear(); +} + +void HKBManager::KBDListenerOwn::OnKeyEvent(KBDKeyEvent* event) { + if (event->device >= 2) return; + if (event->flags & 2) return; + manager->states[event->device].NotifyEvent(event->flags & 1, event->key); +} + +void HKBManager::AttachCB(KBDDevEvent* event) { + KBDListenerOwn* current = &sInstance.listener; + while (current != 0) { + current->OnAttach(event); + current = current->next; + if (current == &sInstance.listener) break; + } +} + +void HKBManager::DetachCB(KBDDevEvent* event) { + KBDListenerOwn* current = &sInstance.listener; + while (current != 0) { + current->OnDetach(event); + current = current->next; + if (current == &sInstance.listener) break; + } +} + +void HKBManager::KeyEventCB(KBDKeyEvent* event) { + KBDListenerOwn* current = &sInstance.listener; + while (current != 0) { + current->OnKeyEvent(event); + current = current->next; + if (current == &sInstance.listener) break; + } +} + +HKBManager::HKBManager() + : initialized(0), lockState(0), retryDevices(0), allowedCharacters(0), + allowedCharacterCount(0), listener(this) { + for (u32 device = 0; device < 2; device++) { + attached[device] = 0; + states[static_cast(device)].Initialize(static_cast(device)); + } +} + +HKBManager::KBDListenerOwn::~KBDListenerOwn() {} + +void HKBManager::Initialize() { + if (initialized == 0) { + initialized = 1; + KBDInitRegionUS(); + KBDInit(); + if (listener.previous == 0) { + KBDSetAttachCallback(AttachCB); + KBDSetDetachCallback(DetachCB); + KBDSetKeyCallback(KeyEventCB); + } + u32 device = 0; + do { + KBDSetCountry(static_cast(device), states[static_cast(device)].country); + device++; + } while (device < 2); + } +} + +void HKBManager::ClearState() { + for (u32 device = 0; device < 2; device++) { + states[static_cast(device)].Clear(); + } +} + +u32 HKBManager::GetModifierState() const { + u32 modifiers = states[0].modifiers; + u32 mask = states[0].forceMask; + modifiers &= ~mask; + modifiers |= states[0].forceState & mask; + u32 forceMask; + u32 force; + force = states[1].modifiers; + forceMask = states[1].forceMask; + force &= ~forceMask; + force |= states[1].forceState & forceMask; + return modifiers | force; +} +void HKBManager::SetCountry(u8 country) { + KeyState_* state; + u32 device = 0; + do { + state = &states[static_cast(device)]; + state->country = country; + KBDSetCountry(device & 0xff, country); + KBDSetModState(state->device, state->modifiers); + u32 modifiers = state->modifiers; + u8 leds = 0; + if ((modifiers & 0x100) != 0) { + leds |= 1; + } + if ((modifiers & 0x200) != 0) { + leds |= 2; + } + if ((modifiers & 0x400) != 0) { + leds |= 4; + } + if (KBDSetLeds(state->device, leds) == 7) { + state->retryLeds = 1; + } + device++; + } while (device < 2); +} + +void HKBManager::SetModifierState(u32 state, u32 mask) { + lockState = lockState & ~mask | state & mask; + KeyState_* slot; + u32 modifiers; + u32 global; + u32 device = 0; + do { + slot = &states[static_cast(device)]; + modifiers = slot->modifiers; + global = lockState & 0x700; + bool changed = global != (modifiers & 0x700); + slot->modifiers &= ~0x700; + slot->modifiers |= lockState; + if (changed && + attached[static_cast(device)] != 0) { + KBDSetModState(slot->device, slot->modifiers); + modifiers = slot->modifiers; + u8 leds = 0; + if ((modifiers & 0x100) != 0) { + leds |= 1; + } + if ((modifiers & 0x200) != 0) { + leds |= 2; + } + if ((modifiers & 0x400) != 0) { + leds |= 4; + } + if (KBDSetLeds(slot->device, leds) == 7) { + slot->retryLeds = 1; + } + } + device++; + } while (device < 2); +} + +void HKBManager::SetForceModifierState(u32 state, u32 mask) { + states[0].forceState = state; + states[0].forceMask = mask; + states[1].forceState = state; + states[1].forceMask = mask; +} + +void HKBManager::Update() { + if (initialized != 0) { + u32 mask; + u32 device = 2; + do { + u8 shiftIndex = static_cast(device); + mask = 1 << shiftIndex; + if ((retryDevices & mask) != 0) { + LedCallbackData callback; + callback.request.device = static_cast(device); + callback.request.leds = 0; + const u8 leds = callback.request.leds; + u32 interrupts = OSDisableInterrupts(); + retryDevices &= ~mask; + OSRestoreInterrupts(interrupts); + if (KBDSetLedsAsync(device & 0xff, leds, + SetLedCB, + callback.pointer) == 7) { + interrupts = OSDisableInterrupts(); + retryDevices |= mask; + OSRestoreInterrupts(interrupts); + } + } + device++; + } while (device < 4); + u32 updateIndex = 0; + do { + states[updateIndex & 0xff].Update(); + updateIndex++; + } while (updateIndex < 2); + } +} + +HKBManager::KeySet HKBManager::GetTriggeredKeySet() const { + KeySet keys; + keys.manager = this; + keys.type = 1; + keys.index = -1; + keys.device = 0; + keys.vcode = 0; + return keys.GetNext(); +} + +HKBManager::KeySet HKBManager::GetReleasedKeySet() const { + KeySet keys; + keys.manager = this; + keys.type = 2; + keys.index = -1; + keys.device = 0; + keys.vcode = 0; + return keys.GetNext(); +} + +HKBManager::KeySet HKBManager::GetRepeatedKeySet() const { + KeySet keys; + keys.manager = this; + keys.type = 3; + keys.index = -1; + keys.device = 0; + keys.vcode = 0; + return keys.GetNext(); +} + +void HKBManager::KeyState_::NotifyEvent(u8 down, u8 code) { + u32 index; + s32 found = -1; + if (down == 1) { + for (index = 0; index < 8; index++) { + if ((pending & (1 << index)) != 0) { + if (code == pendingKeys[index]) { found = index; break; } + } else { + found = index; + break; + } + } + if (found >= 0) { + pendingKeys[found] = code; + pending |= 1 << found; + } + } else { + for (index = 0; index < 8; index++) { + if ((pending & (1 << index)) && code == pendingKeys[index]) { + found = index; + break; + } + } + if (found >= 0) pending &= ~(1 << found); + } +} + +void HKBManager::KeyState_::Update() { + u32 interrupts = OSDisableInterrupts(); + previous = current; + current = pending; + previousKeys[0] = currentKeys[0]; + currentKeys[0] = pendingKeys[0]; + previousKeys[1] = currentKeys[1]; + currentKeys[1] = pendingKeys[1]; + previousKeys[2] = currentKeys[2]; + currentKeys[2] = pendingKeys[2]; + previousKeys[3] = currentKeys[3]; + currentKeys[3] = pendingKeys[3]; + previousKeys[4] = currentKeys[4]; + currentKeys[4] = pendingKeys[4]; + previousKeys[5] = currentKeys[5]; + currentKeys[5] = pendingKeys[5]; + previousKeys[6] = currentKeys[6]; + currentKeys[6] = pendingKeys[6]; + previousKeys[7] = currentKeys[7]; + currentKeys[7] = pendingKeys[7]; + OSRestoreInterrupts(interrupts); + UpdateModState_(); + u32 bit; + u32 index; + u32 heldKeys = current; + triggered = 0; + held = heldKeys; + for (index = 0; index < 8; index++) { + bit = 1 << index; + if ((current & bit) != 0) { + if ((previous & bit) == 0 || previousKeys[index] != currentKeys[index]) { + triggered |= bit; + } + } + } + released = 0; + for (index = 0; index < 8; index++) { + bit = 1 << index; + if ((previous & bit) != 0) { + if ((current & bit) == 0 || previousKeys[index] != currentKeys[index]) { + released |= bit; + } + } + } + repeated = 0; + for (index = 0; index < 8; index++) { + bit = 1 << index; + if ((current & bit) != 0) { + if ((triggered & bit) != 0) { + repeatDelay[index] = 30; + repeated |= bit; + } + repeatDelay[index]--; + if (repeatDelay[index] == 0) { + repeatDelay[index] = 8; + repeated |= bit; + } + } + } +} + +void HKBManager::KeyState_::UpdateModState_() { + u32 index; + u32 oldModifiers = modifiers; + modifiers = oldModifiers & 0xffffffd0; + index = 0; + for (; index < 8; index++) { + u32 keySet; + u8 slot; + keySet = current; + slot = index; + if ((keySet & (1 << slot)) != 0) { + u8 code = currentKeys[slot]; + if (code == 0xe5 || code == 0xe1) { + modifiers |= 2; + } else if (code == 0xe4 || code == 0xe0) { + modifiers |= 1; + } else if (code == 0xe7 || code == 0xe3) { + modifiers |= 8; + } else if (code == 0xe2) { + modifiers |= 4; + } else if (code == 0xe6) { + if (static_cast(country) == 0xf || + static_cast(country) == 0x21) { + modifiers |= 4; + } else { + modifiers |= 0x20; + } + } + } + } + if ((oldModifiers & 0x700) != (modifiers & 0x700) || retryLeds != 0) { + retryLeds = 0; + KBDSetModState(device, modifiers); + u32 modifiers = this->modifiers; + u8 leds = 0; + if ((modifiers & 0x100) != 0) { + leds |= 1; + } + if ((modifiers & 0x200) != 0) { + leds |= 2; + } + if ((modifiers & 0x400) != 0) { + leds |= 4; + } + if (KBDSetLeds(device, leds) == 7) { + retryLeds = 1; + } + } +} + +u8 HKBManager::KeySet::GetKey() const { + if (!CheckValidity()) return 0; + switch (type) { + case 0: return manager->states[device].currentKeys[index]; + case 1: return manager->states[device].currentKeys[index]; + case 2: return manager->states[device].previousKeys[index]; + case 3: return manager->states[device].currentKeys[index]; + } + return 0; +} + +u32 HKBManager::KeySet::GetWChar() const { + u32 code = GetVCode(); + if ((code & 0xffff) >= 0xf130 && (code & 0xffff) <= 0xf139) { + code = code - 0xf100 & 0xffff; + } else { + u32 lowCode = code & 0xffff; + if (lowCode == 0xf10d) { + code = 0xf1cd; + } else if (lowCode >= 0xf100 && lowCode <= 0xf13f) { + code = code - 0xf100 & 0xffff; + } else if ((code & 0xffff) >= 0xf140 && (code & 0xffff) <= 0xf17f) { + code = code + 0x40 & 0xffff; + } + } + u16 character = code; + if ((character & 0xf000) == 0xf000) { + code = 0; + } else if ((code & 0xffff) == 0xeeee) { + code = 0; + } else if ((code & 0xffff) < 0x20) { + code = 0; + } + const HKBManager* owner = manager; + if (owner->allowedCharacters == 0) return code; + u32 count = owner->allowedCharacterCount; + if (count == 0) return code; + for (u32 index = 0; index < count; index++) { + if ((code & 0xffff) == owner->allowedCharacters[index]) return code; + } + return 0; +} + +u32 HKBManager::KeySet::GetVCode() const { + u32 modifiers; + if (!CheckValidity()) return 0; + if (vcode) return vcode; + u32 first = manager->states[0].modifiers; + u32 mask = manager->states[0].forceMask; + first &= ~mask; + first |= manager->states[0].forceState & mask; + u32 secondMask; + u32 second; + second = manager->states[1].modifiers; + secondMask = manager->states[1].forceMask; + second &= ~secondMask; + second |= manager->states[1].forceState & secondMask; + modifiers = first | second; + if ((modifiers & 0xd) != 0 && (modifiers & 5) != 5) return 0; + switch (type) { + case 0: + case 1: + case 3: + if ((manager->states[device].current & (1 << index)) != 0) { + vcode = KBDTranslateHidCode(manager->states[device].currentKeys[index], modifiers, + manager->states[device].country); + } + break; + case 2: + if ((manager->states[device].previous & (1 << index)) != 0) { + vcode = KBDTranslateHidCode(manager->states[device].previousKeys[index], modifiers, + manager->states[device].country); + } + break; + } + return vcode; +} + +u32 HKBManager::KeySet::IsValid() const { + return CheckValidity(); +} + +HKBManager::KeySet HKBManager::KeySet::GetNext() const { + KeySet next(manager, type); + if (manager != 0) { + next.device = device; + while (next.device < 2) { + u32 keyFlags = 0; + switch (type) { + case 0: keyFlags = manager->states[next.device].held; break; + case 1: keyFlags = manager->states[next.device].triggered; break; + case 2: keyFlags = manager->states[next.device].released; break; + case 3: keyFlags = manager->states[next.device].repeated; break; + } + next.index = index + 1; + while (next.index < 8) { + if ((keyFlags & (1 << next.index)) != 0) return next; + next.index++; + } + next.index = -1; + next.device++; + } + } + return next; +} + + +HKBManager::~HKBManager() {} + +} +} diff --git a/tools/decomp-assist/tiHKBManager.attempts.md b/tools/decomp-assist/tiHKBManager.attempts.md new file mode 100644 index 00000000..1ea6c144 --- /dev/null +++ b/tools/decomp-assist/tiHKBManager.attempts.md @@ -0,0 +1,106 @@ +# tiHKBManager attempts + +Baseline: no source, 0/29 instruction-exact functions and 0/6116 code bytes. +The pool is empty and identical from the first object build. All fields and +calls use typed keyboard declarations. Only TIHKBMANAGER_IMPLEMENTATION selects +the added layouts; existing keyboard guard branches remain unchanged. + +## Remaining functions, in object order + +### SetLedCB +1. Initialized pointer union followed by device byte: 41/41 instructions, + six differences in stack offsets and store scheduling. +2. Typed LED request containing the device and LED mask, using its mask as + the asynchronous call argument: 42/41, one extra load and mask conversion. +3. Snapshot the request mask before disabling interrupts: 42/41, unchanged. +4. Make the snapshot const: 42/41, unchanged. Retained this readable request. + A literal mask enum reached 41/41 with zero differences, but left the + request's mask unused; that experiment was discarded. + +### KBDListenerOwn::OnAttach +1. Pointer union initialized to zero: 59/61 instructions; saved-register + allocation and callback stores differed. +2. LED request and mask argument: 60/61; allocation still differed. +3. Declare owner/device outside the branch: request with a literal mask + reached 61/61 and zero differences. +4. Read the actual request mask into a local, then make that local const: + both 62/61, an extra load and mask conversion. Retained the const snapshot + so both request fields have a real use. + +### KBDListenerOwn::OnDetach +1. Owner alias and typed Clear helper: 22/22, four register differences. +2. Declare a state pointer before clearing attached: 22/22, same four. +3. Access manager directly twice: 23/22, an additional owner load. +4. State reference before owner declaration: 22/22, six differences. +5. Owner declared before device: 22/22, six differences. Retained attempt 1. + +### HKBManager::Update +1. Zero-initialized pointer union: 55/55, two callback-store differences. +2. Typed LED request used by the asynchronous call: 55/55, one argument + conversion difference. +3. Literal LED mask enum: 55/55, zero differences, but unused request mask. +4. Snapshot the request mask, then make it const: 55/55, one conversion + difference. Retained the const snapshot with both request fields used. + +### KeyState_::Update +1. Explicit groups of two event/repeat slots: 183/183, 63 differences. +2. Ordinary loops over eight slots: 183/183, 51 differences. +3. Snapshot held keys before clearing triggers: 183/183, 47 differences. +4. Hoist shared bit/index declarations: 183/183, unchanged. +5. Cache previous/current array pointers: 247/183, rejected. +6. Reverse bit/index declaration order: 183/183, 44 differences, retained. + Registers and repeat-array address calculation still differ. + +### KeyState_::UpdateModState_ +1. Loop counter before old modifier snapshot: 104/104, seven differences. +2. Move old modifier snapshot before counter: 104/104, ten differences. +3. Use a byte slot index: 104/104, seven differences. +4. Separate slot/key-mask declarations and assignments: 104/104, unchanged. +5. Scope the counter inside for: 104/104, ten differences. Retained attempt 4. + +### KeySet::GetWChar +1. Original nested descending range tests: 70/77 instructions. +2. Ascending range branches, u32 virtual-code return, direct whitelist + indexing and early exits: 74/77. +3. Signed code temporary: 82/77, rejected. +4. Separate u16 character for the private-code mask: 75/77, retained. +5. Inline range predicate accepting u16: 89/77, rejected. + The retained version lacks two repeated narrowing instructions and uses + different whitelist-loop registers. + +### KeySet::IsValid +1. Direct guard/switch with owner alias: 70/70, 18 register differences. +2. Hoist index/device declarations before owner: 70/70, 12 differences. +3. Reverse those declaration positions: 70/70, unchanged. +4. Widen index/device temporaries: 70/70, 14 differences. +5. Inline the same CheckValidity helper used by GetKey/GetVCode: + 70/70, 12 differences, retained. Index/device registers are exchanged. + +## Exact functions + +All other functions are C++ and instruction-exact. Important successful +changes: sequential Clear stores; callback traversal compares its next node +with the owning listener; a polymorphic listener base initializes its links +before the derived vtable; NotifyEvent shares its loop index outside both +branches; SetModifierState computes its comparison before changing the field +and compares new locks against old locks; KeySet uses typed validity and +separate switch cases; GetVCode declares the second modifier mask before its +state value. The constructor uses the compiler +never_inline attribute so static initialization calls the real constructor. +Both the constructor and its static initializer are exact. A scoped pragma +experiment was rejected by the source gate and removed. Changing the +constructor loop to do/while produced 35/45 constructor and 43/18 initializer +instructions; the ordinary for loop was restored. + +## Data and files + +The singleton and compiler-generated destructor registration supply .bss. +The compiler supplies the constructor entry and listener vtable. No literal +pool, pinned addresses, hand-built vtable, or forced sections are added. +A weak abstract listener-base vtable is also compiler-generated. + +Changed files: +- src/keyboard/tiHKBManager.cpp +- include/keyboard/tiHKBManager.h +- libs/RVL_SDK/include/revolution/kbd.h +- tools/decomp-assist/tiHKBManager.attempts.md