micronona/ConsoleHelper.cs
2026-04-03 10:49:53 +02:00

131 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

public class ConsoleHelper
{
private readonly MemoryModel _mem;
public ConsoleHelper(MemoryModel mem)
{
_mem = mem;
}
public void Delete()
{
int col = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL);
int row = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW);
int textBase = MemoryModel.TextBankIndex * MemoryModel.BankSize;
int rowEnd = (row * MemoryModel.TextCols + MemoryModel.TextCols - 1) * MemoryModel.TextCellSize;
int startOffset = (row * MemoryModel.TextCols + col) * MemoryModel.TextCellSize;
for (int i = startOffset; i < rowEnd; i += MemoryModel.TextCellSize)
{
_mem.WriteFlat(textBase + i,
_mem.ReadFlat(textBase + i + MemoryModel.TextCellSize),
_mem.ReadFlat(textBase + i + MemoryModel.TextCellSize + 1),
_mem.ReadFlat(textBase + i + MemoryModel.TextCellSize + 2));
}
_mem.WriteFlat(textBase + rowEnd, 0x20, MemoryModel.DefaultFg, MemoryModel.DefaultBg);
}
public void Backspace()
{
int col = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL);
int row = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW);
if (col == 0 && row == 0) return;
if (col == 0) { col = MemoryModel.TextCols - 1; row--; }
else col--;
// Rest der Zeile nach links ziehen
int textBase = MemoryModel.TextBankIndex * MemoryModel.BankSize;
int rowEnd = (row * MemoryModel.TextCols + MemoryModel.TextCols - 1) * MemoryModel.TextCellSize;
int startOffset = (row * MemoryModel.TextCols + col) * MemoryModel.TextCellSize;
for (int i = startOffset; i < rowEnd; i += MemoryModel.TextCellSize)
{
_mem.WriteFlat(textBase + i,
_mem.ReadFlat(textBase + i + MemoryModel.TextCellSize),
_mem.ReadFlat(textBase + i + MemoryModel.TextCellSize + 1),
_mem.ReadFlat(textBase + i + MemoryModel.TextCellSize + 2));
}
// Letztes Zeichen der Zeile löschen
_mem.WriteFlat(textBase + rowEnd, 0x20, MemoryModel.DefaultFg, MemoryModel.DefaultBg);
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL, (byte)col);
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW, (byte)row);
}
public void NewLine()
{
int row = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW);
if (row + 1 >= MemoryModel.TextRows)
{
ScrollUp();
}
else
{
row++;
}
SetCursor(0, (byte)row);
}
public void MoveCursor(int dx, int dy)
{
int col = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL);
int row = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW);
col = Math.Clamp(col + dx, 0, MemoryModel.TextCols - 1);
row = Math.Clamp(row + dy, 0, MemoryModel.TextRows - 1);
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL, (byte)col);
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW, (byte)row);
}
public void ScrollUp()
{
int textBase = MemoryModel.TextBankIndex * MemoryModel.BankSize;
int rowSize = MemoryModel.TextCols * MemoryModel.TextCellSize;
// Zeilen 1–17 nach oben kopieren
for (int row = 0; row < MemoryModel.TextRows - 1; row++)
{
for (int i = 0; i < rowSize; i++)
{
_mem.WriteFlat(textBase + row * rowSize + i,
_mem.ReadFlat(textBase + (row + 1) * rowSize + i));
}
}
// Letzte Zeile mit Leerzeichen und Defaultfarben füllen
int lastRow = (MemoryModel.TextRows - 1) * rowSize;
for (int col = 0; col < MemoryModel.TextCols; col++)
{
_mem.WriteFlat(textBase + lastRow + col * MemoryModel.TextCellSize, 0x20, MemoryModel.DefaultFg, MemoryModel.DefaultBg);
}
}
public void SetCursor(byte x, byte y)
{
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL, x);
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW, y);
}
public void WriteText(string text)
{
if (_mem.Read(MemoryModel.IoBase + MemoryModel.REG_MODE) != 0) return;
var x = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL);
var y = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW);
var offset = (y * MemoryModel.TextCols + x) * MemoryModel.TextCellSize;
foreach (char c in text.ToArray())
{
var ascii = (byte)c;
_mem.WriteFlat(MemoryModel.TextBankIndex * MemoryModel.BankSize + offset, ascii, MemoryModel.DefaultFg, MemoryModel.DefaultBg);
offset += MemoryModel.TextCellSize;
while (offset >= MemoryModel.TextCellSize * MemoryModel.TextCols * MemoryModel.TextRows)
{
ScrollUp();
offset -= (MemoryModel.TextCellSize * MemoryModel.TextCols);
}
}
offset /= MemoryModel.TextCellSize;
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL, (byte)(offset % MemoryModel.TextCols));
_mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW, (byte)(offset / MemoryModel.TextCols));
}
}