Skip to content

Commit

Permalink
Make executeNextInstruction() self reliant
Browse files Browse the repository at this point in the history
  • Loading branch information
r41k0u committed Jan 21, 2023
1 parent 7c2e4aa commit 3fca00c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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])();
}
19 changes: 15 additions & 4 deletions src/cpu.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
};
7 changes: 7 additions & 0 deletions src/gameBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions src/gameBoy.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "types.h"
#include "cpu.h"
#include "memoryMap.h"

// GBE stands for GameBoyEmulator

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3fca00c

Please sign in to comment.