Ver Fonte

Improve eventloop

Johannes Zellner há 8 meses atrás
pai
commit
955f4d6741
4 ficheiros alterados com 145 adições e 12 exclusões
  1. 6 3
      Makefile
  2. 2 2
      boot.S
  3. 127 7
      kernel.c
  4. 10 0
      timer.S

+ 6 - 3
Makefile

@@ -24,8 +24,11 @@ kernel.o: kernel.c
 boot.elf: boot.o
 boot.elf: boot.o
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 
 
-kernel.elf: kernel.o
-	$(LD) $(KERNEL_LDFLAGS) -o $@ $<
+timer.o: timer.S
+	$(AS) $(ASFLAGS) -o $@ $<
+
+kernel.elf: kernel.o timer.o
+	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 
 boot.bin: boot.elf
 boot.bin: boot.elf
 	$(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
 	$(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
@@ -45,4 +48,4 @@ 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
 	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:
 clean:
-	rm -f boot.o kernel.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin
+	rm -f boot.o kernel.o timer.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin

+ 2 - 2
boot.S

@@ -19,12 +19,12 @@ _start:
 	movw  $0x0003, %ax
 	movw  $0x0003, %ax
 	int   $0x10
 	int   $0x10
 
 
-	/* Load kernel: 4 sectors from sector 2 into 0x7E00 (ES:BX = 0x07E0:0) */
+	/* Load kernel: 5 sectors from sector 2 into 0x7E00 (ES:BX = 0x07E0:0) */
 	movw  $0x07e0, %ax
 	movw  $0x07e0, %ax
 	movw  %ax, %es
 	movw  %ax, %es
 	xorw  %bx, %bx
 	xorw  %bx, %bx
 	movb  $0x02, %ah          /* int 0x13 read */
 	movb  $0x02, %ah          /* int 0x13 read */
-	movb  $4, %al             /* 4 sectors */
+	movb  $5, %al             /* 5 sectors (kernel can be > 2K) */
 	xorw  %cx, %cx
 	xorw  %cx, %cx
 	movb  $2, %cl             /* sector 2 (1-based) */
 	movb  $2, %cl             /* sector 2 (1-based) */
 	xorw  %dx, %dx             /* head 0, drive in DL */
 	xorw  %dx, %dx             /* head 0, drive in DL */

+ 127 - 7
kernel.c

@@ -19,10 +19,84 @@ static inline unsigned char inb(unsigned short port)
 	return v;
 	return v;
 }
 }
 
 
+static inline void outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
 /* PS/2 keyboard: data port 0x60, status 0x64 (bit 0 = output buffer full) */
 /* PS/2 keyboard: data port 0x60, status 0x64 (bit 0 = output buffer full) */
 #define KB_DATA  0x60
 #define KB_DATA  0x60
 #define KB_STATUS 0x64
 #define KB_STATUS 0x64
 
 
