micronona/Program.cs
2026-04-03 10:49:53 +02:00

313 lines
No EOL
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;