From: acevest Date: Mon, 10 Aug 2026 10:50:21 +0000 (+0800) Subject: vt vga X-Git-Url: http://repos.zhaoyanbai.com/?a=commitdiff_plain;h=2255c52f40b43be09d12fabb07318dadceaceabf;p=kernel.git vt vga --- diff --git a/drivers/vga.c b/drivers/vga.c new file mode 100644 index 0000000..0260864 --- /dev/null +++ b/drivers/vga.c @@ -0,0 +1,74 @@ +/* + * ------------------------------------------------------------------------ + * File Name: vga.c + * Author: Zhao Yanbai + * 2026-08-10 09:01:44 Monday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#include "vga.h" + +static uint16_t *vga_vram_vaddr = (uint16_t *)pa2va(0xB8000); + +// VGA VC backend + +// static int vga_write(const char *buf, size_t size) { +// return 0; +// } + +// static int vga_get_size(int *width, int *height) { +// *width = VGA_WIDTH; +// *height = VGA_HEIGHT; +// return 0; +// } + +static int vga_init(vc_t *vc) { + vc->cols = VGA_WIDTH; + vc->rows = VGA_HEIGHT; + return 0; +} + +static int vga_switch(vc_t *vc) { + + memcpy(vc->vram, vga_vram_vaddr, vc->vram_size); + + return 0; +} + +static int vga_set_vram_addr(vc_t *vc) { + + vc->vram_addr = vga_vram_vaddr; + + return 0; +} + +static int vga_save_screen(vc_t *vc) { + + memcpy(vc->vram, vga_vram_vaddr, vc->vram_size); + + return 0; +} + +static int vga_clear(vc_t *vc) { + // memset(vc->vram, 0, vc->vram_size); + for (int i = 0; i < vc->vram_size; i++) { + vc->vram[i] = 0 | ; + } + return 0; +} + +static uint8_t vga_build_attr(vc_t *vc, uint8_t bg_color, uint8_t fg_color, bool blink, bool highlight) { + uint8_t bgc = bg_color; + uint8_t fgc = fg_color; + return (bgc << 4) | fgc | (blink ? 0b1000 : 0) | (highlight ? 0b10000 : 0); +} + +static vc_backend_t vga_backend = { + .init = vga_init, + // .write = vga_write, + // .get_size = vga_get_size, + .switch = vga_switch, + .set_vram_addr = vga_set_vram_addr, + .save_screen = vga_save_screen, +}; diff --git a/drivers/vga.h b/drivers/vga.h new file mode 100644 index 0000000..2483397 --- /dev/null +++ b/drivers/vga.h @@ -0,0 +1,15 @@ +/* + * ------------------------------------------------------------------------ + * File Name: vga.h + * Author: Zhao Yanbai + * 2026-08-10 09:02:59 Monday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#pragma once + + + +const int VGA_WIDTH = 80; +const int VGA_HEIGHT = 25; \ No newline at end of file diff --git a/drivers/vt.c b/drivers/vt.c new file mode 100644 index 0000000..264860d --- /dev/null +++ b/drivers/vt.c @@ -0,0 +1,88 @@ +/* + * ------------------------------------------------------------------------ + * File Name: vt.c + * Author: Zhao Yanbai + * 2026-08-09 18:38:52 Sunday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#include "vt.h" + + +static vc_t vcs[VC_COUNT]; +static vc_t *fg_vc = &vcs[0]; + +bool vc_is_fg(vc_t *vc) { + return vc == fg_vc; +} + +// // VT +// static vt_t vt = { +// .vcs = {{0},}, +// .fg_vc = &vt.vcs[0], +// }; + + + +// VT CONSOLE: 仅桥接console用 +int vt_console_write(const char *buf, size_t size) { + //找到前台vt调用其write + return 0; +} + +int vt_console_setup(console_t *console) { + return 0; +} + + +static console_t vt_console = { + .name = "VT", + .write = vt_console_write, + .setup = vt_console_setup, +}; + + +// INIT VT + +void init_vt() { + vc_backend_t *backend = &vga_backend; + + for (int i=0; iinit(vc); + + + vc->id = i; + vc->backend = backend; + + vc->vram_size = VGA_WIDTH * VGA_HEIGHT * 2; + vc->vram = kmalloc(PAGE_UP(vc->vram_size), 0); + + memset(vc->vram, 0, vc->vram_size); + + + // int width = 0; + // int height = 0; + // assert(backend->get_size != NULL); + // backend->get_size(&width, &height); + + // assert(width > 0); + // assert(height > 0); + + // vc->cols = width; + // vc->rows = height; + // vc->x = 0; + // vc->y = 0; + // vc->fg_color = 0; + // vc->bg_color = 0; + // vc->vram = NULL; + } + + vt.fg_vc = &vt.vcs[0]; + + + register_console(&vt_console); +} \ No newline at end of file diff --git a/drivers/vt.h b/drivers/vt.h new file mode 100644 index 0000000..96f8d98 --- /dev/null +++ b/drivers/vt.h @@ -0,0 +1,75 @@ +/* + * ------------------------------------------------------------------------ + * File Name: vt.h + * Author: Zhao Yanbai + * 2026-08-09 18:39:06 Sunday CST + * Description: none + * ------------------------------------------------------------------------ + */ + +#pragma once + +#include + +typedef struct vc vc_t; +typedef struct vt vt_t; +typedef struct vc_backend_t vc_backend_t; + + +#define VC_COUNT 4 + + +enum { + VT_BLACK = 0, + VT_BLUE = 1, + VT_GREEN = 2, + VT_CYAN = 3, + VT_RED = 4, + VT_PURPLE = 5, + VT_YELLOW = 6, + VT_WHITE = 7, +}; + +// typedef uint8_t vt_color_t; + +struct vc_backend_t { + int (*init)(vc_t *vc); + // int (*write)(const char *buf, size_t size); + int (*switch)(vc_t *vc); + int (*set_vram_addr)(vc_t *vc); + int (*save_screen)(vc_t *vc); + int (*clear)(vc_t *vc); + uint8_t (*build_attr)(vc_t *vc, uint8_t bg_color, uint8_t fg_color, bool blink, bool highlight); + +}; + +struct vc { + int id; + + int x; + int y; + + int cols; + int rows; + + // vt_color_t fg_color; + // vt_color_t bg_color; + + vc_backend_t *backend; + + uint16_t *vram_addr; + uint16_t *vram; + size_t vram_size; + + size_t bytes_per_row; +}; + + + + +// struct vt { +// vc_t vcs[VC_COUNT]; +// vc_t *fg_vc; +// }; + +bool vc_is_fg(vc_t *vc); \ No newline at end of file diff --git a/kernel/setup.c b/kernel/setup.c index d358b52..95b94da 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -132,6 +132,9 @@ void setup_kernel() { set_printk(_printk); + void init_vt(); + init_vt(); + init_buffer(); void init_mount();