Beispiel #1
0
        public EmulationEngine(string bootromFile, string romFile)
        {
            _logger = Console.Out;

            var vram = new MemoryBlock(8192);
            var oam  = new MemoryBlock(160);
            var io   = new AddressableRegisterField(256);

            // TODO: replace this with the real registers
            for (var i = 0; i < io.Size; i++)
            {
                io.Add(i, new Register <byte>());
            }

            var @if = new Register <byte>();
            var ie  = new Register <byte>();

            var vblankInterrupt      = new InterruptTrigger(new BoolPointer(@if, 0));
            var lcdStatusInterrupt   = new InterruptTrigger(new BoolPointer(@if, 1));
            var timerInterrupt       = new InterruptTrigger(new BoolPointer(@if, 2));
            var buttonPressInterrupt = new InterruptTrigger(new BoolPointer(@if, 4));

            _timerController = new TimerController(timerInterrupt);

            var p1 = new ButtonInputRegister(buttonPressInterrupt);

            Buttons = p1;

            var lcdc       = new LcdControlRegister();
            var lcdcLogger = new LoggingRegister <byte>(lcdc, "lcdc", _logger, logReads: false);
            var stat       = new LcdStatusRegister();
            var statLogger = new LoggingRegister <byte>(stat, "stat", _logger, logReads: false);
            var scy        = new Register <byte>();
            var scx        = new Register <byte>();
            var ly         = new Register <byte>();

            var lyLogger = new LoggingRegister <byte>(ly, "ly", _logger, logReads: false);
            var lyc      = new Register <byte>();

            var bgp  = new Register <byte>();
            var obp0 = new Register <byte>();
            var obp1 = new Register <byte>();

            var wy = new Register <byte>();
            var wx = new Register <byte>();

            var bootromEnable = new Register <byte>();

            io.Add(0x00, p1);

            var serialLog      = new StreamWriter("C:/Users/Andreas/Dropbox/DMG/serial_log.txt", true);
            var lastSerialByte = 0;

            io.Add(0x01, new LambdaRegister <byte>(b => lastSerialByte = b)); // SB serial transfer
            io.Add(0x02, new LambdaRegister <byte>(b => { if (b == 0x81)
                                                          {
                                                              serialLog.Write(Convert.ToChar(lastSerialByte));
                                                          }
                                                          serialLog.Flush(); }));                                                                // SC serial clock

            io.Add(0x04, _timerController.DIV);
            io.Add(0x05, _timerController.TIMA);
            io.Add(0x06, _timerController.TMA);
            io.Add(0x07, _timerController.TAC);

            io.Add(0x40, lcdc);
            io.Add(0x41, stat);
            io.Add(0x42, scy);
            io.Add(0x43, scx);
            io.Add(0x44, lyLogger);
            io.Add(0x45, lyc);
            io.Add(0x47, bgp);
            io.Add(0x48, obp0);
            io.Add(0x49, obp1);
            io.Add(0x4A, wy);
            io.Add(0x4B, wx);
            io.Add(0x50, bootromEnable);

            io.Add(0x0F, @if);
            io.Add(0xFF, ie);

            var cartridge = CartridgeLoader.FromFile(romFile);

            var memoryMap = new TopLevelMemoryMap(
                new ShadowedMemoryBlock(
                    MemoryBlock.LoadFromFile(bootromFile),
                    cartridge.ROM,
                    new BoolPointer(bootromEnable, 0)
                    ),
                cartridge.RAM,
                vram,
                new MemoryBlock(8192), // internal ram
                oam,
                io);

            io.Add(0x46, new OamDmaController(memoryMap));

            State         = new MachineState(new RegisterField(), memoryMap);
            _loggingState = new MachineState(State.Registers,
                                             new LoggingMemoryBlock(State.Memory, _logger));
            _lcdController = new LcdController(lcdc, stat, scx, scy, ly, lyc, bgp,
                                               vram, oam, obp0, obp1, wy, wx, vblankInterrupt, lcdStatusInterrupt);

            //SkipBootrom();

            //_trace = new StreamWriter($"C:/Users/Andreas/Desktop/trace_{DateTime.Now.ToFileTime()}.txt");
        }