Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit c69d854

Browse files
committed
add serial
1 parent 99ced79 commit c69d854

4 files changed

Lines changed: 55 additions & 10 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ IMG_OUT_DIR = $(OUT_DIR)
1616
CFLAGS = -O2 -Wimplicit-function-declaration -Wunused-but-set-variable -ffreestanding -m32 -c -Wall -Wextra -I$(INCLUDE)
1717
LDFLAGS = -m elf_i386
1818
NFLAGS = -f bin
19-
QEMU_FLAGS = -monitor stdio -no-reboot -d int,guest_errors -D kernel.log
19+
QEMU_FLAGS = -serial stdio -display none -monitor none
2020
QEMU_SERIAL = -serial file:kernel_console.log -display none
2121
CONSOLE = -display curses
2222

src/include/util/console.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ int printk(const char *fmt, ...);
77
void console_scroll_page_up(void);
88
void console_scroll_page_down(void);
99

10+
/* シリアル入力関数 */
11+
int serial_received(void);
12+
char serial_getc(void);
13+
char serial_getc_nonblock(void);
14+
1015
#endif /* _CONSOLE_H */

src/kernel/device/keyboard.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ void keyboard_init(void) {
165165

166166
void keyboard_poll(void) {
167167
kbd_isr(0, NULL);
168+
169+
// シリアル入力もポーリング
170+
char c;
171+
while ((c = serial_getc_nonblock()) != 0) {
172+
// シリアル入力をバッファに追加
173+
buffer_put(c);
174+
}
168175
}
169176

170177
/**

src/kernel/util/console.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,55 @@
44
#include <stdint.h>
55
#include <interrupt/irq.h>
66

7-
/* unused function lol
7+
/**
8+
* @brief シリアルポート(COM1)を初期化
9+
*/
810
static void serial_init(void) {
9-
outb(0x3f8 + 1, 0x00); // すべての割り込みを無効化
10-
outb(0x3f8 + 3, 0x80); // DLABを有効化
11-
outb(0x3f8 + 0, 0x03); // 分周値下位バイト (38400ボーレート)
12-
outb(0x3f8 + 1, 0x00); // 分周値上位バイト
13-
outb(0x3f8 + 3, 0x03); // 8ビット、パリティなし、ストップビット1
14-
outb(0x3f8 + 2, 0xC7); // FIFO有効化
15-
outb(0x3f8 + 4, 0x0B); // IRQ有効化、RTS/DSRセット
11+
outb(0x3f8 + 1, 0x00); // すべての割り込みを無効化
12+
outb(0x3f8 + 3, 0x80); // DLABを有効化
13+
outb(0x3f8 + 0, 0x03); // 分周値下位バイト (38400ボーレート)
14+
outb(0x3f8 + 1, 0x00); // 分周値上位バイト
15+
outb(0x3f8 + 3, 0x03); // 8ビット、パリティなし、ストップビット1
16+
outb(0x3f8 + 2, 0xC7); // FIFO有効化
17+
outb(0x3f8 + 4, 0x0B); // IRQ有効化、RTS/DSRセット
1618
}
17-
*/
19+
20+
/**
21+
* @brief シリアルポートに1文字出力
22+
*/
1823
static void serial_putc(char c) {
1924
while ((inb(0x3f8 + 5) & 0x20) == 0) {
2025
}
2126
outb(0x3f8, (uint8_t)c);
2227
}
2328

29+
/**
30+
* @brief シリアルポートからデータが利用可能かチェック
31+
*/
32+
int serial_received(void) {
33+
return inb(0x3f8 + 5) & 1;
34+
}
35+
36+
/**
37+
* @brief シリアルポートから1文字読み取る
38+
*/
39+
char serial_getc(void) {
40+
while (serial_received() == 0) {
41+
}
42+
return inb(0x3f8);
43+
}
44+
45+
/**
46+
* @brief シリアルポートから1文字読み取る(ノンブロッキング)
47+
* @return 文字、またはデータがない場合は0
48+
*/
49+
char serial_getc_nonblock(void) {
50+
if (serial_received()) {
51+
return inb(0x3f8);
52+
}
53+
return 0;
54+
}
55+
2456
/**
2557
* @var cursor_row
2658
* @brief コンソール内のカーソルの現在の行位置
@@ -55,6 +87,7 @@ static int history_lines = 0;
5587
static int history_offset = 0;
5688

5789
void console_init() {
90+
serial_init(); // シリアルポートを初期化
5891
clear_screen();
5992
cursor_row = 0;
6093
cursor_col = 0;

0 commit comments

Comments
 (0)