Johannes Zellner 8 месяцев назад
Родитель
Сommit
66563f62d3
6 измененных файлов с 101 добавлено и 27 удалено
  1. 9 4
      Makefile
  2. 3 3
      boot.S
  3. 8 0
      hello.c
  4. 7 7
      io.c
  5. 9 7
      io.h
  6. 65 6
      terminal.c

+ 9 - 4
Makefile

@@ -27,6 +27,9 @@ io.o: io.c io.h
 terminal.o: terminal.c terminal.h io.h rtc.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+hello.o: hello.c io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 rtc.o: rtc.c rtc.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -36,7 +39,7 @@ boot.elf: boot.o
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o io.o rtc.o terminal.o
+kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -49,12 +52,14 @@ kernel.bin: kernel.elf
 	$(OBJCOPY) -O binary $< $@
 
 image.bin: boot.bin kernel.bin
-	dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
-	dd if=boot.bin of=$@ bs=512 conv=notrunc 2>/dev/null
+	@KERNEL_SECTORS=$$(( ($$(wc -c < kernel.bin) + 511) / 512 )); \
+	TOTAL_SECTORS=$$(( 1 + KERNEL_SECTORS )); \
+	dd if=/dev/zero of=$@ bs=512 count=$$TOTAL_SECTORS 2>/dev/null; \
+	dd if=boot.bin of=$@ bs=512 conv=notrunc 2>/dev/null; \
 	dd if=kernel.bin of=$@ bs=512 seek=1 conv=notrunc 2>/dev/null
 
 run: image.bin
 	qemu-system-i386 -M pc -drive file=image.bin,format=raw 2>/dev/null || qemu-system-x86_64 -M pc -drive file=image.bin,format=raw
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin

+ 3 - 3
boot.S

@@ -19,12 +19,12 @@ _start:
 	movw  $0x0003, %ax
 	int   $0x10
 
-	/* Load kernel: 5 sectors from sector 2 into 0x7E00 (ES:BX = 0x07E0:0) */
+	/* Load kernel: 10 sectors from sector 2 into 0x7E00 (ES:BX = 0x07E0:0) */
 	movw  $0x07e0, %ax
 	movw  %ax, %es
 	xorw  %bx, %bx
 	movb  $0x02, %ah          /* int 0x13 read */
-	movb  $5, %al             /* 5 sectors (kernel can be > 2K) */
+	movb  $10, %al            /* 10 sectors (kernel up to 5K) */
 	xorw  %cx, %cx
 	movb  $2, %cl             /* sector 2 (1-based) */
 	xorw  %dx, %dx             /* head 0, drive in DL */
@@ -76,7 +76,7 @@ pm_start:
 	movw  %ax, %fs
 	movw  %ax, %gs
 	movw  %ax, %ss
-	movl  $0x9000, %esp       /* stack above kernel (0x7E00+2K); was 0x8000 and overwrote kernel */
+	movl  $0x10000, %esp      /* stack well above kernel (grows down from 64K) */
 
 	/* Jump to kernel at 0x7E00 (direct; *0x7e00 would be indirect = wrong) */
 	movl  $0x7e00, %eax

+ 8 - 0
hello.c

@@ -0,0 +1,8 @@
+/* Hello program: prints "Hello World" and exits. Uses io.h. */
+
+#include "io.h"
+
+void program_hello_main(void)
+{
+	io_putstr("Hello World\n");
+}

+ 7 - 7
io.c

@@ -72,7 +72,7 @@ void io_init(void)
 {
 	disable_hw_cursor();
 	io_clear_screen();
-	cursor = 0;
+	cursor = IO_CONTENT_OFFSET;
 	cursor_cell = (unsigned short)(VGA_ATTR << 8);
 	io_drain_keyboard();
 }