+/* Timer: PIT at 1 kHz (1 tick = 1 ms). IRQ0 -> INT 0x20 after PIC remap. */
+#define PIT_HZ      1000
+#define CURSOR_MS   500
+#define BUSY_MS     33
+
+volatile unsigned int ticks;  /* milliseconds since timer_init */
+
+/* IDT entry (8 bytes) and table for vector 0x20 (IRQ0). */
+struct idt_entry {
+	unsigned short offset_lo;
+	unsigned short selector;
+	unsigned char  zero;
+	unsigned char  type;
+	unsigned short offset_hi;
+} __attribute__((packed));
+
+struct idtr_desc {
+	unsigned short limit;
+	unsigned int   base;
+} __attribute__((packed));
+
+static struct idt_entry idt[256];
+static struct idtr_desc idtr;
+
+extern void pit_irq_stub(void);
+
+void pit_irq(void);  /* called from timer.S stub */
+
+void pit_irq(void)
+{
+	ticks++;
+	outb(0x20, 0x20);  /* EOI to master PIC */
+}
+
+static void timer_init(void)
+{
+	unsigned int stub = (unsigned int)pit_irq_stub;
+
+	/* Remap PIC: IRQ0..7 -> INT 0x20..0x27 */
+	outb(0x20, 0x11);
+	outb(0xA0, 0x11);
+	outb(0x21, 0x20);
+	outb(0xA1, 0x28);
+	outb(0x21, 0x04);
+	outb(0xA1, 0x02);
+	outb(0x21, 0x01);
+	outb(0xA1, 0x01);
+	outb(0x21, 0xFE);  /* mask all except IRQ0 on master */
+	outb(0xA1, 0xFF);  /* mask all on slave */
+
+	/* IDT entry for 0x20 (IRQ0) */
+	idt[0x20].offset_lo = (unsigned short)(stub & 0xFFFF);
+	idt[0x20].selector  = 0x08;
+	idt[0x20].zero     = 0;
+	idt[0x20].type     = 0x8E;  /* 32-bit interrupt gate */
+	idt[0x20].offset_hi = (unsigned short)((stub >> 16) & 0xFFFF);
+
+	idtr.limit = (unsigned short)(sizeof(idt) - 1);
+	idtr.base  = (unsigned int)idt;
+	__asm__ volatile ("lidt %0" : : "m"(idtr));
+
+	/* PIT channel 0: 1 kHz (divisor 1193) */
+	outb(0x43, 0x36);
+	outb(0x40, (unsigned char)(1193 & 0xFF));
+	outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
+
+	__asm__ volatile ("sti");
+}
+
 /* Scancode set 1 (XT) -> ASCII. 0 = ignore. Set 1 break = make | 0x80, we ignore those. */
 /* Scancode set 1 (XT) -> ASCII. 0 = ignore. Set 1 break = make | 0x80, we ignore those. */
 static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
 static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
 	[0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
 	[0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
@@ -45,16 +119,29 @@ static int shift_pressed;
 static unsigned int cursor;  /* linear position 0 .. CONTENT_SIZE-1 */
 static unsigned int cursor;  /* linear position 0 .. CONTENT_SIZE-1 */
 static unsigned char last_scancode;     /* set by getchar for debug line */
 static unsigned char last_scancode;     /* set by getchar for debug line */
 #define BUSY_POS (DEBUG_ROW * VGA_COLS + 79)  /* bottom-right: never overwritten by echo */
 #define BUSY_POS (DEBUG_ROW * VGA_COLS + 79)  /* bottom-right: never overwritten by echo */
-static unsigned int busy_tick;
+static unsigned int busy_idx;   /* 0..3 for busy_chars */
+static unsigned int last_busy_tick;
 /* Explicit ASCII so no encoding/escape issues: - \ | / */
 /* Explicit ASCII so no encoding/escape issues: - \ | / */
 static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
 static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
 
 
+/* Blinking cursor: cell content when cursor is hidden; 0xDB = full block (CP437) */
+static unsigned short cursor_cell;
+static int cursor_visible;
+static unsigned int last_cursor_tick;
+#define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
+
+/* Restore cell at current cursor, then update cursor position and cursor_cell. */
+static void hide_cursor_at(unsigned int pos)
+{
+	VGA_BASE[pos] = cursor_cell;
+}
+
 static void putchar(char c)
 static void putchar(char c)
 {
 {
 	if (c == '\n') {
 	if (c == '\n') {
+		hide_cursor_at(cursor);
 		cursor = (cursor / VGA_COLS + 1) * VGA_COLS;
 		cursor = (cursor / VGA_COLS + 1) * VGA_COLS;
 		if (cursor >= CONTENT_SIZE) {
 		if (cursor >= CONTENT_SIZE) {
-			/* Scroll: move lines up, clear bottom of content area */
 			unsigned int i;
 			unsigned int i;
 			for (i = 0; i < (CONTENT_ROWS - 1) * VGA_COLS; i++)
 			for (i = 0; i < (CONTENT_ROWS - 1) * VGA_COLS; i++)
 				VGA_BASE[i] = VGA_BASE[i + VGA_COLS];
 				VGA_BASE[i] = VGA_BASE[i + VGA_COLS];
@@ -62,21 +149,38 @@ static void putchar(char c)
 				VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
 				VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
 			cursor = (CONTENT_ROWS - 1) * VGA_COLS;
 			cursor = (CONTENT_ROWS - 1) * VGA_COLS;
 		}
 		}
+		cursor_cell = (unsigned short)(VGA_ATTR << 8);
 		return;
 		return;
 	}
 	}
 	if (c == '\b') {
 	if (c == '\b') {
 		if (cursor > 0) {
 		if (cursor > 0) {
+			hide_cursor_at(cursor);
 			cursor--;
 			cursor--;
-			VGA_BASE[cursor] = (unsigned short)(VGA_ATTR << 8);
+			cursor_cell = VGA_BASE[cursor];
 		}
 		}
 		return;
 		return;
 	}
 	}
 	if (cursor < CONTENT_SIZE) {
 	if (cursor < CONTENT_SIZE) {
 		VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
 		VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
 		cursor++;
 		cursor++;
+		cursor_cell = (unsigned short)(VGA_ATTR << 8);
 	}
 	}
 }
 }
 
 
+/* Draw blinking cursor at current cursor position (every CURSOR_MS ms). */
+static void update_cursor_blink(void)
+{
+	unsigned int t = ticks;
+	if (t - last_cursor_tick >= CURSOR_MS) {
+		last_cursor_tick = t;
+		cursor_visible = !cursor_visible;
+	}
+	if (cursor_visible)
+		VGA_BASE[cursor] = CURSOR_BLOCK;
+	else
+		VGA_BASE[cursor] = cursor_cell;
+}
+
 /* Write one hex byte at VGA position; returns next position. */
 /* Write one hex byte at VGA position; returns next position. */
 static unsigned int puthex(unsigned int pos, unsigned char byte)
 static unsigned int puthex(unsigned int pos, unsigned char byte)
 {
 {
@@ -130,11 +234,15 @@ static char apply_shift(char c)
 	}
 	}
 }
 }
 
 
