GameBoy Emulator 1
Game Boy emulator core and tooling
Loading...
Searching...
No Matches
ProcessingUnit.hpp
1#pragma once
2
3#include "common.hpp"
4
5class MMU;
6
7class ProcessingUnit{
8private:
9/* -------------------------------- */
10/* 16-bit Registers */
11/* -------------------------------- */
12 // Hi Lo Name/Function
13 u8 A{}, F{}; // Accumulator & Flags
14 u8 B{}, C{}; // BC
15 u8 D{}, E{}; // DE
16 u8 H{}, L{}; // HL
17/* -------------------------------- */
18 u16 SP{}; // Stack Pointer
19 u16 PC{}; // Program Counter
20/* -------------------------------- */
21 bool IME{}; // Interrupt Master Enable
22 bool halt{}; // HALT State { When CPU paused, waiting for interrupt}
23
24public:
25 ProcessingUnit();
26 void reset();
27 void printStatus() const; // print task
28 [[nodiscard]] u16 get_pc() const;
29 void set_pc(u16 value) { PC = value; }
30 [[nodiscard]] u16 get_sp() const;
31 void set_sp(u16 value) { SP = value; }
32 // Getting Full 16bit Register Values
33 [[nodiscard]] u16 get_bc() const { return (B << 8) | C; }
34 [[nodiscard]] u16 get_de() const { return (D << 8) | E; }
35 [[nodiscard]] u16 get_hl() const { return (H << 8) | L; }
36 [[nodiscard]] u16 get_af() const { return (A << 8) | F; }
37
38 // Flag Helpers (Bit positions: Z=7, N=6, H=5, C=4)
39 [[nodiscard]] bool get_flag_z() const { return (F >> 7) & 1; }
40 [[nodiscard]] bool get_flag_n() const { return (F >> 6) & 1; }
41 [[nodiscard]] bool get_flag_h() const { return (F >> 5) & 1; }
42 [[nodiscard]] bool get_flag_c() const { return (F >> 4) & 1; }
43
44 int step(MMU &mmu);
45
46 void check_interrupts(MMU &mmu);
47 void execute_interrupt(MMU &mmu, u16 address, int bit);
48
49 int last_instr_cycles = 0;
50 u16 last_pc = 0;
51
52 void setHalt(bool newValue);
53
54 [[nodiscard]] bool isHalt() const;
55
56 void setIME(bool newValue);
57 [[nodiscard]] bool getIME() const;
58/* -------------------------------- */
59/* Register Identifiers */
60/* -------------------------------- */
61 enum class Register {
62 B, C, D, E, H, L, A, F
63 };
64/* -------------------------------- */
65/* Flags */
66/* -------------------------------- */
67 enum class Flag {
68 Z = 7,
69 N = 6,
70 H = 5,
71 C = 4
72 };
73/* -------------------------------- */
74/* Register Access */
75/* -------------------------------- */
76 u8& reg(Register r);
77 [[nodiscard]] const u8& reg(Register r) const;
78
79 u16 inc_pc();
80/* -------------------------------- */
81/* FLAGS Access */
82/* -------------------------------- */
83 void clearFlags() {
84 F = 0;
85 }
86 void normalizeFlags()
87 {
88 F = 0xF0;
89 }
90 void setFlag(Flag flag, bool value)
91 {
92 const u8 mask = 1 << static_cast<int>(flag);
93 if (value) F |= mask; // set bit
94 else F &= ~mask; // clear bit
95 }
96
97 bool stop{};
98
99 void setStop(const bool value) { stop = value; }
100 [[nodiscard]] bool isStop() const { return stop; }
101};
Definition mmu.hpp:12