micronona/Program.cs

871 lines
30 KiB
C#
Raw Permalink Normal View History

2026-04-03 10:49:53 +02:00
using SDL2;
using Tomlyn;
using Tomlyn.Model;
2026-04-03 10:49:53 +02:00
if (args.Length > 0 && args[0] == "--detect-keyboard")
{
string keymapDir = args.Length > 1 ? args[1] : "/usr/share/keymaps";
string? layout = KeyboardLayoutDetector.DetectInteractive(keymapDir);
if (layout is null) { Console.WriteLine("(undecidable)"); return 0; }
string? stickPath = DiskManager.FindMicrononaMount();
if (stickPath is null)
Console.WriteLine($"{layout} (no Micronona stick mounted — not saved)");
else
{
DiskManager.SaveKeyboardLayout(stickPath, layout);
Console.WriteLine($"{layout} (saved to {Path.Combine(stickPath, DiskManager.MarkerFile)})");
}
return 0;
}
2026-04-03 10:49:53 +02:00
const int ScreenWidth = 320;
const int ScreenHeight = 180;
const string Title = "Micronona";
const int Scale = 3;
string? runFile = null;
for (int i = 0; i < args.Length - 1; i++)
if (args[i] == "--run") { runFile = args[i + 1]; break; }
2026-04-03 10:49:53 +02:00
// -------------------------------------------------------------------------
// Memory
// -------------------------------------------------------------------------
bool isRawConsole = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISPLAY"))
&& string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WAYLAND_DISPLAY"));
if (isRawConsole)
{
Console.CancelKeyPress += (s, e) => e.Cancel = true;
}
else
{
Console.WriteLine("Running in developer mode");
}
var mem = new MemoryModel(ConsoleHelper.DefaultFg, ConsoleHelper.DefaultBg);
var console = new ConsoleHelper(mem);
var diskManager = new DiskManager();
2026-04-03 10:49:53 +02:00
var micrononaRenderer = new MicrononaRenderer(mem);
var consoleHelper = new ConsoleHelper(mem);
var cpu = new Cpu6809(mem, consoleHelper);
2026-04-03 10:49:53 +02:00
var inputInterpreter = new InputInterpreter(mem, consoleHelper, diskManager, cpu);
// Unit Separator (ASCII 31) für Eingabeaufforderung
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 31 * MemoryModel.FontGlyphHeight,
0x00, 0x60, 0x70, 0x78, 0x7E, 0x7E, 0x78, 0x70, 0x60, 0x00
);
// !
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 33 * MemoryModel.FontGlyphHeight,
0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// "
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 34 * MemoryModel.FontGlyphHeight,
0x00, 0x36, 0x66, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
);
// #
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 35 * MemoryModel.FontGlyphHeight,
0x00, 0x034, 0x34, 0x7E, 0x34, 0x7E, 0x34, 0x34, 0x00, 0x00
);
// $
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 36 * MemoryModel.FontGlyphHeight,
// 0x00, 0x18, 0x3E, 0x68, 0x3C, 0x16, 0x7C, 0x18, 0x00, 0x00
0x00, 0x08, 0x3E, 0x68, 0x3C, 0x16, 0x7C, 0x10, 0x00, 0x00
);
// '
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 39 * MemoryModel.FontGlyphHeight,
// 0x00, 0x18, 0x3E, 0x68, 0x3C, 0x16, 0x7C, 0x18, 0x00, 0x00
0x00, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
);
// (
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 40 * MemoryModel.FontGlyphHeight,
// 0x00, 0x08, 0x18, 0x30, 0x20, 0x20, 0x30, 0x18, 0x08, 0x00
//0x00,0x0C,0x08, 0x18,0x10,0x10,0x10,0x18,0x08, 0x0C,0x00
0x00,0x0C,0x18,0x30,0x30,0x30,0x30,0x18,0x0C,0x00
);
// )
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 41 * MemoryModel.FontGlyphHeight,
0x00,0x30,0x18,0x0C,0x0C,0x0C,0x0C,0x18,0x30,0x00
);
2026-04-03 10:49:53 +02:00
// *
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 42 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x34, 0x18, 0x7E, 0x18, 0x2C, 0x00, 0x00, 0x00
);
// +
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 43 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00, 0x00
);
// ,
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 44 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00
);
// -
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 45 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00
);
// .
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 46 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00
);
// /
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 47 * MemoryModel.FontGlyphHeight,
0x00, 0x04, 0x0C, 0x08, 0x18, 0x10, 0x30, 0x20, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// 0
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 48 * MemoryModel.FontGlyphHeight,
// 0x00, 0x18, 0x3C, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00, 0x00
0x00, 0x3C, 0x66, 0x6E, 0x7E, 0x76, 0x66, 0x3C, 0x00, 0x00
);
// 1
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 49 * MemoryModel.FontGlyphHeight,
0x00, 0x0C, 0x1C, 0x3C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00, 0x00
);
// 2
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 50 * MemoryModel.FontGlyphHeight,
// 0x00, 0x3C, 0x66, 0x0C, 0x18, 0x30, 0x60, 0x7F, 0x00, 0x00
0x00, 0x3C, 0x66, 0x06, 0x0C, 0x18, 0x30, 0x7E, 0x00, 0x00
);
// 3
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 51 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x66, 0x06, 0x1C, 0x06, 0x66, 0x3C, 0x00, 0x00
);
// 4
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 52 * MemoryModel.FontGlyphHeight,
0x00, 0x1C, 0x3C, 0x6C, 0x7E, 0x0C, 0x0C, 0x0C, 0x00, 0x00
);
// 5
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 53 * MemoryModel.FontGlyphHeight,
// 0x00, 0x7E, 0x60, 0x7C, 0x06, 0x06, 0x06, 0x7C, 0x00, 0x00
// 0x00, 0x7E, 0x60, 0x60, 0x7C, 0x06, 0x06, 0x7C, 0x00, 0x00
0x00, 0x7E, 0x60, 0x60, 0x7C, 0x06, 0x66, 0x3C, 0x00, 0x00
);
// 6
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 54 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x60, 0x60, 0x7C, 0x66, 0x66, 0x3C, 0x00, 0x00
);
// 7
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 55 * MemoryModel.FontGlyphHeight,
0x00, 0x7E, 0x06, 0x0E, 0x0C, 0x1C, 0x18, 0x18, 0x00, 0x00
);
// 8
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 56 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x66, 0x66, 0x3C, 0x66, 0x66, 0x3C, 0x00, 0x00
);
// 9
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 57 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x3C, 0x00, 0x00
);
// :
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 58 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00
);
// <
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 60 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x00, 0x00, 0x00
);
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 61 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00
);
// >
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 62 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x30, 0x18, 0x0C, 0x18, 0x30, 0x00, 0x00, 0x00
);
// ?
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 63 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x66, 0x06, 0x0C, 0x18, 0x00, 0x18, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// A
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 65 * MemoryModel.FontGlyphHeight,
0x00, 0x18, 0x3C, 0x66, 0x7E, 0x66, 0x66, 0x66, 0x00, 0x00
);
// B
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 66 * MemoryModel.FontGlyphHeight,
0x00, 0x7C, 0x66, 0x66, 0x7C, 0x66, 0x66, 0x7C, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// C
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 67 * MemoryModel.FontGlyphHeight,
// 0x00, 0x3A, 0x66, 0x60,0x60, 0x60,0x66,0x3A,0x00,0x00
0x00, 0x3C, 0x66, 0x60, 0x60, 0x60, 0x66, 0x3C, 0x00, 0x00
);
// D
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 68 * MemoryModel.FontGlyphHeight,
0x00, 0x7C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7C, 0x00, 0x00
);
// E
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 69 * MemoryModel.FontGlyphHeight,
0x00, 0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x7E, 0x00, 0x00
);
// F
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 70 * MemoryModel.FontGlyphHeight,
0x00, 0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x60, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// G
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 71 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x66, 0x60, 0x6E, 0x66, 0x66, 0x3C, 0x00, 0x00
);
// H
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 72 * MemoryModel.FontGlyphHeight,
0x00, 0x66, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x66, 0x00, 0x00
);
// I
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 73 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00
);
// J
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 74 * MemoryModel.FontGlyphHeight,
0x00, 0x06, 0x06, 0x06, 0x06, 0x06, 0x66, 0x3C, 0x00, 0x00
);
// K
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 75 * MemoryModel.FontGlyphHeight,
0x00, 0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// L
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 76 * MemoryModel.FontGlyphHeight,
0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7E, 0x00, 0x00
);
// M
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 77 * MemoryModel.FontGlyphHeight,
// 0x00, 0x62, 0x76, 0x7E,0x6A, 0x62,0x62,0x62,0x00,0x00
// 0x00, 0x42, 0x66, 0x7e, 0x5a, 0x42, 0x66, 0x66
0x00, 0x42, 0x66, 0x7e, 0x7E, 0x66, 0x66, 0x66
// 0x00, 0x62, 0x76, 0x7E, 0x6A, 0x62, 0x62, 0x66, 0x00, 0x00
);
// N
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 78 * MemoryModel.FontGlyphHeight,
0x00, 0x66, 0x66, 0x76, 0x7E, 0x6E, 0x66, 0x66, 0x00, 0x00
);
// O
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 79 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00, 0x00
);
// P
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 80 * MemoryModel.FontGlyphHeight,
0x00, 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60, 0x60, 0x00, 0x00
// 0x00, 0x62, 0x76, 0x7E, 0x6A, 0x62, 0x62, 0x66, 0x00, 0x00
);
// Q
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 81 * MemoryModel.FontGlyphHeight,
0x00, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x6E, 0x3C, 0x06, 0x00
);
2026-04-03 10:49:53 +02:00
// R
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 82 * MemoryModel.FontGlyphHeight,
0x00, 0x7C, 0x66, 0x66, 0x7C, 0x78, 0x6C, 0x66, 0x00, 0x00
// 0x00, 0x62, 0x76, 0x7E, 0x6A, 0x62, 0x62, 0x66, 0x00, 0x00
);
// S
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 83 * MemoryModel.FontGlyphHeight,
// 0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00
0x00, 0x3C, 0x66, 0x60, 0x3C, 0x06, 0x66, 0x3C, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// T
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 84 * MemoryModel.FontGlyphHeight,
// 0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00
0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00
);
// U
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 85 * MemoryModel.FontGlyphHeight,
// 0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00
0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00, 0x00
);
// V
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 86 * MemoryModel.FontGlyphHeight,
0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00, 0x00
);
// W
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 87 * MemoryModel.FontGlyphHeight,
0x00, 0x62, 0x62, 0x6A, 0x7E, 0x76, 0x66, 0x42, 0x00, 0x00
);
// X
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 88 * MemoryModel.FontGlyphHeight,
0x00, 0x66, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x66, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// Y
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 89 * MemoryModel.FontGlyphHeight,
0x00, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00
);
// Z
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 90 * MemoryModel.FontGlyphHeight,
0x00, 0x7E, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x7E, 0x00, 0x00
);
// ^
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 94 * MemoryModel.FontGlyphHeight,
0x00, 0x18, 0x3C, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
);
// _
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 95 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00
);
// a
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 97 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x66, 0x3E, 0x00, 0x00
);
// b
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 98 * MemoryModel.FontGlyphHeight,
0x00, 0x60, 0x60, 0x60, 0x7C, 0x66, 0x76, 0x5C, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// c
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 99 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x3C, 0x66, 0x60, 0x60, 0x66, 0x3C, 0x00, 0x00
);
// d
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 100 * MemoryModel.FontGlyphHeight,
0x00, 0x06, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3E, 0x00, 0x00
);
// e
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 101 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x3C, 0x66, 0x7E, 0x60, 0x66, 0x3C, 0x00, 0x00
);
// f
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 102 * MemoryModel.FontGlyphHeight,
0x00, 0x0C, 0x18, 0x18, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x00
);
// g
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 103 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x3C, 0x66, 0x66, 0x66, 0x3E, 0x06, 0x3C, 0x00
);
// h
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 104 * MemoryModel.FontGlyphHeight,
0x00, 0x60, 0x60, 0x60, 0x7C, 0x66, 0x66, 0x66, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// i
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 105 * MemoryModel.FontGlyphHeight,
// 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x10, 0x18, 0x00, 0x00
// 0x00, 0x00, 0x18, 0x00, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00
0x00, 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00
);
// j
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 106 * MemoryModel.FontGlyphHeight,
0x00, 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x30, 0x00
);
// k
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 107 * MemoryModel.FontGlyphHeight,
0x00, 0x60, 0x60, 0x66, 0x6C, 0x78, 0x6C, 0x66, 0x00, 0x00
);
// l
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 108 * MemoryModel.FontGlyphHeight,
0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x1C, 0x00, 0x00
);
// m
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 109 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x5C, 0x7E, 0x6A, 0x6A, 0x6A, 0x6A, 0x00, 0x00
);
// n
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 110 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x5C, 0x76, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00
);
// o
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 111 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00, 0x00
);
// p
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 112 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60, 0x60, 0x00
);
// r
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 114 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x6E, 0x78, 0x70, 0x60, 0x60, 0x60, 0x00, 0x00
);
// s
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 115 * MemoryModel.FontGlyphHeight,
// 0x00, 0x00, 0x3C, 0x60, 0x38, 0x1C, 0x06, 0x3C, 0x00, 0x00
0x00, 0x00, 0x3E, 0x60, 0x38, 0x1C, 0x06, 0x7C, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// t
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 116 * MemoryModel.FontGlyphHeight,
0x00, 0x18, 0x18, 0x18, 0x3C, 0x18, 0x18, 0x0C, 0x00, 0x00
);
// u
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 117 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x6E, 0x3A, 0x00, 0x00
);
// v
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 118 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00, 0x00
);
// x
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 120 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x66, 0x66, 0x3C, 0x3C, 0x66, 0x66, 0x00, 0x00
);
// y
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 121 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x30, 0x00
2026-04-03 10:49:53 +02:00
);
// z
mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 122 * MemoryModel.FontGlyphHeight,
0x00, 0x00, 0x7E, 0x0C, 0x38,0x1C, 0x30, 0x7E, 0x00, 0x00
);
2026-04-03 10:49:53 +02:00
// -------------------------------------------------------------------------
// SDL init
// -------------------------------------------------------------------------
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO) < 0)
2026-04-03 10:49:53 +02:00
{
Console.Error.WriteLine($"SDL_Init failed: {SDL.SDL_GetError()}");
return 1;
}
SDL.SDL_StartTextInput();
var window = SDL.SDL_CreateWindow(
Title,
SDL.SDL_WINDOWPOS_CENTERED,
SDL.SDL_WINDOWPOS_CENTERED,
ScreenWidth * Scale,
ScreenHeight * Scale,
SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN
);
if (window == IntPtr.Zero)
{
Console.Error.WriteLine($"SDL_CreateWindow failed: {SDL.SDL_GetError()}");
SDL.SDL_Quit();
return 1;
}
var renderer = SDL.SDL_CreateRenderer(
window, -1,
SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC
);
if (renderer == IntPtr.Zero)
{
Console.Error.WriteLine($"SDL_CreateRenderer failed: {SDL.SDL_GetError()}");
SDL.SDL_DestroyWindow(window);
SDL.SDL_Quit();
return 1;
}
// Framebuffer texture: 320×180, RGB24 (expanded from RGB332 each frame)
var texture = SDL.SDL_CreateTexture(
renderer,
SDL.SDL_PIXELFORMAT_RGB24,
(int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING,
ScreenWidth, ScreenHeight
);
// RGB24 output buffer for SDL (expanded from memory each frame)
byte[] rgb24 = new byte[ScreenWidth * ScreenHeight * 3];
// -------------------------------------------------------------------------
// SN76489 PSG — one-byte write port at REG_SOUND ($DF10)
// -------------------------------------------------------------------------
var audioDesired = new SDL.SDL_AudioSpec
{
freq = 44100,
format = SDL.AUDIO_S16SYS,
channels = 1,
samples = 1024,
};
// ppy.SDL2-CS cannot marshal a null device name to SDL_OpenAudioDevice, and
// passing "" is treated as a literal device name. Use the legacy SDL_OpenAudio
// which always targets the default device (device ID 1).
int audioOpenResult = SDL.SDL_OpenAudio(ref audioDesired, out var audioObtained);
uint audioDev = audioOpenResult == 0 ? 1u : 0u;
if (audioDev == 0)
Console.Error.WriteLine($"SDL_OpenAudio failed: {SDL.SDL_GetError()} (continuing without sound)");
int audioSampleRate = audioDev != 0 ? audioObtained.freq : 44100;
var sn = new Sn76489(audioSampleRate);
mem.OnSoundWrite = sn.Write;
int samplesPerFrame = audioSampleRate / 60;
int audioTarget = samplesPerFrame * 3; // keep ~3 frames (~50 ms) queued
short[] audioBuf = new short[audioTarget]; // room for a full top-up in one call
if (audioDev != 0)
SDL.SDL_PauseAudio(0);
2026-04-03 10:49:53 +02:00
var top = "*** MICRONONA 256 ***";
var tab = (byte)((MemoryModel.TextCols - top.Length) / 2);
consoleHelper.SetCursor(tab, 0);
consoleHelper.WriteText(top);
consoleHelper.SetCursor(35, 16);
consoleHelper.WriteText("MICRONONA");
consoleHelper.SetCursor(5, 1);
//SDL.SDL_SetWindowFullscreen(window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP);
var scrollLock = new ScrollLockLed();
scrollLock.Set(false);
// Start from a known baseline so the subsequent loadkeys switch (from marker
// or KMAP result) is visibly distinct on the Pi target.
KeyboardLayoutDetector.ApplyLoadkeys("us");
bool firstStickInsert = true;
diskManager.OnMicrononaStickInserted += path =>
{
Console.WriteLine($"Ready: {path}");
scrollLock.Set(true);
// Only on the first stick after app start: apply the stored layout via
// loadkeys, or auto-run KMAP if the marker has no [keyboard] entry yet.
// Later inserts stay quiet so they can't interrupt a running BASIC session.
if (!firstStickInsert) return;
firstStickInsert = false;
string markerPath = Path.Combine(path, DiskManager.MarkerFile);
try
{
TomlTable model = Toml.ToModel(File.ReadAllText(markerPath));
if (model.TryGetValue("keyboard", out object? kbObj)
&& kbObj is TomlTable kb
&& kb.TryGetValue("layout", out object? layoutObj)
&& layoutObj is string layout
&& !string.IsNullOrWhiteSpace(layout))
{
KeyboardLayoutDetector.ApplyLoadkeys(layout);
}
else
{
inputInterpreter.InterpretInput("KMAP");
}
}
catch (Exception ex)
{
Console.WriteLine($"[keyboard] could not read marker: {ex.Message}");
}
};
diskManager.OnStickRemoved += () => { Console.WriteLine("Stick removed"); scrollLock.Set(false); };
diskManager.Start();
cpu.StartThread();
inputInterpreter.StartThread();
// -------------------------------------------------------------------------
// --run <file>: load and execute file from stick, quit after 5 seconds
// -------------------------------------------------------------------------
long runStartTick = 0;
if (runFile != null)
{
var deadline = Environment.TickCount64 + 3000;
while (diskManager.MountPath == null && Environment.TickCount64 < deadline)
Thread.Sleep(50);
if (diskManager.MountPath == null)
{
Console.Error.WriteLine($"--run: no Micronona stick mounted");
inputInterpreter.StopThread();
cpu.StopThread();
if (audioDev != 0) SDL.SDL_CloseAudio();
SDL.SDL_DestroyTexture(texture);
SDL.SDL_DestroyRenderer(renderer);
SDL.SDL_DestroyWindow(window);
SDL.SDL_Quit();
return 1;
}
var filePath = Path.Combine(diskManager.MountPath, runFile.ToUpperInvariant());
if (!File.Exists(filePath))
{
Console.Error.WriteLine($"--run: file not found on stick: {runFile}");
inputInterpreter.StopThread();
cpu.StopThread();
if (audioDev != 0) SDL.SDL_CloseAudio();
SDL.SDL_DestroyTexture(texture);
SDL.SDL_DestroyRenderer(renderer);
SDL.SDL_DestroyWindow(window);
SDL.SDL_Quit();
return 1;
}
inputInterpreter.InterpretInput($"LOAD \"{runFile}\"");
inputInterpreter.InterpretInput("RUN");
runStartTick = Environment.TickCount64;
}
2026-04-03 10:49:53 +02:00
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
bool running = true;
SDL.SDL_Event e;
while (running)
{
// --- Events ---
while (SDL.SDL_PollEvent(out e) != 0)
{
if (e.type == SDL.SDL_EventType.SDL_QUIT)
running = false;
2026-04-03 10:49:53 +02:00
if (e.type == SDL.SDL_EventType.SDL_KEYDOWN)
{
// KMAP captures key events directly: ESC cancels, mapped scancodes
// get submitted; everything else is ignored until KMAP releases.
if (inputInterpreter.IsWaitingForKmapKey)
{
if (e.key.keysym.sym == SDL.SDL_Keycode.SDLK_ESCAPE)
{
inputInterpreter.SubmitKmapKey(null);
}
else
{
int? kc = KeyboardLayoutDetector.SdlScancodeToLinuxKeycode(e.key.keysym.scancode);
if (kc is not null)
{
int sym = (int)e.key.keysym.sym;
char display = (sym >= 32 && sym <= 126) ? (char)sym : '\0';
inputInterpreter.SubmitKmapKey(kc.Value, display);
}
}
continue;
}
if (inputInterpreter.IsEditorActive)
{
EditorAction? act = MapSdlToEditorAction(e.key);
if (act.HasValue) inputInterpreter.EditorAction(act.Value);
continue;
}
switch (e.key.keysym.sym)
{
case SDL.SDL_Keycode.SDLK_ESCAPE:
running = false;
break;
case SDL.SDL_Keycode.SDLK_c when (e.key.keysym.mod & SDL.SDL_Keymod.KMOD_CTRL) != 0:
case SDL.SDL_Keycode.SDLK_PAUSE when (e.key.keysym.mod & SDL.SDL_Keymod.KMOD_CTRL) != 0:
inputInterpreter.Stop();
cpu.Stop();
break;
}
if (!inputInterpreter.IsKmapActive
&& (!inputInterpreter.IsRunning || inputInterpreter.IsWaitingForInput))
2026-04-03 10:49:53 +02:00
{
switch (e.key.keysym.sym)
{
case SDL.SDL_Keycode.SDLK_LEFT: consoleHelper.MoveCursor(-1, 0); break;
case SDL.SDL_Keycode.SDLK_RIGHT: consoleHelper.MoveCursor(1, 0); break;
case SDL.SDL_Keycode.SDLK_UP: consoleHelper.MoveCursor(0, -1); break;
case SDL.SDL_Keycode.SDLK_DOWN: consoleHelper.MoveCursor(0, 1); break;
case SDL.SDL_Keycode.SDLK_RETURN:
var line = consoleHelper.ReadLine();
consoleHelper.NewLine();
if (inputInterpreter.IsWaitingForInput)
inputInterpreter.SubmitInput(line);
else
inputInterpreter.InterpretInput(line);
break;
2026-04-03 10:49:53 +02:00
case SDL.SDL_Keycode.SDLK_BACKSPACE: consoleHelper.Backspace(); break;
case SDL.SDL_Keycode.SDLK_DELETE: consoleHelper.Delete(); break;
}
}
}
if (e.type == SDL.SDL_EventType.SDL_TEXTINPUT)
2026-04-03 10:49:53 +02:00
{
if (inputInterpreter.IsEditorActive)
2026-04-03 10:49:53 +02:00
{
unsafe
{
byte ascii = e.text.text[0];
if (ascii >= 32 && ascii < 127)
inputInterpreter.EditorChar((char)ascii);
}
continue;
}
if (!inputInterpreter.IsKmapActive
&& (!inputInterpreter.IsRunning || inputInterpreter.IsWaitingForInput))
{
unsafe
{
byte ascii = e.text.text[0];
if (ascii >= 32 && ascii < 127)
consoleHelper.WriteText(((char)ascii).ToString());
}
2026-04-03 10:49:53 +02:00
}
}
}
scrollLock.Refresh();
2026-04-03 10:49:53 +02:00
// --- Render ---
var span = micrononaRenderer.Render();
unsafe
{
fixed (byte* ptr = span)
{
SDL.SDL_UpdateTexture(texture, IntPtr.Zero, (IntPtr)ptr, 320 * 3);
}
}
SDL.SDL_RenderClear(renderer);
SDL.SDL_RenderCopy(renderer, texture, IntPtr.Zero, IntPtr.Zero);
SDL.SDL_RenderPresent(renderer);
mem.SignalVSync();
mem.TickFrame();
if (audioDev != 0)
{
// Top up the audio queue to ~3 frames; absorbs vsync jitter without
// noticeable latency.
uint queuedBytes = SDL.SDL_GetQueuedAudioSize(audioDev);
int queuedSamples = (int)(queuedBytes / sizeof(short));
int need = audioTarget - queuedSamples;
if (need > 0)
{
if (need > audioBuf.Length) need = audioBuf.Length;
sn.GenerateSamples(audioBuf.AsSpan(0, need));
unsafe
{
fixed (short* p = audioBuf)
{
SDL.SDL_QueueAudio(audioDev, (IntPtr)p, (uint)(need * sizeof(short)));
}
}
}
}
if (runStartTick > 0 && Environment.TickCount64 - runStartTick >= 5000)
running = false;
2026-04-03 10:49:53 +02:00
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
static EditorAction? MapSdlToEditorAction(SDL.SDL_KeyboardEvent ke)
{
bool ctrl = (ke.keysym.mod & SDL.SDL_Keymod.KMOD_CTRL) != 0;
return ke.keysym.sym switch
{
SDL.SDL_Keycode.SDLK_LEFT => EditorAction.Left,
SDL.SDL_Keycode.SDLK_RIGHT => EditorAction.Right,
SDL.SDL_Keycode.SDLK_UP => EditorAction.Up,
SDL.SDL_Keycode.SDLK_DOWN => EditorAction.Down,
SDL.SDL_Keycode.SDLK_HOME => EditorAction.Home,
SDL.SDL_Keycode.SDLK_END => EditorAction.End,
SDL.SDL_Keycode.SDLK_PAGEUP => EditorAction.PageUp,
SDL.SDL_Keycode.SDLK_PAGEDOWN => EditorAction.PageDown,
SDL.SDL_Keycode.SDLK_BACKSPACE => EditorAction.Backspace,
SDL.SDL_Keycode.SDLK_DELETE => EditorAction.Delete,
SDL.SDL_Keycode.SDLK_RETURN => EditorAction.Enter,
SDL.SDL_Keycode.SDLK_KP_ENTER => EditorAction.Enter,
SDL.SDL_Keycode.SDLK_ESCAPE => EditorAction.Quit,
SDL.SDL_Keycode.SDLK_s when ctrl => EditorAction.Save,
SDL.SDL_Keycode.SDLK_z when ctrl => EditorAction.Undo,
SDL.SDL_Keycode.SDLK_k when ctrl => EditorAction.CutLine,
SDL.SDL_Keycode.SDLK_u when ctrl => EditorAction.PasteLine,
SDL.SDL_Keycode.SDLK_q when ctrl => EditorAction.Quit,
_ => null
};
}
2026-04-03 10:49:53 +02:00
// -------------------------------------------------------------------------
// Cleanup
// -------------------------------------------------------------------------
inputInterpreter.StopThread();
cpu.StopThread();
if (audioDev != 0)
{
SDL.SDL_PauseAudio(1);
SDL.SDL_CloseAudio();
}
2026-04-03 10:49:53 +02:00
SDL.SDL_DestroyTexture(texture);
SDL.SDL_DestroyRenderer(renderer);
SDL.SDL_DestroyWindow(window);
SDL.SDL_Quit();
return 0;