From 3fca00cb3404d6dfdb864ddd21919a2965e21fe4 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 3 Jan 2023 02:03:05 +0530 Subject: [PATCH] Make executeNextInstruction() self reliant --- src/cpu.cpp | 19 ++++++++++++++++--- src/cpu.h | 19 +++++++++++++++---- src/gameBoy.cpp | 7 +++++++ src/gameBoy.h | 4 ++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/cpu.cpp b/src/cpu.cpp index d87338a..3a842de 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -26,12 +26,25 @@ CPU::CPU() // Does nothing int CPU::NOP() { + reg_PC.dat += 1; printf("NOP"); return 4; } -int CPU::executeNextInstruction(int opcode) +// LD BC, u16 +// Loads a 16 bit immediate value into BC +int CPU::LD_BC_u16() { - method_function func = method_pointer[opcode]; - return (this->*func)(); + // Load the next 2 bytes into BC + + reg_PC.dat += 3; + printf("LD BC, u16"); + return 12; +} + +int CPU::executeNextInstruction() +{ + // Get the opcode + Byte opcode = *mMap[reg_PC.dat]; + return (this->*method_pointer[opcode])(); } \ No newline at end of file diff --git a/src/cpu.h b/src/cpu.h index 645f0ff..15c7211 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -1,5 +1,6 @@ #pragma once #include "types.h" +#include "mmap.h" // CPU Register // Pulled from https://gbdev.io/pandocs/CPU_Registers_and_Flags.html @@ -49,12 +50,15 @@ class CPU FLAG_ZERO_z = 7 }; + // Memory Map + MemoryMap *mMap; + // ISA // Pulled from https://izik1.github.io/gbops/index.html typedef int (CPU::*method_function)(); - method_function method_pointer[0x01] = { - &CPU::NOP - // &CPU::LD_BC_u16, + method_function method_pointer[0x02] = { + &CPU::NOP, + &CPU::LD_BC_u16 // &CPU::LD_BC_A, // &CPU::INC_BC, // &CPU::INC_B, @@ -314,10 +318,17 @@ class CPU // NOP int NOP(); + // LD nn, nn + int LD_BC_u16(); + public: const int clockSpeed = 4194304; // 4.194304 MHz CPU const int clockSpeedPerFrame = 70224; // 4194304 / 59.73fps CPU(); - int executeNextInstruction(int opcode); + + // set the memory map + void setMemory(MemoryMap* memory) { mMap = memory; } + + int executeNextInstruction(); }; \ No newline at end of file diff --git a/src/gameBoy.cpp b/src/gameBoy.cpp index 43bf999..6d48521 100644 --- a/src/gameBoy.cpp +++ b/src/gameBoy.cpp @@ -6,6 +6,13 @@ GBE::GBE() { // Initialize the CPU gbe_cpu = new CPU(); + + // Initialize the MemoryMap + gbe_mMap = new MemoryMap(); + + // Unify the CPU and MemoryMap + gbe_cpu->setMemory(gbe_mMap); + update(); } diff --git a/src/gameBoy.h b/src/gameBoy.h index 0f6dd85..3a78295 100644 --- a/src/gameBoy.h +++ b/src/gameBoy.h @@ -1,6 +1,7 @@ #pragma once #include "types.h" #include "cpu.h" +#include "memoryMap.h" // GBE stands for GameBoyEmulator @@ -32,6 +33,9 @@ class GBE // Pointer to CPU CPU* gbe_cpu; + // Pointer to the MemoryMap + MemoryMap* gbe_mMap; + // Update function of the GBE // Will be called every frame // GB has 59.73 frames per second