Compare commits

...

6 Commits

Author SHA1 Message Date
Devin AI
93d52e5a4a docs: kbd_lib wave-7 attempt notes
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 23:47:57 +00:00
Devin AI
d4adb13616 kbd_led_handler: switch+shared-call form (61.12 fuzzy, 23/25 insns)
default-first switch is MWCC's only non-fold err-select form; residual =
beq->case dispatch vs orig bne->default + prologue/cb-arg scheduling.
dup-call variant scored worse (40.0) despite 24/25 insn count.

Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 23:47:44 +00:00
Devin AI
9c08e34691 kbd: kbdProcMod count-equal, dual-induction cmdBuf loop, led_handler bne dispatch
- kbdProcMod: kbdData[channel] idiom + stateBuf state ptr reaches 256/256 insn
  count-equal (residual: MWCC promotes state.value to reg vs orig per-case
  reloads from 8(r1); rlwimi fused shift vs orig separate rlwinm)
- KBDSetLeds: dual index+ofs induction -> 79/79 count-equal (orig still picks
  lwzx base,ofs load vs ptr-walk, channel->r31 callee remat)
- KBDSetLedsAsync: same dual-induction -> 80/81 (channel->r26 callee save
  missing; orig remats mulli channel,0x268 per kbdData use)
- kbd_led_handler: duplicated-call if/else -> 24/25, correct bne->else
  dispatch; orig merges both indirect calls into one bctr (MWCC does not
  tail-merge here)

Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 23:44:09 +00:00
Devin AI
d21b8f6d43 kbd_lib: led_handler branchy err-select, cmplw read-back, KBDSetLeds* loop fixes
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 23:14:11 +00:00
Devin AI
463ac7e97d kbd_lib: kbdProcKey exact; led_handler switch form; SetLeds* fixes
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 23:07:42 +00:00
Devin AI
8142fbb5eb kbd_lib_map_us: flip to Matching (worker 8b595e4b)
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 22:40:28 +00:00
3 changed files with 134 additions and 112 deletions

View File

