Skip to content

PS/2 Keyboard Emulation - #85

Draft
Q-Bert-Reynolds wants to merge 3 commits into
binji:mainfrom
Q-Bert-Reynolds:main
Draft

Q-Bert-Reynolds wants to merge 3 commits into
binji:mainfrom
Q-Bert-Reynolds:main

Conversation

@Q-Bert-Reynolds

Copy link
Copy Markdown

Games like LSDJ support PS/2 keyboards over link port. This PR allows you to emulate that behavior. I built a text editor demo you can test with.

Changes:

  • PS/2 keyboard emulation over link port
  • configure joypad bindings, emulator shortcuts, and PS/2 key capture through binjgb.ini
  • binjgb.ini file works over both native and WASM builds

Fixes:

  • is_breakpoint returns Bool instead of bool (caused emscripten compile error)

@binji

binji commented Dec 2, 2026

Copy link
Copy Markdown
Owner

This seems cool, but it is a pretty big change. Can we start with the core commit by itself first?

@Q-Bert-Reynolds
Q-Bert-Reynolds marked this pull request as draft December 2, 2026 23:14
@Q-Bert-Reynolds

Copy link
Copy Markdown
Author

Sure. I'll roll this PR back to just the first commit (which reroutes input to PS/2 keyboard over link port when caps lock is on), and I'll move the binjgb.ini stuff to another PR. Moving this into draft until then.

Comment thread src/emulator.c
SERIAL.transferred_bits = 0;
if (SERIAL.clock == SERIAL_CLOCK_INTERNAL) {
/* Nothing drives the data line unless the keyboard has data. */
if (!ps2_keyboard_pop_serial_byte(&e->ps2_keyboard,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this do nothing if the keyboard is not connected?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add an explicit check to see if it's connected first. It is practically equivalent though. If it's not connected, ps2_keyboard_pop_serial_byte returns false because there are not bytes in the queue, and SERIAL.incoming gets set to $FF.

Comment thread src/emulator.c
if (!SERIAL.transferring || SERIAL.clock != SERIAL_CLOCK_EXTERNAL || SERIAL.external_active) {
return FALSE;
}
if (!ps2_keyboard_pop_serial_byte(&e->ps2_keyboard, &SERIAL.incoming)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, should there be an early out if keyboard is not connected?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the note about readability or performance? If it's about readability, I agree that adding an explicit connection check makes it more clear. If it's about performance, ps2_keyboard_pop_serial_byte just does an integer comparison to see if there are bytes in the queue. Either way, happy to add the check.

Comment thread src/emulator.c
/* Nothing drives the data line unless the keyboard has data. */
if (!ps2_keyboard_pop_serial_byte(&e->ps2_keyboard,
&SERIAL.incoming)) {
SERIAL.incoming = 0xff;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think incoming needs to be initialized to 0xff in other cases too to match current behavior?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. ps2_keyboard_pop_serial_byte either sets incoming or returns false and it gets set to $FF.

Comment thread src/ps2keyboard.c
/* Each scan code needs two queue slots. */
u32 used =
(kb->tail + PS2_KEYBOARD_QUEUE_SIZE - kb->head) % PS2_KEYBOARD_QUEUE_SIZE;
if (used + 2 > PS2_KEYBOARD_QUEUE_SIZE - 1) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit subtle, I think. It is - 1 because this queue is considered empty when head == tail, right? Might be worth mentioning that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and agreed that it's not super clear. On an unrelated note, I'm really annoyed that auto-formatting put everything after the equals sign on line 25 on another line.

Comment thread src/ps2keyboard.c
}

/* Odd parity: the parity bit makes the total number of 1s odd. */
u8 ones = 0;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just __builtin_parity(), fine to use that since it doesn't seem that we support msvc anyway.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negative. __builtin_parity() returns 0 when the number of 1s is even. The PS/2 protocol expects the parity bit to be 1 when there are an even number of 1s in the data. I supposed I could make it 1-__builtin_parity() though.

Comment thread src/ps2keyboard.c
/* The serial port shifts MSB first, so the 11-bit frame is laid out as:
* byte 0: start(0) d0 d1 d2 d3 d4 d5 d6
* byte 1: d7 parity stop(1) 0 0 0 0 0 */
u8 frame0 = 0;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use __builtin_bitreverse8 for this, I think

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can definitely use it for the scancode itself, but I'll still need to do some bit shifting. The 11 bit frame makes everything more difficult than it has to be. On the Game Boy side, I have to initiate another transfer from inside of the serial interrupt and poll for the last 3 bits. It feels really gross.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants