added comments

This commit is contained in:
Robert 2021-07-12 05:26:38 +02:00
parent 54d6fc8c1e
commit ad4eafa882
9 changed files with 122 additions and 71 deletions

View file

@ -7,6 +7,13 @@
#include "lcd.hpp"
#include "rom.hpp"
// Why is this typedef? Lol
// This was originally a C project believe it or not. I thought getting
// this tiny performance increase was worth the trouble, but I guess that
// eventually I ran into a problem that couldn't be easily solved in less
// than 5 lines of C code. At least I thought so, but anyways, I decided
// to rewrite the emulator in C++, that's why all this code looks so
// fucked up and messy.
typedef union
{
BYTE b;
@ -18,6 +25,9 @@ typedef union
} w;
} TimerControl;
// JoypadReg = The register in the gameboy
// Joypad = A struct to keep the physical keyboard input
typedef union
{
BYTE b;
@ -38,32 +48,37 @@ struct Joypad
bool a, b, up, down, left, right, start, select;
};
// The Bus class contains all the stuff that I didn't know where else to put
class Bus
{
public:
Bus();
~Bus();
// Used to conn
void AttachCPU(CPU& cpu);
void AttachLCD(LCD& lcd);
void InsertROM(ROM& rom);
bool Tick();
bool Execute();
bool Frame();
bool Tick(); // Execute ONE machine cycle (why would you do that lol)
bool Execute(); // Execute ONE CPU instruction (better but still why)
bool Frame(); // Execute CPU instructions until we rendered one full frame (there we go)
BYTE Read(WORD addr);
void Write(WORD addr, BYTE val);
BYTE Fetch(WORD addr);
BYTE Read(WORD addr); // Read from the bus
void Write(WORD addr, BYTE val); // Write to the bus
BYTE Fetch(WORD addr); // This is literally the same as Read(). Like literally. the. exact. same.
// But I use it a lot in the CPU class so I'm too lazy/afraid to remove it
private:
BYTE& GetReference(WORD addr);
BYTE& GetReference(WORD addr); // Leftovers of a really really really bad idea, but again it's used in a few places so I'm too scared to remove it
public:
// Connected devices
ROM* rom;
CPU* cpu;
LCD* lcd;
// These are I/O registers :)
BYTE invalid;
BYTE div;
BYTE tima;
@ -75,7 +90,6 @@ public:
Joypad joypad;
// std::array<BYTE, 0x2000> vram;
std::array<BYTE, 0x2000> wram;
std::array<BYTE, 0x80> hram;
std::array<BYTE, 0x80> hram; // <-- This should be in the CPU class but who cares
};

View file

@ -6,6 +6,8 @@
class Bus;
// Structure to represent a register (register = 16 bits, but split into 2 "sub registers" of 8 bits).
// I also store the names of the regs for debug purposes
struct Register
{
union
@ -20,6 +22,7 @@ struct Register
char name[3];
};
// Convenience structure for the Interrupts
typedef union {
BYTE b;
struct
@ -46,6 +49,7 @@ typedef union
} f;
} StatusFlag;
// Read about opcode decoding, the link is in the cpu.cpp file
typedef union
{
BYTE b;
@ -66,6 +70,8 @@ typedef union
} pq;
} Opcode;
// Contains everything related to the CPU
class CPU
{
public:
@ -100,11 +106,12 @@ public:
bool stopped;
bool halted;
bool justHaltedWithDI;
bool justHaltedWithDI; // I don't even know
private:
void WriteToRegister(BYTE reg, BYTE val);
void WriteToRegister(BYTE reg, BYTE val); // The cycles, the god DAMN CPU CYCLES
BYTE ReadFromRegister(BYTE reg);
void ALU(BYTE operation, BYTE operand);
void CBPrefixed();
void ALU(BYTE operation, BYTE operand); // Handle any ALU related instructions
void CBPrefixed(); // Handle all CB prefixed instructions
};

View file

@ -5,6 +5,7 @@
class Bus;
// bunch of registers or smthn
typedef union
{
BYTE b;
@ -81,14 +82,13 @@ typedef union
} b;
} OAMEntry;
// The screen. With emphasis on ree
class LCD
{
public:
void Setup();
void Tick();
BYTE& GetReferenceToAddress(WORD addr, bool& handled);
bool Read(WORD addr, BYTE& val);
bool Write(WORD addr, BYTE val);

View file

@ -2,6 +2,7 @@
#include "util.hpp"
// The memory bank controller (MBC) needs to map addresses targeted at rom, to get the appropriate data from the ROM
class IMBC
{
public:
@ -11,7 +12,7 @@ public:
virtual ~IMBC() {}
virtual bool GetMappedRead(WORD address, DWORD& mappedAddr) = 0;
virtual bool GetMappedRead(WORD address, DWORD& mappedAddr) = 0; // Convert CPU address to ROM internal address
virtual bool GetMappedWrite(WORD address, BYTE val, DWORD& mappedAddr) = 0;
protected:

View file

@ -8,17 +8,7 @@
class Bus;
struct MemoryBankController
{
BYTE w;
struct
{
BYTE ROMBankNumber : 5;
BYTE RAMBankNumber : 2;
BYTE Mode : 1;
} b;
};
// Cartridge
class ROM
{
public: