-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.cpp
72 lines (60 loc) · 2.17 KB
/
memory.cpp
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
#include <algorithm>
#include <iostream>
#include <iomanip>
#include "memory.h"
namespace Chip8 {
const uint16_t kFontSetLocation = 0x050;
const uint8_t kFontSet[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
};
Memory::Memory() {
std::fill_n(memory, sizeof(memory), 0);
std::copy(kFontSet, kFontSet + sizeof(kFontSet), &memory[kFontSetLocation]);
}
uint8_t Memory::getByte(int location) {
//std::cout << "accessing 0x" << std::hex << location << std::endl;
return memory[location];
}
void Memory::putByte(int location, uint8_t value) {
memory[location] = value;
}
uint16_t Memory::getFontLocation() {
return kFontSetLocation;
}
void Memory::dump() {
for (int i = 0; i < (kMemorySize / 16); i++) {
// Print the header
if (i%16 == 0) {
std::cout << " ";
for (int j=0; j<16; j++) {
std::cout << j << " ";
}
std::cout << std::endl;
}
// Print out 256 byte block in 16 16-byte rows
std::cout << "0x" << std::setfill('0') << std::setw(3) << std::hex << i*16;
// Print raw memory for this row
for (int j = 0; j < 16; j++)
std::cout << " " << std::hex << std::setw(2) << int(memory[(i*16) + j]);
std::cout << std::endl;
if (i%16 == 15)
std::cout << std::endl;
}
std::cout << std::endl;
}
} // namespace Chip8