-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.cpp
More file actions
33 lines (27 loc) · 701 Bytes
/
Memory.cpp
File metadata and controls
33 lines (27 loc) · 701 Bytes
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
#include <cstdio>
#include <cstring>
#include <iostream>
#include "Memory.h"
uint8_t Memory::read(uint16_t pos) {
if (pos >= 0 && pos < 4096)
return memory[pos];
}
void Memory::write(uint16_t pos, uint8_t data) {
if (pos >= 0 && pos < 4096)
memory[pos] = data;
}
void Memory::readFile(char *name) {
FILE *fp = fopen(name, "r");
if (fp == NULL) {
std::cout << "The file couldn't be found!" << std::endl;
return;
}
uint16_t size;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
auto *buffer = new uint8_t[size];
fread(buffer, 1, size, fp);
fclose(fp);
std::memcpy(&memory[512], buffer, size);
}