/// /// 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: /// Reads 320×180 bytes from extended RAM starting at bank 0 offset 0. /// Each byte is one pixel in RGB332 format, expanded to RGB24. /// 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() { lock (_mem.Lock) { 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() { int pixelBase = MemoryModel.TextBankIndex * MemoryModel.BankSize; for (int i = 0; i < 320 * 180; i++) { byte color332 = _mem.ReadFlat(pixelBase + i); Expand332(_rgb24, i * 3, color332); } } // ------------------------------------------------------------------------- // 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); }