Seyed Masoud Hosseini · Overview · Study log · Ideas · Transcript · RSS feed

Embedded Systems, 6502 breadboard computer · Lecture 13 of 29 · 33:07

How a PS/2 Keyboard Interface Works

So how does a PS/2 keyboard interface work? on YouTube

Study guide

What this lecture covers

PS/2 keyboards send keystrokes as a simple serial bit stream over just two wires, clock and data, which makes them a practical input device to interface with a home-built computer. This lecture asks how to physically wire a PS/2 keyboard, observe its signal on an oscilloscope, and convert that serial stream into readable parallel data using shift registers and LEDs, without yet connecting it to the 6502 itself.

By the end, you can identify the PS/2 connector's four active pins, explain why the keyboard's protocol uses 11-bit packets with start, parity and stop bits, and understand how make and break scan codes (including extended and repeating codes) represent key presses, releases, and held keys.

Key ideas

  • PS/2 pinout: of the six pins, only four are used: two for 5V power and ground, and two (clock and data) for serial communication.
  • Serial-to-parallel conversion: a 74HC595 shift register reads a bit on each clock pulse and shifts previous bits along, so after eight pulses all eight bits are available in parallel.
  • Cascading shift registers: chaining the serial output of one 74HC595 into the next lets you capture more than 8 bits, which is needed since the keyboard sends 11-bit packets.
  • Clock edge mismatch: the keyboard clocks data on the falling edge, but the 74HC595 shifts on the rising edge, so an inverter is used to flip the clock polarity.
  • Latch delay: tying the shift clock and latch clock together risks latching stale data, so a pair of extra inverters adds a small propagation delay between shifting and latching.
  • Packet framing: every keyboard packet is a start bit (always 0), 8 data bits, an odd parity bit, and a stop bit (always 1), which lets a receiver sanity-check the data.
  • Scan codes, not ASCII: each key produces a scan code with no inherent relationship to ASCII; the keyboard reports physical key presses and releases, not characters or case.
  • Make and break codes: pressing a key repeats its scan code while held; releasing it sends an F0 prefix before the code, and some keys (like Home) use an E0 "extended" prefix, with a few keys like Pause and Print Screen behaving unusually.

Walkthrough

Identifying the PS/2 pins (0:00)

The lecture opens by examining the PS/2 connector, cutting a matching cable, and using a multimeter to identify which of its four wires carry ground, 5V, clock and data.

Hooking up power and viewing the signal (2:39)

After powering the keyboard, an oscilloscope is connected to the clock and data lines, revealing a burst of clock pulses with a corresponding data bit on each pulse whenever a key is pressed.

Building a shift register interface (8:18)

A 74HC595 shift register is introduced as a way to turn the serial clock/data stream into 8 parallel output bits. The keyboard's data line connects to the shift register's serial input, and its output enable and reset pins are tied appropriately.

Fixing the clock edge and latch timing (13:14)

Because the keyboard clocks on the falling edge while the 595 shifts on the rising edge, the clock signal is run through an inverter first. A pair of additional inverters delays the latch clock slightly relative to the shift clock, avoiding a race between shifting new data in and latching stale data out.

Cascading registers for the full 11-bit packet (15:26)

Counting clock pulses shows each keypress sends 11 bits, more than one shift register can hold, so a second 74HC595 is cascaded from the first, and LEDs are wired to all 11 outputs to visualize the full packet.

Decoding scan codes and checking start, parity and stop bits (17:36 and 18:46)

With the circuit powered from the keyboard's own supply, pressing keys lights up different LED patterns that match published scan code tables (e.g. 2E for 5, 16 for 1). The lecture explains the surrounding start bit, odd parity bit and stop bit that frame each 8-bit code and allow basic error detection.

Extended and multi-byte scan codes (21:55)

Additional cascaded shift registers capture keys like Home that send 16-bit (E0-prefixed) codes, and reveal that key releases send their own code prefixed with F0, with extended keys combining both prefixes.

Make/break behavior and unusual keys (23:46)

Holding a key shows its scan code repeating; releasing it sends the break code. The lecture demonstrates that capital letters are sent as separate Shift and letter press/release events (the keyboard has no concept of "capital"), and notes that Pause and Print Screen behave unusually, with Pause having no break code at all.

Reflecting on interface design options (30:16)

The video closes by reflecting that this shift-register buffer of raw scan codes is a reasonable, if elaborate, way to interface a keyboard, and contrasts it with alternatives like interrupt-per-scan-code or a microcontroller that translates scan codes to ASCII before handing them to the CPU.

