Let's understand the Keyboard working through a wide angle:
Keyboard basically consists of group of thing transparent plastic layer having a lot of circuits, all made up of carbon (which can conduct current). Each circuit corresponds to a key such that when we press a key the circuit got completed and controller get to know key pressed. Below is the image of circuits in the keyboard.
From Pressing Key to Displaying It
Pressing a key on the keyboard to displaying the corresponding character on the screen. This involves several steps, from hardware interactions to software processing. We'll cover:
- Key Press: Mechanical action.
- Keyboard Hardware: Scan code generation.
- Keyboard Controller: Communication with the CPU.
- Interrupt Handling: CPU response to key press.
- Scan Code Processing: Converting scan codes to characters.
- Displaying the Character: Writing to video memory.
1. Key Press: Mechanical Action Detected by Microcontroller
When you press a key on the keyboard:
- A key switch is closed, completing an electrical circuit.
- This action is detected by the keyboard's internal microcontroller, which maps the key press to a specific scan code.
- The microcontroller in the keyboard detects which key that has been pressed by sending electrical pulses through the circuits running under the base of each key.
- Let for example:
A
key is pressed by the user.
2. Keyboard Hardware: Scan Code Generation
The keyboard's microcontroller generates a scan code corresponding to the key pressed:
- Each key has a unique scan code.
- For example, pressing the 'A' key generates a specific scan code, and releasing it generates a different scan code.
- Let the scan code for the key
A
is0x1E
.
3. Keyboard Controller: Communication with the CPU
The keyboard sends the scan code to the keyboard controller in the computer (usually part of the motherboard)
Keyboard controller interfaces a keyboard to a computer. Historically it was a separate chip (like the intel 8042), but in modern systems, it is typically integrated into the motherboard's chipset.
The keyboard controller stores the scan code in an internal buffer. This buffer allows multiple key presses to be queued up before they are read by the CPU.
Key Functions of the Keyboard Controller
- Receive Scan Codes: It receives scan codes from the keyboard for each key press and release.
- Buffer Management: It buffers these scan codes to ensure they can be read sequentially by the CPU.
- Interrupt Generation: It generates interrupts (IRQ1) to notify the CPU of key events.
- Command Handling: It processes commands sent by the CPU, such as resetting the keyboard, enabling/disabling the keyboard, and controlling keyboard LEDs.
The Keyboard controller primarily used two I/O ports for communication with the CPU:
- Data Port (0x60)
- Reading from Port 0x60: When the CPU reads from this port, it retrieves scan codes or other data sent by the keyboard.
- Writing to Port 0x60: When the CPU writes to this port, it sends commands or data to the keyboard.
- Status/Command Port (0x64)
- Reading from Port 0x64: This provides the status of the keyboard controller. Each bit in the byte read from this port has a specific meaning:
- Bit 0: Output buffer status (1 if full, data available to read from port 0x60).
- Bit 1: Input buffer status (1 if full, controller is busy).
- Bit 2: System flag (set by the BIOS, should remain set).
- Bit 3: Command/data (1 if last write to port 0x60 was a command).
- Bit 4: Keyboard locked (1 if the keyboard is locked).
- Bit 5: Auxiliary device output buffer status (1 if data is from an auxiliary device, such as a mouse).
- Bit 6: Timeout error (1 if a timeout error occurred).
- Bit 7: Parity error (1 if a parity error occurred).
- Reading from Port 0x64: This provides the status of the keyboard controller. Each bit in the byte read from this port has a specific meaning:
- Writing to Port 0x64: This sends commands to the keyboard controller.
- I/O Ports: The scan code is sent through the data port (0x60).
- Status Port: The keyboard controller uses the status port (0x64) to communicate its status (whether it’s ready to send/receive data).
4. Interrupt Handling: CPU Response to Key Press
When the keyboard controller receives a scan code:
- After storing the scan code in its buffer, the keyboard controller sends an interrupt request (IRQ1) to the CPU. This interrupt serves as a signal to the CPU that a key press event has occurred and needs attention.
- It generates an interrupt request (IRQ1 for the keyboard).
- The CPU responds to this interrupt by executing an interrupt handler. This is a special piece of code designed to process keyboard input.
- Upon receiving the IRQ1 interrupt, the CPU suspends its current operations and executes a specific interrupt service routine (ISR) associated with IRQ1. The ISR for keyboard interrupts is typically part of the operating system’s interrupt handling mechanism.
5. Scan Code Processing: Converting Scan Codes to Characters
The interrupt handler reads the scan code from the keyboard controller:
- Reading the Scan Code: The handler reads the scan code from port 0x60.
- Scan Code Translation: The scan code is translated into an ASCII character or other meaningful representation. This translation is often done using a scan code to ASCII table.
- Let the scan code
0x1E
is translated to the ASCII code forA
, which is0x41
.
6. Displaying the Character: Writing to Video Memory
Once the scan code is translated to a character, the character is displayed on the screen:
- Video Memory: On most x86 systems, text mode video memory starts at address 0xB8000.
- Writing to Memory: The character is written to video memory along with its attribute byte (which defines foreground and background color).
The keyboard in a PC communicates with the CPU through the keyboard controller, primarily using two main I/O ports: port 0x60
and port 0x64
.
I/O Ports Used by the Keyboard Controller
1. Data Port (0x60)
The data port 0x60
is used for both input and output:
Input (Reading from Port 0x60):
- When reading from this port, you get the scan codes of the keys pressed or released. A scan code is a hardware-dependent code generated by the keyboard when a key is pressed or released.
Output (Writing to Port 0x60):
- When writing to this port, you send commands or data to the keyboard.
2. Status Port (0x64)
The status port 0x64
is used to read the status of the keyboard controller. This port is used to determine the current state of the controller and whether it's ready to accept new commands or data.
Reading from Port 0x64:
The status port provides information about the keyboard controller's state. Each bit in the byte read from this port has a specific meaning:
Bit | Description |
---|---|
0 | Output buffer status: 1 if the output buffer is full (data available to read from port 0x60) |
1 | Input buffer status: 1 if the input buffer is full (controller busy processing data) |
2 | System flag: set by the BIOS and should remain set |
3 | Command/data: 1 if the last write to port 0x60 was a command, 0 if it was data |
4 | Keyboard locked: 1 if keyboard is locked (inhibited) |
5 | Auxiliary device output buffer: 1 if data is from an auxiliary device (e.g., mouse) |
6 | Timeout error: 1 if a timeout error occurred (keyboard did not respond) |
7 | Parity error: 1 if a parity error occurred (data corruption detected) |
Writing to Port 0x64:
- When writing to this port, you send commands to the keyboard controller. These commands can control various aspects of the keyboard's operation, such as enabling/disabling the keyboard or setting the LEDs.
-: Reading Pressed Key in Real Mode :-
; Function to read scan code from keyboard
read_keyboard:
in al, 0x64 ; Check status port
test al, 0x01 ; Test if output buffer is full
jz .no_key_press ; If not full, no key press
in al, 0x60 ; Read scan code from data port
jmp .key_pressed ; Process key press
.no_key_press:
jmp read_keyboard ; Poll again
.key_pressed:
; AL contains scan code, process accordingly
; Example: Display character on screen or store in buffer