SoraOS/colors.h

104 lines
2.5 KiB
C

#ifndef COLORS_H
#define COLORS_H
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define VGA_MEMORY_ADDRESS 0xB8000
/* IN VGA - Video Memory Arrya:
* short
* 1s byte (left most) -> TEXT COLOR
* 2s byte -> BACK COLOR
* 3rd and 4rt byte -> TEXT (unsigned char) AKA CodePoint
*/
typedef struct __attribute__((packed)) _VGA {
unsigned char codepoint; // 3rd and 4th byte
unsigned char color; // 1st byte: text color, 2nd byte: background color
// LAST BIT IS BLINKING FLAG
} _VGA;
typedef enum _VGA_COLOR {
BLACK = 0x0,
BLUE = 0x1,
GREEN = 0x2,
CYAN = 0x3,
RED = 0x4,
MAGENTA = 0x5,
BROWN = 0x6,
LIGHT_GREY = 0x7,
DARK_GREY = 0x8,
LIGHT_BLUE = 0x9,
LIGHT_GREEN = 0xA,
LIGHT_CYAN = 0xB,
LIGHT_RED = 0xC,
LIGHT_MAGENTA = 0xD,
YELLOW = 0xE,
WHITE = 0xF
} _VGA_COLOR;
typedef struct _VGA_screen_space{
unsigned int line;
unsigned int column;
} _VGA_screen_space;
static _VGA_screen_space cursor_pos = {0, 0};
/*
* END OF DECLARATIONS
*/
/*
* START OF FUNCTIONS
*/
// Convert _VGA_COLOR to color byte
static inline unsigned char __vga_color_byte(_VGA_COLOR fg, _VGA_COLOR bg, int blink) {
unsigned char color = (bg << 4) | fg;
if (blink) {
color |= 0x80; // Set the blinking BIT
// to the highest bit
}
return color;
}
// Get pointer to VGA memory
static inline volatile _VGA* __vga_memory() {
return (volatile _VGA*)VGA_MEMORY_ADDRESS;
}
static inline void __clear_screen() {
volatile _VGA* vga = __vga_memory();
for (int i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++) {
vga[i].codepoint = ' ';
vga[i].color = __vga_color_byte(WHITE, BLACK, 0);
}
}
static inline int __put_char_VGA_POS(_VGA _c, _VGA_screen_space _position, int _blink) {
if(_position.line > VGA_HEIGHT || _position.column > VGA_WIDTH) return 0;
volatile _VGA* vga = __vga_memory();
vga[(_position.line * VGA_WIDTH) + _position.column].codepoint = _c.codepoint;
vga[(_position.line * VGA_WIDTH) + _position.column].color = _c.color;
return 1;
}
static inline int __put_char(int column, int line, unsigned codepoint, _VGA_COLOR foreground, _VGA_COLOR background, int _blink) {
_VGA __temp_vga;
__temp_vga.color = __vga_color_byte(foreground, background, _blink);
__temp_vga.codepoint = codepoint;
_VGA_screen_space __temp_vga_space;
__temp_vga_space.column = column;
__temp_vga_space.line = line;
return __put_char_VGA_POS(__temp_vga, __temp_vga_space, _blink);
}
#endif