-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemulator.hpp
More file actions
72 lines (61 loc) · 1.68 KB
/
emulator.hpp
File metadata and controls
72 lines (61 loc) · 1.68 KB
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
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <string>
constexpr size_t FRAMEBUFFER_SIZE = 64*32;
constexpr uint8_t FONTSET[80] =
{
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
class Chip8 {
public:
Chip8(const std::string& fileName)
: pc(0x200), I(0), sp(0), delayTimer(0), soundTimer(0), drawFlag(false) {
memory.fill(0);
framebuffer.fill(0);
V.fill(0);
stack.fill(0);
keys.fill(false);
this->load_fontmap();
this->load_rom(fileName);
}
void tick();
bool should_draw() const {return this->drawFlag;}
void update_timers();
// private:
void load_fontmap();
void load_rom(const std::string& fileName);
void handle_instruction(const uint16_t instruction);
void blitTexture(uint8_t n, uint16_t x, uint16_t y);
// Memory
std::array<uint8_t, FRAMEBUFFER_SIZE> framebuffer;
std::array<uint8_t, 4096> memory;
std::array<uint8_t, 16> V;
std::array<bool, 16> keys;
// Stack
std::array<uint16_t, 16> stack;
uint16_t sp;
// Not directly addressable registers
uint16_t I;
uint16_t pc;
// Timers
uint8_t delayTimer;
uint8_t soundTimer;
bool drawFlag;
};