-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.hpp
106 lines (92 loc) · 2.33 KB
/
cpu.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef CPU_HPP
#define CPU_HPP
#include <array>
#include <vector>
#include <SFML/Window.hpp>
struct Registers
{
std::array<uint8_t, 16> V;
int16_t I;
int8_t delay;
int8_t sound;
int16_t pc;
int8_t sp;
};
struct Opcode
{
union {
struct {
union {
struct {
uint8_t n4 : 4;
uint8_t n3 : 4;
};
uint8_t n34;
};
uint8_t n2 : 4;
};
struct {
uint16_t n234 : 12;
uint8_t n1 : 4;
};
uint16_t n1234;
};
};
class Chip8Ram;
class Chip8Display;
class Chip8CPU
{
public:
Chip8CPU(Chip8Ram& ram, Chip8Display& display);
void execute(struct Opcode opcode);
bool is_blocked();
struct Opcode get_next_instruction();
void update_timers();
private:
struct Registers regs;
Chip8Ram& ram;
Chip8Display& display;
uint8_t reg_to_set;
bool blocked;
std::vector<uint16_t> stack;
sf::Clock clock;
uint32_t ms_since_last_tick;
std::mt19937 rng;
void unknown_opcode(struct Opcode opcode);
void n1_is_0(struct Opcode opcode);
void n1_is_1(struct Opcode opcode);
void n1_is_2(struct Opcode opcode);
void n1_is_3(struct Opcode opcode);
void n1_is_4(struct Opcode opcode);
void n1_is_5(struct Opcode opcode);
void n1_is_6(struct Opcode opcode);
void n1_is_7(struct Opcode opcode);
void n1_is_8(struct Opcode opcode);
void n1_is_9(struct Opcode opcode);
void n1_is_A(struct Opcode opcode);
void n1_is_B(struct Opcode opcode);
void n1_is_C(struct Opcode opcode);
void n1_is_D(struct Opcode opcode);
void n1_is_E(struct Opcode opcode);
void n1_is_F(struct Opcode opcode);
/* Not sure this is the right place for this... but it will do for now */
std::array<sf::Keyboard::Key, 16> allowed_keys = {{
sf::Keyboard::Num0,
sf::Keyboard::Num1,
sf::Keyboard::Num2,
sf::Keyboard::Num3,
sf::Keyboard::Num4,
sf::Keyboard::Num5,
sf::Keyboard::Num6,
sf::Keyboard::Num7,
sf::Keyboard::Num8,
sf::Keyboard::Num9,
sf::Keyboard::A,
sf::Keyboard::B,
sf::Keyboard::C,
sf::Keyboard::D,
sf::Keyboard::E,
sf::Keyboard::F,
}};
};
#endif