|
@@ -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) */
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|