luxos/Kernel/debug/console-base.c

132 lines
2.6 KiB
C
Raw Permalink Normal View History

#include <debugio.h>
#include <memory.h>
#include <stdio.h>
uint8* VideoPtr = (uint8*) 0xb8000;
Point ConsoleCursor = {0, 0};
UPoint ConsoleSize = {80, 25};
uint8 ConsoleDefaultColor = CONSOLE_DEFAULT_COLOR;
/**************************************
* Basic console operations *
**************************************/
void ConsoleClear()
{
uint32 temp;
for (temp = 0; temp < ConsoleSize.X * ConsoleSize.Y; temp++)
{
VideoPtr[2 * temp] = 0;
VideoPtr[2 * temp + 1] = ConsoleDefaultColor;
}
ConsoleCursor.X = ConsoleCursor.Y = 0;
ConsoleCursorUpdateHardware();
}
void ConsoleScroll (uint32 lines)
{
// Sanity check, don't scroll too much
if (lines > ConsoleSize.Y) lines = ConsoleSize.Y;
uint32 x, y;
for (y = 0; y < ConsoleSize.Y - lines; y++)
for (x = 0; x < 2*ConsoleSize.X; x++)
VideoPtr[y * ConsoleSize.X * 2 + x] = VideoPtr[(y+lines)*ConsoleSize.X*2 + x];
for (y = ConsoleSize.Y - lines; y < ConsoleSize.Y; y++)
for (x = 0; x < ConsoleSize.X; x++)
{
VideoPtr[2 * (y * ConsoleSize.X + x)] = 0;
VideoPtr[2 * (y * ConsoleSize.X + x) + 1] = ConsoleDefaultColor;
}
ConsoleCursor.Y -= lines;
}
/**************************************
* Cursor position *
**************************************/
Point ConsoleGetCursor()
{
return ConsoleCursor;
}
void ConsoleSetCursor(Point p)
{
ConsoleCursor = p;
}
void ConsoleCursorIncreasePos (int32 delta)
{
// Increase X, Y positions
ConsoleCursor.Y += delta / ConsoleSize.X;
ConsoleCursor.X += delta % ConsoleSize.X;
// Next line if X > Screen width
while (ConsoleCursor.X >= (int32)ConsoleSize.X)
{
ConsoleCursor.Y++;
ConsoleCursor.X -= ConsoleSize.X;
}
// Previous line if X < 0
while (ConsoleCursor.X < 0)
{
ConsoleCursor.Y--;
ConsoleCursor.X += ConsoleSize.X;
}
// Scroll if Y > Screen height
if (ConsoleCursor.Y >= (int32)ConsoleSize.Y)
ConsoleScroll(1);
}
void ConsoleCursorNewline()
{
ConsoleCursor.X = 0;
if (++ConsoleCursor.Y >= (int32)ConsoleSize.Y)
ConsoleScroll(1);
}
void ConsoleCursorUpdateHardware()
{
uint16 temp = (uint16) (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X);
uint16* port = (uint16*) 0x463 ;
outportb(*port, 14);
outportb(*port + 1, (temp >> 8) & 0xff);
outportb(*port, 15);
outportb(*port + 1, temp & 0xff);
}
uint8 Color (uint8 back, uint8 fore)
{
return ((back<<4) | fore);
}
void ConsoleSetDefaultColor(uint8 color)
{
ConsoleDefaultColor = color;
}
uint8 ConsoleGetDefaultColor ()
{
return ConsoleDefaultColor;
}
UPoint ConsoleGetSize()
{
return ConsoleSize;
}
void ConsoleCursorGoto(Point p)
{
if (p.X >= 0 && p.X < (int32)ConsoleSize.X) ConsoleCursor.X = p.X;
if (p.Y >= 0 && p.Y < (int32)ConsoleSize.Y) ConsoleCursor.Y = p.Y;
}