-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
356 lines (305 loc) · 9.66 KB
/
memory.cpp
File metadata and controls
356 lines (305 loc) · 9.66 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//******************************************************************
// Ryon Cook
// z1863954
// CSCI 463 Section 1
// Assignment 5
//
// I certify that this is my own work and where appropriate an extension
// of the starter code provided for the assignment
//
//******************************************************************
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <string>
#include <ctype.h>
#include <stdio.h>
#include "memory.h"
#include "hex.h"
/**
* Constructor.
* Allocate siz byes in the mem vector and initalize every bye/element to 0xa5.
*
* @param siz Size to allocate to the vector.
*
********************************************************************************/
memory::memory(uint32_t siz)
{
siz = (siz+15)&0xfffffff0;//round the length up, mod-16
mem.reserve(siz);//set vector size to siz
for(unsigned i = 0; i < siz; ++i)//loop through mem vector
{
mem.push_back(0xa5);//insert 0xa5 into end of vector
}
}
/**
* Destructor.
********************************************************************************/
memory::~memory()
{
mem.clear();
}
/**
* Return true if given address is not in simulated memory. If not in memory, print warning message.
*
* @param i Index of memory vector to be referenced.
*
* @return True if i is in simulated memory, false if not.
*
********************************************************************************/
bool memory::check_illegal(uint32_t i) const
{
try//address of data in memory
{
mem.at(i);
}
catch(...)//print error
{
cout << "WARNING: Address out of range: " << hex::to_hex0x32(i) << endl;
return true;
}
return false;
}
/**
* Return the number of bytes in simulated memory.
*
* @return uint32_t containing size of
********************************************************************************/
uint32_t memory::get_size() const
{
return mem.size();
}
/**
* Check to see if given addr is in mem by calling check_illigal(). If addr is in valid range then return value of byte of simulated memory at given address. If not, return 0.
*
* @param addr Address of memory vector to be referenced.
*
* @return Return value of byte in the simulated memory, or 0.
*
********************************************************************************/
uint8_t memory::get8(uint32_t addr) const
{
if(check_illegal(addr) == false)
{
return mem[addr];
}
else
{
return 0;
}
}
/**
* Call get8 function twice to get two bytes and then combine them in little-endian order to create a 16-bit return value.
*
* @param addr Address of memory vector to be referenced.
*
* @return 16-bit number from memory.
*
********************************************************************************/
uint16_t memory::get16(uint32_t addr) const
{
uint8_t byte1 = get8(addr);//fetch byte 1
uint8_t byte2 = get8(addr+1);//fetch byte 2
uint16_t combined = (byte2 << 8) | (byte1 &0xff);//combine numbers together
return combined;
}
/**
* Call get16() twice and combine results in little endian order.
*
* @param addr Address of memory vector to be referenced.
*
* @return 32-bit number from memory.
*
********************************************************************************/
uint32_t memory::get32(uint32_t addr) const
{
uint16_t byte1 = get16(addr);//fetch byte 1
uint16_t byte2 = get16(addr+2);//fetch byte 2
uint32_t combined = (byte2 << 16) | (byte1 &0xffff);
return combined;
}
/**
* Call get8 and return sign-extended value of the byte as a 32-bit signed integer.
*
* @param addr Address of memory vector to be referenced.
*
* @return 32-bit sign-extended number from memory.
*
********************************************************************************/
int32_t memory::get8_sx(uint32_t addr) const
{
uint8_t temp = get8(addr);//create temp to only call get8 once
uint8_t num = temp;
uint32_t num2 = (num & 0x000000ff); //copy numbers into 32-bit value
num2 >>= 7;
if(num2 == 0x00000001)//if MSB is 1, put 1's in front
{
num2 = (temp | 0xffffff00);
}
else
{
num2 = temp;
}
return num2;
}
/**
* Call get16 and return sign-extended value of the byte as a 32-bit signed integer.
*
* @param addr Address of memory vector to be referenced.
*
* @return 32-bit sign-extended number from memory.
*
********************************************************************************/
int32_t memory::get16_sx(uint32_t addr) const
{
uint16_t temp = get16(addr);//create temp to only call get16 once
uint16_t num = temp;
uint32_t num2 = (num& 0x0000ffff); //copy numbers into 32-bit value
num2 >>= 15;
if(num2 == 0x00000001)//if MSB is 1, put 1's in front
{
num2 = (temp | 0xffff0000);
}
else
{
num2 = temp;
}
return num2;
}
/**
* Call get32 and return sign-extended value of the byte as a 32-bit signed integer.
*
* @param addr Address of memory vector to be referenced.
*
* @return 32-bit sign-extended number from memory.
*
********************************************************************************/
int32_t memory::get32_sx(uint32_t addr) const
{
/*uint32_t num = get32(addr);
uint32_t num2 = (num& 0x00ffffff); //copy numbers into 32-bit value
if(num2 & 0x00000000)//if MSB is 1, put 1's in front
{
num2 = (num2 | 0xff000000);
}*/
return get32(addr);
}
/**
* Call check_illegal to verify if the addr is valid. If valid, set byte in simulated memory at that address to the given val. If not, discard data and return to caller
*
* @param addr Address of memory vector to be referenced.
*
* @param val Value to change address to.
*
********************************************************************************/
void memory::set8(uint32_t addr, uint8_t val)
{
if(check_illegal(addr) == false)//if valid
{
mem[addr] = val;//set value at addr to given val
}
}
/**
* Call set8 twice to store given val in little-endian order into simulated memory starting at address given in addr.
*
* @param addr Address of memory vector to be referenced.
*
* @param val Value to change address to.
*
********************************************************************************/
void memory::set16(uint32_t addr, uint16_t val)
{
set8(addr,val & 0x000000ff);
set8(addr+1,(val & 0x0000ff00) >> 8);
}
/**
* Call set16 twice to store given val in little-endian order into simulated memory starting at address given in addr.
*
* @param addr Address of memory vector to be referenced.
*
* @param val Value to change address to.
*
********************************************************************************/
void memory::set32(uint32_t addr, uint32_t val)
{
//split val in half, then send them
set16(addr,val & 0x0000ffff);
set16(addr+2,(val & 0xffff0000) >> 16);
}
/**
* Dump entire contents of sumulated memory in hex with corresponding ASCII characters on right
*
********************************************************************************/
void memory::dump() const
{
int header = 0;//keep track of header
int newLine = 0;//keep track of when to newline
uint8_t ch;
std::cout << std::setfill('0');
for(auto i = std::begin(mem); i != std::end(mem); i = i + 16)//use iterator to loop through vector
{
std::cout << std::setw(8) << hex::to_hex32(header) << ": ";//print out number
header = header + 16;//add 16 to header
auto a = i;//store hex characters
auto b = i;//store ascii characters
for(int k = 0; k != 16; ++k)//while not a newline
{
newLine++;//increment newLine
std::cout << hex::to_hex8(*a) << " ";//pint out hex characters
std::advance(a, 1);//advance vector by 1
if(newLine == 8)//break up dump
{
std::cout << " ";
}
}
newLine = 0;//zero out newline
std::cout << "*";
for(int j = 0; j != 16; ++j)//print ascii characters
{
ch = isprint(*b) ? *b : '.';
std::cout << ch;
std::advance(b,1);
}
std::cout << "*" << std::endl;
}
}
/**
* Open file in binary mode and read contents into simulated memory.
*
* @param fname Name of file to be read.
*
* @return True if file can be opened and read, false if not.
*
********************************************************************************/
bool memory::load_file(const std::string &fname)
{
ifstream infile(fname, std::ios::in|std::ios::binary);
uint8_t i;//index variable
if(!infile)
{
cout << "Can't open file '" << fname << "' for reading." << endl;
return false;
}
//check to see if file is too big
infile.seekg(0,std::ios::end);
size_t fileSize = infile.tellg();
if(fileSize > get_size())
{
check_illegal(get_size());
std::cerr << "Program too big." << endl;
infile.close();
return false;
}
//reopen file, because seekg
infile.close();
infile.open(fname);
infile >> std::noskipws;
//loop through file and add elements to vector
for (uint32_t addr = 0; infile >> i; ++addr)
{
mem[addr] = i;//add element to memory vector
}
infile.close();//close file
return true;
}