- Full TinyBasic interpreter (InputInterpreter) with tick-based execution: PRINT, LET, IF/THEN, GOTO, GOSUB/RETURN, INPUT, FOR/NEXT, REM, END, RUN, LIST, NEW, SAVE, LOAD, POKE/PEEK, POKEX/PEEKX, RND, MOD - Expression evaluator with hex literals ($xx), operator precedence, variables A-Z, and functions (PEEK, PEEKX, RND) - CTRL+C / CTRL+PAUSE to break running programs - DiskManager: USB stick detection via netlink, udisksctl mount, Micronona stick initialization - ScrollLockLed: Scroll Lock LED indicates stick presence via sysfs - ConsoleHelper: ReadLine follows ASCII-31 wrap markers across lines - udev rule for LED brightness write permissions Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
186 lines
7.3 KiB
C#
186 lines
7.3 KiB
C#
public class ConsoleHelper
|
||
{
|
||
private readonly MemoryModel _mem;
|
||
|
||
public ConsoleHelper(MemoryModel mem)
|
||
{
|
||
_mem = mem;
|
||
}
|
||
|
||
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, 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()
|
||
{
|
||
ReadLine(); // Optional: Aktuelle Zeile lesen und verarbeiten
|
||
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 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;
|
||
|
||
// Erste Zeile der logischen Zeile finden:
|
||
// Solange Spalte 0 der aktuellen Zeile ASCII 31 enthält, ist sie eine Folgezeile.
|
||
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; // Umbruch-Marker überspringen
|
||
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 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;
|
||
byte column = (byte)(offset % MemoryModel.TextCols);
|
||
if (column == 0)
|
||
{
|
||
byte color = Rgb332Mid(MemoryModel.DefaultBg,MemoryModel.DefaultFg);
|
||
_mem.WriteFlat(MemoryModel.TextBankIndex * MemoryModel.BankSize + offset, 31, color, 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));
|
||
}
|
||
}
|