-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.cpp
206 lines (190 loc) · 6.42 KB
/
memory.cpp
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
/* ****************************************************************
RISC-V Instruction Set Simulator
Class members for memory
**************************************************************** */
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <cstdio>
#include "memory.h"
using namespace std;
// Constructor
memory::memory(bool verbose) {
// TODO: ...
this->verbose = verbose;
}
// Read a doubleword of data from a doubleword-aligned address.
// If the address is not a multiple of 8, it is rounded down to a multiple of 8.
uint64_t memory::read_doubleword (uint64_t address) {
// TODO: ...
// find the block that the address belong to
int block = address / 1024;
// align address to double word
address -= (address % 8);
if (verbose)
{
cout << "Reading double word: address = " << setw(16) << setfill('0') << hex << address;
cout << ", block = " << block << endl;
}
// check if block exist
if (mem.find(block) != mem.end())
{
// block exists, proceed reading
if (mem[block]->find(address) != mem[block]->end())
{
// memory location exist
return (*mem[block])[address];
}
}
else{
// block doesn't exist, initialise the block
mem.insert({block,new unordered_map<uint64_t,uint64_t>(block_size)});
}
// return zero by default
return 0;
}
// Write a doubleword of data to a doubleword-aligned address.
// If the address is not a multiple of 8, it is rounded down to a multiple of 8.
// The mask contains 1s for bytes to be updated and 0s for bytes that are to be unchanged.
void memory::write_doubleword (uint64_t address, uint64_t data, uint64_t mask) {
// TODO: ...
// find the block that the address belong to
int block = address / 1024;
// align address to double word
address -= (address % 8);
if (verbose)
{
cout << "Writing double word: address = " << setw(16) << setfill('0') << hex << address;
cout << ", block = " << block;
cout << ", data = " << setw(16) << setfill('0') << hex << data;
cout << ", mask = " << setw(16) << setfill('0') << hex << mask << endl;
}
// check if block exist
if (mem.find(block) != mem.end())
{
// block exists, proceed writing
if (mem[block]->find(address) != mem[block]->end())
{
// memory location exist
data &= mask;
(*mem[block])[address] &= (~mask);
(*mem[block])[address] |= data;
}
else
{
// memory location empty
mem[block]->insert({address,(data & mask)});
}
}
else{
// block doesn't exist, initialise the block and write to memory
mem.insert({block,new unordered_map<uint64_t,uint64_t>(block_size)});
mem[block]->insert({address,(data & mask)});
}
}
// Load a hex image file and provide the start address for execution from the file in start_address.
// Return true if the file was read without error, or false otherwise.
bool memory::load_file(string file_name, uint64_t &start_address) {
ifstream input_file(file_name);
string input;
unsigned int line_count = 0;
unsigned int byte_count = 0;
char record_start;
char byte_string[3];
char halfword_string[5];
unsigned int record_length;
unsigned int record_address;
unsigned int record_type;
unsigned int record_data;
unsigned int record_checksum;
bool end_of_file_record = false;
uint64_t load_address;
uint64_t load_data;
uint64_t load_mask;
uint64_t load_base_address = 0x0000000000000000ULL;
start_address = 0x0000000000000000ULL;
if (input_file.is_open()) {
while (true) {
line_count++;
input_file >> record_start;
if (record_start != ':') {
cout << "Input line " << dec << line_count << " does not start with colon character" << endl;
return false;
}
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_length);
input_file.get(halfword_string, 5);
sscanf(halfword_string, "%x", &record_address);
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_type);
switch (record_type) {
case 0x00: // Data record
for (unsigned int i = 0; i < record_length; i++) {
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_data);
load_address = (load_base_address | (uint64_t)(record_address)) + i;
load_data = (uint64_t)(record_data) << ((load_address % 8) * 8);
load_mask = 0x00000000000000ffULL << ((load_address % 8) * 8);
write_doubleword(load_address & 0xfffffffffffffff8ULL, load_data, load_mask);
byte_count++;
}
break;
case 0x01: // End of file
end_of_file_record = true;
break;
case 0x02: // Extended segment address (set bits 19:4 of load base address)
load_base_address = 0x0000000000000000ULL;
for (unsigned int i = 0; i < record_length; i++) {
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_data);
load_base_address = (load_base_address << 8) | (record_data << 4);
}
break;
case 0x03: // Start segment address (ignored)
for (unsigned int i = 0; i < record_length; i++) {
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_data);
}
break;
case 0x04: // Extended linear address (set upper halfword of load base address)
load_base_address = 0x0000000000000000ULL;
for (unsigned int i = 0; i < record_length; i++) {
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_data);
load_base_address = (load_base_address << 8) | (record_data << 16);
}
break;
case 0x05: // Start linear address (set execution start address)
start_address = 0x0000000000000000ULL;
for (unsigned int i = 0; i < record_length; i++) {
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_data);
start_address = (start_address << 8) | record_data;
}
break;
}
input_file.get(byte_string, 3);
sscanf(byte_string, "%x", &record_checksum);
input_file.ignore();
if (end_of_file_record)
break;
}
input_file.close();
cout << dec << byte_count << " bytes loaded, start address = "
<< setw(16) << setfill('0') << hex << start_address << endl;
return true;
}
else {
cout << "Failed to open file" << endl;
return false;
}
}
memory::~memory()
{
// clean memory
for (const pair<int,unordered_map<uint64_t,uint64_t>*>& n : mem)
{
delete n.second;
}
}