Title: The Keyboard Controller
1The Keyboard Controller
- Essential steps for implementing an interrupt
service routine for the Keyboard Controller
2Our near-term goal
- To build a mini x86 operating system
- It should be able run a Linux application
- It needs to support
- basic console input/output (keyboard, screen)
- handling of device-interrupts (timer, keyboard)
- exit gracefully when application is finished
- utilize x86 privilege restrictions and 32bit code
- Most needed building-blocks already done
3Keyboard and its controller
CPU
PIC
RAM
PIC
0x64
0x64
0x60
0x60
bus
Keyboard Controller
TIMER
output buffer
status reg
control reg
input buffer
output port
input port
KEYBOARD
PS/2 MOUSE
4KB Controller Status
6
7
5
4
3
2
1
0
PARE
TIM
AUXB
KEYL
C / D
SYSF
INPB
OUTB
LEGEND OUTB Output-Buffer Full (1yes, 0no)
INPB Input-Buffer Full (1yes, 0no) SYSF
System-Flag (1self-test successful, 0power-on
reset) C/D Command/Data was written to
(1port 0x64, 0port 0x60) KEYL Keyboard-Lock
status (1keyboard available, 0locked) AUXB
Output-Buffers data is (1for mouse, 0for
keyboard) TIM General Timeout-Error has
occurred (1yes, 0no) PARE Parity-Error on
last byte from keyboard/mouse (1yes, 0no)
5A few Controller Commands
- 0xAD Disable Keyboard
- 0xAE Enable Keyboard
- 0xA7 Disable PS/2 Mouse
- 0xA8 Enable PS/2 Mouse
- 0xC0 Read input-port (to output buffer)
- 0XD0 Read output-port (to output buffer)
6Interrupt-Handler actions
- When the keyboard-controller issues an interrupt,
these actions should be taken - Save values in the working CPU registers
- Read keyboard-controllers status-register
- If the output-buffer has new data, read it
- If a special key (shift/toggle), adjust
kb_flags - For a normal key, translate scancode to ascii
- Insert the code-pair at tail-end of kb-queue
- Send EOI-command to Interrupt Controller
- Restore the saved values from CPU registers
- Resume the interrupted procedure (with iret)
7Keyboard Queue
KBHEAD
KBTAIL
8BIOS DATA AREA
- KBFLAGS is word at address 0x400x17
- KBHEAD is word at address 0x400x1A
- KBTAIL is word at address 0x400x1C
- KBQUEUE is array of 16 words
- array base-address at 0x400x1E
- KBBASE is word at 0x400x80
- KBTAIL is word at 0x400x82
9Format of KBFLAGS LSB
0
1
2
3
4
5
6
7
Insert-mode is active (1yes, 0no)
Caps-Lock is active (1yes, 0no)
Num-Lock is active (1yes, 0no)
Scroll-Lock is active (1yes, 0no)
Alt-Key is pressed (1yes, 0no)
Ctrl-Key is pressed (1yes, 0no)
Right-Shift key is pressed (1yes, 0no)
Left-Shift key is pressed (1yes, 0no)
10Demo minikybd.s
- It shows a minimal implementation for the
keyboard-controllers interrupt-handler - It runs in Real-Mode
- It echos a users keystrokes
- IN-CLASS EXERCISE
- modify this demo to run in Protected-Mode