-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMemory.cpp
More file actions
150 lines (139 loc) · 2.92 KB
/
Memory.cpp
File metadata and controls
150 lines (139 loc) · 2.92 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <iomanip>
#include "Memory.h"
// btw baddy stands for byte address
Memory::Memory(){
reset();
}
void Memory::reset(void){
for(int i = 0; i < MEMORY_SIZE; i++){
mem[i] = 0;
}
lastUsed = 0;
memError = false;
}
/*
* Trace
* Returns the last utilized memory location.
*/
uint16_t Memory::trace(){
return lastUsed;
}
/*
* getByte
* Returns a single byte in memory, given a 16-bit byte address.
*/
uint8_t Memory::getByte(uint16_t baddy)
{
lastUsed = baddy;
if (baddy >= 0 && baddy < MEMORY_SIZE){
return mem[baddy];
}
else{
memError = true;
return 0;
}
}
/*
* getWord
* returns a 16-bit value from memory, given a 16-bit byte address.
*/
uint16_t Memory::getWord(uint16_t baddy){
lastUsed = baddy;
if(baddy >= 0 && baddy < MEMORY_SIZE - 1){
return (mem[baddy] << 8) + mem[baddy + 1];
}
else{
memError = true;
return 0;
}
}
/*
* getTile
* Returns a pointer to the start of a tile (16 bytes), given a 16-bit byte address.
*/
uint8_t * Memory::getTile(uint16_t baddy)
{
lastUsed = baddy;
if (VRAM <= baddy && (baddy + 15) <= VRAM_END){
return &(mem[baddy]);
}
else
{
memError = true;
return 0;
}
}
uint16_t Memory::getTileRow(uint16_t baddy){
lastUsed = baddy;
if(VRAM <= baddy && (baddy + 15) <= VRAM_END){
uint16_t final = 0;
final += mem[baddy];
final += (mem[baddy+1]) << 8;
return final;
}
else{
memError = true;
return 0;
}
}
/*
* putByte
* Puts a single byte into memory, given a 16-bit byte address.
*/
void Memory::putByte(uint16_t baddy, uint8_t val)
{
lastUsed = baddy;
if (baddy < MEMORY_SIZE){
mem[baddy] = val;
}
else{
memError = true;
}
}
/*
* putTile
* Puts a tile (16 bytes) into memory, given a 16-bit byte address.
*/
void Memory::putTile(uint16_t baddy, uint8_t val[16]){
lastUsed = baddy;
if (VRAM <= baddy && (baddy + 15) <= VRAM_END){
for (int i = 0; i < 16; i++) mem[baddy + i] = val[i];
}
else{
memError = true;
}
}
/*
* dumpROM
* Dumps the contents in memory from range 0x100 to 0x3FFF (CART_HEADER to ROM_END).
* Formats into 16-byte chunks with the starting address prefixed.
*/
void Memory::dump(int start, int end){
// uint8_t prevLine[16];
uint8_t currLine[16];
// initializing arrays to 0
for (int i = 0; i < 16; i++){
// prevLine[i] = 0;
currLine[i] = 0;
}
for (int i = start; i < end; i+=16){
for (int j = 0; j < 16; j++){
currLine[j] = getByte(i + j);
}
// printing out the correct address and formatting
std::cout << std::endl << std::setw(4) << std::setfill('0') << std::hex << i << ": ";
// printing out the contents of the address in 16 bytes per line
for (int j = 0; j < 16; j++){
std::cout << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(currLine[j]) << " ";
}
}
std::cout << std::endl;
}
/*
* isMemError
* Returns status of memory error flag, so we can tell if our abort was due to memory accesses.
*/
bool Memory::isMemError(void){
return memError;
}