Skip to content

Commit

Permalink
Keyboard driver and fancy screens (#6)
Browse files Browse the repository at this point in the history
* Keyboard basic input handling, basic screens

* Cleaner screen handling

* Use VNC by default with QEMU

* Use pointer on functions to define screens

* Add current commit to homepage

* Silly box, improved vga api, improved headers

* CRTC VGA cursor BS

* Add colors test, fix cursor, no infinite buff cycling
  • Loading branch information
selfsigned authored Mar 20, 2023
1 parent 01848b4 commit d9c746b
Show file tree
Hide file tree
Showing 23 changed files with 729 additions and 95 deletions.
1 change: 1 addition & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"EditorConfig.EditorConfig",
"13xforever.language-x86-64-assembly",
"zixuanwang.linkerscript",
"king2021.vnc-extension",
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"editor.defaultFormatter": "ms-vscode.cpptools",
"editor.formatOnSave": true
},
"remote.extensionKind": {
"king2021.vnc-extension": [
"ui"
]
},
"editor.insertSpaces": true,
"editor.tabSize": 2,
"terminal.integrated.macOptionIsMeta": true
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
"label": "Run QEMU",
"type": "shell",
"command": "make use_docker && echo Starting QEMU& docker run -it --platform linux/amd64 --rm -p 4666:4666 -v ${workspaceFolder}:/build ghcr.io/l-aurelie/i686-cc:latest make run",
"command": "make use_docker && echo Starting QEMU& docker run -it --platform linux/amd64 --rm -p 4666:4666 -p 5999:5999 -v ${workspaceFolder}:/build ghcr.io/l-aurelie/i686-cc:latest make run",
"isBackground": true,
"problemMatcher": {
"pattern": {
Expand Down
29 changes: 20 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ HAS_CC := $(shell $(CC) --version 2>/dev/null)
SIGNATURE_ADDRESS=0x0030DEAD
SIGNATURE_VALUE=0x00DEAD42 # !! NEEDS TO BE A WORD FOR THE TEST TO WORK !!

# git version
GIT_VERSION := "$(shell git describe --abbrev=4 --dirty --always --tags)"

CPPFLAGS += \
-MMD \
-D SIGNATURE_ADDRESS=$(SIGNATURE_ADDRESS) \
-D SIGNATURE_VALUE=$(SIGNATURE_VALUE)
-D SIGNATURE_VALUE=$(SIGNATURE_VALUE) \
-D VERSION=\"$(GIT_VERSION)\"

DBGFLAGS := \
-g \
Expand Down Expand Up @@ -61,8 +65,10 @@ ifneq (,$(wildcard /.dockerenv))
INSIDE_DOCKER := true
endif

# gbb
GDB_PORT := 4666
#qemu
VNC_DISPLAY := 99
GDB_PORT := 4666
QEMU_RAM := 4M

# sources
SRC_PATH := src
Expand All @@ -80,7 +86,7 @@ ERROR_CC = @$(error "[ERROR] $(CC) not found, either run `make use_docker $(MAK
ERROR_GRUB = @$(error "[ERROR] grub, xorriso or grub*-bios missing, either run `make use_docker $(MAKECMDGOALS)` or run: $(DOCKER_CMD)")

## Rulez ##
.PHONY: all use_docker run gdb test clean fclean re
.PHONY: all use_docker run run-curses gdb test clean fclean re

all: $(NAME) $(IMG_NAME)

Expand Down Expand Up @@ -130,17 +136,22 @@ else
endif

run: $(IMG_NAME)
@echo "[INFO] Qemu is running a gdb server at $(GDB_PORT)"
qemu-system-i386 -boot d -cdrom $(IMG_NAME) \
-m 4M \
-display curses \
@echo "[INFO] QEMU is running a vnc server at localhost:`echo 5900+$(VNC_DISPLAY) | bc` gdb server at $(GDB_PORT)"
qemu-system-i386 -name $(NAME) -boot d -cdrom $(IMG_NAME) \
-m $(QEMU_RAM) \
-display vnc=:$(VNC_DISPLAY) \
-gdb tcp:0.0.0.0:$(GDB_PORT) # ncurses interface and gdb server

run-curses: $(IMG_NAME)
qemu-system-i386 -name $(NAME) -boot d -cdrom $(IMG_NAME) \
-m $(QEMU_RAM) \
-display curses

test: $(IMG_NAME)
@echo "[INFO] Starting up $(IMG_NAME) and checking if signature is set in memory]"
timeout 15 qemu-system-i386 \
-boot d -cdrom $(IMG_NAME) \
-m 4M -nographic -gdb tcp:localhost:$(GDB_PORT) &
-m $(QEMU_RAM) -nographic -gdb tcp:localhost:$(GDB_PORT) &
sleep 10 && (echo "x/wx $(SIGNATURE_ADDRESS)") | make gdb 2>/dev/null | grep -i $(SIGNATURE_VALUE)
@echo "[INFO] Signature matched, test passed!"

Expand Down
2 changes: 1 addition & 1 deletion build/i686-cc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ RUN apk add --no-cache \
gmp-dev \
mpc1-dev \
mpfr-dev \
grub grub-bios xorriso \
grub grub-bios xorriso git\
qemu-system-i386 qemu-ui-curses \
gdb \
bash
Expand Down
23 changes: 23 additions & 0 deletions src/drivers/keyboard/keyboard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "keyboard.h"
#include "../../klibc/libc.h"
#include "keyboard_internal.h"

// TODO use partial init with ranges
// http://www.osdever.net/bkerndev/Docs/keyboard.htm
unsigned char us_scancode_1[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-',
'=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
'[', ']', '\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',',
'.', '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, '-', 0, 0, 0, '+', 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

scancode kbd_get() {
return (inb(KBD_IO_STATUS_REGISTER) & 1) ? inb(KBD_IO_DATA_PORT) : 0;
}

char kbd_code_to_ascii(scancode code) {
return (code <= 127) ? us_scancode_1[code] : 0;
}
55 changes: 55 additions & 0 deletions src/drivers/keyboard/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H

#include <stdint.h>

// scancode set 1
// https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_1
typedef enum scancode {
KBD_ESCAPE = 0x1,
KBD_BACKSPACE = 0x0E,
KBD_TAB,
KBD_ENTER = 0x1C,
KBD_LEFT_CTRL,
KBD_LEFT_SHIFT = 0x2A,
KBD_RIGHT_SHIFT = 0x36,
KBD_LEFT_ALT = 0x38,
KBD_SPACE,
KBD_CAPS_LOCK,
KBD_F1,
KBD_F2,
KBD_F3,
KBD_F4,
KBD_F5,
KBD_F6,
KBD_F7,
KBD_F8,
KBD_F9,
KBD_F10,
KBD_NUMLOCK,
KBD_SCROLLLOCK,
KBD_F11 = 0x57,
KBD_F12,
// Hack from here, only released
KBD_HOME = 0xC7,
KBD_CURSOR_UP,
KBD_PAGE_UP,
KBD_CURSOR_LEFT = 0xCB,
KBD_CURSOR_RIGHT = 0xCD,
KBD_END = 0xCF,
KBD_CURSOR_DOWN,
KBD_PAGE_DOWN,
KBD_INSERT,
KBD_DELETE,
} scancode;

// http://www.osdever.net/bkerndev/Docs/keyboard.htm
extern unsigned char us_scancode_1[128];

/// @brief returns a keyboard scancode
/// @return keyboard scancode
scancode kbd_get();

char kbd_code_to_ascii(scancode code);

#endif
9 changes: 9 additions & 0 deletions src/drivers/keyboard/keyboard_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef KEYBOARD_INTERNAL_H
#define KEYBOARD_INTERNAL_H

// PS/2 controller I/O registers
#define KBD_IO_DATA_PORT 0x60
#define KBD_IO_STATUS_REGISTER 0x64
#define KBD_IO_COMMAND_REGISTER 0x64

#endif
Loading

0 comments on commit d9c746b

Please sign in to comment.