From: Matthias Kruk Date: Wed, 6 May 2020 04:00:45 +0000 (+0900) Subject: Add VGA system process X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=b58a04e159e5663cb63f467d63f470ac28806c56;p=corax Add VGA system process --- diff --git a/config.h b/config.h index 341b667..7cad81f 100644 --- a/config.h +++ b/config.h @@ -63,7 +63,7 @@ #define CONFIG_ELF 0 #define CONFIG_KBD 1 -#define CONFIG_VGA 0 +#define CONFIG_VGA 1 /* klibc configuration */ #define CONFIG_SNPRINTF_FLAG_MINUS 0 diff --git a/sys/Makefile b/sys/Makefile index 1170973..61c8ab7 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -1,4 +1,4 @@ -DEPS = kbd +DEPS = kbd vga PHONY = $(DEPS) clean OUTPUT = initfs.a INCLUDES = -I.. -I../include @@ -9,7 +9,9 @@ all: $(OUTPUT) kbd.ko: kbd -$(OUTPUT): initfs.o kbd.ko +vga.ko: vga + +$(OUTPUT): initfs.o kbd.ko vga.ko ar -rc $@ $^ updateinitfs: diff --git a/sys/vga/Makefile b/sys/vga/Makefile new file mode 100644 index 0000000..8e4552b --- /dev/null +++ b/sys/vga/Makefile @@ -0,0 +1,26 @@ +OBJECTS = main.o +OUTPUT = vga +MODULE = vga.ko + +INCLUDES = -I../../include -I../.. +CFLAGS = -m32 -Wall -nostdlib -nodefaultlibs -nostartfiles -ffreestanding \ + -nostdinc -fno-builtin -fno-builtin-memcpy +LIBS = ../../libc/libc.a +CFLAGS += $(INCLUDES) + +ASFLAGS = $(CFLAGS) +PHONY = clean + +all: $(MODULE) + +$(MODULE): $(OUTPUT) + ld -r --oformat=elf32-i386 -m elf_i386 -b binary -o $@ $< + cp $@ ../ + +$(OUTPUT): $(OBJECTS) + gcc -static $(CFLAGS) -o $@ $^ $(LIBS) + +clean: + rm -f $(OBJECTS) $(OUTPUT) + +.PHONY: $(PHONY) diff --git a/sys/vga/main.c b/sys/vga/main.c new file mode 100644 index 0000000..8f52c7c --- /dev/null +++ b/sys/vga/main.c @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include + +enum vgacolor { + BLACK = 0, + BLUE, + GREEN, + CYAN, + RED, + MAGENTA, + BROWN, + LGRAY, + GRAY, + LBLUE, + LGREEN, + LCYAN, + LRED, + LMAGENTA, + YELLOW, + WHITE +}; + +#define FG(c) ((unsigned short)(((c) & 0xf) << 8)) +#define BG(c) ((unsigned short)(((c) & 0xf) << 12)) + +#define VGA_BUFFER_ADDR ((void*)0xb8000) +#define VGA_BUFFER_COLS 80 +#define VGA_BUFFER_ROWS 24 +#define VGA_ROW_BYTES (VGA_BUFFER_COLS << 1) +#define VGA_SCROLL_SIZE (VGA_BUFFER_COLS * (VGA_BUFFER_ROWS - 1)) +#define VGA_SCROLL_BYTES (VGA_BUFFER_SIZE << 1) +#define VGA_BUFFER_SIZE (VGA_BUFFER_COLS * VGA_BUFFER_ROWS) +#define VGA_BUFFER_BYTES (VGA_BUFFER_SIZE << 1) + +struct context { + unsigned short *buffer; + long offset; + unsigned short attrs; +}; + +static void _putchar(struct context *ctx, int c) +{ + if(c == '\n') { + ctx->offset += VGA_BUFFER_COLS - + (ctx->offset % VGA_BUFFER_COLS); + } else { + if(ctx->offset >= VGA_BUFFER_SIZE) { + ctx->offset = VGA_SCROLL_SIZE; + memcpy((void*)ctx->buffer, + (void*)ctx->buffer + VGA_ROW_BYTES, + VGA_SCROLL_BYTES); + memset((void*)ctx->buffer + VGA_SCROLL_BYTES, + 0, VGA_ROW_BYTES); + } + + ctx->buffer[ctx->offset++] = ctx->attrs | (c & 0xff); + } + + return; +} + +static int _puts(struct context *ctx, const char *s) +{ + int i; + + for(i = 0; s[i]; i++) { + _putchar(ctx, s[i]); + } + + return(i); +} + +int main(int argc, char *argv[]) +{ + struct context ctx; + char line[80]; + + ctx.buffer = mmap(VGA_BUFFER_ADDR, 0x1000, PROT_READ | PROT_WRITE, + MAP_PHYS, 0, 0); + + if(!ctx.buffer) { + /* nothing to do if we can't get access to the VGA buffer */ + return(1); + } + + ctx.offset = VGA_BUFFER_SIZE; + ctx.attrs = FG(LGRAY) | BG(BLACK); + memset(ctx.buffer, 0, VGA_BUFFER_SIZE); + + snprintf(line, sizeof(line), "VGA video memory mapped at %p\n", + ctx.buffer); + + _puts(&ctx, line); + + while(1) { + struct cxmsg msg; + int err; + + memset(&msg, 0, sizeof(msg)); + + err = cxrecv(PID_ANY, &msg); + + /* FIXME: Validate message */ + /* FIXME: Only accept messages from IO process */ + + if(!err) { + err = _puts(&ctx, (const char*)msg.cm_data); + } + + msg.cm_retval = err; + cxsend(msg.cm_src, &msg); + } + + munmap(ctx.buffer, 0x1000); + + return(0); +}