yabgbe/include/bus.hpp

95 lines
2.2 KiB
C++
Raw Normal View History

2021-07-12 02:54:24 +00:00
#pragma once
#include <array>
#include "util.hpp"
#include "cpu.hpp"
#include "lcd.hpp"
#include "rom.hpp"
2021-07-12 03:26:38 +00:00
// 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.
2021-07-12 02:54:24 +00:00
typedef union
{
BYTE b;
struct
{
BYTE select : 2;
BYTE enable : 1;
BYTE padding : 5;
} w;
} TimerControl;
2021-07-12 03:26:38 +00:00
// JoypadReg = The register in the gameboy
// Joypad = A struct to keep the physical keyboard input
2021-07-12 02:54:24 +00:00
typedef union
{
BYTE b;
struct
{
BYTE rightA : 1;
BYTE leftB : 1;
BYTE upSelect : 1;
BYTE downStart : 1;
BYTE selectDirKeys : 1;
BYTE selectButtonKeys : 1;
BYTE unused : 2;
} w;
} JoypadReg;
struct Joypad
{
bool a, b, up, down, left, right, start, select;
};
2021-07-12 03:26:38 +00:00
// The Bus class contains all the stuff that I didn't know where else to put
2021-07-12 02:54:24 +00:00
class Bus
{
public:
Bus();
~Bus();
2021-07-12 03:26:38 +00:00
// Used to conn
2021-07-12 02:54:24 +00:00
void AttachCPU(CPU& cpu);
void AttachLCD(LCD& lcd);
void InsertROM(ROM& rom);
2021-07-12 03:26:38 +00:00
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)
2021-07-12 02:54:24 +00:00
2021-07-12 03:26:38 +00:00
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
2021-07-12 02:54:24 +00:00
private:
2021-07-12 03:26:38 +00:00
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
2021-07-12 02:54:24 +00:00
public:
2021-07-12 03:26:38 +00:00
// Connected devices
2021-07-12 02:54:24 +00:00
ROM* rom;
CPU* cpu;
LCD* lcd;
2021-07-12 03:26:38 +00:00
// These are I/O registers :)
2021-07-12 02:54:24 +00:00
BYTE invalid;
BYTE div;
BYTE tima;
BYTE tma;
BYTE dmg_rom;
JoypadReg joypadReg;
TimerControl tac;
size_t internalCounter = 0;
Joypad joypad;
std::array<BYTE, 0x2000> wram;
2021-07-12 03:26:38 +00:00
std::array<BYTE, 0x80> hram; // <-- This should be in the CPU class but who cares
2021-07-12 02:54:24 +00:00
};