2023-09-01 23:19:02 +02:00
|
|
|
#include <cpu/cpu.h>
|
|
|
|
#include <cartridge/cartridge.h>
|
|
|
|
#include <memory/mbc/mbc1.h>
|
|
|
|
#include <memory/ram.h>
|
|
|
|
#include <memory/bus.h>
|
|
|
|
#include <memory/register.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2023-08-28 21:56:54 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2023-09-01 23:19:02 +02:00
|
|
|
if(argc < 2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
std::ifstream file(argv[1]);
|
|
|
|
Cartridge cart(file);
|
|
|
|
|
|
|
|
if(cart.type() != CT_MBC1)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
RAM wram1(0x1000);
|
|
|
|
RAM wram2(0x1000);
|
|
|
|
RAM hram(0x100);
|
|
|
|
RAM oam(0x100);
|
|
|
|
|
|
|
|
MBC1 mbc1(cart);
|
|
|
|
|
|
|
|
ConstRegister ly_reg(0x90);
|
|
|
|
|
|
|
|
Bus b;
|
|
|
|
b.map_device(0x0000, 0x7FFF, &mbc1);
|
|
|
|
b.map_device(0xA000, 0xBFFF, &mbc1, 0xA000);
|
|
|
|
b.map_device(0xC000, 0xCFFF, &wram1);
|
|
|
|
b.map_device(0xD000, 0xDFFF, &wram2);
|
|
|
|
b.map_device(0xE000, 0xFDFF, &wram1);
|
|
|
|
b.map_device(0xFE00, 0xFE9F, &oam);
|
|
|
|
b.map_device(0xFF44, 0xFF44, &ly_reg);
|
|
|
|
b.map_device(0xFF80, 0xFFFE, &hram);
|
|
|
|
|
|
|
|
Cpu cpu(&b);
|
|
|
|
|
|
|
|
BoundRegister ie_mapped(cpu.state.IE);
|
|
|
|
BoundRegister if_mapped(cpu.state.IF);
|
|
|
|
b.map_device(0xFFFF, 0xFFFF, &ie_mapped);
|
|
|
|
b.map_device(0xFF0F, 0xFF0F, &ie_mapped);
|
|
|
|
|
|
|
|
cpu.state.A = 0x1;
|
|
|
|
cpu.state.carry = true;
|
|
|
|
cpu.state.halfcarry = true;
|
|
|
|
cpu.state.subtract = false;
|
|
|
|
cpu.state.zero = true;
|
|
|
|
|
|
|
|
cpu.state.B = 0x00;
|
|
|
|
cpu.state.C = 0x13;
|
|
|
|
cpu.state.D = 0x00;
|
|
|
|
cpu.state.E = 0xD8;
|
|
|
|
cpu.state.H = 0x01;
|
|
|
|
cpu.state.L = 0x4D;
|
|
|
|
|
|
|
|
cpu.state.SP = 0xFFFE;
|
|
|
|
cpu.state.PC = 0x100;
|
|
|
|
|
|
|
|
const std::chrono::milliseconds delay(1);
|
|
|
|
|
|
|
|
while(!cpu.state.stopped)
|
|
|
|
{
|
|
|
|
std::cout
|
|
|
|
<< "A:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.A
|
|
|
|
<< " F:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.getF()
|
|
|
|
<< " B:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.B
|
|
|
|
<< " C:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.C
|
|
|
|
<< " D:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.D
|
|
|
|
<< " E:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.E
|
|
|
|
<< " H:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.H
|
|
|
|
<< " L:" << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << (unsigned)cpu.state.L
|
|
|
|
<< " SP:" << std::setfill('0') << std::setw(4) << std::hex << std::uppercase << (unsigned)cpu.state.SP
|
|
|
|
<< " PC:" << std::setfill('0') << std::setw(4) << std::hex << std::uppercase << (unsigned)cpu.state.PC
|
|
|
|
<< " PCMEM:"
|
|
|
|
<< std::setw(2) << std::hex << std::uppercase << (unsigned)b.read8(cpu.state.PC) << ","
|
|
|
|
<< std::setw(2) << std::hex << std::uppercase << (unsigned)b.read8(cpu.state.PC+1) << ","
|
|
|
|
<< std::setw(2) << std::hex << std::uppercase << (unsigned)b.read8(cpu.state.PC+2) << ","
|
|
|
|
<< std::setw(2) << std::hex << std::uppercase << (unsigned)b.read8(cpu.state.PC+3) << std::endl;
|
|
|
|
|
|
|
|
cpu.step();
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(delay);
|
|
|
|
}
|
|
|
|
|
2023-08-28 21:56:54 +02:00
|
|
|
}
|