summaryrefslogtreecommitdiff
path: root/Emulator/Program.cs
blob: 675b8d6c589666692ed9df42347282328f341cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
namespace Q1.Emulator;

using System;
using System.IO;
using System.Threading;
using Chip;
using CommandLine;
using Microsoft.Xna.Framework;

internal class Program
{
    public static void Main(string[] args)
    {
        Parser.Default.ParseArguments<Options>(args).WithParsed(Program.Main);
    }

    static void Main(Options options)
    {
        Q1Cpu cpu = new();
        Bus bus = new(u16.MinValue, u16.MaxValue);
        RamDevice rom = new(Q1Layout.Rom.start, Q1Layout.Rom.size);
        File.ReadAllBytes(options.InputFile).CopyTo(rom.Memory, 0);
        
        DisplayDevice display = new(16, 64, 32, Q1Layout.Display);
        display.SetPalette([
            Color.Red,
            Color.Green,
            Color.Blue,
            Color.Yellow,
            Color.Cyan,
            Color.Magenta,
            Color.White,
            Color.Black,
            Color.Gray,
            Color.Orange,
            Color.Purple,
            Color.Brown,
            Color.LightGray,
            Color.DarkGray,
            Color.LightBlue,
            Color.LightGreen
        ]);
        
        RamDevice memory = new(Q1Layout.Memory.start, Q1Layout.Memory.size);
        
        cpu.Bus = bus;
        rom.AttachToBus(bus);
        display.AttachToBus(bus);
        memory.AttachToBus(bus);

        Thread t = new(() =>
        {
            cpu.Reset();
            while (!cpu.AbortRequested)
            {
                // for (int i = 0; i < 64; i++)
                //     cpu.Clock();
                //
                // Thread.Sleep(1);
                //
                cpu.Clock();
            }
            Console.WriteLine($"Exit code: 0x{cpu.V[0]:X4}");
        });
        t.IsBackground = true;
        t.Start();

        using DisplayEngine game = new(64, 32, 10,
            tex =>
            {
                display.OutputTexture = tex;
                Console.WriteLine("Display surface initialized.");
            }, () => display.Clock(), () => cpu.AbortRequested);
        game.Run();
        
        Console.WriteLine("Display surface closed. Waiting for CPU thread to finish...");
        t.Join();
    }
}