Compare commits

..

2 Commits

Author SHA1 Message Date
2edc92bbcc Merge branch 'main' of git.chauvet.pro:starnakin/42_KFS 2024-12-11 21:20:34 +01:00
5383c4eced wip: putchar and stuff 2024-12-11 21:19:40 +01:00
2 changed files with 99 additions and 114 deletions

View File

@ -5,17 +5,18 @@
#include <stddef.h>
#include <stdint.h>
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
#define TERM_BUF ((uint16_t *)0xC03FF000)
#define TERM_COUNT 10
struct screen {
size_t row;
size_t column;
uint8_t color;
uint16_t buffer[VGA_WIDTH * VGA_HEIGHT];
uint8_t default_color;
uint32_t fg_color;
uint32_t bg_color;
uint16_t buffer[SCREEN_WIDTH * SCREEN_HEIGHT];
uint32_t default_color;
char line[256];
};
@ -41,8 +42,8 @@ typedef enum {
enum cursor_direction { LEFT, RIGHT, UP, DOWN };
void terminal_initialize(void);
void terminal_set_bg_color(uint8_t color);
void terminal_set_fg_color(uint8_t color);
void terminal_set_bg_color(uint32_t color);
void terminal_set_fg_color(uint32_t color);
int terminal_putchar(char c);
int terminal_write(const char *data, size_t size);
int terminal_writestring(const char *data);
@ -53,8 +54,8 @@ struct key_event terminal_getkey(void);
void update_cursor(void);
void move_cursor(int direction);
void set_color_level(int level);
void terminal_set_default_fg_color(uint8_t fg_color);
void terminal_set_default_bg_color(uint8_t fg_color);
void terminal_change_default_fg_color(uint8_t color);
uint8_t terminal_get_default_color(void);
void terminal_set_default_fg_color(uint32_t fg_color);
void terminal_set_default_bg_color(uint32_t fg_color);
void terminal_change_default_fg_color(uint32_t color);
uint32_t terminal_get_default_color(void);
uint8_t terminal_get_char(int column, int row);

View File

@ -15,163 +15,147 @@
static struct screen screens[TERM_COUNT];
struct screen *screen = &screens[0];
static inline uint8_t vga_entry_color(vga_color fg, vga_color bg)
{
return fg | bg << 4;
}
static inline uint16_t vga_entry(unsigned char uc, uint8_t color)
{
return (uint16_t)uc | (uint16_t)color << 8;
}
void terminal_initialize(void)
{
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
TERM_BUF[index] = vga_entry(' ', VGA_COLOR_WHITE);
}
}
for (int i = 0; i < TERM_COUNT; i++) {
screens[i].row = 0;
screens[i].column = 0;
screens[i].default_color =
vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
screens[i].color = screens[i].default_color;
screens[i].default_color = 0xffffff;
screens[i].fg_color = screens[i].default_color;
memcpy(screens[i].buffer, TERM_BUF, sizeof(screen->buffer));
}
}
void terminal_set_screen(int pos)
{
memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer));
screen = &screens[pos];
memcpy(TERM_BUF, screen->buffer, sizeof(screen->buffer));
update_cursor();
if (TERM_BUF[0] == vga_entry(' ', VGA_COLOR_WHITE))
kprintf(PROMPT);
// TODO
/* memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer)); */
/* screen = &screens[pos]; */
/* memcpy(TERM_BUF, screen->buffer, sizeof(screen->buffer)); */
/* update_cursor(); */
/* if (TERM_BUF[0] == vga_entry(' ', VGA_COLOR_WHITE)) */
/* kprintf(PROMPT); */
}
void terminal_set_bg_color(uint8_t bg_color)
void terminal_set_bg_color(uint32_t bg_color)
{
screen->color = bg_color << 4 | (screen->color & 0x0F);
screen->bg_color = bg_color;
}
void terminal_set_fg_color(uint8_t fg_color)
void terminal_set_fg_color(uint32_t fg_color)
{
screen->color = (screen->color & 0xF0) | fg_color;
screen->fg_color = screen->fg_color;
}
void terminal_set_color(uint8_t color)
void terminal_set_color(uint32_t fg_color, uint32_t bg_color)
{
screen->color = color;
screen->fg_color = fg_color;
screen->bg_color = bg_color;
}
void terminal_set_default_fg_color(uint8_t fg_color)
void terminal_set_default_fg_color(uint32_t fg_color)
{
screen->default_color = (screen->default_color & 0xF0) | fg_color;
screen->default_color = fg_color;
}
void terminal_set_default_bg_color(uint8_t bg_color)
{
screen->default_color = bg_color << 4 | (screen->default_color & 0x0F);
}
uint8_t terminal_get_default_color(void)
uint32_t terminal_get_default_color(void)
{
return screen->default_color;
}
uint8_t terminal_get_char(int column, int row)
uint8_t terminal_get_char(int x, int y)
{
return screen->buffer[row * VGA_WIDTH + column];
return screen->buffer[y * SCREEN_WIDTH + x];
}
void terminal_putentryat(char c, uint32_t color, size_t x, size_t y)
void terminal_putentryat(char c, uint32_t fg_color, uint32_t bg_color, size_t x,
size_t y)
{
struct font node = eating_pasta_regular_32_font[(int)c];
char *glyph = node.bitmap;
for (size_t cy = 0; cy < node.height; cy++)
for (size_t cx = 0; cx < node.width; cx++)
for (size_t cy = 0; cy < node.height; cy++) {
for (size_t cx = 0; cx < node.width; cx++) {
if (glyph[cy * node.width + cx] == '#')
put_pixel(color, x + cx, y + cy);
/* const size_t index = y * VGA_WIDTH + x; */
/* TERM_BUF[index] = vga_entry(c, color); */
put_pixel(fg_color, x + cx, y + cy);
else if (bg_color)
put_pixel(bg_color, x + cx, y + cy);
}
}
}
static void terminal_scroll(void)
{
screen->row--;
for (size_t i = 0; i < VGA_WIDTH * (VGA_HEIGHT - 1); i++)
TERM_BUF[i] = TERM_BUF[i + VGA_WIDTH];
for (size_t i = 0; i < VGA_WIDTH; i++)
terminal_putentryat(' ', VGA_COLOR_WHITE, i, VGA_HEIGHT - 1);
// TODO
/* screen->row--; */
/* for (size_t i = 0; i < SCREEN_WIDTH * (SCREEN_HEIGHT - 1); i++) */
/* TERM_BUF[i] = TERM_BUF[i + SCREEN_WIDTH]; */
/* for (size_t i = 0; i < SCREEN_WIDTH; i++) */
/* terminal_putentryat(' ', VGA_COLOR_WHITE, i, SCREEN_HEIGHT - 1);
*/
}
static void terminal_new_line(void)
{
screen->column = 0;
if (++screen->row == VGA_HEIGHT)
if (++screen->row == SCREEN_HEIGHT)
terminal_scroll();
}
static uint8_t get_entry_color(uint16_t vga_entry)
void terminal_change_default_color(uint32_t color)
{
return vga_entry >> 8;
// TODO
/* terminal_set_color(color); */
/* for (size_t y = 0; y < SCREEN_HEIGHT; y++) { */
/* for (size_t x = 0; x < SCREEN_WIDTH; x++) { */
/* const size_t index = y * SCREEN_WIDTH + x; */
/* uint8_t entry_color = get_entry_color(TERM_BUF[index]);
*/
/* TERM_BUF[index] = vga_entry( */
/* TERM_BUF[index], */
/* entry_color == screen->default_color ? color */
/* : entry_color);
*/
/* } */
/* } */
/* memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer)); */
/* screen->default_color = color; */
/* screen->color = color; */
}
void terminal_change_default_color(uint8_t color)
{
terminal_set_color(color);
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
uint8_t entry_color = get_entry_color(TERM_BUF[index]);
TERM_BUF[index] = vga_entry(
TERM_BUF[index],
entry_color == screen->default_color ? color
: entry_color);
}
}
memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer));
screen->default_color = color;
screen->color = color;
}
void terminal_change_default_fg_color(uint8_t fg_color)
void terminal_change_default_fg_color(uint32_t fg_color)
{
terminal_set_fg_color(fg_color);
terminal_change_default_color(screen->color);
terminal_change_default_color(screen->fg_color);
}
void terminal_clear(void)
{
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
TERM_BUF[index] = vga_entry(' ', screen->color);
}
}
memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer));
screen->column = 0;
screen->row = 0;
// TODO
/* for (size_t y = 0; y < SCREEN_HEIGHT; y++) { */
/* for (size_t x = 0; x < SCREEN_WIDTH; x++) { */
/* const size_t index = y * SCREEN_WIDTH + x; */
/* TERM_BUF[index] = vga_entry(' ', screen->color); */
/* } */
/* } */
/* memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer)); */
/* screen->column = 0; */
/* screen->row = 0; */
}
int terminal_putchar(char c)
{
terminal_putentryat(c, 0xCCFFE5, 10, 10);
/* if (c == '\r') */
/* screen->column = 0; */
/* else if (c == '\n') */
/* terminal_new_line(); */
/* if (!isprint(c)) */
/* return 1; */
/* terminal_putentryat(c, screen->color, screen->column, screen->row);
*/
/* if (++screen->column == VGA_WIDTH) */
/* terminal_new_line(); */
/* return 1; */
terminal_putentryat(c, 0xCCFFE5, 0xCCCC00, 10, 10);
if (c == '\r')
screen->column = 0;
else if (c == '\n')
terminal_new_line();
if (!isprint(c))
return 1;
terminal_putentryat(c, screen->fg_color, screen->bg_color,
screen->column, screen->row);
if (++screen->column == SCREEN_WIDTH)
terminal_new_line();
return 1;
}
int terminal_write(const char *data, size_t size)
@ -191,7 +175,7 @@ int terminal_writestring(const char *data)
void update_cursor(void)
{
uint16_t pos = screen->row * VGA_WIDTH + screen->column;
uint16_t pos = screen->row * SCREEN_WIDTH + screen->column;
outb(0x3D4, 0x0F);
outb(0x3D5, pos & 0xFF);
@ -206,14 +190,14 @@ void move_cursor(int direction)
if (screen->column) {
screen->column--;
} else if (screen->row) {
screen->column = VGA_WIDTH - 1;
screen->column = SCREEN_WIDTH - 1;
screen->row--;
}
break;
case RIGHT:
if (screen->column < VGA_WIDTH - 1) {
if (screen->column < SCREEN_WIDTH - 1) {
screen->column++;
} else if (screen->row < VGA_HEIGHT - 1) {
} else if (screen->row < SCREEN_HEIGHT - 1) {
screen->column = 0;
screen->row++;
} else {
@ -240,7 +224,7 @@ void set_color_level(int level)
VGA_COLOR_LIGHT_GREY};
if (level == 0) {
terminal_set_color(screen->default_color);
terminal_set_color(screen->default_color, 0);
return;
}
terminal_set_fg_color(color_translation[level]);