commit e422b5b56179f27487f272dfb9404d91dcc588a8 Author: Hans-Jörg Meyer Date: Fri Apr 3 10:49:53 2026 +0200 First Commit diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..0cc3d41 --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,10 @@ +{ + "permissions": { + "allow": [ + "Bash(dotnet remove:*)", + "Bash(dotnet add:*)", + "Bash(dotnet build:*)", + "Bash(dotnet run:*)" + ] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..10f9606 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/ +obj/ +*.user +.vs/ diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..f449376 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,39 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## Project Overview + +**Mironona** is a retro computer emulator (targeting a 6809-based system) built with C# (.NET 10) and SDL2 via the `SDL2-CS` binding. The window renders at 320x180 (upscaled 3x to 960x540) using a CPU-side RGB24 framebuffer that is blitted each frame to an SDL streaming texture. + +## Commands + +```bash +# Build +dotnet build + +# Run +dotnet run + +# Build release +dotnet build -c Release +``` + +## Architecture + +The project is currently a single-file bootstrap (`Program.cs`) with: + +- **SDL2 init** — video subsystem, window (960x540 display), accelerated renderer with VSync +- **Framebuffer** — `byte[320*180*3]` in RAM (RGB24), uploaded each frame via `SDL_UpdateTexture` to a streaming texture +- **Main loop** — polls SDL events (Quit / Escape to exit), then uploads framebuffer and presents + +The comment `// Hier später: 6809-Schritt, VSync-Flag setzen, etc.` marks where the 6809 CPU step and VSync logic will be inserted. + +## Key Constants + +| Name | Value | Notes | +|---|---|---| +| `ScreenWidth` | 320 | Native framebuffer width | +| `ScreenHeight` | 180 | Native framebuffer height | +| Display scale | 3x | Window is 960x540 | +| Pixel format | `SDL_PIXELFORMAT_RGB24` | 3 bytes/pixel | diff --git a/ConsoleHelper.cs b/ConsoleHelper.cs new file mode 100644 index 0000000..8fffbc1 --- /dev/null +++ b/ConsoleHelper.cs @@ -0,0 +1,131 @@ +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)); + } +} diff --git a/MemoryModel.cs b/MemoryModel.cs new file mode 100644 index 0000000..4fcad8d --- /dev/null +++ b/MemoryModel.cs @@ -0,0 +1,212 @@ +/// +/// Memory model for Micronona 256. +/// +/// Address space as seen by the CPU and all high-level layers (Lua, TinyBasic, host): +/// +/// 0x0000 – 0xDEFF Fixed RAM (56,832 bytes, general purpose) +/// 0xDF00 – 0xDFFF Hardware registers (256 bytes, I/O) +/// 0xE000 – 0xFFFF Bank window (8KB, switchable via REG_BANK) +/// +/// Extended RAM: 256 banks × 8KB = 2MB total. +/// The bank window at 0xE000 maps to one 8KB slice of extended RAM at a time. +/// Bank switching is done by writing the desired bank number to REG_BANK (0xDF01). +/// +/// Bank layout conventions: +/// Bank 0 Text buffer (40×18×3 bytes: char, fg, bg) starts at offset 0 +/// Bank 255 Font bitmaps (256 glyphs × 8×10 bytes) at end of extended RAM +/// +/// The CPU and all high-level layers use Read(address)/Write(address) exclusively. +/// Bank switching is transparent — callers set REG_BANK and then access 0xE000–0xFFFF. +/// +/// The host (renderer, font loader, etc.) uses ReadFlat/WriteFlat to access extended +/// RAM directly by absolute byte offset, without affecting the bank register. +/// +public class MemoryModel +{ + // ------------------------------------------------------------------------- + // Layout constants + // ------------------------------------------------------------------------- + + public const int BankCount = 25; + public const int BankSize = 8192; // 8KB per bank + public const int FixedRamSize = 0xE000; // 56KB fixed RAM (0x0000–0xDFFF) + public const int IoBase = 0xDF00; // I/O register block start + public const int IoSize = 256; // 256 I/O registers + public const int BankWindowBase = 0xE000; // Bank window start address + + // ------------------------------------------------------------------------- + // I/O register offsets (relative to IoBase = 0xDF00) + // ------------------------------------------------------------------------- + + public const int REG_MODE = 0x00; // Display mode: 0 = text, 1 = graphics + public const int REG_BANK = 0x01; // Active bank in the 0xE000 window (0–255) + public const int REG_KEY_CHAR = 0x02; // Next keypress character (0 = none) + public const int REG_KEY_FLAGS = 0x03; // Modifier flags (Shift, Ctrl, …) + public const int REG_TIMER = 0x04; // Auto-incrementing timer; saturates at 255; resets on read + public const int REG_VSYNC = 0x05; // VSync flag; set by host each frame + public const int REG_CURSOR_COL = 0x06; // Text cursor position horizontally + public const int REG_CURSOR_ROW = 0x07; // Text cursor position vertically + + // ------------------------------------------------------------------------- + // Text mode layout (Bank 0, starting at offset 0) + // Each cell is 3 bytes: character code, foreground RGB332, background RGB332 + // ------------------------------------------------------------------------- + + public const byte TextCols = 40; + public const int TextRows = 18; + public const int TextCellSize = 3; + public const int TextBankIndex = 0; // Text buffer lives in bank 0 + public const int TextBufferSize = TextCols * TextRows * TextCellSize; // 2160 bytes + + // Default terminal colors (RGB332) + 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 + + // ------------------------------------------------------------------------- + // Font layout (Bank 255, starting at offset 0) + // 256 glyphs, each 8 pixels wide × 10 pixels tall, 1 bit per pixel packed + // into 2 bytes per row (only 8 bits used), so 10 bytes per glyph. + // ------------------------------------------------------------------------- + + public const int FontGlyphWidth = 8; + public const int FontGlyphHeight = 10; + public const int FontBankIndex = BankCount-1; + public const int FontSize = 256 * FontGlyphWidth * FontGlyphHeight; // 20480 bytes + + // ------------------------------------------------------------------------- + // Internal storage + // ------------------------------------------------------------------------- + + // Fixed RAM: 0x0000–0xDEFF (excludes I/O block) + private readonly byte[] _fixedRam = new byte[FixedRamSize]; + + // I/O registers: 0xDF00–0xDFFF + private readonly byte[] _io = new byte[IoSize]; + + // Extended RAM: 256 × 8KB = 2MB, bank-switched into 0xE000–0xFFFF + private readonly byte[] _extendedRam = new byte[BankCount * BankSize]; + + // ------------------------------------------------------------------------- + // Constructor + // ------------------------------------------------------------------------- + + public MemoryModel() + { + // Bank 0 active by default + _io[REG_BANK] = TextBankIndex; + + // Fill text buffer with spaces in default colors + int textBase = TextBankIndex * BankSize; + for (int i = 0; i < TextCols * TextRows; i++) + { + _extendedRam[textBase + i * TextCellSize + 0] = 0x20; // Space + _extendedRam[textBase + i * TextCellSize + 1] = DefaultFg; + _extendedRam[textBase + i * TextCellSize + 2] = DefaultBg; + } + } + + // ------------------------------------------------------------------------- + // CPU / high-level interface (Read/Write by 16-bit address) + // This is the only interface the CPU emulator, Lua, and TinyBasic use. + // ------------------------------------------------------------------------- + + /// + /// Read a single byte from the 16-bit address space. + /// + public byte Read(ushort address) + { + if (address >= IoBase && address < IoBase + IoSize) + return ReadIo(address - IoBase); + if (address >= BankWindowBase) + return ReadBank(address - BankWindowBase); + return _fixedRam[address]; + } + + /// + /// Write a single byte to the 16-bit address space. + /// + public void Write(ushort address, byte value) + { + if (address >= IoBase && address < IoBase + IoSize) + WriteIo(address - IoBase, value); + else if (address >= BankWindowBase) + WriteBank(address - BankWindowBase, value); + else + _fixedRam[address] = value; + } + + /// + /// Write multiple bytes sequentially starting at address. + /// Convenient for loading font glyphs, tile data, strings, etc. + /// Example: mem.Write(0xE000, 0x48, 0x1C, 0x08); + /// + public void Write(ushort address, params byte[] data) + { + for (int i = 0; i < data.Length; i++) + Write((ushort)(address + i), data[i]); + } + + // ------------------------------------------------------------------------- + // Host interface (flat access to extended RAM) + // Used by the renderer, font loader, and other host-side components. + // Does not affect the bank register; does not go through the bank window. + // Address is an absolute byte offset into the 2MB extended RAM (0–2097151). + // ------------------------------------------------------------------------- + + /// + /// Read a single byte from extended RAM by absolute offset. + /// + public byte ReadFlat(int address) + => _extendedRam[address]; + + /// + /// Write one or more bytes to extended RAM starting at absolute offset. + /// Example: mem.WriteFlat(255 * BankSize, 0x00, 0x18, 0x3C, 0x66, ...); + /// + public void WriteFlat(int address, params byte[] data) + { + for (int i = 0; i < data.Length; i++) + _extendedRam[address + i] = data[i]; + } + + /// + /// Return a Span over one full bank in extended RAM. + /// Useful for bulk operations like SDL texture uploads. + /// + public Span GetBankSpan(int bank) + => _extendedRam.AsSpan(bank * BankSize, BankSize); + + // ------------------------------------------------------------------------- + // Internal helpers + // ------------------------------------------------------------------------- + + private byte ReadIo(int reg) + { + if (reg == REG_TIMER) + { + // Timer resets to 0 on read (after returning current value) + byte val = _io[REG_TIMER]; + _io[REG_TIMER] = 0; + return val; + } + return _io[reg]; + } + + private void WriteIo(int reg, byte value) + { + // All register writes are simple stores; bank switch takes effect immediately + _io[reg] = value; + } + + private byte ReadBank(int offset) + { + int bank = _io[REG_BANK]; + return _extendedRam[bank * BankSize + offset]; + } + + private void WriteBank(int offset, byte value) + { + int bank = _io[REG_BANK]; + _extendedRam[bank * BankSize + offset] = value; + } +} \ No newline at end of file diff --git a/MicrononaRenderer.cs b/MicrononaRenderer.cs new file mode 100644 index 0000000..9902472 --- /dev/null +++ b/MicrononaRenderer.cs @@ -0,0 +1,124 @@ +/// +/// Renders the Micronona screen into an internal RGB24 buffer. +/// +/// Reads REG_MODE to determine text or graphics mode. +/// +/// Text mode: +/// Reads the text buffer from extended RAM bank 0. +/// Each cell is 3 bytes: character code, foreground RGB332, background RGB332. +/// Looks up the glyph bitmap from font data in bank 255. +/// Each glyph is 8×10 pixels, 1 bit per pixel, 1 byte per row. +/// Expands RGB332 to RGB24 for each pixel. +/// +/// Graphics mode: +/// Not yet implemented. +/// +public class MicrononaRenderer +{ + private readonly MemoryModel _mem; + private readonly byte[] _rgb24 = new byte[320 * 180 * 3]; + + public MicrononaRenderer(MemoryModel mem) + { + _mem = mem; + } + + /// + /// Render one frame into the internal buffer. + /// Returns a ReadOnlySpan over the RGB24 data, ready for SDL_UpdateTexture. + /// + public ReadOnlySpan Render() + { + byte mode = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_MODE); + + if (mode == 0) + RenderText(); + else + RenderGraphics(); + + return _rgb24; + } + + // ------------------------------------------------------------------------- + // Text mode + // ------------------------------------------------------------------------- + + private void RenderText() + { + int textBase = MemoryModel.TextBankIndex * MemoryModel.BankSize; + int fontBase = MemoryModel.FontBankIndex * MemoryModel.BankSize; + + byte cursorCol = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_COL); + byte cursorRow = _mem.Read(MemoryModel.IoBase + MemoryModel.REG_CURSOR_ROW); + + + for (int row = 0; row < MemoryModel.TextRows; row++) + { + for (int col = 0; col < MemoryModel.TextCols; col++) + { + int cellOffset = (row * MemoryModel.TextCols + col) * MemoryModel.TextCellSize; + + byte charCode = _mem.ReadFlat(textBase + cellOffset + 0); + byte fg332 = _mem.ReadFlat(textBase + cellOffset + 1); + byte bg332 = _mem.ReadFlat(textBase + cellOffset + 2); + + bool isCursor = col == cursorCol && row == cursorRow; + byte fg = isCursor ? bg332 : fg332; + byte bg = isCursor ? fg332 : bg332; + + + // Glyph bitmap: FontGlyphHeight bytes, one byte per row (8 pixels) + int glyphOffset = fontBase + charCode * MemoryModel.FontGlyphHeight; + + int pixelX = col * MemoryModel.FontGlyphWidth; + int pixelY = row * MemoryModel.FontGlyphHeight; + + for (int glyphRow = 0; glyphRow < MemoryModel.FontGlyphHeight; glyphRow++) + { + byte bitmapRow = _mem.ReadFlat(glyphOffset + glyphRow); + + for (int glyphCol = 0; glyphCol < MemoryModel.FontGlyphWidth; glyphCol++) + { + // MSB is leftmost pixel + bool lit = (bitmapRow & (0x80 >> glyphCol)) != 0; + byte color332 = lit ? fg : bg; + + int screenX = pixelX + glyphCol; + int screenY = pixelY + glyphRow; + int bufOffset = (screenY * 320 + screenX) * 3; + + Expand332(_rgb24, bufOffset, color332); + } + } + } + } + } + + // ------------------------------------------------------------------------- + // Graphics mode (placeholder) + // ------------------------------------------------------------------------- + + private void RenderGraphics() + { + // TODO: read framebuffer from extended RAM, expand RGB332 → RGB24 + } + + // ------------------------------------------------------------------------- + // RGB332 → RGB24 expansion + // + // RGB332 bit layout: RRRGGGBB + // Red: bits 7–5 (3 bits) → scale to 0–255 + // Green: bits 4–2 (3 bits) → scale to 0–255 + // Blue: bits 1–0 (2 bits) → scale to 0–255 + // ------------------------------------------------------------------------- + + private static void Expand332(byte[] buf, int offset, byte color) + { + buf[offset + 0] = Scale3to8((color >> 5) & 0x07); + buf[offset + 1] = Scale3to8((color >> 2) & 0x07); + buf[offset + 2] = Scale2to8(color & 0x03); + } + + private static byte Scale3to8(int val) => (byte)((val * 255) / 7); + private static byte Scale2to8(int val) => (byte)((val * 255) / 3); +} \ No newline at end of file diff --git a/Mironona.csproj b/Mironona.csproj new file mode 100644 index 0000000..e54b34b --- /dev/null +++ b/Mironona.csproj @@ -0,0 +1,15 @@ + + + + Exe + net10.0 + enable + enable + true + + + + + + + diff --git a/Mironona.sln b/Mironona.sln new file mode 100644 index 0000000..7ddfb69 --- /dev/null +++ b/Mironona.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mironona", "Mironona.csproj", "{FAA5F1D8-9867-FE93-EB3A-67848F811D6E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FAA5F1D8-9867-FE93-EB3A-67848F811D6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAA5F1D8-9867-FE93-EB3A-67848F811D6E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAA5F1D8-9867-FE93-EB3A-67848F811D6E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAA5F1D8-9867-FE93-EB3A-67848F811D6E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {777F0882-23AC-419E-B040-920E6893B7F9} + EndGlobalSection +EndGlobal diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..64c2f7f --- /dev/null +++ b/Program.cs @@ -0,0 +1,313 @@ +using SDL2; + +const int ScreenWidth = 320; +const int ScreenHeight = 180; +const string Title = "Micronona"; +const int Scale = 3; + +// ------------------------------------------------------------------------- +// Memory +// ------------------------------------------------------------------------- + +var mem = new MemoryModel(); +var micrononaRenderer = new MicrononaRenderer(mem); + +var consoleHelper = new ConsoleHelper(mem); + +// " +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 + 42 * MemoryModel.FontGlyphHeight, + 0x00, 0x00, 0x34, 0x18, 0x7E, 0x18, 0x2C, 0x00, 0x00, 0x00 +); + +// 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 +); + +// A +mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 65 * MemoryModel.FontGlyphHeight, + 0x00, 0x18, 0x3C, 0x66, 0x7E, 0x66, 0x66, 0x66, 0x00, 0x00 +); + +// 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 +); + +// 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 +); + +// 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 +); + +// 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 +); + +// 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 +); + +// Y +mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 89 * MemoryModel.FontGlyphHeight, + 0x00, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00 +); + +// c +mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 99 * MemoryModel.FontGlyphHeight, + 0x00, 0x00, 0x3C, 0x66, 0x60, 0x60, 0x66, 0x3C, 0x00, 0x00 +); + +// 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 +); + +// t +mem.WriteFlat(MemoryModel.FontBankIndex * MemoryModel.BankSize + 116 * MemoryModel.FontGlyphHeight, + 0x00, 0x00, 0x18, 0x18, 0x3C, 0x18, 0x18, 0x0C, 0x00, 0x00 +); + +// ------------------------------------------------------------------------- +// SDL init +// ------------------------------------------------------------------------- + +if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) < 0) +{ + 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]; + + +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); +// ------------------------------------------------------------------------- +// 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; + + if (e.type == SDL.SDL_EventType.SDL_KEYDOWN) + { + switch (e.key.keysym.sym) + { + case SDL.SDL_Keycode.SDLK_ESCAPE: + running = false; + break; + 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: consoleHelper.NewLine(); break; + 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) + { + unsafe + { + byte ascii = e.text.text[0]; + if (ascii >= 32 && ascii < 127) + consoleHelper.WriteText(((char)ascii).ToString()); + } + } + } + // --- VSync flag setzen (wird später vom 6809-Emulator gelesen) --- + mem.Write(MemoryModel.IoBase + MemoryModel.REG_VSYNC, 1); + + // --- 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); +} + +// ------------------------------------------------------------------------- +// Cleanup +// ------------------------------------------------------------------------- + +SDL.SDL_DestroyTexture(texture); +SDL.SDL_DestroyRenderer(renderer); +SDL.SDL_DestroyWindow(window); +SDL.SDL_Quit(); +return 0; \ No newline at end of file