public class ConsoleHelper { private readonly MemoryModel _mem; public const byte DefaultFg = 0x1C; // Bright green: R=0, G=224, B=0 public const byte DefaultBg = 0x08; // Dark green: R=0, G=64, B=0 public byte CurrentFg { get; set; } public byte CurrentBg { get; set; } public ConsoleHelper(MemoryModel mem) { _mem = mem; CurrentFg = DefaultFg; CurrentBg = DefaultBg; } static byte Rgb332Mid(byte a, byte b) { int r = ((a >> 5) + (b >> 5)) / 2; int g = (((a >> 2) & 0x07) + ((b >> 2) & 0x07)) / 2; int bl = ((a & 0x03) + (b & 0x03)) / 2; return (byte)((r << 5) | (g << 2) | bl); } 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, CurrentFg, CurrentBg); } 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--; 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, CurrentFg, CurrentBg); _mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL, (byte)col); _mem.Write(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW, (byte)row); } public void NewLine() { ReadLine(); 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; 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)); } } int lastRow = (MemoryModel.TextRows - 1) * rowSize; for (int col = 0; col < MemoryModel.TextCols; col++) { _mem.WriteFlat(textBase + lastRow + col * MemoryModel.TextCellSize, 0x20, CurrentFg, CurrentBg); } } public string ReadLine() { 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 startRow = row; while (startRow > 0) { int firstCell = startRow * MemoryModel.TextCols * MemoryModel.TextCellSize; if (_mem.ReadFlat(textBase + firstCell) == 31) startRow--; else break; } var sb = new System.Text.StringBuilder(); for (int r = startRow; r <= row; r++) { int endCol = (r == row) ? col : MemoryModel.TextCols; for (int c = 0; c < endCol; c++) { int offset = (r * MemoryModel.TextCols + c) * MemoryModel.TextCellSize; byte ch = _mem.ReadFlat(textBase + offset); if (ch == 31) continue; sb.Append((char)ch); } } string result = sb.ToString(); Console.WriteLine($"[ConsoleHelper] Read: -{result}-"); return result; } 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 Cls() { int textBase = MemoryModel.TextBankIndex * MemoryModel.BankSize; for (int i = 0; i < MemoryModel.TextCols * MemoryModel.TextRows; i++) _mem.WriteFlat(textBase + i * MemoryModel.TextCellSize, 0x20, CurrentFg, CurrentBg); SetCursor(0, 0); } public void SetChar(int x, int y, byte ascii, byte fg, byte bg) { if (x < 0 || x >= MemoryModel.TextCols || y < 0 || y >= MemoryModel.TextRows) return; int textBase = MemoryModel.TextBankIndex * MemoryModel.BankSize; int offset = (y * MemoryModel.TextCols + x) * MemoryModel.TextCellSize; _mem.WriteFlat(textBase + offset, ascii, fg, bg); } 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) { var ascii = (byte)c; _mem.WriteFlat(MemoryModel.TextBankIndex * MemoryModel.BankSize + offset, ascii, CurrentFg, CurrentBg); offset += MemoryModel.TextCellSize; byte column = (byte)(offset % MemoryModel.TextCols); if (column == 0) { byte color = Rgb332Mid(CurrentBg, CurrentFg); _mem.WriteFlat(MemoryModel.TextBankIndex * MemoryModel.BankSize + offset, 31, color, CurrentBg); 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)); } }