@@ -1887,7 +1887,7 @@ config.libs = [
RVLSDKLib("kbd", [
Object(NonMatching, "kbd/kbd_lib.c"),
Object(NonMatching, "kbd/kbd_lib_map_jp.c"),
Object(NonMatching, "kbd/kbd_lib_map_us.c"),
Object(Matching, "kbd/kbd_lib_map_us.c"),
Object(NonMatching, "kbd/kbd_lib_map_eu.c"),
]
),

View File

@@ -69,17 +69,17 @@ typedef struct {
} KBDChannel;
typedef struct {
u32 callbackAddress;
USBKBDCmdLEDCallback 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);
static USBKBDDetachCallback kbdDevDetachCallback;
static USBKBDAttachCallback kbdDevAttachCallback;
static BOOL kbdInitialized;
KBDKeyMapTable kbdKeyMaps = {0};
typedef struct {
@@ -346,7 +346,6 @@ static void kbdProcKey(u32 key, u32 pressed, u32 channel) {
event.channel = channel;
event.state = pressed;
event.modifiers = 0;
kbdGetModState(channel, &event.modifiers);
map = &kbdKeyMaps.maps[data->country];
keyCount = map->keypadCount;
@@ -401,131 +400,132 @@ static void kbdProcKey(u32 key, u32 pressed, u32 channel) {
}
static void kbdProcMod(u32 key, u32 pressed, u32 channel) {
KBDChannel* data;
u32 stateBuf[1];
KBDModifierState* state;
s32 delta;
u8 flags;
KBDModifierState state;
u32 finalState;
s8 value;
data = &kbdData[channel];
delta = (pressed & 1) != 0 ? 1 : -1;
flags = kbdKeyMaps.maps[data->country].flags;
state.value = data->modState;
state = (KBDModifierState*)stateBuf;
state->value = kbdData[channel].modState;
flags = kbdKeyMaps.maps[kbdData[channel].country].flags;
kbdGetModState(channel, &state.value);
kbdGetModState(channel, &state->value);
switch (key) {
case 0xF001:
if (data->lockState != 0) {
if (kbdData[channel].lockState != 0) {
if ((pressed & 1) != 0) {
state.value ^= 2;
state->value ^= 2;
}
} else {
value = data->lockCount[1] + delta;
data->lockCount[1] = value;
state.keys.rightControl = (u8)value != 0;
value = kbdData[channel].lockCount[1] + delta;
kbdData[channel].lockCount[1] = value;
state->keys.rightControl = (u8)value != 0;
}
break;
case 0xF005:
if (data->lockState != 0) {
if (kbdData[channel].lockState != 0) {
if ((pressed & 1) != 0) {
state.value ^= 0x20;
state->value ^= 0x20;
}
} else {
value = data->lockCount[4] + delta;
data->lockCount[4] = value;
state.keys.rightAlt = (u8)value != 0;
value = kbdData[channel].lockCount[4] + delta;
kbdData[channel].lockCount[4] = value;
state->keys.rightAlt = (u8)value != 0;
}
break;
case 0xF000:
if (data->lockState != 0) {
if (kbdData[channel].lockState != 0) {
if ((pressed & 1) != 0) {
state.value ^= 1;
state->value ^= 1;
}
} else {
value = data->lockCount[0] + delta;
data->lockCount[0] = value;
state.keys.leftControl = (u8)value != 0;
value = kbdData[channel].lockCount[0] + delta;
kbdData[channel].lockCount[0] = value;
state->keys.leftControl = (u8)value != 0;
}
break;
case 0xF008:
if (((pressed & 1) != 0) && (data->lockProcessing != 0)) {
state.value ^= 0x200;
if (((pressed & 1) != 0) && (kbdData[channel].lockProcessing != 0)) {
state->value ^= 0x200;
}
break;
case 0xF007:
if (((pressed & 1) != 0) && (data->lockProcessing != 0)) {
state.value ^= 0x100;
if (((pressed & 1) != 0) && (kbdData[channel].lockProcessing != 0)) {
state->value ^= 0x100;
}
break;
case 0xF006:
if (((pressed & 1) != 0) && (data->lockProcessing != 0)) {
if (((pressed & 1) != 0) && (kbdData[channel].lockProcessing != 0)) {
u32 oldState;
u32 lockState;
oldState = state.value;
oldState = state->value;
lockState = oldState & 0xC0;
switch (lockState) {
case 0:
if ((flags & 1) == 1) {
state.value = oldState | 0x40;
state->value = oldState | 0x40;
}
break;
case 0x40:
{
state.value = oldState & 0xFFFFFFBF;
state->value = oldState & 0xFFFFFFBF;
if ((flags & 4) == 4) {
state.value = oldState & 0xFFFFFFBF | 0x80;
state->value = oldState & 0xFFFFFFBF | 0x80;
}
}
break;
case 0x80:
state.value = oldState & 0xFFFFFF7F;
state->value = oldState & 0xFFFFFF7F;
break;
case 0xC0:
state.value = oldState & 0xFFFFFF3F;
state->value = oldState & 0xFFFFFF3F;
break;
}
}
break;
case 0xF002:
if (data->lockState != 0) {
if (kbdData[channel].lockState != 0) {
if ((pressed & 1) != 0) {
state.value ^= 4;
state->value ^= 4;
}
} else {
value = data->lockCount[2] + delta;
data->lockCount[2] = value;
state.keys.leftShift = (u8)value != 0;
value = kbdData[channel].lockCount[2] + delta;
kbdData[channel].lockCount[2] = value;
state->keys.leftShift = (u8)value != 0;
}
break;
case 0xF003:
if (data->lockState != 0) {
if (kbdData[channel].lockState != 0) {
if ((pressed & 1) != 0) {
state.value ^= 8;
state->value ^= 8;
}
} else {
value = data->lockCount[3] + delta;
data->lockCount[3] = value;
state.keys.rightShift = (u8)value != 0;
value = kbdData[channel].lockCount[3] + delta;
kbdData[channel].lockCount[3] = value;
state->keys.rightShift = (u8)value != 0;
}
break;
case 0xF004:
if (data->lockState != 0) {
if (kbdData[channel].lockState != 0) {
if ((pressed & 1) != 0) {
state.value ^= 0x10;
state->value ^= 0x10;
}
} else {
value = data->lockCount[5] + delta;
data->lockCount[5] = value;
state.keys.leftAlt = (u8)value != 0;
value = kbdData[channel].lockCount[5] + delta;
kbdData[channel].lockCount[5] = value;
state->keys.leftAlt = (u8)value != 0;
}
break;
case 0xF021:
if (((pressed & 1) != 0) && (data->lockProcessing != 0)) {
state.value ^= 0x400;
if (((pressed & 1) != 0) && (kbdData[channel].lockProcessing != 0)) {
state->value ^= 0x400;
}
break;
}
finalState = state.value | 0x1000;
finalState = state->value | 0x1000;
KBDSetModState(channel, finalState);
}
@@ -564,28 +564,26 @@ static inline u32 kbdChannelFlags(u32 channel) {
static void kbd_led_handler(BOOL success, void* callbackArg) {
u32 index;
u32 result;
u32 callbackAddress;
u32 err;
index = (u32)callbackArg;
callbackAddress = kbdLCBuf[index].callbackAddress;
kbdCmdBuf[index].device = 0;
if (callbackAddress == 0) {
if (kbdLCBuf[index].callbackAddress == (USBKBDCmdLEDCallback)(u32)kbdCmdBuf[index].device) {
return;
}
switch (success) {
case TRUE:
result = 0;
break;
default:
result = 7;
err = 7;
break;
case TRUE:
err = 0;
break;
}
((USBKBDCmdLEDCallback)kbdLCBuf[index].callbackAddress)(result, kbdLCBuf[index].callbackArg);
kbdLCBuf[index].callbackAddress(err, kbdLCBuf[index].callbackArg);
}
USBKBDErr KBDSetLedsAsync(u32 channel, u32 leds, USBKBDCmdLEDCallback callback, void* callbackArg) {
u32 index;
s32 index;
u32 ofs;
u8 ledBits;
BOOL interrupts;
USBKBDErr result;
@@ -595,62 +593,66 @@ USBKBDErr KBDSetLedsAsync(u32 channel, u32 leds, USBKBDCmdLEDCallback callback,
if (channel >= 4) {
return 4;
}
if ((s32)kbdData[(u8)channel].flags == 1 || (s32)kbdData[(u8)channel].flags == 4) {
return 5;
}
ledBits = leds;
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, ledBits,
(USBKBDCmdLEDAsync*)&kbdCmdBuf[index],
kbd_led_handler, (void*)index);
switch (result) {
case 0:
return 0;
default:
return 7;
}
}
USBKBDErr KBDSetLeds(u32 channel, u32 leds) {
u32 index;
BOOL interrupts;
USBKBDErr result;
if (!kbdInitialized) {
return 2;
}
if (channel >= 4) {
return 4;
}
if ((s32)kbdChannelFlags(channel) == 1 || (s32)kbdChannelFlags(channel) == 4) {
return 5;
}
leds &= 0xff;
ledBits = leds;
interrupts = OSDisableInterrupts();
index = 0;
while (index < 12) {
if ((kbdCmdBuf + index)->device == 0) {
kbdCmdBuf[index].device = (u32)kbdData[channel].device;
for (index = 0, ofs = 0; index < 12; index++, ofs += sizeof(KBDLEDCommand)) {
if (*(u32*)&((u8*)kbdCmdBuf)[ofs] == 0) {
*(u32*)&((u8*)kbdCmdBuf)[ofs] = (u32)kbdData[channel].device;
break;
}
index++;
}
OSRestoreInterrupts(interrupts);
if (index == 12) {
return 7;
}
result = USBKBDSetLED((u32)kbdData[channel].device, leds,
kbdLCBuf[index].callbackAddress = callback;
kbdLCBuf[index].callbackArg = callbackArg;
result = USBKBDSetLEDAsync((u32)kbdData[channel].device, ledBits,
(USBKBDCmdLEDAsync*)&kbdCmdBuf[index],
kbd_led_handler, (void*)index);
switch (result) {
default:
return 7;
case 0:
return 0;
}
}
USBKBDErr KBDSetLeds(u32 channel, u32 leds) {
u32 index;
u32 ofs;
BOOL interrupts;
USBKBDErr result;
u8 ledBits;
if (!kbdInitialized) {
return 2;
}
if (channel >= 4) {
return 4;
}
if ((s32)kbdChannelFlags(channel) == 1 || (s32)kbdChannelFlags(channel) == 4) {
return 5;
}
ledBits = leds & 0xff;
interrupts = OSDisableInterrupts();
index = 0;
ofs = 0;
while (index < 12) {
if (*(u32*)&((u8*)kbdCmdBuf)[ofs] == 0) {
*(u32*)&((u8*)kbdCmdBuf)[ofs] = (u32)kbdData[channel].device;
break;
}
index++;
ofs += sizeof(KBDLEDCommand);
}
OSRestoreInterrupts(interrupts);
if (index == 12) {
return 7;
}
result = USBKBDSetLED((u32)kbdData[channel].device, ledBits,
(USBKBDCmdLED*)&kbdCmdBuf[index]);
kbdCmdBuf[index].device = 0;
switch (result) {

View File

@@ -207,3 +207,23 @@ Target blocks were re-derived from kbd_lib.s: LED checks precede interrupt exclu
kbdProcKey retains initialization of the getter destination. kbdProcMod retains initialization from channel state. The original getter may skip its output on invalid input; matching its missing initialization would introduce uninitialized reads and was rejected. Modifier counter cases were re-derived as separate counter, whole-word mask, and lock-toggle blocks.
Data: .bss, .sbss and .sdata remain exact; the real map, command, channel, and callback workspaces retain original order. The first 0x490 data bytes, including the SDK version literal and both maps, match raw target bytes. The remaining .data differences are the compiler-generated switch table; the version text has an anonymous source symbol and a target lbl symbol, which is not recreated. No packed pool, manual jump table, address pins or section directives were introduced.
KBDResetChannel remains objdiff 100% with identical raw instruction bytes. Its gate count discrepancy remains the documented CR1 branch-disassembly normalization issue; no tooling was modified.
## Wave 7 (continuation)
- kbdProcMod: reached 256/256 count-equal via `KBDModifierState* state` over
`u32 stateBuf[1]` + `kbdData[channel].modState` idiom. Residual: MWCC
promotes state.value to a register; orig reloads from 8(r1) per case arm
and splits rlwinm-shift/rlwimi-insert. No source lever found.
- KBDSetLeds/Async: dual `index`+`ofs` induction in the cmdBuf scan loop is
the right shape (orig keeps index for post-loop LCBuf access AND a byte
offset for lwzx). KBDSetLeds 79/79 count-equal; Async 80/81 (missing
channel->r26 callee save; orig remats `mulli channel,0x268` per kbdData
access). `u8* cmdBytes` named-base variant regressed (78/79).
- kbd_led_handler: if/else+shared call (22/25, folded), dup-call arms
(24/25, fuzzy 40), `default:7;case TRUE:0` switch+shared call = best fuzzy
61.12 at 23/25 insns — switch is MWCC's only non-fold err-select; orig's
`bne->err7 / li 0 first` arm order is unreachable (MWCC dispatches
beq->case, never bne->default). goto explicitly folds.
- .sdata: orig has +4B zero entry after __KBDVersion — unnamed, unreferenced,
no relocs, no .text reads. Blind `=0` static is banned filler. Leftover.
- kbdEventHandler/KBDSetModState/KBDTranslateHidCode: insn-count-equal
residual regname/sched swaps; probed variants regressed.