Skip to content

Commit fc1c53c

Browse files
committed
update kbd
1 parent ca313e3 commit fc1c53c

22 files changed

+491
-402
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
Table of Contents
22

3-
- [VGA Rainbow](#vgarainbow) - Display *Hello World!* in all colors provided by VGA text mode.
3+
- [Keyboard on Interrupts](#keyboard-on-interrupts) - Keyboard controller on x86 with GDT, IDT and IRQ.
4+
- [VGA Rainbow](#vga-rainbow) - Display *Hello World!* in all colors provided by VGA text mode.
45

56

7+
## Keyboard on Interrupts
8+
9+
KBD is a continuation of my bare-metal journey. Thanks to this [stackoverflow answer](https://stackoverflow.com/a/37635449) I was able to successfully recreate a keyboard controller on interrupts.
10+
11+
![Keyboard on Interrupts](./pub/kbd.gif)
12+
13+
Most of the program is done in [boot.asm](./kbd/boot.asm), where I initialize GDT, and in [idt.h](./kbd/idt/idt.h), where I use the interrupt controller. A little dependency injection was made in the `console.c`, as the `console_input()` is actually passed as a function to be called after a key is pressed.
14+
615
## VGA Rainbow
716

817
VGA Rainbow is a small bare-metal program created to display *Hello World!* in all colors provided by [VGA text mode](https://en.wikipedia.org/wiki/VGA_text_mode).

kbd/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_build
2+
serial.log

kbd/Makefile

+15-14
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@ LD = ld
33
CFLAGS = -ffreestanding -nostdlib -nodefaultlibs -no-pie -fno-pic -static -fno-stack-protector -O2 -m32 -Iinclude
44
LDFLAGS = -m elf_i386
55

6-
kernel := build/kernel.bin
7-
iso := build/hello.iso
6+
kernel := _build/kernel.bin
7+
iso := _build/hello.iso
88

99
linker_script := linker.ld
1010
grub_cfg := grub.cfg
11-
assembly_source_files := multiboot_header.asm boot.asm idt.asm
12-
assembly_object_files := $(patsubst %.asm, build/%.o, $(assembly_source_files))
13-
c_source_files := $(wildcard *.c)
14-
c_object_files := $(patsubst %.c, build/%.o, $(c_source_files))
11+
assembly_source_files := multiboot_header.asm boot.asm $(wildcard asm/*.asm)
12+
assembly_object_files := $(patsubst %.asm, _build/%.o, $(assembly_source_files))
13+
c_source_dirs := $(shell find . -type d)
14+
c_source_files := $(foreach dir, $(c_source_dirs), $(wildcard $(dir)/*.c))
15+
c_object_files := $(patsubst %.c, _build/%.o, $(c_source_files))
1516

1617
.PHONY: all clean iso kernel qemu qemu-gdb
1718

1819
all: $(kernel)
1920

2021
clean:
21-
- @rm -fr build
22+
- @rm -fr _build
2223
- @rm -f serial.log *.o
23-
- @rm -fr build/isofiles
24+
- @rm -fr _build/isofiles
2425

2526
qemu: $(iso)
2627
qemu-system-x86_64 -cdrom $(iso) -vga std -s -serial file:serial.log
@@ -32,21 +33,21 @@ iso: $(iso)
3233
@echo "Done"
3334

3435
$(iso): $(kernel) $(grub_cfg)
35-
mkdir -p build/isofiles/boot/grub
36-
cp $(kernel) build/isofiles/boot/kernel.bin
37-
cp $(grub_cfg) build/isofiles/boot/grub
38-
grub-mkrescue -o $(iso) build/isofiles #2> /dev/null
36+
mkdir -p _build/isofiles/boot/grub
37+
cp $(kernel) _build/isofiles/boot/kernel.bin
38+
cp $(grub_cfg) _build/isofiles/boot/grub
39+
grub-mkrescue -o $(iso) _build/isofiles #2> /dev/null
3940

4041
$(kernel): $(c_object_files) $(assembly_object_files) $(linker_script)
4142
echo $(c_object_files)
4243
ld $(LDFLAGS) -T $(linker_script) -o $(kernel) $(assembly_object_files) $(c_object_files)
4344

4445

45-
build/%.o: %.c
46+
_build/%.o: %.c
4647
@mkdir -p $(shell dirname $@)
4748
$(CC) $(CFLAGS) -c $< -o $@
4849

4950
# compile assembly files
50-
build/%.o: %.asm
51+
_build/%.o: %.asm
5152
@mkdir -p $(shell dirname $@)
5253
nasm -felf32 $< -o $@

kbd/asm/asm.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef BASE_ASM_H
2+
#define BASE_ASM_H
3+
4+
// idt.asm
5+
extern void keyboard_handler_interrupt(void);
6+
extern unsigned char read_port(int port);
7+
extern void write_port(int port, unsigned char val);
8+
9+
#endif // BASE_ASM_H

kbd/idt.asm renamed to kbd/asm/idt.asm

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
section .text
2-
31
extern keyboard_handler
4-
global read_port
5-
global write_port
2+
63
global load_idt
7-
global keyboard_handler_int
84

9-
keyboard_handler_int:
10-
pushad
11-
cld
12-
call keyboard_handler
13-
popad
14-
iretd
5+
global keyboard_handler_interrupt
6+
global read_port
7+
global write_port
158

169
load_idt:
1710
mov edx, [esp + 4]
1811
lidt [edx]
1912
sti
2013
ret
2114

22-
; arg: int, port number.
15+
keyboard_handler_interrupt:
16+
pushad
17+
cld
18+
call keyboard_handler
19+
popad
20+
iretd
21+
2322
read_port:
2423
mov edx, [esp + 4]
2524
in al, dx
2625
ret
2726

28-
; arg: int, (dx)port number
29-
; int, (al)value to write
3027
write_port:
3128
mov edx, [esp + 4]
3229
mov al, [esp + 4 + 4]

kbd/base/itoa.h

-27
This file was deleted.

kbd/base/kbd.h

-90
This file was deleted.

kbd/base/scr.h

-31
This file was deleted.

kbd/base/time.h

-14
This file was deleted.

kbd/base/virtscr.h

-84
This file was deleted.

0 commit comments

Comments
 (0)