Implemented some console methods and fixed a bug in the console, copying the behaviour of background color in

windows console applications.
This commit is contained in:
José Pedro 2016-06-24 00:41:30 +01:00
parent 610f2b652a
commit fbac0cabcf
2 changed files with 111 additions and 84 deletions

View file

@ -8,82 +8,89 @@ using Cosmos.Debug.Kernel;
namespace Cosmos.HAL { namespace Cosmos.HAL {
// Dont hold state here. This is a raw to hardware class. Virtual screens should be done // Dont hold state here. This is a raw to hardware class. Virtual screens should be done
// by memory moves // by memory moves
public class TextScreen : TextScreenBase { public class TextScreen : TextScreenBase
protected byte Color = 0x0F; // White
protected UInt16 mClearCellValue;
protected UInt32 mRow2Addr;
protected UInt32 mScrollSize;
protected Int32 mCursorSize = 25; // 25 % as C# Console class
protected bool mCursorVisible = true;
protected Core.IOGroup.TextScreen IO = new Cosmos.Core.IOGroup.TextScreen();
protected readonly MemoryBlock08 mRAM;
public TextScreen() {
if (this is TextScreen)
{
TextScreenHelpers.Debug("this is TextScreen");
}
else
{
TextScreenHelpers.Debug("ERROR: This is not of type TextScreen!");
}
mRAM = IO.Memory.Bytes;
// Set the Console default colors: White foreground on Black background, the default value of mClearCellValue is set there too as it is linked with the Color
SetColors(ConsoleColor.White, ConsoleColor.Black);
mRow2Addr = (UInt32)(Cols * 2);
mScrollSize = (UInt32)(Cols * (Rows - 1) * 2);
SetCursorSize(mCursorSize);
SetCursorVisible(mCursorVisible);
TextScreenHelpers.Debug("End of TextScreen..ctor");
}
public override UInt16 Rows { get { return 25; } }
public override UInt16 Cols { get { return 80; } }
public override void Clear() {
TextScreenHelpers.Debug("Clearing screen with value ");
TextScreenHelpers.DebugNumber(mClearCellValue);
IO.Memory.Fill(mClearCellValue);
}
public override void ScrollUp()
{ {
IO.Memory.MoveDown(0, mRow2Addr, mScrollSize); protected byte Color = 0x0F; // White
//IO.Memory.Fill(mScrollSize, mRowSize32, mClearCellValue32); protected UInt16 mBackgroundClearCellValue;
IO.Memory.Fill(mScrollSize, Cols, mClearCellValue); protected UInt16 mTextClearCellValue;
} protected UInt32 mRow2Addr;
protected UInt32 mScrollSize;
protected Int32 mCursorSize = 25; // 25 % as C# Console class
protected bool mCursorVisible = true;
public override char this[int aX, int aY] protected Core.IOGroup.TextScreen IO = new Cosmos.Core.IOGroup.TextScreen();
{ protected readonly MemoryBlock08 mRAM;
get {
var xScreenOffset = (UInt32)((aX + aY * Cols) * 2);
return (char)mRAM[xScreenOffset];
}
set {
var xScreenOffset = (UInt32)((aX + aY * Cols) * 2);
mRAM[xScreenOffset] = (byte)value;
mRAM[xScreenOffset + 1] = Color;
}
}
public override void SetColors(ConsoleColor aForeground, ConsoleColor aBackground) { public TextScreen()
Color = (byte)((byte)(aForeground) | ((byte)(aBackground) << 4)); {
// The Color | the NUL character this is used to Clear the Screen if (this is TextScreen)
mClearCellValue = (UInt16)(Color << 8 | 0x00); {
TextScreenHelpers.Debug("this is TextScreen");
}
else
{
TextScreenHelpers.Debug("ERROR: This is not of type TextScreen!");
}
mRAM = IO.Memory.Bytes;
// Set the Console default colors: White foreground on Black background, the default value of mClearCellValue is set there too as it is linked with the Color
SetColors(ConsoleColor.White, ConsoleColor.Black);
mBackgroundClearCellValue = mTextClearCellValue;
mRow2Addr = (UInt32)(Cols * 2);
mScrollSize = (UInt32)(Cols * (Rows - 1) * 2);
SetCursorSize(mCursorSize);
SetCursorVisible(mCursorVisible);
TextScreenHelpers.Debug("End of TextScreen..ctor");
} }
public override void SetCursorPos(int aX, int aY) public override UInt16 Rows { get { return 25; } }
{ public override UInt16 Cols { get { return 80; } }
char xPos = (char)((aY * Cols) + aX);
// Cursor low byte to VGA index register public override void Clear() {
IO.Idx3.Byte = 0x0F; TextScreenHelpers.Debug("Clearing screen with value ");
IO.Data3.Byte = (byte)(xPos & 0xFF); TextScreenHelpers.DebugNumber(mTextClearCellValue);
// Cursor high byte to VGA index register IO.Memory.Fill(mTextClearCellValue);
IO.Idx3.Byte = 0x0E; mBackgroundClearCellValue = mTextClearCellValue;
IO.Data3.Byte = (byte)(xPos >> 8); }
}
public override void ScrollUp()
{
IO.Memory.MoveDown(0, mRow2Addr, mScrollSize);
//IO.Memory.Fill(mScrollSize, mRowSize32, mClearCellValue32);
IO.Memory.Fill(mScrollSize, Cols, mBackgroundClearCellValue);
}
public override char this[int aX, int aY]
{
get
{
var xScreenOffset = (UInt32)((aX + aY * Cols) * 2);
return (char)mRAM[xScreenOffset];
}
set
{
var xScreenOffset = (UInt32)((aX + aY * Cols) * 2);
mRAM[xScreenOffset] = (byte)value;
mRAM[xScreenOffset + 1] = Color;
}
}
public override void SetColors(ConsoleColor aForeground, ConsoleColor aBackground)
{
Color = (byte)((byte)(aForeground) | ((byte)(aBackground) << 4));
// The Color | the NUL character this is used to Clear the Screen
mTextClearCellValue = (UInt16)(Color << 8 | 0x00);
}
public override void SetCursorPos(int aX, int aY)
{
char xPos = (char)((aY * Cols) + aX);
// Cursor low byte to VGA index register
IO.Idx3.Byte = 0x0F;
IO.Data3.Byte = (byte)(xPos & 0xFF);
// Cursor high byte to VGA index register
IO.Idx3.Byte = 0x0E;
IO.Data3.Byte = (byte)(xPos >> 8);
}
public override byte GetColor() public override byte GetColor()
{ {
return Color; return Color;

View file

@ -57,8 +57,7 @@ namespace Cosmos.System.Plugs.System
public static bool get_CapsLock() public static bool get_CapsLock()
{ {
WriteLine("Not implemented: get_CapsLock"); return Global.CapsLock;
return false;
} }
public static int get_CursorLeft() public static int get_CursorLeft()
@ -80,7 +79,15 @@ namespace Cosmos.System.Plugs.System
// for now: // for now:
return; return;
} }
xConsole.X = x;
if (x < get_WindowWidth())
{
xConsole.X = x;
}
else
{
WriteLine("x must be lower than the console width!");
}
} }
public static int get_CursorSize() public static int get_CursorSize()
@ -124,13 +131,25 @@ namespace Cosmos.System.Plugs.System
// for now: // for now:
return; return;
} }
GetConsole().Y = y;
if (y < get_WindowHeight())
{
xConsole.Y = y;
}
else
{
WriteLine("y must be lower than the console height!");
}
} }
public static bool get_CursorVisible() public static bool get_CursorVisible()
{ {
WriteLine("Not implemented: get_CursorVisible"); var xConsole = GetConsole();
return false; if (xConsole == null)
{
return false;
}
return GetConsole().CursorVisible;
} }
public static void set_CursorVisible(bool value) public static void set_CursorVisible(bool value)
@ -199,8 +218,7 @@ namespace Cosmos.System.Plugs.System
public static bool get_NumberLock() public static bool get_NumberLock()
{ {
WriteLine("Not implemented: get_NumberLock"); return Global.NumLock;
return false;
} }
//public static TextWriter get_Out() { //public static TextWriter get_Out() {
@ -494,7 +512,8 @@ namespace Cosmos.System.Plugs.System
public static void ResetColor() public static void ResetColor()
{ {
WriteLine("Not implemented: ResetColor"); set_BackgroundColor(ConsoleColor.Black);
set_ForegroundColor(ConsoleColor.White);
} }
public static void SetBufferSize(int width, int height) public static void SetBufferSize(int width, int height)
@ -504,7 +523,8 @@ namespace Cosmos.System.Plugs.System
public static void SetCursorPosition(int left, int top) public static void SetCursorPosition(int left, int top)
{ {
WriteLine("Not implemented: SetCursorPosition"); set_CursorLeft(left);
set_CursorTop(top);
} }
//public static void SetError(TextWriter newError) { //public static void SetError(TextWriter newError) {