Before you watch

  • Familiarity with shift registers (such as the 74HC595) and how serial data becomes parallel output is helpful before this lecture.
  • No prior 6502 assembly is needed for this video, since it focuses purely on the keyboard's hardware protocol; the software side is covered in later videos in this series.

Check your understanding

  1. Why does converting the keyboard's serial signal to parallel output require inverting the clock signal before feeding it to the shift register?
  2. What are the start bit, parity bit and stop bit used for in a PS/2 keyboard packet, and what values do the start and stop bits always take?
  3. How does the keyboard distinguish a key being pressed from a key being released in its scan code stream?
  4. Why do some keys, like Home, require more than 8 bits to represent, and how is that reflected in the circuit built in this lecture?
  5. Why doesn't the keyboard send any notion of "capital A" directly, and what does it send instead?

Chapters

From the YouTube description

Check out https://eater.net for more computer engineering stuff!
List of scancodes from the video: https://docs.google.com/document/d/1w--ouGvSSf93tFltmBcSRGMN8Rte7ayalQZRDPG0ezk/edit

Support these videos on Patreon: https://www.patreon.com/beneater or https://eater.net/support for other ways to support.

------------------

Social media:
Website: https://www.eater.net
Twitter: https://x.com/beneater
Patreon: https://patreon.com/beneater
Reddit: https://www.reddit.com/r/beneater

Special thanks to these supporters for making this video possible:
Aleksey Smolenchuk, Alexander Wendland, Andrew C. Young, Anson VanDoren, Anthanasius, Armin Brauns, Ben Cochran, Ben Dyson, Ben Kamens, Ben Williams, Bill Cooksey, Binh Tran, Bouke Groenescheij, Bradley Pirtle, Bradley Stach, Brent Reusing, Brian T Hoover, Bryan Brickman, Bryan Glezerson, CaffeinatedClockmaker, Carlos Ambrozak, Christopher Blackmon, Dale Andrew Darling, Daniel Jeppsson, Daniel Tang, Dave Burley, Dave Walter, David Brown, David Clark, David Cox, David House, David Sastre Medina, David Turner, David Worsham, Dean Winger, Dissy, dko, Dmitry Guyvoronsky, dstensnes, Dušan Dželebdžić, Dzevad Trumic, Emilio Mendoza, Eric Busalacchi, Eric Dynowski, Erik Broeders, Eugene Bulkin, George Foot, George Miroshnykov, Harry McDow, Ingo Eble, Ivan Sorokin, james schaefer, Jared Dziedzic, Jason DeStefano, Jason Specland, JavaXP, Jay Binks, Jayne Gabriele, Jeffery Springs, Jeremy A., Jim Kelly, Jim Knowler, Jim Van Meggelen, Joe Beda, Joe OConnor, Joe Pregracke, Joel Jakobsson, Joel Miller, John Fenwick, John Meade, Jon Dugan, Joseph Portaro, Joshua King, Kai Wells, Kefen, Kenneth Christensen, Kent Collins, Koreo, Lambda GPU Workstations, Larry, László Bácsi, Lukasz Pacholik, Marcus Classon, Martin Roth, Mats Fredriksson, Matt Krueger, Matthäus Pawelczyk, melvin2001, Michael Garland, Michael Tedder, Michael Timbrook, Michael Weitman, Miguel Ríos, mikebad, Mikel Lindsaar, Miles Macchiaroli, moi n, Nicholas Counts, Nicholas Moresco, Nick, Örn Arnarson, Paul Pluzhnikov, Paul Randal, Pete Dietl, Phil Dennis, Philip Hofstetter, Phillip Glau, PixelSergey, ProgrammerDor, Randal Masutani, Randy True, Raoul van Prooijen, Renaldas Zioma, Ric King, Rob Bruno, Robert Comyn, Robert Diaz, Roland Bobek, sam raza, Sergey Kruk, SonOfSofaman, Stefan Nesinger, Stefanus Du Toit, Stephen, Stephen Riley, Stephen Smithstone, Steve Jones, Tayler Porter, TheWebMachine, Tom, Tyler Latham, Vincent Bernat, Vladimir Solomatin, Walter Montalvo, Warren Miller, xisente, Yee Lam Wan, דויד אללוף

← Interrupt Handling with the 6522 VIA · Keyboard Interface Hardware: Triggering Interrupts →