From 00a8c3acac5e8d8af057aa766f8c97887835df6f Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 27 Nov 2024 21:29:33 +0900 Subject: [PATCH] add pio.h --- boot/boot.c | 50 ++++++++++++++++++++++++++++++++++++++------------ boot/pio.h | 8 ++++++++ 2 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 boot/pio.h diff --git a/boot/boot.c b/boot/boot.c index dad5c78..5106edb 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -1,8 +1,11 @@ +#include "pio.h" + +typedef unsigned short u16; +typedef unsigned char u8; typedef int dev_t; -#define SCREEN_WIDTH 80 -#define SCREEN_LENGTH 25 -#define VGA_BUFFER_ADDR 0xb8000 +#define VGA_WIDTH 80 +#define VGA_LENGTH 25 char* memcpy(char *a, char *b, int l) @@ -37,20 +40,43 @@ itostr(int n, char *s) return s; } +void +setcursor(int x, int y) +{ + u16 pos = y * VGA_WIDTH + x; + + outb(0x3D4, 0x0F); + outb(0x3D5, (u8) (pos & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (u8) ((pos >> 8) & 0xFF)); +} + +void +wr8(u8 c, u8 fg, u8 bg, int x, int y) +{ + u16 attrib = (bg << 4) | (fg & 0x0F); + volatile u16 * where; + where = (volatile u16 *)0xB8000 + (y * 80 + x); + *where = c | (attrib << 8); +} + + void boot(dev_t bootdev) { char *p; - char s[SCREEN_WIDTH] = "booted on disk:0x"; - short* vga = (short*)VGA_BUFFER_ADDR; + char s[VGA_WIDTH] = "booted on disk:0x"; p = s+strlen(s); p = itostr((int)bootdev, p); - for(int j = 0; j < SCREEN_LENGTH; ++j){ - for (int i = 0; i < sizeof(s); ++i) { - vga[(j*SCREEN_WIDTH) + (i + SCREEN_WIDTH*2)] = 0x9100 | s[i]; - } - } - while(1) - ; + setcursor(0,0); + for (int i = 0; i < sizeof(s); ++i) + wr8(s[i], 0xe8, 0x00, i, 0); + for(int i = 1; i < VGA_LENGTH; ++i) + for(int j = 0; j < VGA_WIDTH; ++j) + wr8(0x00, 0x91, 0x00, j, i); + // disable cursor + outb(0x3D4, 0x0a); + outb(0x3D5, 0x20); + asm("hlt"); } diff --git a/boot/pio.h b/boot/pio.h new file mode 100644 index 0000000..4c98414 --- /dev/null +++ b/boot/pio.h @@ -0,0 +1,8 @@ +#define outb(port, data) \ + (__outb(port, data)) + +static __inline void +__outb(int port, unsigned char data) +{ + __asm volatile("outb %0,%w1" : : "a" (data), "d" (port)); +}