-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented CPU, with few instructions.
- Loading branch information
1 parent
caa91df
commit fee4c1a
Showing
17 changed files
with
536 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "CPU.h" | ||
#include <iomanip> | ||
#include <iostream> | ||
|
||
void CPU::execute_CP_n(){ | ||
MemoryUnitObj->registers.pc++; | ||
uint8_t n = MemoryUnitObj->read_memory(MemoryUnitObj->registers.pc); | ||
uint8_t result = MemoryUnitObj->registers.a - n; | ||
|
||
MemoryUnitObj->set_flag(FLAG_Z, MemoryUnitObj->registers.c == 0); | ||
MemoryUnitObj->set_flag(FLAG_N, 1); | ||
MemoryUnitObj->checknset_halfcarry_flag(result); | ||
MemoryUnitObj->set_flag(FLAG_C, MemoryUnitObj->registers.a < n); | ||
MemoryUnitObj->registers.pc++; | ||
|
||
std::cout << "cp a," << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(n) << "\n"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include "CPU.h" | ||
#include "iostream" | ||
#include <iomanip> | ||
#include <cassert> | ||
|
||
void CPU::executeNOP() | ||
{ | ||
std::cout << "nop\n"; | ||
MemoryUnitObj->registers.pc++; | ||
} | ||
|
||
void CPU::execute_XOR_A() { | ||
// XOR A | ||
MemoryUnitObj->registers.a ^= MemoryUnitObj->registers.a; | ||
|
||
// Update flags | ||
MemoryUnitObj->set_flag(FLAG_Z, MemoryUnitObj->registers.a == 0); | ||
MemoryUnitObj->set_flag(FLAG_N, 0); | ||
MemoryUnitObj->set_flag(FLAG_H, 0); | ||
MemoryUnitObj->set_flag(FLAG_C, 0); | ||
|
||
//Update PC | ||
MemoryUnitObj->registers.pc++; | ||
|
||
std::cout << "XOR A\n"; | ||
} | ||
|
||
void CPU::execute_HL_nn() { | ||
MemoryUnitObj->registers.pc++; | ||
uint16_t nn = MemoryUnitObj->read_memory_16_le(MemoryUnitObj->registers.pc); | ||
MemoryUnitObj->registers.hl = nn; | ||
MemoryUnitObj->registers.pc += 2; | ||
std::cout << "ld hl " << std::setw(4) << std::setfill('0') << std::hex << nn << "\n"; | ||
|
||
} | ||
|
||
CPU::CPU(MemoryUnit *memoryUnitPointer) | ||
{ | ||
MemoryUnitObj = memoryUnitPointer; | ||
initialize_memory(); | ||
initialize_registers(); | ||
MemoryUnitObj->load_rom("..\\roms\\Tetris.gb"); | ||
} | ||
|
||
void CPU::initialize_memory() | ||
{ | ||
MemoryUnitObj->load_vram("..\\roms\\VRAM.dump"); | ||
MemoryUnitObj->reset_eram(); | ||
MemoryUnitObj->reset_notusuable(); | ||
MemoryUnitObj->load_ioregisters("..\\roms\\IO.dump"); | ||
MemoryUnitObj->reset_ieregister(); | ||
} | ||
|
||
void CPU::initialize_registers() | ||
{ | ||
MemoryUnitObj->registers.af = 0x01B0; // Example: Set flags (Z=0, N=1, H=1, C=0) | ||
MemoryUnitObj->registers.bc = 0x0013; // Example: Set BC register pair | ||
MemoryUnitObj->registers.de = 0x00D8; // Example: Set DE register pair | ||
MemoryUnitObj->registers.hl = 0x014D; // Example: Set HL register pair | ||
MemoryUnitObj->registers.sp = 0xFFFE; // Example: Set Stack Pointer | ||
MemoryUnitObj->registers.pc = 0x0100; // Example: Set Program Counter | ||
} | ||
|
||
void CPU::debug_registers() { | ||
std::cout << "AF = 0x" << std::setw(4) << std::setfill('0') << std::hex << MemoryUnitObj->registers.af << " "; | ||
std::cout << "BC = 0x" << std::setw(4) << std::setfill('0') << std::hex << MemoryUnitObj->registers.bc << " "; | ||
std::cout << "DE = 0x" << std::setw(4) << std::setfill('0') << std::hex << MemoryUnitObj->registers.de << " "; | ||
std::cout << "HL = 0x" << std::setw(4) << std::setfill('0') << std::hex << MemoryUnitObj->registers.hl << " "; | ||
std::cout << "SP = 0x" << std::setw(4) << std::setfill('0') << std::hex << MemoryUnitObj->registers.sp << " "; | ||
std::cout << "PC = 0x" << std::setw(4) << std::setfill('0') << std::hex << MemoryUnitObj->registers.pc << " "; | ||
std::cout << "INS = 0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(MemoryUnitObj->read_memory(MemoryUnitObj->registers.pc)) << "\n"; | ||
} | ||
|
||
void CPU::execute_unimplemented() { | ||
// Print an error message | ||
std::cout<<"Error: Instruction not implemented\n"; | ||
assert(0); // This will cause the program to terminate with an error message | ||
} | ||
|
||
void CPU::start_execution() | ||
{ | ||
while (true) { | ||
debug_registers(); | ||
uint8_t opcode = MemoryUnitObj->read_memory(MemoryUnitObj->registers.pc); | ||
switch (opcode) { | ||
case 0x00: | ||
executeNOP(); | ||
break; | ||
case 0x05: | ||
execute_DEC_B(); | ||
break; | ||
case 0x06: | ||
execute_LD_B_n(); | ||
break; | ||
case 0x0D: | ||
execute_DEC_C(); | ||
break; | ||
case 0x0E: | ||
execute_LD_C_n(); | ||
break; | ||
case 0x20: | ||
execute_JR_NZ_n(); | ||
break; | ||
case 0x21: | ||
execute_HL_nn(); | ||
break; | ||
case 0x32: | ||
execute_LD_HL_dec_A(); | ||
break; | ||
case 0x36: | ||
execute_LD_HL_n(); | ||
break; | ||
case 0x3E: | ||
execute_LD_A_n(); | ||
break; | ||
case 0xAF: | ||
execute_XOR_A(); | ||
break; | ||
case 0xC3: | ||
execute_JP_nn(); | ||
break; | ||
case 0xE0: | ||
execute_LDH_n_A(); | ||
break; | ||
case 0xEA: | ||
execute_LD_nn_A(); | ||
break; | ||
case 0xF0: | ||
execute_LDH_A_n(); | ||
break; | ||
case 0xF3: | ||
execute_DI(); | ||
break; | ||
case 0xFE: | ||
execute_CP_n(); | ||
break; | ||
default: | ||
// debug_registers(); | ||
execute_unimplemented(); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
#include "MemoryUnit.h" | ||
|
||
class CPU { | ||
private: | ||
void executeNOP(); | ||
void execute_JP_nn(); | ||
void execute_XOR_A(); | ||
void execute_HL_nn(); | ||
void execute_LD_A_n(); | ||
void execute_LD_nn_A(); | ||
void execute_LDH_n_A(); | ||
void execute_LD_HL_n(); | ||
void execute_LDH_A_n(); | ||
void execute_LD_B_n(); | ||
void execute_LD_C_n(); | ||
void execute_LD_HL_dec_A(); | ||
void execute_DEC_B(); | ||
void execute_DEC_C(); | ||
void execute_JR_NZ_n(); | ||
void execute_DI(); | ||
void execute_CP_n(); | ||
public: | ||
MemoryUnit *MemoryUnitObj; | ||
CPU(MemoryUnit*); | ||
void initialize_memory(); | ||
void initialize_registers(); | ||
void start_execution(); | ||
void debug_registers(); | ||
|
||
void execute_unimplemented(); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "CPU.h" | ||
#include <iomanip> | ||
#include <iostream> | ||
|
||
void CPU::execute_DEC_B() { | ||
MemoryUnitObj->registers.pc++; | ||
MemoryUnitObj->registers.b--; | ||
|
||
MemoryUnitObj->set_flag(FLAG_Z, MemoryUnitObj->registers.b == 0); | ||
MemoryUnitObj->set_flag(FLAG_N, 1); | ||
MemoryUnitObj->checknset_halfcarry_flag(MemoryUnitObj->registers.b); | ||
std::cout << "dec b\n"; | ||
} | ||
|
||
void CPU::execute_DEC_C() { | ||
MemoryUnitObj->registers.pc++; | ||
MemoryUnitObj->registers.c--; | ||
|
||
MemoryUnitObj->set_flag(FLAG_Z, MemoryUnitObj->registers.c == 0); | ||
MemoryUnitObj->set_flag(FLAG_N, 1); | ||
MemoryUnitObj->checknset_halfcarry_flag(MemoryUnitObj->registers.c); | ||
std::cout << "dec c\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.