2018-07-20 15:41:58 +02:00
|
|
|
#include "vga.h"
|
|
|
|
#include "io.h"
|
2020-08-20 23:54:01 +02:00
|
|
|
#include "irq.h"
|
2021-01-24 23:51:21 +01:00
|
|
|
#include "klibc.h"
|
2018-07-20 15:41:58 +02:00
|
|
|
|
|
|
|
static uint vgaBgColor;
|
|
|
|
static uint vgaColor;
|
|
|
|
static int line, col;
|
|
|
|
|
2021-01-24 23:51:21 +01:00
|
|
|
static volatile short *vga = (short *)VGA_ADDR;
|
|
|
|
static void clearScreen(uint bgColor);
|
2021-01-25 14:00:06 +01:00
|
|
|
static void cursorMove(int x, int y);
|
|
|
|
static void cursorEnable(uint8_t cursor_start, uint8_t cursor_end);
|
|
|
|
static void printCharDetails(char str, uint color, uint bgColor, int startX, int startY);
|
2021-01-24 23:51:21 +01:00
|
|
|
|
2018-11-08 22:08:27 +01:00
|
|
|
int VGASetup(uint bgColor, uint color)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2020-04-27 00:14:37 +02:00
|
|
|
vgaBgColor = bgColor;
|
|
|
|
vgaColor = color;
|
2021-01-24 23:51:21 +01:00
|
|
|
line = 0;
|
|
|
|
col = 0;
|
2020-04-27 00:14:37 +02:00
|
|
|
clearScreen(bgColor);
|
2021-01-25 14:00:06 +01:00
|
|
|
cursorEnable(14, 15);
|
2020-04-27 00:14:37 +02:00
|
|
|
return 0;
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-24 23:51:21 +01:00
|
|
|
static void clearScreen(uint bgColor)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2020-08-20 23:54:01 +02:00
|
|
|
uint32_t flags;
|
2021-01-25 20:05:38 +01:00
|
|
|
long int colorAttr = bgColor << 12;
|
2020-08-20 23:54:01 +02:00
|
|
|
|
|
|
|
disable_IRQs(flags);
|
2020-04-27 00:14:37 +02:00
|
|
|
for (int i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++) {
|
|
|
|
vga[i] = colorAttr;
|
|
|
|
}
|
2020-08-20 23:54:01 +02:00
|
|
|
restore_IRQs(flags);
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:00:06 +01:00
|
|
|
void VGAclearLine(uint bgColor, uint line)
|
2018-11-16 19:56:38 +01:00
|
|
|
{
|
2020-08-20 23:54:01 +02:00
|
|
|
uint32_t flags;
|
2021-01-25 20:05:38 +01:00
|
|
|
long int colorAttr = bgColor << 12;
|
2020-08-20 23:54:01 +02:00
|
|
|
|
2021-01-24 23:51:21 +01:00
|
|
|
if (line >= VGA_HEIGHT)
|
|
|
|
return;
|
|
|
|
|
2020-08-20 23:54:01 +02:00
|
|
|
disable_IRQs(flags);
|
2020-04-27 00:14:37 +02:00
|
|
|
for (uint i = VGA_WIDTH * line; i < VGA_WIDTH * (line + 1); i++) {
|
|
|
|
vga[i] = colorAttr;
|
|
|
|
}
|
2020-08-20 23:54:01 +02:00
|
|
|
restore_IRQs(flags);
|
2018-11-16 19:56:38 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:00:06 +01:00
|
|
|
void VGAPrintf(uint color, uint bgColor, int startX, int startY, const char *format, ...)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2021-01-24 23:51:21 +01:00
|
|
|
int flags;
|
|
|
|
char tmp[VGA_WIDTH];
|
2021-01-25 20:05:38 +01:00
|
|
|
int idx = 0;
|
2021-01-24 23:51:21 +01:00
|
|
|
|
|
|
|
disable_IRQs(flags);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
2021-01-25 20:05:38 +01:00
|
|
|
int ret = vsnprintf(tmp, sizeof(tmp), format, ap);
|
2021-01-24 23:51:21 +01:00
|
|
|
while (ret > 0 && tmp[idx]) {
|
2021-01-25 20:05:38 +01:00
|
|
|
printCharDetails(tmp[idx++], color, bgColor, startX++, startY);
|
2020-04-27 00:14:37 +02:00
|
|
|
}
|
2021-01-24 23:51:21 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
restore_IRQs(flags);
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:00:06 +01:00
|
|
|
static void printCharDetails(const char str, uint color, uint bgColor, int startX, int startY)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2021-01-25 20:05:38 +01:00
|
|
|
int pos = VGA_WIDTH * startY + startX;
|
|
|
|
|
|
|
|
if (pos > VGA_WIDTH * VGA_HEIGHT || pos < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8;
|
|
|
|
vga[pos] = colorAttr | str;
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:00:06 +01:00
|
|
|
void VGAScrollUp(void)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2021-01-25 20:05:38 +01:00
|
|
|
long int colorAttr = vgaBgColor << 12;
|
2020-08-20 23:54:01 +02:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
disable_IRQs(flags);
|
2021-01-25 20:05:38 +01:00
|
|
|
for (int i = 1; i < VGA_HEIGHT - VGA_STATUS_LINE_HEIGHT;
|
|
|
|
i++) { // last line is status line. Do not scroll it
|
2020-04-27 00:14:37 +02:00
|
|
|
memcpy((void *)&vga[VGA_WIDTH * (i - 1)], (void *)&vga[VGA_WIDTH * i],
|
|
|
|
VGA_WIDTH * sizeof(short));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < VGA_WIDTH; i++) {
|
2021-01-25 20:05:38 +01:00
|
|
|
vga[(VGA_HEIGHT - 1 - VGA_STATUS_LINE_HEIGHT) * VGA_WIDTH + i] = colorAttr;
|
2020-04-27 00:14:37 +02:00
|
|
|
}
|
2020-08-20 23:54:01 +02:00
|
|
|
restore_IRQs(flags);
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:00:06 +01:00
|
|
|
void VGAPutc(const char str)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2020-08-20 23:54:01 +02:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
disable_IRQs(flags);
|
2020-04-27 00:14:37 +02:00
|
|
|
if (str == '\n') {
|
|
|
|
line++;
|
|
|
|
col = 0;
|
2021-01-25 20:05:38 +01:00
|
|
|
if (line >= VGA_HEIGHT - VGA_STATUS_LINE_HEIGHT) {
|
2021-01-25 14:00:06 +01:00
|
|
|
VGAScrollUp();
|
2020-04-27 00:14:37 +02:00
|
|
|
line--;
|
|
|
|
}
|
|
|
|
} else if (str == '\r') {
|
|
|
|
col = 0;
|
|
|
|
} else if (str == '\b') {
|
|
|
|
col--;
|
|
|
|
if (col < 0) {
|
|
|
|
col = VGA_WIDTH - 1;
|
|
|
|
line--;
|
2021-01-25 20:05:38 +01:00
|
|
|
if (line < 0)
|
|
|
|
line = 0;
|
2020-04-27 00:14:37 +02:00
|
|
|
}
|
|
|
|
printCharDetails(' ', vgaColor, vgaBgColor, col, line);
|
|
|
|
} else {
|
|
|
|
printCharDetails(str, vgaColor, vgaBgColor, col++, line);
|
|
|
|
if (col == VGA_WIDTH) {
|
|
|
|
col = 0;
|
|
|
|
line++;
|
|
|
|
}
|
2021-01-25 20:05:38 +01:00
|
|
|
if (line >= VGA_HEIGHT - VGA_STATUS_LINE_HEIGHT) {
|
2021-01-25 14:00:06 +01:00
|
|
|
VGAScrollUp();
|
2020-04-27 00:14:37 +02:00
|
|
|
line--;
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 15:41:58 +02:00
|
|
|
|
2021-01-24 23:51:21 +01:00
|
|
|
cursorMove(col, line);
|
2020-08-20 23:54:01 +02:00
|
|
|
restore_IRQs(flags);
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|
2018-11-14 18:03:11 +01:00
|
|
|
|
2021-01-25 14:00:06 +01:00
|
|
|
static void cursorEnable(uint8_t cursor_start, uint8_t cursor_end)
|
2018-11-14 18:03:11 +01:00
|
|
|
{
|
2020-04-27 00:14:37 +02:00
|
|
|
outb(0x3D4, 0x0A);
|
|
|
|
outb(0x3D5, (inb(0x3D5) & 0xC0) | cursor_start);
|
2018-11-14 18:03:11 +01:00
|
|
|
|
2020-04-27 00:14:37 +02:00
|
|
|
outb(0x3D4, 0x0B);
|
|
|
|
outb(0x3D5, (inb(0x3D5) & 0xE0) | cursor_end);
|
2018-11-14 18:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cursorDisable(void)
|
|
|
|
{
|
2020-04-27 00:14:37 +02:00
|
|
|
outb(0x3D4, 0x0A);
|
|
|
|
outb(0x3D5, 0x20);
|
2018-11-14 18:03:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 14:00:06 +01:00
|
|
|
static void cursorMove(int x, int y)
|
2018-11-14 18:03:11 +01:00
|
|
|
{
|
2021-01-25 20:05:38 +01:00
|
|
|
long int colorAttr = (vgaBgColor << 4 | (vgaColor & 0x0f)) << 8;
|
|
|
|
uint16_t pos = y * VGA_WIDTH + x;
|
|
|
|
vga[pos] = colorAttr;
|
2020-04-27 00:14:37 +02:00
|
|
|
|
|
|
|
outb(0x3D4, 0x0F);
|
|
|
|
outb(0x3D5, (uint8_t)(pos & 0xFF));
|
|
|
|
outb(0x3D4, 0x0E);
|
|
|
|
outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF));
|
2018-11-14 18:03:11 +01:00
|
|
|
}
|