-/* Draw busy indicator at bottom-right so we know the event loop runs. */
+/* Draw busy indicator at bottom-right (every BUSY_MS ms). */
 static void update_busy(void)
 static void update_busy(void)
 {
 {
-	busy_tick++;
-	VGA_BASE[BUSY_POS] = (unsigned short)((VGA_ATTR << 8) | busy_chars[busy_tick % 4]);
+	unsigned int t = ticks;
+	if (t - last_busy_tick >= BUSY_MS) {
+		last_busy_tick = t;
+		busy_idx = (busy_idx + 1) % 4;
+	}
+	VGA_BASE[BUSY_POS] = (unsigned short)((VGA_ATTR << 8) | busy_chars[busy_idx]);
 }
 }
 
 
 /* String in .text so it loads with the kernel */
 /* String in .text so it loads with the kernel */
@@ -151,17 +259,27 @@ void kernel_main(void)
 	for (i = 0; i < VGA_SIZE; i++)
 	for (i = 0; i < VGA_SIZE; i++)
 		VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
 		VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
 	cursor = 0;
 	cursor = 0;
+	cursor_cell = (unsigned short)(VGA_ATTR << 8);
+	cursor_visible = 1;
+	last_cursor_tick = 0;
+	last_busy_tick = 0;
+	busy_idx = 0;
 
 
 	putstr(msg);
 	putstr(msg);
 	putstr(prompt);
 	putstr(prompt);
 
 
+	timer_init();  /* PIT 1 kHz, IDT, sti; ticks counts ms */
+	last_cursor_tick = ticks;
+	last_busy_tick   = ticks;
+
 	/* Drain keyboard buffer so we don't echo leftover/garbage bytes */
 	/* Drain keyboard buffer so we don't echo leftover/garbage bytes */
 	while (inb(KB_STATUS) & 1)
 	while (inb(KB_STATUS) & 1)
 		(void)inb(KB_DATA);
 		(void)inb(KB_DATA);
 
 
-	/* Terminal echo loop: always update busy indicator, then poll keyboard */
+	/* Terminal echo loop: busy indicator, blinking cursor at cursor, then poll keyboard */
 	for (;;) {
 	for (;;) {
 		update_busy();
 		update_busy();
+		update_cursor_blink();
 		if (inb(KB_STATUS) & 1) {
 		if (inb(KB_STATUS) & 1) {
 			unsigned char sc = inb(KB_DATA);
 			unsigned char sc = inb(KB_DATA);
 			if (sc & 0x80) {
 			if (sc & 0x80) {
@@ -185,5 +303,7 @@ void kernel_main(void)
 				}
 				}
 			}
 			}
 		}
 		}
+		else
+			__asm__ volatile ("hlt");  /* wait for next interrupt (PIT or other) */
 	}
 	}
 }
 }

+ 10 - 0
timer.S

@@ -0,0 +1,10 @@
+/* PIT IRQ0 stub: save state, call C handler, restore, iret. */
+.code32
+.text
+
+.globl pit_irq_stub
+pit_irq_stub:
+	pusha
+	call  pit_irq
+	popa
+	iret