@@ -97,27 +97,27 @@ void io_putchar(char c)
 	if (c == '\n') {
 		hide_cursor_at(cursor);
 		cursor = (cursor / IO_VGA_COLS + 1) * IO_VGA_COLS;
-		if (cursor >= IO_CONTENT_SIZE) {
+		if (cursor >= IO_CONTENT_OFFSET + IO_CONTENT_SIZE) {
 			unsigned int i;
 			for (i = 0; i < (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i++)
-				VGA_BASE[i] = VGA_BASE[i + IO_VGA_COLS];
+				VGA_BASE[IO_CONTENT_OFFSET + i] = VGA_BASE[IO_CONTENT_OFFSET + i + IO_VGA_COLS];
 			for (i = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i < IO_CONTENT_SIZE; i++)
-				VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
-			cursor = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS;
+				VGA_BASE[IO_CONTENT_OFFSET + i] = (unsigned short)(VGA_ATTR << 8);
+			cursor = IO_CONTENT_OFFSET + (IO_CONTENT_ROWS - 1) * IO_VGA_COLS;
 			scroll_occurred = 1;
 		}
 		cursor_cell = (unsigned short)(VGA_ATTR << 8);
 		return;
 	}
 	if (c == '\b') {
-		if (cursor > 0) {
+		if (cursor > IO_CONTENT_OFFSET) {
 			hide_cursor_at(cursor);
 			cursor--;
 			cursor_cell = VGA_BASE[cursor];
 		}
 		return;
 	}
-	if (cursor < IO_CONTENT_SIZE) {
+	if (cursor < IO_CONTENT_OFFSET + IO_CONTENT_SIZE) {
 		VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
 		cursor++;
 		cursor_cell = (unsigned short)(VGA_ATTR << 8);

+ 9 - 7
io.h

@@ -2,13 +2,15 @@
 #ifndef IO_H
 #define IO_H
 
-#define IO_VGA_COLS      80
-#define IO_VGA_ROWS      25
-#define IO_CONTENT_ROWS  IO_VGA_ROWS
-#define IO_CONTENT_SIZE  (IO_CONTENT_ROWS * IO_VGA_COLS)
-/* Top-right datetime: row 0, 19 chars "YYYY-MM-DD HH:MM:SS" */
-#define IO_DATETIME_LEN  19
-#define IO_DATETIME_POS  (IO_VGA_COLS - IO_DATETIME_LEN)
+#define IO_VGA_COLS       80
+#define IO_VGA_ROWS       25
+#define IO_HEADER_ROWS    1
+#define IO_CONTENT_OFFSET (IO_HEADER_ROWS * IO_VGA_COLS)   /* row 1 start */
+#define IO_CONTENT_ROWS   (IO_VGA_ROWS - IO_HEADER_ROWS)   /* 24 rows */
+#define IO_CONTENT_SIZE   (IO_CONTENT_ROWS * IO_VGA_COLS)
+/* Row 0: banner left, datetime right (19 chars "YYYY-MM-DD HH:MM:SS") */
+#define IO_DATETIME_LEN   19
+#define IO_DATETIME_POS   (IO_VGA_COLS - IO_DATETIME_LEN)
 
 void io_init(void);
 void io_clear_screen(void);

+ 65 - 6
terminal.c

@@ -1,11 +1,14 @@
-/* Terminal: prompt, datetime (top right), blinking cursor, echo loop. */
+/* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch. */
 
 #include "io.h"
 #include "rtc.h"
 #include "terminal.h"
 #include "timer.h"
 
+extern void program_hello_main(void);
+
 #define DATETIME_MS 1000
+#define LINE_MAX 64
 static unsigned int last_datetime_tick;
 static char datetime_buf[IO_DATETIME_LEN + 1];
 
@@ -45,17 +48,51 @@ static void update_cursor_blink(void)
 		io_cursor_hide_block();
 }
 
-static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
+static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
+#define BANNER_LEN 12
 static const char prompt[] __attribute__((section(".text"))) = "\n> ";
+static const char prompt_first[] __attribute__((section(".text"))) = "> ";
+
+/* Draw row 0: banner left, datetime right. Call when terminal is active. */
+static void draw_header_row(void)
+{
+	unsigned int i;
+	for (i = 0; i < BANNER_LEN && banner[i]; i++)
+		io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
+	for (; i < IO_DATETIME_POS; i++)
+		io_vga_write(i, (unsigned short)(0x0A << 8));
+	redraw_datetime();
+}
+
+static int is_cmd_hello(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
+		return 0;
+	i += 5;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
 
 void terminal_run(void)
 {
+	static char line[LINE_MAX];
+	static unsigned int line_len;
+
 	last_cursor_tick   = get_ticks_ms();
 	last_datetime_tick = get_ticks_ms();
 	cursor_visible     = 1;
+	line_len           = 0;
+	line[0]            = '\0';
 
-	io_putstr(msg);
-	io_putstr(prompt);
+	io_drain_keyboard();
+	draw_header_row();
+	io_putstr(prompt_first);
 
 	for (;;) {
 		update_datetime();
@@ -63,11 +100,33 @@ void terminal_run(void)
 		{
 			char c = io_poll_key();
 			if (c) {
-				io_putchar(c);
-				if (c == '\n') {
+				if (c == '\b') {
+					if (line_len > 0) {
+						line_len--;
+						line[line_len] = '\0';
+						io_putchar(c);
+					}
+				} else if (c == '\n') {
+					io_putchar(c);
+					if (is_cmd_hello(line, line_len)) {
+						program_hello_main();
+						io_drain_keyboard();
+						draw_header_row();
+					}
+					line_len = 0;
+					line[0]  = '\0';
 					io_putstr(prompt);
 					if (io_did_scroll())
 						redraw_datetime();
+				} else {
+					{
+						unsigned char u = (unsigned char)c;
+						if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
+							line[line_len++] = c;
+							line[line_len]   = '\0';
+						}
+					}
+					io_putchar(c);
 				}
 			} else {
 				__asm__ volatile ("hlt");