-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
85 lines (74 loc) · 1.69 KB
/
memory.h
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
#ifndef MEMORY_H
#define MEMORY_H
#include <array>
#include <istream>
#include <string.h>
template <std::size_t size>
class Memory
{
static_assert((size % 2)==0, "size must be a multiple of 2");
uint8_t mem8[size];
public:
void load(unsigned int address, std::size_t n, std::istream& src)
{
src.read(reinterpret_cast<char *>(&mem8[address]), n);
}
void load(unsigned int address, std::size_t n, uint8_t *src)
{
memcpy(&mem8[address], src, n);
}
void set8(unsigned int address, uint8_t value)
{
if (address >=0 && address < size)
{
mem8[address] = value;
}
else
{
fprintf(stderr, "set8 address = %u\n", address);
abort();
}
}
uint8_t get8(unsigned int address)
{
if (address >=0 && address < size)
return mem8[address];
fprintf(stderr, "get8 address = %u\n", address);
abort();
}
void set16(unsigned int address, uint16_t value)
{
if (address >=0 && address+1 < size)
{
uint8_t upper = (value & 0xff00) >> 8;
uint8_t lower = (value & 0x00ff);
mem8[address] = upper;
mem8[address+1] = lower;
}
else
{
fprintf(stderr, "set16 address = %u\n", address);
abort();
}
}
uint16_t get16(unsigned int address)
{
if (address >=0 && address+1 < size)
{
uint8_t upper = mem8[address];
uint8_t lower = mem8[address+1];
uint16_t value = (upper << 8) | lower;
return value;
}
fprintf(stderr, "get16 address = %u\n", address);
abort();
}
void print(unsigned int start, unsigned int range=10)
{
for (unsigned int i=start; i<start+range; i++)
{
printf("0x%04X: 0x%02X\n", i, mem8[i]);
}
}
};
#endif