-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathram.cpp
More file actions
48 lines (41 loc) · 1.07 KB
/
ram.cpp
File metadata and controls
48 lines (41 loc) · 1.07 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
#include <iostream>
#include <fstream>
#include "ram.hpp"
#include "log.hpp"
Chip8Ram::Chip8Ram()
{
/* Init the ram by zeroing out all the memory */
LOG(INFO) << "Start up, clearing memory";
for(int8_t byte : ram) {
byte = 0;
}
}
int8_t& Chip8Ram::operator[](std::size_t i)
{
return ram[i];
}
const int8_t& Chip8Ram::operator[](std::size_t i) const
{
return ram[i];
}
uint16_t Chip8Ram::read_instruction(uint16_t addr)
{
uint16_t most = ram[addr];
uint16_t least = 0x00FF & ram[addr + 1];
uint16_t result = (most << 8) | least;
return result;
}
void Chip8Ram::write_instruction(uint16_t addr, uint16_t instr)
{
uint16_t* ptr = (uint16_t*)&ram[addr];
*ptr = instr;
}
void Chip8Ram::load_ram(const std::string& filename)
{
LOG(INFO) << "Loading " << filename;
std::ifstream file(filename, std::ios::in | std::ios::binary);
file.read((char*)&ram[0x200], ram_size - 0x200);
LOG(INFO) << "ram[0]=0x" << std::hex << (uint16_t)ram[0];
LOG(INFO) << "ram[0x200]=0x" << std::hex << (uint16_t)ram[0x201];
file.close();
}