formatting
This commit is contained in:
@@ -2,66 +2,58 @@
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
bool AddressSpace::getBootromState() const {
|
||||
return bootromLoaded;
|
||||
bool AddressSpace::getBootromState() const { return bootromLoaded; }
|
||||
|
||||
void AddressSpace::unmapBootrom() { bootromLoaded = false; }
|
||||
|
||||
void AddressSpace::mapBootrom() { bootromLoaded = true; }
|
||||
|
||||
void AddressSpace::loadBootrom(const std::string &filename) {
|
||||
std::ifstream file;
|
||||
file.open(filename, std::ios::binary);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Bootrom was not found!\nQuitting!\n" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const uintmax_t size = std::filesystem::file_size(filename);
|
||||
if (size != 256) {
|
||||
std::cerr << "Bootrom was an unexpected size!\nQuitting!\n" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
file.read(reinterpret_cast<char *>(bootrom), BOOTROM_SIZE);
|
||||
}
|
||||
|
||||
void AddressSpace::unmapBootrom() {
|
||||
bootromLoaded = false;
|
||||
}
|
||||
void AddressSpace::loadGame(const std::string &filename) {
|
||||
std::ifstream rom(filename, std::ios::binary);
|
||||
rom.unsetf(std::ios::skipws);
|
||||
|
||||
void AddressSpace::mapBootrom() {
|
||||
bootromLoaded = true;
|
||||
}
|
||||
if (!rom.is_open()) {
|
||||
std::cerr << "Game was not found!\nQuitting!\n" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void AddressSpace::loadBootrom(const std::string& filename) {
|
||||
std::ifstream file;
|
||||
file.open(filename, std::ios::binary);
|
||||
rom.seekg(0, std::ios::end);
|
||||
const std::streampos rom_size = rom.tellg();
|
||||
rom.seekg(0, std::ios::beg);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Bootrom was not found!\nQuitting!\n" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
game.reserve(rom_size);
|
||||
game.insert(game.begin(), std::istream_iterator<Byte>(rom),
|
||||
std::istream_iterator<Byte>());
|
||||
|
||||
const uintmax_t size = std::filesystem::file_size(filename);
|
||||
if (size != 256) {
|
||||
std::cerr << "Bootrom was an unexpected size!\nQuitting!\n" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
file.read(reinterpret_cast<char*>(bootrom), BOOTROM_SIZE);
|
||||
}
|
||||
|
||||
void AddressSpace::loadGame(const std::string& filename) {
|
||||
std::ifstream rom(filename, std::ios::binary);
|
||||
rom.unsetf(std::ios::skipws);
|
||||
|
||||
if (!rom.is_open()) {
|
||||
std::cerr << "Game was not found!\nQuitting!\n" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
rom.seekg(0, std::ios::end);
|
||||
const std::streampos rom_size = rom.tellg();
|
||||
rom.seekg(0, std::ios::beg);
|
||||
|
||||
game.reserve(rom_size);
|
||||
game.insert(game.begin(),
|
||||
std::istream_iterator<Byte>(rom),
|
||||
std::istream_iterator<Byte>());
|
||||
|
||||
memoryLayout.romBank0 = game.data();
|
||||
memoryLayout.romBankSwitch = game.data() + ROM_BANK_SIZE;
|
||||
memoryLayout.romBank0 = game.data();
|
||||
memoryLayout.romBankSwitch = game.data() + ROM_BANK_SIZE;
|
||||
}
|
||||
|
||||
void AddressSpace::dmaTransfer() {
|
||||
dmaTransferRequested = false;
|
||||
const Word addr = memoryLayout.DMA << 8;
|
||||
for (int i = 0; i < 0xA0; i++) {
|
||||
const Byte data = (*this)[addr + i];;
|
||||
(*this)[0xFE00 + i] = data;
|
||||
}
|
||||
dmaTransferRequested = false;
|
||||
const Word addr = memoryLayout.DMA << 8;
|
||||
for (int i = 0; i < 0xA0; i++) {
|
||||
const Byte data = (*this)[addr + i];
|
||||
;
|
||||
(*this)[0xFE00 + i] = data;
|
||||
}
|
||||
}
|
||||
|
||||
void AddressSpace::setTesting(const bool state) {
|
||||
testing = state;
|
||||
}
|
||||
void AddressSpace::setTesting(const bool state) { testing = state; }
|
||||
|
||||
@@ -1,383 +1,384 @@
|
||||
#ifndef ADDRESSSPACE_HPP
|
||||
#define ADDRESSSPACE_HPP
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "defines.hpp"
|
||||
|
||||
class AddressSpace {
|
||||
bool bootromLoaded = true;
|
||||
Byte bootrom[BOOTROM_SIZE] = {0};
|
||||
std::vector<Byte> game;
|
||||
bool testing;
|
||||
Byte testRam[0xFFFF];
|
||||
Byte* cartridgeRam = nullptr;
|
||||
bool bootromLoaded = true;
|
||||
Byte bootrom[BOOTROM_SIZE] = {0};
|
||||
std::vector<Byte> game;
|
||||
bool testing;
|
||||
Byte testRam[0xFFFF];
|
||||
Byte *cartridgeRam = nullptr;
|
||||
|
||||
public:
|
||||
AddressSpace() {
|
||||
// Initialize the memory to zero
|
||||
memoryLayout = {};
|
||||
}
|
||||
AddressSpace() {
|
||||
// Initialize the memory to zero
|
||||
memoryLayout = {};
|
||||
}
|
||||
|
||||
struct {
|
||||
Byte* romBank0; //[ROM_BANK_SIZE] Mapped to 0x0000
|
||||
Byte* romBankSwitch; //[ROM_BANK_SIZE] Mapped to 0x4000
|
||||
Byte vram[0x2000]; //Mapped to 0x8000
|
||||
Byte* externalRam; //[0x2000]; Mapped to 0xA000
|
||||
Byte memoryBank1[0x1000]; //Mapped to 0xC000
|
||||
Byte memoryBank2[0x1000]; //Mapped to 0xD000
|
||||
Byte oam[0xA0]; //Mapped to 0xFE00
|
||||
Byte notUsable[0x60]; //Mapped to 0xFEA0
|
||||
//General purpose hardware registers
|
||||
Byte JOYP = 0xCF;
|
||||
Byte SB;
|
||||
Byte SC = 0x7E;
|
||||
Byte DIV;
|
||||
//Timer registers
|
||||
Byte TIMA;
|
||||
Byte TMA;
|
||||
Byte TAC = 0xF8;
|
||||
//interrupt flag and enable
|
||||
Byte IF = 0xE1;;
|
||||
//Sound registers
|
||||
Byte NR10;
|
||||
Byte NR11;
|
||||
Byte NR12;
|
||||
Byte NR13;
|
||||
Byte NR14;
|
||||
Byte NR20; //not used
|
||||
Byte NR21;
|
||||
Byte NR22;
|
||||
Byte NR23;
|
||||
Byte NR24;
|
||||
Byte NR30;
|
||||
Byte NR31;
|
||||
Byte NR32;
|
||||
Byte NR33;
|
||||
Byte NR34;
|
||||
Byte NR40; //unused
|
||||
Byte NR41;
|
||||
Byte NR42;
|
||||
Byte NR43;
|
||||
Byte NR44;
|
||||
Byte NR50;
|
||||
Byte NR51;
|
||||
Byte NR52;
|
||||
Byte waveRam[0x10];
|
||||
//PPU registers
|
||||
Byte LCDC;
|
||||
Byte STAT;
|
||||
Byte SCY;
|
||||
Byte SCX;
|
||||
Byte LY;
|
||||
Byte LYC;
|
||||
Byte DMA;
|
||||
Byte BGP;
|
||||
Byte OBP0;
|
||||
Byte OBP1;
|
||||
Byte WY;
|
||||
Byte WX;
|
||||
Byte specialRam[0x7F]; //Mapped to 0xFF80
|
||||
Byte IE; // Mapped to 0xFFFF
|
||||
} memoryLayout{};
|
||||
struct {
|
||||
Byte *romBank0; //[ROM_BANK_SIZE] Mapped to 0x0000
|
||||
Byte *romBankSwitch; //[ROM_BANK_SIZE] Mapped to 0x4000
|
||||
Byte vram[0x2000]; // Mapped to 0x8000
|
||||
Byte *externalRam; //[0x2000]; Mapped to 0xA000
|
||||
Byte memoryBank1[0x1000]; // Mapped to 0xC000
|
||||
Byte memoryBank2[0x1000]; // Mapped to 0xD000
|
||||
Byte oam[0xA0]; // Mapped to 0xFE00
|
||||
Byte notUsable[0x60]; // Mapped to 0xFEA0
|
||||
// General purpose hardware registers
|
||||
Byte JOYP = 0xCF;
|
||||
Byte SB;
|
||||
Byte SC = 0x7E;
|
||||
Byte DIV;
|
||||
// Timer registers
|
||||
Byte TIMA;
|
||||
Byte TMA;
|
||||
Byte TAC = 0xF8;
|
||||
// interrupt flag and enable
|
||||
Byte IF = 0xE1;
|
||||
;
|
||||
// Sound registers
|
||||
Byte NR10;
|
||||
Byte NR11;
|
||||
Byte NR12;
|
||||
Byte NR13;
|
||||
Byte NR14;
|
||||
Byte NR20; // not used
|
||||
Byte NR21;
|
||||
Byte NR22;
|
||||
Byte NR23;
|
||||
Byte NR24;
|
||||
Byte NR30;
|
||||
Byte NR31;
|
||||
Byte NR32;
|
||||
Byte NR33;
|
||||
Byte NR34;
|
||||
Byte NR40; // unused
|
||||
Byte NR41;
|
||||
Byte NR42;
|
||||
Byte NR43;
|
||||
Byte NR44;
|
||||
Byte NR50;
|
||||
Byte NR51;
|
||||
Byte NR52;
|
||||
Byte waveRam[0x10];
|
||||
// PPU registers
|
||||
Byte LCDC;
|
||||
Byte STAT;
|
||||
Byte SCY;
|
||||
Byte SCX;
|
||||
Byte LY;
|
||||
Byte LYC;
|
||||
Byte DMA;
|
||||
Byte BGP;
|
||||
Byte OBP0;
|
||||
Byte OBP1;
|
||||
Byte WY;
|
||||
Byte WX;
|
||||
Byte specialRam[0x7F]; // Mapped to 0xFF80
|
||||
Byte IE; // Mapped to 0xFFFF
|
||||
} memoryLayout{};
|
||||
|
||||
void unmapBootrom();
|
||||
void mapBootrom();
|
||||
bool getBootromState() const;
|
||||
void loadBootrom(const std::string& filename);
|
||||
void loadGame(const std::string& filename);
|
||||
void unmapBootrom();
|
||||
void mapBootrom();
|
||||
bool getBootromState() const;
|
||||
void loadBootrom(const std::string &filename);
|
||||
void loadGame(const std::string &filename);
|
||||
|
||||
void determineMBCInfo();
|
||||
bool MBCWrite(Word address);
|
||||
Byte* MBCRead(Word address);
|
||||
//prevents seg faults when programs with no MBC try to write to ROM
|
||||
Byte dummyVal = 0;
|
||||
void MBCUpdate();
|
||||
void loadRomBank();
|
||||
void createRamBank();
|
||||
void loadRamBank();
|
||||
MBCType MBC = {};
|
||||
uint32_t romSize = 0;
|
||||
uint32_t romBanks = 0;
|
||||
uint32_t externalRamSize = 0;
|
||||
uint32_t externalRamBanks = 0;
|
||||
void determineMBCInfo();
|
||||
bool MBCWrite(Word address);
|
||||
Byte *MBCRead(Word address);
|
||||
// prevents seg faults when programs with no MBC try to write to ROM
|
||||
Byte dummyVal = 0;
|
||||
void MBCUpdate();
|
||||
void loadRomBank();
|
||||
void createRamBank();
|
||||
void loadRamBank();
|
||||
MBCType MBC = {};
|
||||
uint32_t romSize = 0;
|
||||
uint32_t romBanks = 0;
|
||||
uint32_t externalRamSize = 0;
|
||||
uint32_t externalRamBanks = 0;
|
||||
|
||||
bool dmaTransferRequested = false;
|
||||
void dmaTransfer();
|
||||
bool dmaTransferRequested = false;
|
||||
void dmaTransfer();
|
||||
|
||||
//Selected ROM Bank = (Secondary Bank << 5) + ROM Bank
|
||||
Byte selectedRomBank = 0;
|
||||
Byte romBankRegister = 0x00;
|
||||
//2 bit register acts as secondary rom bank register or ram bank number
|
||||
Byte twoBitBankRegister = 0x0;
|
||||
Byte selectedExternalRamBank = 0;
|
||||
Byte romRamSelect = 0x00;
|
||||
Byte ramEnable = 0x00;
|
||||
//MBC3
|
||||
Byte latchClockData = 0x00;
|
||||
Byte ramBankRTCRegister = 0x00;
|
||||
// Selected ROM Bank = (Secondary Bank << 5) + ROM Bank
|
||||
Byte selectedRomBank = 0;
|
||||
Byte romBankRegister = 0x00;
|
||||
// 2 bit register acts as secondary rom bank register or ram bank number
|
||||
Byte twoBitBankRegister = 0x0;
|
||||
Byte selectedExternalRamBank = 0;
|
||||
Byte romRamSelect = 0x00;
|
||||
Byte ramEnable = 0x00;
|
||||
// MBC3
|
||||
Byte latchClockData = 0x00;
|
||||
Byte ramBankRTCRegister = 0x00;
|
||||
|
||||
void setTesting(bool state);
|
||||
void setTesting(bool state);
|
||||
|
||||
//read
|
||||
Byte operator[](const Word address) const {
|
||||
if (testing)
|
||||
return testRam[address];
|
||||
if (address < 0x0100 && bootromLoaded)
|
||||
return bootrom[address];
|
||||
if (address < 0x4000)
|
||||
return memoryLayout.romBank0[address];
|
||||
if (address < 0x8000)
|
||||
return memoryLayout.romBankSwitch[address - 0x4000];
|
||||
if (address < 0xA000)
|
||||
return memoryLayout.vram[address - 0x8000];
|
||||
if (address < 0xC000) {
|
||||
if (externalRamSize == 0)
|
||||
return 0xFF;
|
||||
return memoryLayout.externalRam[address - 0xA000];
|
||||
}
|
||||
if (address < 0xD000)
|
||||
return memoryLayout.memoryBank1[address - 0xC000];
|
||||
if (address < 0xE000)
|
||||
return memoryLayout.memoryBank2[address - 0xD000];
|
||||
if (address < 0xFE00)
|
||||
return memoryLayout.memoryBank1[address - 0xE000];
|
||||
if (address < 0xFEA0)
|
||||
return memoryLayout.oam[address - 0xFE00];
|
||||
if (address < 0xFF00) {
|
||||
if ((memoryLayout.STAT & 0x03) == 2 || (memoryLayout.STAT & 0x03) == 3)
|
||||
return 0xFF;
|
||||
return 0x00;
|
||||
}
|
||||
if (address < 0xFF80)
|
||||
switch (address) {
|
||||
case 0xFF00:
|
||||
return memoryLayout.JOYP;
|
||||
case 0xFF01:
|
||||
return memoryLayout.SB;
|
||||
case 0xFF02:
|
||||
return memoryLayout.SC;
|
||||
case 0xFF04:
|
||||
return memoryLayout.DIV;
|
||||
case 0xFF05:
|
||||
return memoryLayout.TIMA;
|
||||
case 0xFF06:
|
||||
return memoryLayout.TMA;
|
||||
case 0xFF07:
|
||||
return memoryLayout.TAC | 0xF8;;
|
||||
case 0xFF0F:
|
||||
return memoryLayout.IF | 0xE0;
|
||||
case 0xFF10:
|
||||
return memoryLayout.NR10;
|
||||
case 0xFF11:
|
||||
return memoryLayout.NR11;
|
||||
case 0xFF12:
|
||||
return memoryLayout.NR12;
|
||||
case 0xFF13:
|
||||
return memoryLayout.NR13;
|
||||
case 0xFF14:
|
||||
return memoryLayout.NR14;
|
||||
case 0xFF16:
|
||||
return memoryLayout.NR21;
|
||||
case 0xFF17:
|
||||
return memoryLayout.NR22;
|
||||
case 0xFF18:
|
||||
return memoryLayout.NR23;
|
||||
case 0xFF19:
|
||||
return memoryLayout.NR24;
|
||||
case 0xFF1A:
|
||||
return memoryLayout.NR30;
|
||||
case 0xFF1B:
|
||||
return memoryLayout.NR31;
|
||||
case 0xFF1C:
|
||||
return memoryLayout.NR32;
|
||||
case 0xFF1D:
|
||||
return memoryLayout.NR33;
|
||||
case 0xFF1E:
|
||||
return memoryLayout.NR34;
|
||||
case 0xFF20:
|
||||
return memoryLayout.NR41;
|
||||
case 0xFF21:
|
||||
return memoryLayout.NR42;
|
||||
case 0xFF22:
|
||||
return memoryLayout.NR43;
|
||||
case 0xFF23:
|
||||
return memoryLayout.NR44;
|
||||
case 0xFF24:
|
||||
return memoryLayout.NR50;
|
||||
case 0xFF25:
|
||||
return memoryLayout.NR51;
|
||||
case 0xFF26:
|
||||
return memoryLayout.NR52;
|
||||
// PPU registers
|
||||
case 0xFF40:
|
||||
return memoryLayout.LCDC;
|
||||
case 0xFF41:
|
||||
return memoryLayout.STAT;
|
||||
case 0xFF42:
|
||||
return memoryLayout.SCY;
|
||||
case 0xFF43:
|
||||
return memoryLayout.SCX;
|
||||
case 0xFF44:
|
||||
//for debugging only
|
||||
//return 0x90;
|
||||
return memoryLayout.LY;
|
||||
case 0xFF45:
|
||||
return memoryLayout.LYC;
|
||||
case 0xFF46:
|
||||
return memoryLayout.DMA;
|
||||
case 0xFF47:
|
||||
return memoryLayout.BGP;
|
||||
case 0xFF48:
|
||||
return memoryLayout.OBP0;
|
||||
case 0xFF49:
|
||||
return memoryLayout.OBP1;
|
||||
case 0xFF4A:
|
||||
return memoryLayout.WY;
|
||||
case 0xFF4B:
|
||||
return memoryLayout.WX;
|
||||
default:
|
||||
if (address >= 0xFF30 && address <= 0xFF3F) {
|
||||
return memoryLayout.waveRam[address - 0xFF30];
|
||||
}
|
||||
return 0xFF;
|
||||
}
|
||||
if (address < 0xFFFF)
|
||||
return memoryLayout.specialRam[address - 0xFF80];
|
||||
//0xFFFF
|
||||
return memoryLayout.IE;
|
||||
}
|
||||
// read
|
||||
Byte operator[](const Word address) const {
|
||||
if (testing)
|
||||
return testRam[address];
|
||||
if (address < 0x0100 && bootromLoaded)
|
||||
return bootrom[address];
|
||||
if (address < 0x4000)
|
||||
return memoryLayout.romBank0[address];
|
||||
if (address < 0x8000)
|
||||
return memoryLayout.romBankSwitch[address - 0x4000];
|
||||
if (address < 0xA000)
|
||||
return memoryLayout.vram[address - 0x8000];
|
||||
if (address < 0xC000) {
|
||||
if (externalRamSize == 0)
|
||||
return 0xFF;
|
||||
return memoryLayout.externalRam[address - 0xA000];
|
||||
}
|
||||
if (address < 0xD000)
|
||||
return memoryLayout.memoryBank1[address - 0xC000];
|
||||
if (address < 0xE000)
|
||||
return memoryLayout.memoryBank2[address - 0xD000];
|
||||
if (address < 0xFE00)
|
||||
return memoryLayout.memoryBank1[address - 0xE000];
|
||||
if (address < 0xFEA0)
|
||||
return memoryLayout.oam[address - 0xFE00];
|
||||
if (address < 0xFF00) {
|
||||
if ((memoryLayout.STAT & 0x03) == 2 || (memoryLayout.STAT & 0x03) == 3)
|
||||
return 0xFF;
|
||||
return 0x00;
|
||||
}
|
||||
if (address < 0xFF80)
|
||||
switch (address) {
|
||||
case 0xFF00:
|
||||
return memoryLayout.JOYP;
|
||||
case 0xFF01:
|
||||
return memoryLayout.SB;
|
||||
case 0xFF02:
|
||||
return memoryLayout.SC;
|
||||
case 0xFF04:
|
||||
return memoryLayout.DIV;
|
||||
case 0xFF05:
|
||||
return memoryLayout.TIMA;
|
||||
case 0xFF06:
|
||||
return memoryLayout.TMA;
|
||||
case 0xFF07:
|
||||
return memoryLayout.TAC | 0xF8;
|
||||
;
|
||||
case 0xFF0F:
|
||||
return memoryLayout.IF | 0xE0;
|
||||
case 0xFF10:
|
||||
return memoryLayout.NR10;
|
||||
case 0xFF11:
|
||||
return memoryLayout.NR11;
|
||||
case 0xFF12:
|
||||
return memoryLayout.NR12;
|
||||
case 0xFF13:
|
||||
return memoryLayout.NR13;
|
||||
case 0xFF14:
|
||||
return memoryLayout.NR14;
|
||||
case 0xFF16:
|
||||
return memoryLayout.NR21;
|
||||
case 0xFF17:
|
||||
return memoryLayout.NR22;
|
||||
case 0xFF18:
|
||||
return memoryLayout.NR23;
|
||||
case 0xFF19:
|
||||
return memoryLayout.NR24;
|
||||
case 0xFF1A:
|
||||
return memoryLayout.NR30;
|
||||
case 0xFF1B:
|
||||
return memoryLayout.NR31;
|
||||
case 0xFF1C:
|
||||
return memoryLayout.NR32;
|
||||
case 0xFF1D:
|
||||
return memoryLayout.NR33;
|
||||
case 0xFF1E:
|
||||
return memoryLayout.NR34;
|
||||
case 0xFF20:
|
||||
return memoryLayout.NR41;
|
||||
case 0xFF21:
|
||||
return memoryLayout.NR42;
|
||||
case 0xFF22:
|
||||
return memoryLayout.NR43;
|
||||
case 0xFF23:
|
||||
return memoryLayout.NR44;
|
||||
case 0xFF24:
|
||||
return memoryLayout.NR50;
|
||||
case 0xFF25:
|
||||
return memoryLayout.NR51;
|
||||
case 0xFF26:
|
||||
return memoryLayout.NR52;
|
||||
// PPU registers
|
||||
case 0xFF40:
|
||||
return memoryLayout.LCDC;
|
||||
case 0xFF41:
|
||||
return memoryLayout.STAT;
|
||||
case 0xFF42:
|
||||
return memoryLayout.SCY;
|
||||
case 0xFF43:
|
||||
return memoryLayout.SCX;
|
||||
case 0xFF44:
|
||||
// for debugging only
|
||||
// return 0x90;
|
||||
return memoryLayout.LY;
|
||||
case 0xFF45:
|
||||
return memoryLayout.LYC;
|
||||
case 0xFF46:
|
||||
return memoryLayout.DMA;
|
||||
case 0xFF47:
|
||||
return memoryLayout.BGP;
|
||||
case 0xFF48:
|
||||
return memoryLayout.OBP0;
|
||||
case 0xFF49:
|
||||
return memoryLayout.OBP1;
|
||||
case 0xFF4A:
|
||||
return memoryLayout.WY;
|
||||
case 0xFF4B:
|
||||
return memoryLayout.WX;
|
||||
default:
|
||||
if (address >= 0xFF30 && address <= 0xFF3F) {
|
||||
return memoryLayout.waveRam[address - 0xFF30];
|
||||
}
|
||||
return 0xFF;
|
||||
}
|
||||
if (address < 0xFFFF)
|
||||
return memoryLayout.specialRam[address - 0xFF80];
|
||||
// 0xFFFF
|
||||
return memoryLayout.IE;
|
||||
}
|
||||
|
||||
//write
|
||||
Byte& operator[](const Word address) {
|
||||
dummyVal = 0xFF;
|
||||
if (testing)
|
||||
return testRam[address];
|
||||
if (address < 0x0100 && bootromLoaded)
|
||||
return bootrom[address];
|
||||
if (address < 0x8000)
|
||||
return (*MBCRead(address));
|
||||
if (address < 0xA000)
|
||||
return memoryLayout.vram[address - 0x8000];
|
||||
if (address < 0xC000) {
|
||||
if (externalRamSize == 0)
|
||||
return dummyVal;
|
||||
return memoryLayout.externalRam[address - 0xA000];
|
||||
}
|
||||
if (address < 0xD000)
|
||||
return memoryLayout.memoryBank1[address - 0xC000];
|
||||
if (address < 0xE000)
|
||||
return memoryLayout.memoryBank2[address - 0xD000];
|
||||
if (address < 0xFE00)
|
||||
return memoryLayout.memoryBank1[address - 0xE000];
|
||||
if (address < 0xFEA0)
|
||||
return memoryLayout.oam[address - 0xFE00];
|
||||
if (address < 0xFF00)
|
||||
return memoryLayout.notUsable[address - 0xFEA0];
|
||||
if (address < 0xFF80)
|
||||
switch (address) {
|
||||
case 0xFF00:
|
||||
return memoryLayout.JOYP;
|
||||
case 0xFF01:
|
||||
return memoryLayout.SB;
|
||||
case 0xFF02:
|
||||
return memoryLayout.SC;
|
||||
case 0xFF04:
|
||||
memoryLayout.DIV = 0;
|
||||
return dummyVal;
|
||||
// Timer registers
|
||||
case 0xFF05:
|
||||
return memoryLayout.TIMA;
|
||||
case 0xFF06:
|
||||
return memoryLayout.TMA;
|
||||
case 0xFF07:
|
||||
return memoryLayout.TAC;
|
||||
case 0xFF0F:
|
||||
return memoryLayout.IF;
|
||||
case 0xFF10:
|
||||
return memoryLayout.NR10;
|
||||
case 0xFF11:
|
||||
return memoryLayout.NR11;
|
||||
case 0xFF12:
|
||||
return memoryLayout.NR12;
|
||||
case 0xFF13:
|
||||
return memoryLayout.NR13;
|
||||
case 0xFF14:
|
||||
return memoryLayout.NR14;
|
||||
case 0xFF16:
|
||||
return memoryLayout.NR21;
|
||||
case 0xFF17:
|
||||
return memoryLayout.NR22;
|
||||
case 0xFF18:
|
||||
return memoryLayout.NR23;
|
||||
case 0xFF19:
|
||||
return memoryLayout.NR24;
|
||||
case 0xFF1A:
|
||||
return memoryLayout.NR30;
|
||||
case 0xFF1B:
|
||||
return memoryLayout.NR31;
|
||||
case 0xFF1C:
|
||||
return memoryLayout.NR32;
|
||||
case 0xFF1D:
|
||||
return memoryLayout.NR33;
|
||||
case 0xFF1E:
|
||||
return memoryLayout.NR34;
|
||||
case 0xFF20:
|
||||
return memoryLayout.NR41;
|
||||
case 0xFF21:
|
||||
return memoryLayout.NR42;
|
||||
case 0xFF22:
|
||||
return memoryLayout.NR43;
|
||||
case 0xFF23:
|
||||
return memoryLayout.NR44;
|
||||
case 0xFF24:
|
||||
return memoryLayout.NR50;
|
||||
case 0xFF25:
|
||||
return memoryLayout.NR51;
|
||||
case 0xFF26:
|
||||
return memoryLayout.NR52;
|
||||
case 0xFF40:
|
||||
return memoryLayout.LCDC;
|
||||
case 0xFF41:
|
||||
return memoryLayout.STAT;
|
||||
case 0xFF42:
|
||||
return memoryLayout.SCY;
|
||||
case 0xFF43:
|
||||
return memoryLayout.SCX;
|
||||
case 0xFF44:
|
||||
dummyVal = memoryLayout.LY;
|
||||
return dummyVal;
|
||||
case 0xFF45:
|
||||
return memoryLayout.LYC;
|
||||
case 0xFF46:
|
||||
dmaTransferRequested = true;
|
||||
return memoryLayout.DMA;
|
||||
case 0xFF47:
|
||||
return memoryLayout.BGP;
|
||||
case 0xFF48:
|
||||
return memoryLayout.OBP0;
|
||||
case 0xFF49:
|
||||
return memoryLayout.OBP1;
|
||||
case 0xFF4A:
|
||||
return memoryLayout.WY;
|
||||
case 0xFF4B:
|
||||
return memoryLayout.WX;
|
||||
default:
|
||||
if (address >= 0xFF30 && address <= 0xFF3F) {
|
||||
return memoryLayout.waveRam[address - 0xFF30];
|
||||
}
|
||||
return dummyVal;
|
||||
}
|
||||
if (address < 0xFFFF)
|
||||
return memoryLayout.specialRam[address - 0xFF80];
|
||||
//0xFFFF
|
||||
return memoryLayout.IE;
|
||||
}
|
||||
// write
|
||||
Byte &operator[](const Word address) {
|
||||
dummyVal = 0xFF;
|
||||
if (testing)
|
||||
return testRam[address];
|
||||
if (address < 0x0100 && bootromLoaded)
|
||||
return bootrom[address];
|
||||
if (address < 0x8000)
|
||||
return (*MBCRead(address));
|
||||
if (address < 0xA000)
|
||||
return memoryLayout.vram[address - 0x8000];
|
||||
if (address < 0xC000) {
|
||||
if (externalRamSize == 0)
|
||||
return dummyVal;
|
||||
return memoryLayout.externalRam[address - 0xA000];
|
||||
}
|
||||
if (address < 0xD000)
|
||||
return memoryLayout.memoryBank1[address - 0xC000];
|
||||
if (address < 0xE000)
|
||||
return memoryLayout.memoryBank2[address - 0xD000];
|
||||
if (address < 0xFE00)
|
||||
return memoryLayout.memoryBank1[address - 0xE000];
|
||||
if (address < 0xFEA0)
|
||||
return memoryLayout.oam[address - 0xFE00];
|
||||
if (address < 0xFF00)
|
||||
return memoryLayout.notUsable[address - 0xFEA0];
|
||||
if (address < 0xFF80)
|
||||
switch (address) {
|
||||
case 0xFF00:
|
||||
return memoryLayout.JOYP;
|
||||
case 0xFF01:
|
||||
return memoryLayout.SB;
|
||||
case 0xFF02:
|
||||
return memoryLayout.SC;
|
||||
case 0xFF04:
|
||||
memoryLayout.DIV = 0;
|
||||
return dummyVal;
|
||||
// Timer registers
|
||||
case 0xFF05:
|
||||
return memoryLayout.TIMA;
|
||||
case 0xFF06:
|
||||
return memoryLayout.TMA;
|
||||
case 0xFF07:
|
||||
return memoryLayout.TAC;
|
||||
case 0xFF0F:
|
||||
return memoryLayout.IF;
|
||||
case 0xFF10:
|
||||
return memoryLayout.NR10;
|
||||
case 0xFF11:
|
||||
return memoryLayout.NR11;
|
||||
case 0xFF12:
|
||||
return memoryLayout.NR12;
|
||||
case 0xFF13:
|
||||
return memoryLayout.NR13;
|
||||
case 0xFF14:
|
||||
return memoryLayout.NR14;
|
||||
case 0xFF16:
|
||||
return memoryLayout.NR21;
|
||||
case 0xFF17:
|
||||
return memoryLayout.NR22;
|
||||
case 0xFF18:
|
||||
return memoryLayout.NR23;
|
||||
case 0xFF19:
|
||||
return memoryLayout.NR24;
|
||||
case 0xFF1A:
|
||||
return memoryLayout.NR30;
|
||||
case 0xFF1B:
|
||||
return memoryLayout.NR31;
|
||||
case 0xFF1C:
|
||||
return memoryLayout.NR32;
|
||||
case 0xFF1D:
|
||||
return memoryLayout.NR33;
|
||||
case 0xFF1E:
|
||||
return memoryLayout.NR34;
|
||||
case 0xFF20:
|
||||
return memoryLayout.NR41;
|
||||
case 0xFF21:
|
||||
return memoryLayout.NR42;
|
||||
case 0xFF22:
|
||||
return memoryLayout.NR43;
|
||||
case 0xFF23:
|
||||
return memoryLayout.NR44;
|
||||
case 0xFF24:
|
||||
return memoryLayout.NR50;
|
||||
case 0xFF25:
|
||||
return memoryLayout.NR51;
|
||||
case 0xFF26:
|
||||
return memoryLayout.NR52;
|
||||
case 0xFF40:
|
||||
return memoryLayout.LCDC;
|
||||
case 0xFF41:
|
||||
return memoryLayout.STAT;
|
||||
case 0xFF42:
|
||||
return memoryLayout.SCY;
|
||||
case 0xFF43:
|
||||
return memoryLayout.SCX;
|
||||
case 0xFF44:
|
||||
dummyVal = memoryLayout.LY;
|
||||
return dummyVal;
|
||||
case 0xFF45:
|
||||
return memoryLayout.LYC;
|
||||
case 0xFF46:
|
||||
dmaTransferRequested = true;
|
||||
return memoryLayout.DMA;
|
||||
case 0xFF47:
|
||||
return memoryLayout.BGP;
|
||||
case 0xFF48:
|
||||
return memoryLayout.OBP0;
|
||||
case 0xFF49:
|
||||
return memoryLayout.OBP1;
|
||||
case 0xFF4A:
|
||||
return memoryLayout.WY;
|
||||
case 0xFF4B:
|
||||
return memoryLayout.WX;
|
||||
default:
|
||||
if (address >= 0xFF30 && address <= 0xFF3F) {
|
||||
return memoryLayout.waveRam[address - 0xFF30];
|
||||
}
|
||||
return dummyVal;
|
||||
}
|
||||
if (address < 0xFFFF)
|
||||
return memoryLayout.specialRam[address - 0xFF80];
|
||||
// 0xFFFF
|
||||
return memoryLayout.IE;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //ADDRESSSPACE_HPP
|
||||
#endif // ADDRESSSPACE_HPP
|
||||
|
||||
106
src/defines.hpp
106
src/defines.hpp
@@ -10,10 +10,10 @@
|
||||
// 5 h - - Half Carry Flag (BCD)
|
||||
// 4 cy C NC Carry Flag
|
||||
// 3-0 - - - Not used
|
||||
#define CARRY_FLAG 4 //'C'
|
||||
#define CARRY_FLAG 4 //'C'
|
||||
#define HALFCARRY_FLAG 5 //'H'
|
||||
#define SUBTRACT_FLAG 6 //'N'
|
||||
#define ZERO_FLAG 7 //'Z'
|
||||
#define SUBTRACT_FLAG 6 //'N'
|
||||
#define ZERO_FLAG 7 //'Z'
|
||||
|
||||
#define VBLANK_INTERRUPT 0
|
||||
#define LCD_STAT_INTERRUPT 1
|
||||
@@ -21,9 +21,9 @@
|
||||
#define SERIAL_INTERRUPT 3
|
||||
#define JOYPAD_INTERRUPT 4
|
||||
|
||||
#define T_CLOCK_FREQ 4194304 //2^22
|
||||
#define T_CLOCK_FREQ 4194304 // 2^22
|
||||
|
||||
#define DIVIDER_REGISTER_FREQ (4194304/16384)
|
||||
#define DIVIDER_REGISTER_FREQ (4194304 / 16384)
|
||||
|
||||
#define BOOTROM_SIZE 0x100
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
#define RESOLUTION_Y 144
|
||||
#define SCREEN_BPP 3
|
||||
|
||||
//lcdc
|
||||
// lcdc
|
||||
#define BG_WINDOW_ENABLE 0
|
||||
#define OBJ_ENABLE 1
|
||||
#define OBJ_SIZE 2
|
||||
@@ -41,13 +41,12 @@
|
||||
#define WINDOW_TILE_MAP_AREA 6
|
||||
#define LCD_ENABLE 7
|
||||
|
||||
//oam
|
||||
// oam
|
||||
#define PRIORITY 7
|
||||
#define Y_FLIP 6
|
||||
#define X_FLIP 5
|
||||
#define OBJ_PALETTE 4
|
||||
|
||||
|
||||
#define ROM_BANK_SIZE 0x4000
|
||||
#define RAM_BANK_SIZE 0x2000
|
||||
|
||||
@@ -59,10 +58,10 @@
|
||||
|
||||
#define H_SYNC 9198
|
||||
#define V_SYNC 59.73
|
||||
#define HBLANK_DURATION 204 //PPU_MODE 0
|
||||
#define HBLANK_DURATION 204 // PPU_MODE 0
|
||||
#define VBLANK_DURATION 4560
|
||||
#define SCANLINE_OAM_FREQ 80 //PPU_MODE 2
|
||||
#define SCANLINE_VRAM_FREQ 80 //PPU_MODE 3
|
||||
#define SCANLINE_OAM_FREQ 80 // PPU_MODE 2
|
||||
#define SCANLINE_VRAM_FREQ 80 // PPU_MODE 3
|
||||
|
||||
#define WHITE 0xFFFFFFFF;
|
||||
#define LIGHT_GRAY 0xFFAAAAAA;
|
||||
@@ -70,52 +69,57 @@
|
||||
#define BLACK 0xFF000000
|
||||
|
||||
struct Input {
|
||||
bool UP = false;
|
||||
bool DOWN = false;
|
||||
bool LEFT = false;
|
||||
bool RIGHT = false;
|
||||
bool B = false;
|
||||
bool A = false;
|
||||
bool START = false;
|
||||
bool SELECT = false;
|
||||
bool UP = false;
|
||||
bool DOWN = false;
|
||||
bool LEFT = false;
|
||||
bool RIGHT = false;
|
||||
bool B = false;
|
||||
bool A = false;
|
||||
bool START = false;
|
||||
bool SELECT = false;
|
||||
};
|
||||
|
||||
enum MBCType {
|
||||
romOnly = 0x00,
|
||||
MBC1 = 0x01,
|
||||
MBC1Ram = 0x02,
|
||||
MBC1RamBattery = 0x03,
|
||||
MBC2 = 0x05,
|
||||
MBC2Battery = 0x06,
|
||||
RomRam = 0x08, //unused
|
||||
RomRamBattery = 0x09, //unused
|
||||
MMM01 = 0x0B, //multigame roms only
|
||||
MMM01Ram = 0x0C,
|
||||
MMM01RamBattery = 0x0D,
|
||||
MBC3TimerBattery = 0x0F,
|
||||
MBC3TimerRamBattery = 0x10,
|
||||
MBC3 = 0x11,
|
||||
MBC3Ram = 0x12, // MBC3 with 64 KiB of SRAM refers to MBC30, used only in Pocket Monsters: Crystal Version.
|
||||
MBC3RamBattery = 0x13,
|
||||
MBC5 = 0x19,
|
||||
MBC5Ram = 0x1A,
|
||||
MBC5RamBattery = 0x1B,
|
||||
MBC5Rumble = 0x1C,
|
||||
MBC5RumbleRam = 0x1D,
|
||||
MBC5RumbleRamBattery = 0x1E,
|
||||
MBC6 = 0x20,
|
||||
MBC7SensorRumbleRamBattery = 0x22,
|
||||
PocketCamera = 0xFC,
|
||||
BandaiTama5 = 0xFD,
|
||||
HuC3 = 0xFE,
|
||||
HuC1RamBattery = 0xFF
|
||||
romOnly = 0x00,
|
||||
MBC1 = 0x01,
|
||||
MBC1Ram = 0x02,
|
||||
MBC1RamBattery = 0x03,
|
||||
MBC2 = 0x05,
|
||||
MBC2Battery = 0x06,
|
||||
RomRam = 0x08, // unused
|
||||
RomRamBattery = 0x09, // unused
|
||||
MMM01 = 0x0B, // multigame roms only
|
||||
MMM01Ram = 0x0C,
|
||||
MMM01RamBattery = 0x0D,
|
||||
MBC3TimerBattery = 0x0F,
|
||||
MBC3TimerRamBattery = 0x10,
|
||||
MBC3 = 0x11,
|
||||
MBC3Ram = 0x12, // MBC3 with 64 KiB of SRAM refers to MBC30, used only in
|
||||
// Pocket Monsters: Crystal Version.
|
||||
MBC3RamBattery = 0x13,
|
||||
MBC5 = 0x19,
|
||||
MBC5Ram = 0x1A,
|
||||
MBC5RamBattery = 0x1B,
|
||||
MBC5Rumble = 0x1C,
|
||||
MBC5RumbleRam = 0x1D,
|
||||
MBC5RumbleRamBattery = 0x1E,
|
||||
MBC6 = 0x20,
|
||||
MBC7SensorRumbleRamBattery = 0x22,
|
||||
PocketCamera = 0xFC,
|
||||
BandaiTama5 = 0xFD,
|
||||
HuC3 = 0xFE,
|
||||
HuC1RamBattery = 0xFF
|
||||
};
|
||||
|
||||
enum PPUMode {
|
||||
mode0, // Horizontal Blank (Mode 0): No access to video RAM, occurs during horizontal blanking period.
|
||||
mode1, // Vertical Blank (Mode 1): No access to video RAM, occurs during vertical blanking period.
|
||||
mode2, // OAM Search (Mode 2): Access to OAM (Object Attribute Memory) only, sprite evaluation.
|
||||
mode3 // Pixel Transfer (Mode 3): Access to both OAM and video RAM, actual pixel transfer to the screen.
|
||||
mode0, // Horizontal Blank (Mode 0): No access to video RAM, occurs during
|
||||
// horizontal blanking period.
|
||||
mode1, // Vertical Blank (Mode 1): No access to video RAM, occurs during
|
||||
// vertical blanking period.
|
||||
mode2, // OAM Search (Mode 2): Access to OAM (Object Attribute Memory) only,
|
||||
// sprite evaluation.
|
||||
mode3 // Pixel Transfer (Mode 3): Access to both OAM and video RAM, actual
|
||||
// pixel transfer to the screen.
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
358
src/gameboy.cpp
358
src/gameboy.cpp
@@ -1,202 +1,194 @@
|
||||
#include <iostream>
|
||||
#include "gameboy.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void GameBoy::addCycles(const uint8_t ticks) {
|
||||
cycles += ticks;
|
||||
if (ppuEnabled) {
|
||||
ppuCycles += ticks;
|
||||
}
|
||||
lastOpTicks = ticks;
|
||||
cycles += ticks;
|
||||
if (ppuEnabled) {
|
||||
ppuCycles += ticks;
|
||||
}
|
||||
lastOpTicks = ticks;
|
||||
}
|
||||
|
||||
GameboyTestState GameBoy::runTest(GameboyTestState initial) {
|
||||
addressSpace.setTesting(true);
|
||||
addressSpace.setTesting(true);
|
||||
|
||||
PC = initial.PC;
|
||||
SP = initial.SP;
|
||||
AF.hi = initial.A;
|
||||
AF.lo = initial.F;
|
||||
BC.hi = initial.B;
|
||||
BC.lo = initial.C;
|
||||
DE.hi = initial.D;
|
||||
DE.lo = initial.E;
|
||||
HL.hi = initial.H;
|
||||
HL.lo = initial.L;
|
||||
addressSpace.memoryLayout.IE = 1;
|
||||
PC = initial.PC;
|
||||
SP = initial.SP;
|
||||
AF.hi = initial.A;
|
||||
AF.lo = initial.F;
|
||||
BC.hi = initial.B;
|
||||
BC.lo = initial.C;
|
||||
DE.hi = initial.D;
|
||||
DE.lo = initial.E;
|
||||
HL.hi = initial.H;
|
||||
HL.lo = initial.L;
|
||||
addressSpace.memoryLayout.IE = 1;
|
||||
|
||||
for (const auto& [addr, val] : initial.RAM) {
|
||||
addressSpace[addr] = val;
|
||||
}
|
||||
for (const auto &[addr, val] : initial.RAM) {
|
||||
addressSpace[addr] = val;
|
||||
}
|
||||
|
||||
opcodeResolver();
|
||||
opcodeResolver();
|
||||
|
||||
std::vector<std::tuple<Word, Byte>> returnRAM;
|
||||
for (const auto& [addr, val] : initial.RAM) {
|
||||
returnRAM.emplace_back(addr, addressSpace[addr]);
|
||||
}
|
||||
return {
|
||||
PC, SP,
|
||||
AF.hi, AF.lo,
|
||||
BC.hi, BC.lo,
|
||||
DE.hi, DE.lo,
|
||||
HL.hi, HL.lo,
|
||||
returnRAM
|
||||
};
|
||||
std::vector<std::tuple<Word, Byte>> returnRAM;
|
||||
for (const auto &[addr, val] : initial.RAM) {
|
||||
returnRAM.emplace_back(addr, addressSpace[addr]);
|
||||
}
|
||||
return {PC, SP, AF.hi, AF.lo, BC.hi, BC.lo,
|
||||
DE.hi, DE.lo, HL.hi, HL.lo, returnRAM};
|
||||
}
|
||||
|
||||
void GameBoy::start(const std::string &bootrom, const std::string &game) {
|
||||
addressSpace.loadBootrom(bootrom);
|
||||
addressSpace.loadGame(game);
|
||||
addressSpace.determineMBCInfo();
|
||||
addressSpace.createRamBank();
|
||||
|
||||
void GameBoy::start(const std::string& bootrom, const std::string& game) {
|
||||
addressSpace.loadBootrom(bootrom);
|
||||
addressSpace.loadGame(game);
|
||||
addressSpace.determineMBCInfo();
|
||||
addressSpace.createRamBank();
|
||||
bool quit = false;
|
||||
bool setIME = false;
|
||||
bool debug = false;
|
||||
bool step = false;
|
||||
|
||||
bool quit = false;
|
||||
bool setIME = false;
|
||||
bool debug = false;
|
||||
bool step = false;
|
||||
while (!quit) {
|
||||
// Event loop
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
quit = true;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_a:
|
||||
joypadInput.LEFT = true;
|
||||
break;
|
||||
case SDLK_d:
|
||||
joypadInput.RIGHT = true;
|
||||
break;
|
||||
case SDLK_w:
|
||||
joypadInput.UP = true;
|
||||
break;
|
||||
case SDLK_s:
|
||||
joypadInput.DOWN = true;
|
||||
break;
|
||||
case SDLK_k:
|
||||
joypadInput.A = true;
|
||||
break;
|
||||
case SDLK_l:
|
||||
joypadInput.B = true;
|
||||
break;
|
||||
case SDLK_o:
|
||||
joypadInput.SELECT = true;
|
||||
break;
|
||||
case SDLK_p:
|
||||
joypadInput.START = true;
|
||||
break;
|
||||
case SDLK_h:
|
||||
debug = !debug;
|
||||
break;
|
||||
case SDLK_n:
|
||||
step = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_a:
|
||||
joypadInput.LEFT = false;
|
||||
break;
|
||||
case SDLK_d:
|
||||
joypadInput.RIGHT = false;
|
||||
break;
|
||||
case SDLK_w:
|
||||
joypadInput.UP = false;
|
||||
break;
|
||||
case SDLK_s:
|
||||
joypadInput.DOWN = false;
|
||||
break;
|
||||
case SDLK_k:
|
||||
joypadInput.A = false;
|
||||
break;
|
||||
case SDLK_l:
|
||||
joypadInput.B = false;
|
||||
break;
|
||||
case SDLK_o:
|
||||
joypadInput.SELECT = false;
|
||||
break;
|
||||
case SDLK_p:
|
||||
joypadInput.START = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (!quit) {
|
||||
// Event loop
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
quit = true;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_a:
|
||||
joypadInput.LEFT = true;
|
||||
break;
|
||||
case SDLK_d:
|
||||
joypadInput.RIGHT = true;
|
||||
break;
|
||||
case SDLK_w:
|
||||
joypadInput.UP = true;
|
||||
break;
|
||||
case SDLK_s:
|
||||
joypadInput.DOWN = true;
|
||||
break;
|
||||
case SDLK_k:
|
||||
joypadInput.A = true;
|
||||
break;
|
||||
case SDLK_l:
|
||||
joypadInput.B = true;
|
||||
break;
|
||||
case SDLK_o:
|
||||
joypadInput.SELECT = true;
|
||||
break;
|
||||
case SDLK_p:
|
||||
joypadInput.START = true;
|
||||
break;
|
||||
case SDLK_h:
|
||||
debug = !debug;
|
||||
break;
|
||||
case SDLK_n:
|
||||
step = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_a:
|
||||
joypadInput.LEFT = false;
|
||||
break;
|
||||
case SDLK_d:
|
||||
joypadInput.RIGHT = false;
|
||||
break;
|
||||
case SDLK_w:
|
||||
joypadInput.UP = false;
|
||||
break;
|
||||
case SDLK_s:
|
||||
joypadInput.DOWN = false;
|
||||
break;
|
||||
case SDLK_k:
|
||||
joypadInput.A = false;
|
||||
break;
|
||||
case SDLK_l:
|
||||
joypadInput.B = false;
|
||||
break;
|
||||
case SDLK_o:
|
||||
joypadInput.SELECT = false;
|
||||
break;
|
||||
case SDLK_p:
|
||||
joypadInput.START = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (!rendered) {
|
||||
if (debug == true && step == false)
|
||||
break;
|
||||
step = false;
|
||||
joypadHandler();
|
||||
if (PC > 0xFF && addressSpace.getBootromState()) {
|
||||
addressSpace.unmapBootrom();
|
||||
}
|
||||
ppuEnabled = addressSpace.memoryLayout.LCDC & 0x80;
|
||||
prevTMA = addressSpace.memoryLayout.TMA;
|
||||
|
||||
while (!rendered) {
|
||||
if (debug == true && step == false)
|
||||
break;
|
||||
step = false;
|
||||
joypadHandler();
|
||||
if (PC > 0xFF && addressSpace.getBootromState()) {
|
||||
addressSpace.unmapBootrom();
|
||||
}
|
||||
ppuEnabled = addressSpace.memoryLayout.LCDC & 0x80;
|
||||
prevTMA = addressSpace.memoryLayout.TMA;
|
||||
if (debug) {
|
||||
printf("A: %.2X F: %.2X B: %.2X C: %.2X D: %.2X E: %.2X H: %.2X L: "
|
||||
"%.2X SP: %.4X PC: 00:%.4X (%.2X %.2X %.2X %.2X)\n",
|
||||
AF.hi, AF.lo, BC.hi, BC.lo, DE.hi, DE.lo, HL.hi, HL.lo, SP, PC,
|
||||
readOnlyAddressSpace[PC], readOnlyAddressSpace[PC + 1],
|
||||
readOnlyAddressSpace[PC + 2], readOnlyAddressSpace[PC + 3]);
|
||||
|
||||
if (debug) {
|
||||
printf(
|
||||
"A: %.2X F: %.2X B: %.2X C: %.2X D: %.2X E: %.2X H: %.2X L: %.2X SP: %.4X PC: 00:%.4X (%.2X %.2X %.2X %.2X)\n",
|
||||
AF.hi, AF.lo, BC.hi, BC.lo, DE.hi, DE.lo, HL.hi, HL.lo, SP, PC, readOnlyAddressSpace[PC],
|
||||
readOnlyAddressSpace[PC + 1], readOnlyAddressSpace[PC + 2], readOnlyAddressSpace[PC + 3]);
|
||||
// printf("Cycles: %lu, Opcode: 0x%.2x PPU cycles: %lu, PPMode: %d\n",
|
||||
// cycles, readOnlyAddressSpace[PC],
|
||||
// cyclesSinceLastScanline(), currentMode);
|
||||
// printf("AF:0x%.4x, BC:0x%.4x\n", AF.reg, BC.reg);
|
||||
// printf("DE:0x%.4x, HL:0x%.4x\n", DE.reg, HL.reg);
|
||||
// printf("IME:%d IF:0x%.2x IE:0x%.2x\n", IME, (*IF), (*IE));
|
||||
// printf("PC:0x%.4x, SP:0x%.4x\n", PC, SP);
|
||||
// printf("LCDC:%.2x STAT:0x%.2x LY:%d LYC:%d\n", (*LCDC), (*STAT),
|
||||
// (*LY), (*LYC)); printf("\n");
|
||||
}
|
||||
|
||||
|
||||
// printf("Cycles: %lu, Opcode: 0x%.2x PPU cycles: %lu, PPMode: %d\n", cycles, readOnlyAddressSpace[PC],
|
||||
// cyclesSinceLastScanline(), currentMode);
|
||||
// printf("AF:0x%.4x, BC:0x%.4x\n", AF.reg, BC.reg);
|
||||
// printf("DE:0x%.4x, HL:0x%.4x\n", DE.reg, HL.reg);
|
||||
// printf("IME:%d IF:0x%.2x IE:0x%.2x\n", IME, (*IF), (*IE));
|
||||
// printf("PC:0x%.4x, SP:0x%.4x\n", PC, SP);
|
||||
// printf("LCDC:%.2x STAT:0x%.2x LY:%d LYC:%d\n", (*LCDC), (*STAT), (*LY), (*LYC));
|
||||
// printf("\n");
|
||||
}
|
||||
|
||||
if (!halted) {
|
||||
opcodeResolver();
|
||||
addressSpace.MBCUpdate();
|
||||
}
|
||||
else {
|
||||
addCycles(4);
|
||||
}
|
||||
timingHandler();
|
||||
interruptHandler();
|
||||
if (ppuEnabled) {
|
||||
ppuUpdate();
|
||||
}
|
||||
else {
|
||||
ppuCycles = 2;
|
||||
lastScanline = 0;
|
||||
lastRefresh = 0;
|
||||
addressSpace.memoryLayout.LY = 0x00;
|
||||
addressSpace.memoryLayout.STAT &= 0xfc;
|
||||
}
|
||||
if (setIME) {
|
||||
IME = 1;
|
||||
setIME = false;
|
||||
}
|
||||
if (IME_togge) {
|
||||
setIME = true;
|
||||
IME_togge = false;
|
||||
}
|
||||
if (addressSpace.dmaTransferRequested) {
|
||||
cyclesUntilDMATransfer -= lastOpTicks;
|
||||
if (cyclesUntilDMATransfer <= 0) {
|
||||
cyclesUntilDMATransfer = 160;
|
||||
addressSpace.dmaTransfer();
|
||||
}
|
||||
}
|
||||
}
|
||||
rendered = false;
|
||||
}
|
||||
if (!halted) {
|
||||
opcodeResolver();
|
||||
addressSpace.MBCUpdate();
|
||||
} else {
|
||||
addCycles(4);
|
||||
}
|
||||
timingHandler();
|
||||
interruptHandler();
|
||||
if (ppuEnabled) {
|
||||
ppuUpdate();
|
||||
} else {
|
||||
ppuCycles = 2;
|
||||
lastScanline = 0;
|
||||
lastRefresh = 0;
|
||||
addressSpace.memoryLayout.LY = 0x00;
|
||||
addressSpace.memoryLayout.STAT &= 0xfc;
|
||||
}
|
||||
if (setIME) {
|
||||
IME = 1;
|
||||
setIME = false;
|
||||
}
|
||||
if (IME_togge) {
|
||||
setIME = true;
|
||||
IME_togge = false;
|
||||
}
|
||||
if (addressSpace.dmaTransferRequested) {
|
||||
cyclesUntilDMATransfer -= lastOpTicks;
|
||||
if (cyclesUntilDMATransfer <= 0) {
|
||||
cyclesUntilDMATransfer = 160;
|
||||
addressSpace.dmaTransfer();
|
||||
}
|
||||
}
|
||||
}
|
||||
rendered = false;
|
||||
}
|
||||
}
|
||||
|
||||
290
src/gameboy.hpp
290
src/gameboy.hpp
@@ -1,186 +1,172 @@
|
||||
#ifndef GBPP_SRC_GAMEBOY_HPP_
|
||||
#define GBPP_SRC_GAMEBOY_HPP_
|
||||
|
||||
#include <filesystem>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
#include "defines.hpp"
|
||||
#include "addressSpace.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "testing.hpp"
|
||||
#include <SDL.h>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
union RegisterPair {
|
||||
Word reg; //register.reg == (hi << 8) + lo. (hi is more significant than lo)
|
||||
Word reg; // register.reg == (hi << 8) + lo. (hi is more significant than lo)
|
||||
|
||||
struct {
|
||||
Byte lo;
|
||||
Byte hi;
|
||||
};
|
||||
struct {
|
||||
Byte lo;
|
||||
Byte hi;
|
||||
};
|
||||
};
|
||||
|
||||
class GameBoy {
|
||||
//T-cycles not M-cycles (4 T-cycles = 1 M-cycle)
|
||||
uint64_t cycles = 0;
|
||||
//Start at 2 T-cycles https://github.com/Gekkio/mooneye-test-suite/blob/main/acceptance/ppu/lcdon_timing-GS.s
|
||||
uint64_t ppuCycles = 2;
|
||||
bool ppuEnabled = false;
|
||||
uint16_t lastOpTicks = 0;
|
||||
uint64_t lastRefresh = 0;
|
||||
uint64_t lastScanline = 0;
|
||||
uint64_t cyclesToStayInHblank = -1;
|
||||
uint64_t lastDivUpdate = 0;
|
||||
bool rendered = false;
|
||||
// T-cycles not M-cycles (4 T-cycles = 1 M-cycle)
|
||||
uint64_t cycles = 0;
|
||||
// Start at 2 T-cycles
|
||||
// https://github.com/Gekkio/mooneye-test-suite/blob/main/acceptance/ppu/lcdon_timing-GS.s
|
||||
uint64_t ppuCycles = 2;
|
||||
bool ppuEnabled = false;
|
||||
uint16_t lastOpTicks = 0;
|
||||
uint64_t lastRefresh = 0;
|
||||
uint64_t lastScanline = 0;
|
||||
uint64_t cyclesToStayInHblank = -1;
|
||||
uint64_t lastDivUpdate = 0;
|
||||
bool rendered = false;
|
||||
|
||||
uint8_t IME = 0; //enables interupts
|
||||
// EI is actually "disable interrupts for one instruction, then enable them"
|
||||
// This keeps track of that
|
||||
bool IME_togge = false;
|
||||
uint8_t IME = 0; // enables interupts
|
||||
// EI is actually "disable interrupts for one instruction, then enable them"
|
||||
// This keeps track of that
|
||||
bool IME_togge = false;
|
||||
|
||||
//Accumulator and flags
|
||||
RegisterPair AF = {0};
|
||||
//General purpose CPU registers
|
||||
RegisterPair BC = {0};
|
||||
RegisterPair DE = {0};
|
||||
RegisterPair HL = {0};
|
||||
// Accumulator and flags
|
||||
RegisterPair AF = {0};
|
||||
// General purpose CPU registers
|
||||
RegisterPair BC = {0};
|
||||
RegisterPair DE = {0};
|
||||
RegisterPair HL = {0};
|
||||
|
||||
Word SP = 0xFFFE; //stack pointer
|
||||
Word PC = 0x0000; //program counter
|
||||
Word SP = 0xFFFE; // stack pointer
|
||||
Word PC = 0x0000; // program counter
|
||||
|
||||
AddressSpace addressSpace;
|
||||
const AddressSpace& readOnlyAddressSpace = addressSpace;
|
||||
AddressSpace addressSpace;
|
||||
const AddressSpace &readOnlyAddressSpace = addressSpace;
|
||||
|
||||
PPUMode currentMode = PPUMode::mode0;
|
||||
Byte windowLineCounter = 0;
|
||||
int16_t cyclesUntilDMATransfer = 160;
|
||||
PPUMode currentMode = PPUMode::mode0;
|
||||
Byte windowLineCounter = 0;
|
||||
int16_t cyclesUntilDMATransfer = 160;
|
||||
|
||||
Byte prevTMA = 0;
|
||||
uint64_t lastTIMAUpdate = 0;
|
||||
bool halted = false;
|
||||
bool haltBug = true;
|
||||
bool stopped = false;
|
||||
Byte prevTMA = 0;
|
||||
uint64_t lastTIMAUpdate = 0;
|
||||
bool halted = false;
|
||||
bool haltBug = true;
|
||||
bool stopped = false;
|
||||
|
||||
//3 colour channels
|
||||
uint32_t* framebuffer = new uint32_t[RESOLUTION_X * RESOLUTION_Y * SCREEN_BPP];
|
||||
SDL_Window* screen = nullptr;
|
||||
SDL_Renderer* renderer = nullptr;
|
||||
SDL_Texture* texture = nullptr;
|
||||
SDL_Event event = {0};
|
||||
uint32_t frameStart = 0;
|
||||
uint32_t frameTime = 0;
|
||||
const int frameDelay = 1000 / V_SYNC;
|
||||
// 3 colour channels
|
||||
uint32_t *framebuffer =
|
||||
new uint32_t[RESOLUTION_X * RESOLUTION_Y * SCREEN_BPP];
|
||||
SDL_Window *screen = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
SDL_Texture *texture = nullptr;
|
||||
SDL_Event event = {0};
|
||||
uint32_t frameStart = 0;
|
||||
uint32_t frameTime = 0;
|
||||
const int frameDelay = 1000 / V_SYNC;
|
||||
|
||||
Input joypadInput;
|
||||
void joypadHandler();
|
||||
Input joypadInput;
|
||||
void joypadHandler();
|
||||
|
||||
void opcodeResolver();
|
||||
void opcodeResolver();
|
||||
|
||||
bool statInteruptLine = false;
|
||||
bool LCDCBitEnabled(Byte bit) const;
|
||||
void incLY();
|
||||
void ppuUpdate();
|
||||
void drawLine();
|
||||
static bool oamBitEnabled(Byte oamAttributeByte, Byte bit);
|
||||
static unsigned int getColourFromPalette(Byte palette);
|
||||
void SDL2present();
|
||||
bool statInteruptLine = false;
|
||||
bool LCDCBitEnabled(Byte bit) const;
|
||||
void incLY();
|
||||
void ppuUpdate();
|
||||
void drawLine();
|
||||
static bool oamBitEnabled(Byte oamAttributeByte, Byte bit);
|
||||
static unsigned int getColourFromPalette(Byte palette);
|
||||
void SDL2present();
|
||||
|
||||
void checkPPUMode();
|
||||
void setPPUMode(PPUMode mode);
|
||||
uint64_t cyclesSinceLastScanline() const;
|
||||
uint64_t cyclesSinceLastRefresh() const;
|
||||
void checkPPUMode();
|
||||
void setPPUMode(PPUMode mode);
|
||||
uint64_t cyclesSinceLastScanline() const;
|
||||
uint64_t cyclesSinceLastRefresh() const;
|
||||
|
||||
void timingHandler();
|
||||
void timingHandler();
|
||||
|
||||
void interruptHandler();
|
||||
bool testInterruptEnabled(Byte interrupt) const;
|
||||
void setInterrupt(Byte interrupt);
|
||||
void resetInterrupt(Byte interrupt);
|
||||
void interruptHandler();
|
||||
bool testInterruptEnabled(Byte interrupt) const;
|
||||
void setInterrupt(Byte interrupt);
|
||||
void resetInterrupt(Byte interrupt);
|
||||
|
||||
void VBlankHandle();
|
||||
void LCDStatHandle();
|
||||
void timerHandle();
|
||||
void serialHandle();
|
||||
void joypadHandle();
|
||||
void VBlankHandle();
|
||||
void LCDStatHandle();
|
||||
void timerHandle();
|
||||
void serialHandle();
|
||||
void joypadHandle();
|
||||
|
||||
void setFlag(Byte bit);
|
||||
void resetFlag(Byte bit);
|
||||
bool getFlag(Byte bit) const;
|
||||
void setFlag(Byte bit);
|
||||
void resetFlag(Byte bit);
|
||||
bool getFlag(Byte bit) const;
|
||||
|
||||
Word getWordPC();
|
||||
Byte getBytePC();
|
||||
Word getWordSP();
|
||||
Byte getByteSP();
|
||||
Word getWordPC();
|
||||
Byte getBytePC();
|
||||
Word getWordSP();
|
||||
Byte getByteSP();
|
||||
|
||||
void addCycles(Byte ticks);
|
||||
void addCycles(Byte ticks);
|
||||
|
||||
//OPCODE FUNCTIONS
|
||||
template <typename T>
|
||||
void ld(T& dest, T src);
|
||||
void ldW(Word destAddr, Word src);
|
||||
template <typename T>
|
||||
void orBitwise(T& dest, T src);
|
||||
template <typename T>
|
||||
void andBitwise(T& dest, T src);
|
||||
template <typename T>
|
||||
void xorBitwise(T& dest, T src);
|
||||
void bit(Byte testBit, Byte reg);
|
||||
void extendedOpcodeResolver();
|
||||
static void set(uint8_t testBit, uint8_t& reg);
|
||||
static void res(uint8_t testBit, uint8_t& reg);
|
||||
template <typename T>
|
||||
void jp(T address);
|
||||
template <typename T>
|
||||
bool jrNZ(T offset);
|
||||
template <class T>
|
||||
bool jrNC(T offset);
|
||||
template <class T>
|
||||
bool jrC(T offset);
|
||||
template <typename T>
|
||||
void inc(T& reg);
|
||||
template <typename T>
|
||||
void call(T address);
|
||||
void halt();
|
||||
void daa();
|
||||
void stop();
|
||||
void cp(Byte value);
|
||||
template <typename T>
|
||||
void dec(T& reg);
|
||||
template <typename T>
|
||||
bool jrZ(T offset);
|
||||
void sub(Byte value);
|
||||
void sbc(Byte value);
|
||||
template <typename T>
|
||||
void jr(T OFFSET);
|
||||
void push(Word reg);
|
||||
void rl(Byte& reg);
|
||||
void sla(Byte& reg);
|
||||
void sra(uint8_t& reg);
|
||||
void srl(uint8_t& reg);
|
||||
void rrc(Byte& reg);
|
||||
void rrca();
|
||||
void rra();
|
||||
void rr(Byte& reg);
|
||||
void rlc(Byte& reg);
|
||||
void rlca();
|
||||
void rla();
|
||||
template <typename T>
|
||||
void pop(T& reg);
|
||||
template <typename T>
|
||||
void rst(T address);
|
||||
void ret();
|
||||
template <typename T>
|
||||
void add(T& reg, T value);
|
||||
void adc(Byte value);
|
||||
void cpl();
|
||||
void scf();
|
||||
void ccf();
|
||||
void swap(Byte& value);
|
||||
// OPCODE FUNCTIONS
|
||||
template <typename T> void ld(T &dest, T src);
|
||||
void ldW(Word destAddr, Word src);
|
||||
template <typename T> void orBitwise(T &dest, T src);
|
||||
template <typename T> void andBitwise(T &dest, T src);
|
||||
template <typename T> void xorBitwise(T &dest, T src);
|
||||
void bit(Byte testBit, Byte reg);
|
||||
void extendedOpcodeResolver();
|
||||
static void set(uint8_t testBit, uint8_t ®);
|
||||
static void res(uint8_t testBit, uint8_t ®);
|
||||
template <typename T> void jp(T address);
|
||||
template <typename T> bool jrNZ(T offset);
|
||||
template <class T> bool jrNC(T offset);
|
||||
template <class T> bool jrC(T offset);
|
||||
template <typename T> void inc(T ®);
|
||||
template <typename T> void call(T address);
|
||||
void halt();
|
||||
void daa();
|
||||
void stop();
|
||||
void cp(Byte value);
|
||||
template <typename T> void dec(T ®);
|
||||
template <typename T> bool jrZ(T offset);
|
||||
void sub(Byte value);
|
||||
void sbc(Byte value);
|
||||
template <typename T> void jr(T OFFSET);
|
||||
void push(Word reg);
|
||||
void rl(Byte ®);
|
||||
void sla(Byte ®);
|
||||
void sra(uint8_t ®);
|
||||
void srl(uint8_t ®);
|
||||
void rrc(Byte ®);
|
||||
void rrca();
|
||||
void rra();
|
||||
void rr(Byte ®);
|
||||
void rlc(Byte ®);
|
||||
void rlca();
|
||||
void rla();
|
||||
template <typename T> void pop(T ®);
|
||||
template <typename T> void rst(T address);
|
||||
void ret();
|
||||
template <typename T> void add(T ®, T value);
|
||||
void adc(Byte value);
|
||||
void cpl();
|
||||
void scf();
|
||||
void ccf();
|
||||
void swap(Byte &value);
|
||||
|
||||
public:
|
||||
void start(const std::string& bootrom, const std::string& game);
|
||||
void SDL2setup();
|
||||
void SDL2destroy() const;
|
||||
void start(const std::string &bootrom, const std::string &game);
|
||||
void SDL2setup();
|
||||
void SDL2destroy() const;
|
||||
|
||||
GameboyTestState runTest(GameboyTestState initial);
|
||||
GameboyTestState runTest(GameboyTestState initial);
|
||||
};
|
||||
|
||||
#endif //GBPP_SRC_GAMEBOY_HPP_
|
||||
#endif // GBPP_SRC_GAMEBOY_HPP_
|
||||
|
||||
@@ -2,88 +2,94 @@
|
||||
#include "gameboy.hpp"
|
||||
|
||||
bool GameBoy::testInterruptEnabled(const Byte interrupt) const {
|
||||
return readOnlyAddressSpace.memoryLayout.IE & static_cast<Byte>(1 << interrupt);
|
||||
return readOnlyAddressSpace.memoryLayout.IE &
|
||||
static_cast<Byte>(1 << interrupt);
|
||||
}
|
||||
|
||||
void GameBoy::setInterrupt(const Byte interrupt) {
|
||||
addressSpace.memoryLayout.IF |= 1 << interrupt;
|
||||
addressSpace.memoryLayout.IF |= 0xE0;
|
||||
addressSpace.memoryLayout.IF |= 1 << interrupt;
|
||||
addressSpace.memoryLayout.IF |= 0xE0;
|
||||
}
|
||||
|
||||
void GameBoy::resetInterrupt(const Byte interrupt) {
|
||||
addressSpace.memoryLayout.IF &= ~(1 << interrupt);
|
||||
addressSpace.memoryLayout.IF |= 0xE0;
|
||||
addressSpace.memoryLayout.IF &= ~(1 << interrupt);
|
||||
addressSpace.memoryLayout.IF |= 0xE0;
|
||||
}
|
||||
|
||||
void GameBoy::interruptHandler() {
|
||||
if (readOnlyAddressSpace.memoryLayout.IF & static_cast<Byte>(1 << VBLANK_INTERRUPT) && testInterruptEnabled(
|
||||
VBLANK_INTERRUPT)) {
|
||||
if (IME)
|
||||
VBlankHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF & static_cast<Byte>(1 << LCD_STAT_INTERRUPT) && testInterruptEnabled(
|
||||
LCD_STAT_INTERRUPT)) {
|
||||
if (IME)
|
||||
LCDStatHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF & static_cast<Byte>(1 << TIMER_INTERRUPT) && testInterruptEnabled(
|
||||
TIMER_INTERRUPT)) {
|
||||
if (IME)
|
||||
timerHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF & static_cast<Byte>(1 << SERIAL_INTERRUPT) && testInterruptEnabled(
|
||||
SERIAL_INTERRUPT)) {
|
||||
if (IME)
|
||||
serialHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF & static_cast<Byte>(1 << JOYPAD_INTERRUPT) && testInterruptEnabled(
|
||||
JOYPAD_INTERRUPT)) {
|
||||
if (IME)
|
||||
joypadHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF &
|
||||
static_cast<Byte>(1 << VBLANK_INTERRUPT) &&
|
||||
testInterruptEnabled(VBLANK_INTERRUPT)) {
|
||||
if (IME)
|
||||
VBlankHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF &
|
||||
static_cast<Byte>(1 << LCD_STAT_INTERRUPT) &&
|
||||
testInterruptEnabled(LCD_STAT_INTERRUPT)) {
|
||||
if (IME)
|
||||
LCDStatHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF &
|
||||
static_cast<Byte>(1 << TIMER_INTERRUPT) &&
|
||||
testInterruptEnabled(TIMER_INTERRUPT)) {
|
||||
if (IME)
|
||||
timerHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF &
|
||||
static_cast<Byte>(1 << SERIAL_INTERRUPT) &&
|
||||
testInterruptEnabled(SERIAL_INTERRUPT)) {
|
||||
if (IME)
|
||||
serialHandle();
|
||||
halted = false;
|
||||
}
|
||||
if (readOnlyAddressSpace.memoryLayout.IF &
|
||||
static_cast<Byte>(1 << JOYPAD_INTERRUPT) &&
|
||||
testInterruptEnabled(JOYPAD_INTERRUPT)) {
|
||||
if (IME)
|
||||
joypadHandle();
|
||||
halted = false;
|
||||
}
|
||||
}
|
||||
|
||||
void GameBoy::VBlankHandle() {
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x40;
|
||||
resetInterrupt(VBLANK_INTERRUPT);
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x40;
|
||||
resetInterrupt(VBLANK_INTERRUPT);
|
||||
}
|
||||
|
||||
void GameBoy::LCDStatHandle() {
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x48;
|
||||
resetInterrupt(LCD_STAT_INTERRUPT);
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x48;
|
||||
resetInterrupt(LCD_STAT_INTERRUPT);
|
||||
}
|
||||
|
||||
void GameBoy::timerHandle() {
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x50;
|
||||
resetInterrupt(TIMER_INTERRUPT);
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x50;
|
||||
resetInterrupt(TIMER_INTERRUPT);
|
||||
}
|
||||
|
||||
void GameBoy::serialHandle() {
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x58;
|
||||
resetInterrupt(SERIAL_INTERRUPT);
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x58;
|
||||
resetInterrupt(SERIAL_INTERRUPT);
|
||||
}
|
||||
|
||||
void GameBoy::joypadHandle() {
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x60;
|
||||
resetInterrupt(JOYPAD_INTERRUPT);
|
||||
IME = 0;
|
||||
push(PC);
|
||||
addCycles(20);
|
||||
PC = 0x60;
|
||||
resetInterrupt(JOYPAD_INTERRUPT);
|
||||
}
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
#include "gameboy.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "gameboy.hpp"
|
||||
|
||||
void GameBoy::joypadHandler() {
|
||||
const Byte joyP = addressSpace.memoryLayout.JOYP;
|
||||
const bool buttons = (joyP & 0x20) == 0;
|
||||
const bool dpad = (joyP & 0x10) == 0;
|
||||
const Byte joyP = addressSpace.memoryLayout.JOYP;
|
||||
const bool buttons = (joyP & 0x20) == 0;
|
||||
const bool dpad = (joyP & 0x10) == 0;
|
||||
|
||||
addressSpace.memoryLayout.JOYP |= 0xCF;
|
||||
addressSpace.memoryLayout.JOYP |= 0xCF;
|
||||
|
||||
if (buttons && !dpad) {
|
||||
if (joypadInput.A)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x1;
|
||||
if (joypadInput.B)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x2;
|
||||
if (joypadInput.SELECT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x4;
|
||||
if (joypadInput.START)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x8;
|
||||
}
|
||||
if (!buttons && dpad) {
|
||||
if (joypadInput.RIGHT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x1;
|
||||
if (joypadInput.LEFT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x2;
|
||||
if (joypadInput.UP)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x4;
|
||||
if (joypadInput.DOWN)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x8;
|
||||
}
|
||||
if (buttons && !dpad) {
|
||||
if (joypadInput.A)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x1;
|
||||
if (joypadInput.B)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x2;
|
||||
if (joypadInput.SELECT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x4;
|
||||
if (joypadInput.START)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x8;
|
||||
}
|
||||
if (!buttons && dpad) {
|
||||
if (joypadInput.RIGHT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x1;
|
||||
if (joypadInput.LEFT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x2;
|
||||
if (joypadInput.UP)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x4;
|
||||
if (joypadInput.DOWN)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x8;
|
||||
}
|
||||
|
||||
if (buttons && dpad) {
|
||||
addressSpace.memoryLayout.JOYP |= 0xCF;
|
||||
if (buttons && dpad) {
|
||||
addressSpace.memoryLayout.JOYP |= 0xCF;
|
||||
|
||||
if (joypadInput.RIGHT && joypadInput.A)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x1;
|
||||
if (joypadInput.LEFT && joypadInput.B)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x2;
|
||||
if (joypadInput.UP && joypadInput.SELECT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x4;
|
||||
if (joypadInput.DOWN && joypadInput.START)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x8;
|
||||
}
|
||||
if (joypadInput.RIGHT && joypadInput.A)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x1;
|
||||
if (joypadInput.LEFT && joypadInput.B)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x2;
|
||||
if (joypadInput.UP && joypadInput.SELECT)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x4;
|
||||
if (joypadInput.DOWN && joypadInput.START)
|
||||
addressSpace.memoryLayout.JOYP &= ~0x8;
|
||||
}
|
||||
}
|
||||
|
||||
147
src/main.cpp
147
src/main.cpp
@@ -1,94 +1,91 @@
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include "3rdParty/json.hpp"
|
||||
#include "gameboy.hpp"
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
using json = nlohmann::json;
|
||||
|
||||
void runJSONTests(GameBoy* gb);
|
||||
void runJSONTests(GameBoy *gb);
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 3) {
|
||||
std::cerr << "Usage: " << argv[0] << " <bios> <game>\n" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 3) {
|
||||
std::cerr << "Usage: " << argv[0] << " <bios> <game>\n" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto* gb = new GameBoy();
|
||||
gb->SDL2setup();
|
||||
//runJSONTests(gb);
|
||||
gb->start(argv[1], argv[2]);
|
||||
gb->SDL2destroy();
|
||||
delete gb;
|
||||
auto *gb = new GameBoy();
|
||||
gb->SDL2setup();
|
||||
// runJSONTests(gb);
|
||||
gb->start(argv[1], argv[2]);
|
||||
gb->SDL2destroy();
|
||||
delete gb;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void runJSONTests(GameBoy* gb) {
|
||||
std::string path = "../tests/sm83/v1";
|
||||
std::vector<std::string> testFiles;
|
||||
int failed = 0;
|
||||
for (const auto& entry : fs::directory_iterator(path))
|
||||
testFiles.emplace_back(entry.path());
|
||||
void runJSONTests(GameBoy *gb) {
|
||||
std::string path = "../tests/sm83/v1";
|
||||
std::vector<std::string> testFiles;
|
||||
int failed = 0;
|
||||
for (const auto &entry : fs::directory_iterator(path))
|
||||
testFiles.emplace_back(entry.path());
|
||||
|
||||
for (const auto &testFile : testFiles) {
|
||||
std::ifstream file(testFile);
|
||||
std::cout << "Running test: " << testFile << std::endl;
|
||||
const json tests = json::parse(file);
|
||||
|
||||
for (const auto& testFile : testFiles) {
|
||||
std::ifstream file(testFile);
|
||||
std::cout << "Running test: " << testFile << std::endl;
|
||||
const json tests = json::parse(file);
|
||||
for (auto &test : tests) {
|
||||
// create state
|
||||
std::vector<std::tuple<Word, Byte>> initialRAM;
|
||||
for (int i = 0; i < test["initial"]["ram"].size(); i++)
|
||||
initialRAM.emplace_back(test["initial"]["ram"][i][0],
|
||||
test["initial"]["ram"][i][1]);
|
||||
|
||||
for (auto& test : tests) {
|
||||
//create state
|
||||
std::vector<std::tuple<Word, Byte>> initialRAM;
|
||||
for (int i = 0; i < test["initial"]["ram"].size(); i++)
|
||||
initialRAM.emplace_back(test["initial"]["ram"][i][0], test["initial"]["ram"][i][1]);
|
||||
GameboyTestState initialState = {test["initial"]["pc"],
|
||||
test["initial"]["sp"],
|
||||
test["initial"]["a"],
|
||||
test["initial"]["f"],
|
||||
test["initial"]["b"],
|
||||
test["initial"]["c"],
|
||||
test["initial"]["d"],
|
||||
test["initial"]["e"],
|
||||
test["initial"]["h"],
|
||||
test["initial"]["l"],
|
||||
initialRAM};
|
||||
|
||||
GameboyTestState initialState = {
|
||||
test["initial"]["pc"],
|
||||
test["initial"]["sp"],
|
||||
test["initial"]["a"],
|
||||
test["initial"]["f"],
|
||||
test["initial"]["b"],
|
||||
test["initial"]["c"],
|
||||
test["initial"]["d"],
|
||||
test["initial"]["e"],
|
||||
test["initial"]["h"],
|
||||
test["initial"]["l"],
|
||||
initialRAM
|
||||
};
|
||||
// run
|
||||
GameboyTestState result = gb->runTest(initialState);
|
||||
|
||||
//run
|
||||
GameboyTestState result = gb->runTest(initialState);
|
||||
// compare new state to expected
|
||||
std::vector<std::tuple<Word, Byte>> finalRAM;
|
||||
for (int i = 0; i < test["final"]["ram"].size(); i++)
|
||||
finalRAM.emplace_back(test["final"]["ram"][i][0],
|
||||
test["final"]["ram"][i][1]);
|
||||
|
||||
//compare new state to expected
|
||||
std::vector<std::tuple<Word, Byte>> finalRAM;
|
||||
for (int i = 0; i < test["final"]["ram"].size(); i++)
|
||||
finalRAM.emplace_back(test["final"]["ram"][i][0], test["final"]["ram"][i][1]);
|
||||
GameboyTestState finalState = {test["final"]["pc"],
|
||||
test["final"]["sp"],
|
||||
test["final"]["a"],
|
||||
test["final"]["f"],
|
||||
test["final"]["b"],
|
||||
test["final"]["c"],
|
||||
test["final"]["d"],
|
||||
test["final"]["e"],
|
||||
test["final"]["h"],
|
||||
test["final"]["l"],
|
||||
finalRAM};
|
||||
|
||||
GameboyTestState finalState = {
|
||||
test["final"]["pc"],
|
||||
test["final"]["sp"],
|
||||
test["final"]["a"],
|
||||
test["final"]["f"],
|
||||
test["final"]["b"],
|
||||
test["final"]["c"],
|
||||
test["final"]["d"],
|
||||
test["final"]["e"],
|
||||
test["final"]["h"],
|
||||
test["final"]["l"],
|
||||
finalRAM
|
||||
};
|
||||
|
||||
if (finalState != result) {
|
||||
std::cout << "Test " << testFile << " failed!" << std::endl;
|
||||
failed += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!failed)
|
||||
std::cout << "Success!" << std::endl;
|
||||
else
|
||||
std::cout << failed << "/" << testFiles.size() << " failed!" << std::endl;
|
||||
if (finalState != result) {
|
||||
std::cout << "Test " << testFile << " failed!" << std::endl;
|
||||
failed += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!failed)
|
||||
std::cout << "Success!" << std::endl;
|
||||
else
|
||||
std::cout << failed << "/" << testFiles.size() << " failed!" << std::endl;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
616
src/ppu.cpp
616
src/ppu.cpp
@@ -1,390 +1,408 @@
|
||||
#include "gameboy.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "gameboy.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
bool GameBoy::LCDCBitEnabled(const Byte bit) const {
|
||||
return readOnlyAddressSpace.memoryLayout.LCDC & static_cast<Byte>(1 << bit);
|
||||
return readOnlyAddressSpace.memoryLayout.LCDC & static_cast<Byte>(1 << bit);
|
||||
}
|
||||
|
||||
bool GameBoy::oamBitEnabled(const Byte oamAttributeByte, const Byte bit) {
|
||||
return oamAttributeByte & static_cast<Byte>(1 << bit);
|
||||
return oamAttributeByte & static_cast<Byte>(1 << bit);
|
||||
}
|
||||
|
||||
unsigned int GameBoy::getColourFromPalette(const Byte palette) {
|
||||
switch (palette & 0x3) {
|
||||
case 0:
|
||||
return WHITE;
|
||||
case 1:
|
||||
return LIGHT_GRAY;
|
||||
case 2:
|
||||
return DARK_GRAY;
|
||||
case 3:
|
||||
return BLACK;
|
||||
default:
|
||||
std::unreachable();
|
||||
}
|
||||
switch (palette & 0x3) {
|
||||
case 0:
|
||||
return WHITE;
|
||||
case 1:
|
||||
return LIGHT_GRAY;
|
||||
case 2:
|
||||
return DARK_GRAY;
|
||||
case 3:
|
||||
return BLACK;
|
||||
default:
|
||||
std::unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
void GameBoy::ppuUpdate() {
|
||||
//test for HBlank
|
||||
checkPPUMode();
|
||||
// test for HBlank
|
||||
checkPPUMode();
|
||||
|
||||
// TODO:
|
||||
// bug on DMG models triggers a STAT interrupt anytime the STAT register is written
|
||||
// Road Rage and Zerd no Denetsu rely on this
|
||||
// TODO:
|
||||
// bug on DMG models triggers a STAT interrupt anytime the STAT register is
|
||||
// written Road Rage and Zerd no Denetsu rely on this
|
||||
|
||||
// Check for STAT interrupts and request if needed (e.g., when entering specific modes)
|
||||
const bool hBlankInterruptEnabled = readOnlyAddressSpace.memoryLayout.STAT & (1 << 3);
|
||||
const bool drawingInterruptEnabled = readOnlyAddressSpace.memoryLayout.STAT & (1 << 4);
|
||||
const bool oamInterruptEnabled = readOnlyAddressSpace.memoryLayout.STAT & (1 << 5);
|
||||
const bool previousInterruptLine = statInteruptLine;
|
||||
// Check for STAT interrupts and request if needed (e.g., when entering
|
||||
// specific modes)
|
||||
const bool hBlankInterruptEnabled =
|
||||
readOnlyAddressSpace.memoryLayout.STAT & (1 << 3);
|
||||
const bool drawingInterruptEnabled =
|
||||
readOnlyAddressSpace.memoryLayout.STAT & (1 << 4);
|
||||
const bool oamInterruptEnabled =
|
||||
readOnlyAddressSpace.memoryLayout.STAT & (1 << 5);
|
||||
const bool previousInterruptLine = statInteruptLine;
|
||||
|
||||
if (currentMode == PPUMode::mode0 && hBlankInterruptEnabled ||
|
||||
currentMode == PPUMode::mode3 && drawingInterruptEnabled ||
|
||||
currentMode == PPUMode::mode2 && oamInterruptEnabled) {
|
||||
statInteruptLine = true;
|
||||
}
|
||||
else {
|
||||
statInteruptLine = false;
|
||||
}
|
||||
if (currentMode == PPUMode::mode0 && hBlankInterruptEnabled ||
|
||||
currentMode == PPUMode::mode3 && drawingInterruptEnabled ||
|
||||
currentMode == PPUMode::mode2 && oamInterruptEnabled) {
|
||||
statInteruptLine = true;
|
||||
} else {
|
||||
statInteruptLine = false;
|
||||
}
|
||||
|
||||
const bool lyLycInterruptEnabled = readOnlyAddressSpace.memoryLayout.STAT & (1 << 6);
|
||||
const bool lyLycInterruptEnabled =
|
||||
readOnlyAddressSpace.memoryLayout.STAT & (1 << 6);
|
||||
|
||||
if (readOnlyAddressSpace.memoryLayout.LY == readOnlyAddressSpace.memoryLayout.LYC) {
|
||||
addressSpace.memoryLayout.STAT |= (1 << 2);
|
||||
if (lyLycInterruptEnabled)
|
||||
statInteruptLine = true;
|
||||
}
|
||||
else {
|
||||
addressSpace.memoryLayout.STAT &= ~(1 << 2);
|
||||
}
|
||||
if (statInteruptLine && !previousInterruptLine)
|
||||
addressSpace.memoryLayout.IF |= 1 << LCD_STAT_INTERRUPT;
|
||||
if (readOnlyAddressSpace.memoryLayout.LY ==
|
||||
readOnlyAddressSpace.memoryLayout.LYC) {
|
||||
addressSpace.memoryLayout.STAT |= (1 << 2);
|
||||
if (lyLycInterruptEnabled)
|
||||
statInteruptLine = true;
|
||||
} else {
|
||||
addressSpace.memoryLayout.STAT &= ~(1 << 2);
|
||||
}
|
||||
if (statInteruptLine && !previousInterruptLine)
|
||||
addressSpace.memoryLayout.IF |= 1 << LCD_STAT_INTERRUPT;
|
||||
}
|
||||
|
||||
void GameBoy::checkPPUMode() {
|
||||
// Check the PPU mode (HBlank, VBlank, OAM Search, or Pixel Transfer)
|
||||
const uint64_t cyclesSinceScanline = cyclesSinceLastScanline();
|
||||
// Check the PPU mode (HBlank, VBlank, OAM Search, or Pixel Transfer)
|
||||
const uint64_t cyclesSinceScanline = cyclesSinceLastScanline();
|
||||
|
||||
switch (currentMode) {
|
||||
//hblank AND vblank
|
||||
case 0:
|
||||
case 1:
|
||||
if (cyclesSinceScanline > SCANLINE_DURATION) {
|
||||
lastScanline = ppuCycles - (cyclesSinceScanline - SCANLINE_DURATION);
|
||||
incLY();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (cyclesSinceScanline > MODE2_DURATION) {
|
||||
setPPUMode(PPUMode::mode3);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (cyclesSinceScanline > MODE2_DURATION + MODE3_MIN_DURATION) {
|
||||
drawLine();
|
||||
setPPUMode(PPUMode::mode0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
switch (currentMode) {
|
||||
// hblank AND vblank
|
||||
case 0:
|
||||
case 1:
|
||||
if (cyclesSinceScanline > SCANLINE_DURATION) {
|
||||
lastScanline = ppuCycles - (cyclesSinceScanline - SCANLINE_DURATION);
|
||||
incLY();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (cyclesSinceScanline > MODE2_DURATION) {
|
||||
setPPUMode(PPUMode::mode3);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (cyclesSinceScanline > MODE2_DURATION + MODE3_MIN_DURATION) {
|
||||
drawLine();
|
||||
setPPUMode(PPUMode::mode0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void GameBoy::incLY() {
|
||||
addressSpace.memoryLayout.LY += 1;
|
||||
setPPUMode(PPUMode::mode2);
|
||||
if (addressSpace.memoryLayout.LY > SCANLINES_PER_FRAME - 1) {
|
||||
addressSpace.memoryLayout.LY = 0;
|
||||
windowLineCounter = 0;
|
||||
}
|
||||
else if (addressSpace.memoryLayout.LY == 144) {
|
||||
// VBlank Period
|
||||
SDL2present();
|
||||
setPPUMode(PPUMode::mode1);
|
||||
addressSpace.memoryLayout.IF |= 0x1;
|
||||
}
|
||||
addressSpace.memoryLayout.LY += 1;
|
||||
setPPUMode(PPUMode::mode2);
|
||||
if (addressSpace.memoryLayout.LY > SCANLINES_PER_FRAME - 1) {
|
||||
addressSpace.memoryLayout.LY = 0;
|
||||
windowLineCounter = 0;
|
||||
} else if (addressSpace.memoryLayout.LY == 144) {
|
||||
// VBlank Period
|
||||
SDL2present();
|
||||
setPPUMode(PPUMode::mode1);
|
||||
addressSpace.memoryLayout.IF |= 0x1;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t GameBoy::cyclesSinceLastScanline() const {
|
||||
const uint64_t difference = ppuCycles - lastScanline;
|
||||
return difference;
|
||||
const uint64_t difference = ppuCycles - lastScanline;
|
||||
return difference;
|
||||
}
|
||||
|
||||
uint64_t GameBoy::cyclesSinceLastRefresh() const {
|
||||
const uint64_t difference = ppuCycles - lastRefresh;
|
||||
return difference;
|
||||
const uint64_t difference = ppuCycles - lastRefresh;
|
||||
return difference;
|
||||
}
|
||||
|
||||
void GameBoy::setPPUMode(const PPUMode mode) {
|
||||
switch (mode) {
|
||||
case mode0:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
currentMode = mode0;
|
||||
break;
|
||||
case mode1:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
addressSpace.memoryLayout.STAT |= 0x01;
|
||||
currentMode = mode1;
|
||||
break;
|
||||
case mode2:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
addressSpace.memoryLayout.STAT |= 0x02;
|
||||
currentMode = mode2;
|
||||
break;
|
||||
case mode3:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
addressSpace.memoryLayout.STAT |= 0x03;
|
||||
currentMode = mode3;
|
||||
break;
|
||||
}
|
||||
//7th bit is unused but always set
|
||||
addressSpace.memoryLayout.STAT |= 0x80;
|
||||
switch (mode) {
|
||||
case mode0:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
currentMode = mode0;
|
||||
break;
|
||||
case mode1:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
addressSpace.memoryLayout.STAT |= 0x01;
|
||||
currentMode = mode1;
|
||||
break;
|
||||
case mode2:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
addressSpace.memoryLayout.STAT |= 0x02;
|
||||
currentMode = mode2;
|
||||
break;
|
||||
case mode3:
|
||||
addressSpace.memoryLayout.STAT &= ~0x03;
|
||||
addressSpace.memoryLayout.STAT |= 0x03;
|
||||
currentMode = mode3;
|
||||
break;
|
||||
}
|
||||
// 7th bit is unused but always set
|
||||
addressSpace.memoryLayout.STAT |= 0x80;
|
||||
}
|
||||
|
||||
void GameBoy::drawLine() {
|
||||
const uint8_t line = readOnlyAddressSpace.memoryLayout.LY;
|
||||
const uint8_t line = readOnlyAddressSpace.memoryLayout.LY;
|
||||
|
||||
// Calculate the starting index of the current scanline in the framebuffer
|
||||
const uint32_t lineStartIndex = line * RESOLUTION_X;
|
||||
// Calculate the starting index of the current scanline in the framebuffer
|
||||
const uint32_t lineStartIndex = line * RESOLUTION_X;
|
||||
|
||||
// Pointer to the current line's pixel data in the framebuffer
|
||||
uint32_t* currentLinePixels = framebuffer + lineStartIndex;
|
||||
std::fill_n(currentLinePixels, RESOLUTION_X, 0xFFFFFFFF);
|
||||
// Pointer to the current line's pixel data in the framebuffer
|
||||
uint32_t *currentLinePixels = framebuffer + lineStartIndex;
|
||||
std::fill_n(currentLinePixels, RESOLUTION_X, 0xFFFFFFFF);
|
||||
|
||||
const uint16_t backgroundMapAddr =
|
||||
LCDCBitEnabled(BG_TILE_MAP_AREA) ? 0x9C00 : 0x9800;
|
||||
const uint16_t windowMapAddr =
|
||||
LCDCBitEnabled(WINDOW_TILE_MAP_AREA) ? 0x9C00 : 0x9800;
|
||||
const uint16_t tileDataTableAddr =
|
||||
LCDCBitEnabled(BG_WINDOW_TILE_DATA_AREA) ? 0x8000 : 0x8800;
|
||||
const bool signedIndex = !LCDCBitEnabled(BG_WINDOW_TILE_DATA_AREA);
|
||||
|
||||
const uint16_t backgroundMapAddr = LCDCBitEnabled(BG_TILE_MAP_AREA) ? 0x9C00 : 0x9800;
|
||||
const uint16_t windowMapAddr = LCDCBitEnabled(WINDOW_TILE_MAP_AREA) ? 0x9C00 : 0x9800;
|
||||
const uint16_t tileDataTableAddr = LCDCBitEnabled(BG_WINDOW_TILE_DATA_AREA) ? 0x8000 : 0x8800;
|
||||
const bool signedIndex = !LCDCBitEnabled(BG_WINDOW_TILE_DATA_AREA);
|
||||
// BG
|
||||
if (LCDCBitEnabled(BG_WINDOW_ENABLE)) {
|
||||
for (int pixel = 0; pixel < RESOLUTION_X; pixel++) {
|
||||
const uint16_t xIndex =
|
||||
(pixel + readOnlyAddressSpace.memoryLayout.SCX) % 256;
|
||||
// 256 pixels in total BG width
|
||||
const uint16_t yIndex =
|
||||
(line + readOnlyAddressSpace.memoryLayout.SCY) % 256;
|
||||
// 256 pixels in total BG height
|
||||
|
||||
//BG
|
||||
if (LCDCBitEnabled(BG_WINDOW_ENABLE)) {
|
||||
for (int pixel = 0; pixel < RESOLUTION_X; pixel++) {
|
||||
const uint16_t xIndex = (pixel + readOnlyAddressSpace.memoryLayout.SCX) % 256;
|
||||
// 256 pixels in total BG width
|
||||
const uint16_t yIndex = (line + readOnlyAddressSpace.memoryLayout.SCY) % 256;
|
||||
// 256 pixels in total BG height
|
||||
const uint16_t tileUpper = (yIndex / 8) << 5;
|
||||
const uint16_t tileLower = xIndex / 8 & 0x1F;
|
||||
const uint16_t tileIndex = tileUpper + tileLower;
|
||||
|
||||
const uint16_t tileUpper = (yIndex / 8) << 5;
|
||||
const uint16_t tileLower = xIndex / 8 & 0x1F;
|
||||
const uint16_t tileIndex = tileUpper + tileLower;
|
||||
const uint16_t tileAddr = backgroundMapAddr + tileIndex;
|
||||
const int16_t tileID =
|
||||
signedIndex ? static_cast<int16_t>(readOnlyAddressSpace[tileAddr])
|
||||
: readOnlyAddressSpace[tileAddr];
|
||||
|
||||
const uint16_t tileAddr = backgroundMapAddr + tileIndex;
|
||||
const int16_t tileID = signedIndex
|
||||
? static_cast<int16_t>(readOnlyAddressSpace[tileAddr])
|
||||
: readOnlyAddressSpace[tileAddr];
|
||||
uint16_t tileDataAddr;
|
||||
if (signedIndex)
|
||||
tileDataAddr = tileDataTableAddr +
|
||||
(((tileID + 128) % 256) << 4); // For signed, wrap around
|
||||
else
|
||||
tileDataAddr = tileDataTableAddr + (tileID * 16);
|
||||
|
||||
uint16_t tileDataAddr;
|
||||
if (signedIndex)
|
||||
tileDataAddr = tileDataTableAddr + (((tileID + 128) % 256) << 4); // For signed, wrap around
|
||||
else
|
||||
tileDataAddr = tileDataTableAddr + (tileID * 16);
|
||||
const uint8_t lineOffset = yIndex % 8;
|
||||
const uint8_t tileRowData1 =
|
||||
readOnlyAddressSpace[tileDataAddr + (lineOffset * 2)];
|
||||
const uint8_t tileRowData2 =
|
||||
readOnlyAddressSpace[tileDataAddr + (lineOffset * 2) + 1];
|
||||
|
||||
// get pixel data (2 bits)
|
||||
const uint8_t colourBit = 7 - (xIndex % 8);
|
||||
const uint8_t colourNum = ((tileRowData2 >> colourBit) & 0x1) << 1 |
|
||||
((tileRowData1 >> colourBit) & 0x1);
|
||||
|
||||
const uint8_t lineOffset = yIndex % 8;
|
||||
const uint8_t tileRowData1 = readOnlyAddressSpace[tileDataAddr + (lineOffset * 2)];
|
||||
const uint8_t tileRowData2 = readOnlyAddressSpace[tileDataAddr + (lineOffset * 2) + 1];
|
||||
// Apply the BGP register for palette mapping
|
||||
const uint8_t palette =
|
||||
(readOnlyAddressSpace.memoryLayout.BGP >> (colourNum * 2)) & 0x3;
|
||||
|
||||
//get pixel data (2 bits)
|
||||
const uint8_t colourBit = 7 - (xIndex % 8);
|
||||
const uint8_t colourNum = ((tileRowData2 >> colourBit) & 0x1) << 1 | ((tileRowData1 >> colourBit) & 0x1);
|
||||
currentLinePixels[pixel] = getColourFromPalette(palette);
|
||||
}
|
||||
|
||||
// Apply the BGP register for palette mapping
|
||||
const uint8_t palette = (readOnlyAddressSpace.memoryLayout.BGP >> (colourNum * 2)) & 0x3;
|
||||
// For the window to be displayed on a scanline, the following conditions
|
||||
// must be met: WY condition was triggered: i.e. at some point in this frame
|
||||
// the value of WY was equal to LY (checked at the start of Mode 2 only) WX
|
||||
// condition was triggered: i.e. the current X coordinate being rendered + 7
|
||||
// was equal to WX Window enable bit in LCDC is set
|
||||
// Window
|
||||
const uint8_t windowY = readOnlyAddressSpace.memoryLayout.WY;
|
||||
const int16_t windowX =
|
||||
static_cast<int16_t>(readOnlyAddressSpace.memoryLayout.WX - 7);
|
||||
if (LCDCBitEnabled(WINDOW_ENABLE) && windowX >= 0 &&
|
||||
windowX < RESOLUTION_X && line >= windowY) {
|
||||
for (int pixel = windowX; pixel < RESOLUTION_X; pixel++) {
|
||||
const uint16_t yIndex = windowLineCounter;
|
||||
const uint16_t windowTileUpper = (yIndex / 8) << 5;
|
||||
|
||||
currentLinePixels[pixel] = getColourFromPalette(palette);
|
||||
}
|
||||
const uint16_t xIndex = pixel - windowX;
|
||||
const uint16_t windowTileLower = (xIndex / 8) & 0x1F;
|
||||
|
||||
// For the window to be displayed on a scanline, the following conditions must be met:
|
||||
// WY condition was triggered: i.e. at some point in this frame the value of WY was equal to LY (checked at the start of Mode 2 only)
|
||||
// WX condition was triggered: i.e. the current X coordinate being rendered + 7 was equal to WX
|
||||
// Window enable bit in LCDC is set
|
||||
//Window
|
||||
const uint8_t windowY = readOnlyAddressSpace.memoryLayout.WY;
|
||||
const int16_t windowX = static_cast<int16_t>(readOnlyAddressSpace.memoryLayout.WX - 7);
|
||||
if (LCDCBitEnabled(WINDOW_ENABLE) && windowX >= 0 && windowX < RESOLUTION_X && line >= windowY) {
|
||||
for (int pixel = windowX; pixel < RESOLUTION_X; pixel++) {
|
||||
const uint16_t yIndex = windowLineCounter;
|
||||
const uint16_t windowTileUpper = (yIndex / 8) << 5;
|
||||
const uint16_t tileIndex = windowTileUpper + windowTileLower;
|
||||
const uint16_t tileAddr = windowMapAddr + tileIndex;
|
||||
|
||||
const uint16_t xIndex = pixel - windowX;
|
||||
const uint16_t windowTileLower = (xIndex / 8) & 0x1F;
|
||||
const int16_t tileID =
|
||||
signedIndex ? static_cast<int16_t>(readOnlyAddressSpace[tileAddr])
|
||||
: readOnlyAddressSpace[tileAddr];
|
||||
|
||||
const uint16_t tileIndex = windowTileUpper + windowTileLower;
|
||||
const uint16_t tileAddr = windowMapAddr + tileIndex;
|
||||
uint16_t tileDataAddr;
|
||||
if (signedIndex)
|
||||
tileDataAddr = tileDataTableAddr + (((tileID + 128) % 256) *
|
||||
16); // For signed, wrap around
|
||||
else
|
||||
tileDataAddr = tileDataTableAddr + (tileID * 16);
|
||||
|
||||
const int16_t tileID = signedIndex
|
||||
? static_cast<int16_t>(readOnlyAddressSpace[tileAddr])
|
||||
: readOnlyAddressSpace[tileAddr];
|
||||
const uint8_t lineOffset = yIndex % 8;
|
||||
const uint8_t tileRowData1 =
|
||||
readOnlyAddressSpace[tileDataAddr + (lineOffset * 2)];
|
||||
const uint8_t tileRowData2 =
|
||||
readOnlyAddressSpace[tileDataAddr + (lineOffset * 2) + 1];
|
||||
|
||||
uint16_t tileDataAddr;
|
||||
if (signedIndex)
|
||||
tileDataAddr = tileDataTableAddr + (((tileID + 128) % 256) * 16); // For signed, wrap around
|
||||
else
|
||||
tileDataAddr = tileDataTableAddr + (tileID * 16);
|
||||
// get pixel data (2 bits)
|
||||
const uint8_t colourBit = 7 - (xIndex % 8);
|
||||
const uint8_t colourNum = ((tileRowData2 >> colourBit) & 0x1) << 1 |
|
||||
((tileRowData1 >> colourBit) & 0x1);
|
||||
|
||||
// Apply the BGP register for palette mapping
|
||||
const uint8_t palette =
|
||||
(readOnlyAddressSpace.memoryLayout.BGP >> (colourNum * 2)) & 0x3;
|
||||
|
||||
const uint8_t lineOffset = yIndex % 8;
|
||||
const uint8_t tileRowData1 = readOnlyAddressSpace[tileDataAddr + (lineOffset * 2)];
|
||||
const uint8_t tileRowData2 = readOnlyAddressSpace[tileDataAddr + (lineOffset * 2) + 1];
|
||||
currentLinePixels[pixel] = getColourFromPalette(palette);
|
||||
}
|
||||
windowLineCounter += 1;
|
||||
}
|
||||
}
|
||||
// oam/sprites
|
||||
if (LCDCBitEnabled(OBJ_ENABLE)) {
|
||||
uint32_t oamPixels[RESOLUTION_X];
|
||||
std::fill_n(oamPixels, RESOLUTION_X, 0);
|
||||
|
||||
//get pixel data (2 bits)
|
||||
const uint8_t colourBit = 7 - (xIndex % 8);
|
||||
const uint8_t colourNum = ((tileRowData2 >> colourBit) & 0x1) << 1 | ((tileRowData1 >> colourBit) &
|
||||
0x1);
|
||||
constexpr Word oamAddrStart = 0xFE00;
|
||||
const int spriteHeight = LCDCBitEnabled(OBJ_SIZE) ? 16 : 8;
|
||||
|
||||
// Apply the BGP register for palette mapping
|
||||
const uint8_t palette = (readOnlyAddressSpace.memoryLayout.BGP >> (colourNum * 2)) & 0x3;
|
||||
int objects[10] = {0};
|
||||
int found = 0;
|
||||
|
||||
currentLinePixels[pixel] = getColourFromPalette(palette);
|
||||
}
|
||||
windowLineCounter += 1;
|
||||
}
|
||||
}
|
||||
// oam/sprites
|
||||
if (LCDCBitEnabled(OBJ_ENABLE)) {
|
||||
uint32_t oamPixels[RESOLUTION_X];
|
||||
std::fill_n(oamPixels, RESOLUTION_X, 0);
|
||||
// oam hold 40 objects
|
||||
// find first 10
|
||||
for (int i = 0; i < 40 && found < 10; i++) {
|
||||
const int offset = i * 4;
|
||||
const int yPos = readOnlyAddressSpace[oamAddrStart + offset] - 16;
|
||||
|
||||
constexpr
|
||||
Word oamAddrStart = 0xFE00;
|
||||
const int spriteHeight = LCDCBitEnabled(OBJ_SIZE) ? 16 : 8;
|
||||
if (line >= yPos && line <= yPos + spriteHeight - 1) {
|
||||
objects[found] = oamAddrStart + offset;
|
||||
found += 1;
|
||||
}
|
||||
}
|
||||
|
||||
int objects[10] = {0};
|
||||
int found = 0;
|
||||
// sort by xPos (lower has higher priority when rendering) and then earlier
|
||||
// objects
|
||||
for (int i = 0; i < found; i++) {
|
||||
for (int j = 0; j < found - i - 1; j++) {
|
||||
const int xPos1 = readOnlyAddressSpace[objects[j] + 1];
|
||||
const int xPos2 = readOnlyAddressSpace[objects[j + 1] + 1];
|
||||
|
||||
//oam hold 40 objects
|
||||
//find first 10
|
||||
for (int i = 0; i < 40 && found < 10; i++) {
|
||||
const int offset = i * 4;
|
||||
const int yPos = readOnlyAddressSpace[oamAddrStart + offset] - 16;
|
||||
if (xPos1 > xPos2) {
|
||||
const int tmp = objects[j];
|
||||
objects[j] = objects[j + 1];
|
||||
objects[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (line >= yPos && line <= yPos + spriteHeight - 1) {
|
||||
objects[found] = oamAddrStart + offset;
|
||||
found += 1;
|
||||
}
|
||||
}
|
||||
for (int objectIndex = found - 1; objectIndex >= 0; objectIndex--) {
|
||||
const int yPos = readOnlyAddressSpace[objects[objectIndex]] - 16;
|
||||
const int xPos = readOnlyAddressSpace[objects[objectIndex] + 1] - 8;
|
||||
const int tileIndex = readOnlyAddressSpace[objects[objectIndex] + 2];
|
||||
const Byte attributes = readOnlyAddressSpace[objects[objectIndex] + 3];
|
||||
|
||||
//sort by xPos (lower has higher priority when rendering) and then earlier objects
|
||||
for (int i = 0; i < found; i++) {
|
||||
for (int j = 0; j < found - i - 1; j++) {
|
||||
const int xPos1 = readOnlyAddressSpace[objects[j] + 1];
|
||||
const int xPos2 = readOnlyAddressSpace[objects[j + 1] + 1];
|
||||
const bool priority = oamBitEnabled(attributes, PRIORITY);
|
||||
const bool yFlip = oamBitEnabled(attributes, Y_FLIP);
|
||||
const bool xFlip = oamBitEnabled(attributes, X_FLIP);
|
||||
// get obp0 or obj1
|
||||
const Byte objPalette = oamBitEnabled(attributes, OBJ_PALETTE)
|
||||
? addressSpace.memoryLayout.OBP1
|
||||
: addressSpace.memoryLayout.OBP0;
|
||||
|
||||
if (xPos1 > xPos2) {
|
||||
const int tmp = objects[j];
|
||||
objects[j] = objects[j + 1];
|
||||
objects[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int pixel = xPos; pixel < RESOLUTION_X && pixel < xPos + 8;
|
||||
pixel++) {
|
||||
constexpr Word objectTileAddr = 0x8000;
|
||||
if (pixel < 0)
|
||||
continue;
|
||||
|
||||
const uint32_t colour = currentLinePixels[pixel];
|
||||
const Byte BGP = readOnlyAddressSpace.memoryLayout.BGP;
|
||||
if (priority && (colour == getColourFromPalette((BGP >> 2) & 0x3) ||
|
||||
colour == getColourFromPalette((BGP >> 4) & 0x3) ||
|
||||
colour == getColourFromPalette((BGP >> 6) & 0x3))) {
|
||||
oamPixels[pixel] = colour;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int objectIndex = found - 1; objectIndex >= 0; objectIndex--) {
|
||||
const int yPos = readOnlyAddressSpace[objects[objectIndex]] - 16;
|
||||
const int xPos = readOnlyAddressSpace[objects[objectIndex] + 1] - 8;
|
||||
const int tileIndex = readOnlyAddressSpace[objects[objectIndex] + 2];
|
||||
const Byte attributes = readOnlyAddressSpace[objects[objectIndex] + 3];
|
||||
Byte objectX = pixel - xPos;
|
||||
Byte objectY = line - yPos;
|
||||
if (xFlip)
|
||||
objectX = 7 - objectX;
|
||||
if (yFlip)
|
||||
objectY = (spriteHeight - 1) - objectY;
|
||||
|
||||
const bool priority = oamBitEnabled(attributes, PRIORITY);
|
||||
const bool yFlip = oamBitEnabled(attributes, Y_FLIP);
|
||||
const bool xFlip = oamBitEnabled(attributes, X_FLIP);
|
||||
//get obp0 or obj1
|
||||
const Byte objPalette = oamBitEnabled(attributes, OBJ_PALETTE)
|
||||
? addressSpace.memoryLayout.OBP1
|
||||
: addressSpace.memoryLayout.OBP0;
|
||||
const Word objTileDataAddr =
|
||||
spriteHeight == 8 ? objectTileAddr + (tileIndex * 16)
|
||||
: objectTileAddr + ((tileIndex & 0xFE) * 16);
|
||||
|
||||
for (int pixel = xPos; pixel < RESOLUTION_X && pixel < xPos + 8; pixel++) {
|
||||
constexpr
|
||||
Word objectTileAddr = 0x8000;
|
||||
if (pixel < 0)
|
||||
continue;
|
||||
const Byte tileRow = objectY * 2;
|
||||
const Byte tileRowData1 =
|
||||
readOnlyAddressSpace[objTileDataAddr + tileRow];
|
||||
const Byte tileRowData2 =
|
||||
readOnlyAddressSpace[objTileDataAddr + tileRow + 1];
|
||||
|
||||
const uint32_t colour = currentLinePixels[pixel];
|
||||
const Byte BGP = readOnlyAddressSpace.memoryLayout.BGP;
|
||||
if (priority && (colour == getColourFromPalette((BGP >> 2) & 0x3) ||
|
||||
colour == getColourFromPalette((BGP >> 4) & 0x3) ||
|
||||
colour == getColourFromPalette((BGP >> 6) & 0x3)
|
||||
)) {
|
||||
oamPixels[pixel] = colour;
|
||||
continue;
|
||||
}
|
||||
const int bit = 7 - objectX;
|
||||
const int colorIndex =
|
||||
((tileRowData2 >> bit) & 1) << 1 | ((tileRowData1 >> bit) & 1);
|
||||
|
||||
Byte objectX = pixel - xPos;
|
||||
Byte objectY = line - yPos;
|
||||
if (xFlip)
|
||||
objectX = 7 - objectX;
|
||||
if (yFlip)
|
||||
objectY = (spriteHeight - 1) - objectY;
|
||||
// 0 is always transparent
|
||||
if (colorIndex != 0) {
|
||||
const uint8_t paletteColor = (objPalette >> (colorIndex * 2)) & 0x3;
|
||||
const uint32_t finalColor = getColourFromPalette(paletteColor);
|
||||
oamPixels[pixel] = finalColor;
|
||||
} else if (oamPixels[pixel] == 0) {
|
||||
oamPixels[pixel] = currentLinePixels[pixel];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Word objTileDataAddr = spriteHeight == 8
|
||||
? objectTileAddr + (tileIndex * 16)
|
||||
: objectTileAddr + ((tileIndex & 0xFE) * 16);
|
||||
|
||||
const Byte tileRow = objectY * 2;
|
||||
const Byte tileRowData1 = readOnlyAddressSpace[objTileDataAddr + tileRow];
|
||||
const Byte tileRowData2 = readOnlyAddressSpace[objTileDataAddr + tileRow + 1];
|
||||
|
||||
const int bit = 7 - objectX;
|
||||
const int colorIndex = ((tileRowData2 >> bit) & 1) << 1 | ((tileRowData1 >> bit) & 1);
|
||||
|
||||
// 0 is always transparent
|
||||
if (colorIndex != 0) {
|
||||
const uint8_t paletteColor = (objPalette >> (colorIndex * 2)) & 0x3;
|
||||
const uint32_t finalColor = getColourFromPalette(paletteColor);
|
||||
oamPixels[pixel] = finalColor;
|
||||
}
|
||||
else if (oamPixels[pixel] == 0) {
|
||||
oamPixels[pixel] = currentLinePixels[pixel];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//help ensure correct interaction with "BG OVER OBJ" flag
|
||||
for (int i = 0; i < RESOLUTION_X; i++) {
|
||||
if (oamPixels[i] != currentLinePixels[i] && oamPixels[i] != 0)
|
||||
currentLinePixels[i] = oamPixels[i];
|
||||
}
|
||||
}
|
||||
// help ensure correct interaction with "BG OVER OBJ" flag
|
||||
for (int i = 0; i < RESOLUTION_X; i++) {
|
||||
if (oamPixels[i] != currentLinePixels[i] && oamPixels[i] != 0)
|
||||
currentLinePixels[i] = oamPixels[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameBoy::SDL2setup() {
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
screen = SDL_CreateWindow("GameBoy++",
|
||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
RESOLUTION_X, RESOLUTION_Y,
|
||||
0);
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
screen =
|
||||
SDL_CreateWindow("GameBoy++", SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, RESOLUTION_X, RESOLUTION_Y, 0);
|
||||
|
||||
// Create an SDL renderer to draw on the window
|
||||
renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
|
||||
// Create an SDL renderer to draw on the window
|
||||
renderer = SDL_CreateRenderer(
|
||||
screen, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
|
||||
|
||||
// Create an SDL texture to hold the framebuffer data
|
||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
|
||||
SDL_TEXTUREACCESS_STREAMING, RESOLUTION_X, RESOLUTION_Y);
|
||||
// Create an SDL texture to hold the framebuffer data
|
||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
|
||||
SDL_TEXTUREACCESS_STREAMING, RESOLUTION_X,
|
||||
RESOLUTION_Y);
|
||||
}
|
||||
|
||||
void GameBoy::SDL2destroy() const {
|
||||
SDL_DestroyTexture(texture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(screen);
|
||||
SDL_Quit();
|
||||
SDL_DestroyTexture(texture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(screen);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void GameBoy::SDL2present() {
|
||||
SDL_UpdateTexture(texture, nullptr, framebuffer, RESOLUTION_X * sizeof(uint32_t));
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
|
||||
SDL_UpdateTexture(texture, nullptr, framebuffer,
|
||||
RESOLUTION_X * sizeof(uint32_t));
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
|
||||
|
||||
frameTime = SDL_GetTicks() - frameStart;
|
||||
|
||||
frameTime = SDL_GetTicks() - frameStart;
|
||||
if (frameDelay > frameTime) {
|
||||
SDL_Delay(frameDelay - frameTime);
|
||||
}
|
||||
|
||||
if (frameDelay > frameTime) {
|
||||
SDL_Delay(frameDelay - frameTime);
|
||||
}
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
frameStart = SDL_GetTicks();
|
||||
rendered = true;
|
||||
SDL_RenderPresent(renderer);
|
||||
frameStart = SDL_GetTicks();
|
||||
rendered = true;
|
||||
}
|
||||
|
||||
@@ -1,43 +1,39 @@
|
||||
#ifndef TESTING_HPP
|
||||
#define TESTING_HPP
|
||||
|
||||
#include "defines.hpp"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include "defines.hpp"
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
struct GameboyTestState {
|
||||
Word PC;
|
||||
Word SP;
|
||||
Byte A;
|
||||
Byte F;
|
||||
Byte B;
|
||||
Byte C;
|
||||
Byte D;
|
||||
Byte E;
|
||||
Byte H;
|
||||
Byte L;
|
||||
//Byte IME;
|
||||
//Byte IE;
|
||||
std::vector<std::tuple<Word, Byte>> RAM;
|
||||
Word PC;
|
||||
Word SP;
|
||||
Byte A;
|
||||
Byte F;
|
||||
Byte B;
|
||||
Byte C;
|
||||
Byte D;
|
||||
Byte E;
|
||||
Byte H;
|
||||
Byte L;
|
||||
// Byte IME;
|
||||
// Byte IE;
|
||||
std::vector<std::tuple<Word, Byte>> RAM;
|
||||
};
|
||||
|
||||
inline bool operator==(const GameboyTestState& lhs, const GameboyTestState& rhs) {
|
||||
for (int i = 0; i < lhs.RAM.size(); i++) {
|
||||
if (std::get<1>(lhs.RAM[i]) != std::get<1>(rhs.RAM[i]))
|
||||
return false;
|
||||
}
|
||||
return (lhs.A == rhs.A &&
|
||||
lhs.F == rhs.F &&
|
||||
lhs.B == rhs.B &&
|
||||
lhs.C == rhs.C &&
|
||||
lhs.D == rhs.D &&
|
||||
lhs.E == rhs.E &&
|
||||
lhs.H == rhs.H &&
|
||||
lhs.L == rhs.L);
|
||||
inline bool operator==(const GameboyTestState &lhs,
|
||||
const GameboyTestState &rhs) {
|
||||
for (int i = 0; i < lhs.RAM.size(); i++) {
|
||||
if (std::get<1>(lhs.RAM[i]) != std::get<1>(rhs.RAM[i]))
|
||||
return false;
|
||||
}
|
||||
return (lhs.A == rhs.A && lhs.F == rhs.F && lhs.B == rhs.B &&
|
||||
lhs.C == rhs.C && lhs.D == rhs.D && lhs.E == rhs.E &&
|
||||
lhs.H == rhs.H && lhs.L == rhs.L);
|
||||
}
|
||||
|
||||
#endif //TESTING_HPP
|
||||
#endif // TESTING_HPP
|
||||
|
||||
@@ -1,43 +1,45 @@
|
||||
#include "gameboy.hpp"
|
||||
|
||||
//handles most of the behavoir as described here: https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff04--div-divider-register
|
||||
// handles most of the behavoir as described here:
|
||||
// https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff04--div-divider-register
|
||||
void GameBoy::timingHandler() {
|
||||
//can't do this as we use cycles for PPU timing but this is what should happen
|
||||
//addressSpace.memoryLayout.DIV = ((cycles / 4) >> 6) & 0xFF;
|
||||
// can't do this as we use cycles for PPU timing but this is what should
|
||||
// happen addressSpace.memoryLayout.DIV = ((cycles / 4) >> 6) & 0xFF;
|
||||
|
||||
if (cycles - lastDivUpdate >= DIVIDER_REGISTER_FREQ) {
|
||||
const uint8_t increments = (cycles - lastDivUpdate) / DIVIDER_REGISTER_FREQ;
|
||||
addressSpace.memoryLayout.DIV += increments;
|
||||
lastDivUpdate += increments * DIVIDER_REGISTER_FREQ;
|
||||
}
|
||||
if (cycles - lastDivUpdate >= DIVIDER_REGISTER_FREQ) {
|
||||
const uint8_t increments = (cycles - lastDivUpdate) / DIVIDER_REGISTER_FREQ;
|
||||
addressSpace.memoryLayout.DIV += increments;
|
||||
lastDivUpdate += increments * DIVIDER_REGISTER_FREQ;
|
||||
}
|
||||
|
||||
//if enabled
|
||||
uint64_t TIMAFrequency = 0;
|
||||
if (addressSpace.memoryLayout.TAC & 0x04) {
|
||||
switch (addressSpace.memoryLayout.TAC & 0x03) {
|
||||
case 0:
|
||||
TIMAFrequency = 1024;
|
||||
break;
|
||||
case 1:
|
||||
TIMAFrequency = 16;
|
||||
break;
|
||||
case 2:
|
||||
TIMAFrequency = 64;
|
||||
break;
|
||||
case 3:
|
||||
TIMAFrequency = 256;
|
||||
break;
|
||||
}
|
||||
//if TIMA overflowed and prevTMA != current TMA, use prevTMA (ie use prevTMA regardless)
|
||||
const int increments = (cycles - lastTIMAUpdate) / TIMAFrequency;
|
||||
if (cycles - lastTIMAUpdate >= TIMAFrequency) {
|
||||
if (static_cast<int>(addressSpace.memoryLayout.TIMA) + increments > 255) {
|
||||
addressSpace.memoryLayout.TIMA = prevTMA + ((addressSpace.memoryLayout.TIMA + increments) % 256);
|
||||
setInterrupt(TIMER_INTERRUPT);
|
||||
}
|
||||
else
|
||||
addressSpace.memoryLayout.TIMA += increments;
|
||||
lastTIMAUpdate += increments * TIMAFrequency;
|
||||
}
|
||||
}
|
||||
// if enabled
|
||||
uint64_t TIMAFrequency = 0;
|
||||
if (addressSpace.memoryLayout.TAC & 0x04) {
|
||||
switch (addressSpace.memoryLayout.TAC & 0x03) {
|
||||
case 0:
|
||||
TIMAFrequency = 1024;
|
||||
break;
|
||||
case 1:
|
||||
TIMAFrequency = 16;
|
||||
break;
|
||||
case 2:
|
||||
TIMAFrequency = 64;
|
||||
break;
|
||||
case 3:
|
||||
TIMAFrequency = 256;
|
||||
break;
|
||||
}
|
||||
// if TIMA overflowed and prevTMA != current TMA, use prevTMA (ie use
|
||||
// prevTMA regardless)
|
||||
const int increments = (cycles - lastTIMAUpdate) / TIMAFrequency;
|
||||
if (cycles - lastTIMAUpdate >= TIMAFrequency) {
|
||||
if (static_cast<int>(addressSpace.memoryLayout.TIMA) + increments > 255) {
|
||||
addressSpace.memoryLayout.TIMA =
|
||||
prevTMA + ((addressSpace.memoryLayout.TIMA + increments) % 256);
|
||||
setInterrupt(TIMER_INTERRUPT);
|
||||
} else
|
||||
addressSpace.memoryLayout.TIMA += increments;
|
||||
lastTIMAUpdate += increments * TIMAFrequency;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user