Repository navigation
PS/2 Keyboard Emulation - #85
Q-Bert-Reynolds wants to merge 3 commits into
Conversation
|
This seems cool, but it is a pretty big change. Can we start with the core commit by itself first? |
|
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 |
| 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, |
There was a problem hiding this comment.
shouldn't this do nothing if the keyboard is not connected?
There was a problem hiding this comment.
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.
| if (!SERIAL.transferring || SERIAL.clock != SERIAL_CLOCK_EXTERNAL || SERIAL.external_active) { | ||
| return FALSE; | ||
| } | ||
| if (!ps2_keyboard_pop_serial_byte(&e->ps2_keyboard, &SERIAL.incoming)) { |
There was a problem hiding this comment.
same here, should there be an early out if keyboard is not connected?
There was a problem hiding this comment.
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.
| /* Nothing drives the data line unless the keyboard has data. */ | ||
| if (!ps2_keyboard_pop_serial_byte(&e->ps2_keyboard, | ||
| &SERIAL.incoming)) { | ||
| SERIAL.incoming = 0xff; |
There was a problem hiding this comment.
I think incoming needs to be initialized to 0xff in other cases too to match current behavior?
There was a problem hiding this comment.
It does. ps2_keyboard_pop_serial_byte either sets incoming or returns false and it gets set to $FF.
| /* 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| /* Odd parity: the parity bit makes the total number of 1s odd. */ | ||
| u8 ones = 0; |
There was a problem hiding this comment.
This is just __builtin_parity(), fine to use that since it doesn't seem that we support msvc anyway.
There was a problem hiding this comment.
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.
| /* 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; |
There was a problem hiding this comment.
can use __builtin_bitreverse8 for this, I think
There was a problem hiding this comment.
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.
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:
Fixes:
Boolinstead ofbool(caused emscripten compile error)