提交 0f218772 创建 作者: Silas Boyd-Wickizer's avatar Silas Boyd-Wickizer

The start of some CGA console code.

上级 f037a9b1
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
OBJS = \ OBJS = \
asm.o \ asm.o \
cga.o \
main.o \ main.o \
string.o \
uart.o \ uart.o \
trap.o trap.o
...@@ -45,7 +47,7 @@ clean: ...@@ -45,7 +47,7 @@ clean:
ifndef CPUS ifndef CPUS
CPUS := 2 CPUS := 2
endif endif
QEMUOPTS = -smp $(CPUS) -m 512 -nographic QEMUOPTS = -smp $(CPUS) -m 512
qemu: kernel qemu: kernel
$(QEMU) $(QEMUOPTS) -kernel kernel $(QEMU) $(QEMUOPTS) -kernel kernel
#include "types.h"
#include "kernel.h"
static volatile u16 *screen = (u16*)(KBASE+0xb8000);
// Black background, (high intensity) white foreground
static int color = ((0 << 4) | 15) << 8;
static int cursor;
extern void setcursor(int);
void
putsn(const char *s, int n)
{
int i;
uint8 *p, *ep;
p = (uint8*)s;
ep = p+n;
for(; p < ep; p++) {
if(*p == '\n') {
cursor += 80 - cursor%80;
} else {
screen[cursor] = color | *p;
cursor++;
}
if(cursor == 25*80) {
memmove((void*)screen+80*2, (void*)screen+80*3, 80*(25-3)*2);
for(i=0; i<80; i++)
screen[(24*80+i)] = color | ' ' & 0xff;
cursor -= 80;
}
}
}
static void
cgaputs(const char *s)
{
putsn(s, strlen(s));
}
void
panic(const char *s)
{
cgaputs("panic: ");
cgaputs(s);
cgaputs("\n");
setcursor(cursor);
for(;;)
screen[0] = screen[0];
}
void
initcga(void)
{
// Assume boot.S set up the screen.
// We're just taking over the cursor position.
cursor = 2*80;
setcursor(cursor);
cgaputs("booting...\n");
setcursor(cursor);
}
#include "types.h" #include "types.h"
#define KBASE 0xFFFFFFFF80000000ull
#define KADDR(x) ((void*)(KBASE+(uintptr)(x)))
#define PADDR(x) ((uintptr)(x) - KBASE)
#define PGSIZE (2*1024*1024ull)
#define KCSEG (2<<3) /* kernel code segment */
#define KDSEG (3<<3) /* kernel data segment */
#define nil ((void*)0)
void memmove(void *dst, void *src, int64 n);
int strlen(const char*);
#include "types.h" #include "types.h"
#include "multiboot.h" #include "multiboot.h"
extern void uartinit(void); extern void inituart(void);
extern void initcga(void);
void void
cmain(void) cmain(void)
{ {
uartinit(); inituart();
initcga();
} }
...@@ -2,16 +2,16 @@ ...@@ -2,16 +2,16 @@
#include "x86.h" #include "x86.h"
void* void*
memset(void *dst, int c, uint n) memset(void *dst, int c, u32 n)
{ {
stosb(dst, c, n); stosb(dst, c, n);
return dst; return dst;
} }
int int
memcmp(const void *v1, const void *v2, uint n) memcmp(const void *v1, const void *v2, u32 n)
{ {
const uchar *s1, *s2; const u8 *s1, *s2;
s1 = v1; s1 = v1;
s2 = v2; s2 = v2;
...@@ -25,7 +25,7 @@ memcmp(const void *v1, const void *v2, uint n) ...@@ -25,7 +25,7 @@ memcmp(const void *v1, const void *v2, uint n)
} }
void* void*
memmove(void *dst, const void *src, uint n) memmove(void *dst, const void *src, u32 n)
{ {
const char *s; const char *s;
char *d; char *d;
...@@ -46,23 +46,23 @@ memmove(void *dst, const void *src, uint n) ...@@ -46,23 +46,23 @@ memmove(void *dst, const void *src, uint n)
// memcpy exists to placate GCC. Use memmove. // memcpy exists to placate GCC. Use memmove.
void* void*
memcpy(void *dst, const void *src, uint n) memcpy(void *dst, const void *src, u32 n)
{ {
return memmove(dst, src, n); return memmove(dst, src, n);
} }
int int
strncmp(const char *p, const char *q, uint n) strncmp(const char *p, const char *q, u32 n)
{ {
while(n > 0 && *p && *p == *q) while(n > 0 && *p && *p == *q)
n--, p++, q++; n--, p++, q++;
if(n == 0) if(n == 0)
return 0; return 0;
return (uchar)*p - (uchar)*q; return (u8)*p - (u8)*q;
} }
char* char*
strncpy(char *s, const char *t, int n) strncpy(char *s, const char *t, u32 n)
{ {
char *os; char *os;
...@@ -76,7 +76,7 @@ strncpy(char *s, const char *t, int n) ...@@ -76,7 +76,7 @@ strncpy(char *s, const char *t, int n)
// Like strncpy but guaranteed to NUL-terminate. // Like strncpy but guaranteed to NUL-terminate.
char* char*
safestrcpy(char *s, const char *t, int n) safestrcpy(char *s, const char *t, u32 n)
{ {
char *os; char *os;
...@@ -104,6 +104,5 @@ strcmp(const char *p, const char *q) ...@@ -104,6 +104,5 @@ strcmp(const char *p, const char *q)
{ {
while(*p && *p == *q) while(*p && *p == *q)
p++, q++; p++, q++;
return (uchar)*p - (uchar)*q; return (u8)*p - (u8)*q;
} }
...@@ -19,7 +19,7 @@ uartputc(int c) ...@@ -19,7 +19,7 @@ uartputc(int c)
} }
void void
uartinit(void) inituart(void)
{ {
char *p; char *p;
......
...@@ -19,7 +19,6 @@ uint8 inb(uint16); ...@@ -19,7 +19,6 @@ uint8 inb(uint16);
void outb(uint16, uint8); void outb(uint16, uint8);
void microdelay(int); void microdelay(int);
uint32 readeflags(void); uint32 readeflags(void);
void setcursor(int);
void memmove(void *dst, void *src, int64 n); void memmove(void *dst, void *src, int64 n);
int memcmp(const void*, const void*, int64); int memcmp(const void*, const void*, int64);
void hlt(void); void hlt(void);
......
...@@ -17,6 +17,15 @@ outb(u16 port, u8 data) ...@@ -17,6 +17,15 @@ outb(u16 port, u8 data)
} }
static inline void static inline void
stosb(void *addr, int data, int cnt)
{
__asm volatile("cld; rep stosb" :
"=D" (addr), "=c" (cnt) :
"0" (addr), "1" (cnt), "a" (data) :
"memory", "cc");
}
static inline void
microdelay(u32 delay) microdelay(u32 delay)
{ {
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论