forked from mdbGalvan/DAA_P01
/
RAM.cpp
327 lines (263 loc) · 12.8 KB
/
RAM.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
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
#include "RAM.h"
//========================================================================================
// DEVUELVE EL ÍNDICE DEL VECTOR CORRESPONDIENTE DE LA MATRIZ
//========================================================================================
index RAM::position(index i, index j)
{
if ((i<0) || (i>get_m()) || (j<0) || (j>get_n())) // Verificamos que está dentro de los límites, para indexar en (1,1), i<1, j< 1, ....
{
cerr << "Error en los índices de la matriz." << endl;
return 0;
}
return i*get_n() + j; // (i - 1)*get_n() + (j - 1) si lo queremos indexar desde (1,1) en vez (0,0)
}
//========================================================================================
// DEVUELVE EL STRING PERO ELIMINÁNDOLE TODOS LOS ESPACIOS EN BLANCO
//========================================================================================
string RAM::Trim(string::const_iterator i, string::const_iterator f)
{
string::const_iterator it;
string buff;
for (it = i; it != f; ++it) // Recorre todo el string
if (*it != ' ') buff += *it; // Sólo almacena en buff un caracter que no sea espacio en blanco
return buff;
}
//========================================================================================
// DEVUELVE EL STRING PERO ELIMINÁNDOLE LOS ESPACIOS DEL LADO IZQUIERDO
//========================================================================================
string RAM::LTrim(string::const_iterator i, string::const_iterator f)
{
string::const_iterator it;
string buff;
while (*i == ' ') i++; // Recorre los espacios en blanco hasta encontrar un caracter
for (it = i; it != f; ++it) buff += *it; // Almacena el resto del string
return buff;
}
//========================================================================================
// DEVUELVE EL STRING QUE SE ENCUENTRA EN SEGUNDA POSICIÓN
//========================================================================================
string RAM::TrimSecond(string::const_iterator i, string::const_iterator f)
{
string::const_iterator it;
string buff;
while (*i == ' ') i++; // Recorre los espacios en blanco hasta encontrar un caracter
while (*i != ' ') i++; // Recorre hasta que se encuentre el primer espacio
while (*i == ' ') i++; // Recorre los espacios en blanco hasta encontrar un caracter
for (it = i; it != f; ++it) // Recorre hasta el final
if (*it != ' ') buff += *it; // Si no tiene espacio en blanco lo almacena en buff
return buff;
}
//========================================================================================
// DEVUELVE EL STRING QUE SE ENCUENTRA EN PRIMERA POSICIÓN
//========================================================================================
string RAM::TrimFirst(string::const_iterator i, string::const_iterator f)
{
string::const_iterator it;
string buff;
while (*i == ' ') i++; // Recorre los espacios en blanco hasta encontrar un caracter
while (*i != ' ') { // Recorre hasta que se encuentre el primer espacio
buff += *i; // Almacena en buff la palabra encontrada
i++;
}
return buff;
}
//========================================================================================
// DEVUELVE EL STRING CODIFICADO DEL OPCODE
//========================================================================================
string RAM::codOpcode(string str) {
string out; // Devuelve el string con su respectivo código
if (str == "LOAD") out = "1";
else if (str == "STORE") out = "2";
else if (str == "ADD") out = "3";
else if (str == "SUB") out = "4";
else if (str == "MULT") out = "5";
else if (str == "DIV") out = "6";
else if (str == "READ") out = "7";
else if (str == "WRITE") out = "8";
else if (str == "JUMP") out = "9";
else if (str == "JGTZ") out = "10";
else if (str == "JZERO") out = "11";
else if (str == "HALT") out = "12";
return out;
}
//========================================================================================
// DEVUELVE EL STRING DESCODIFICADO DEL OPCODE
//========================================================================================
string RAM::desOpcode(string str) {
string out; // Devuelve el string con su respectivo OPCODE
if (str == "1") out = "LOAD";
else if (str == "2") out = "STORE";
else if (str == "3") out = "ADD";
else if (str == "4") out = "SUB";
else if (str == "5") out = "MULT";
else if (str == "6") out = "DIV";
else if (str == "7") out = "READ";
else if (str == "8") out = "WRITE";
else if (str == "9") out = "JUMP";
else if (str == "10") out = "JGTZ";
else if (str == "11") out = "JZERO";
else if (str == "12") out = "HALT";
return out;
}
//========================================================================================
// CONSTRUCTOR POR DEFECTO
// El nº de columnas será 2 ó 3, 1ª almacena comando, 2ª operando o etiqueta, 3ª comentario
//========================================================================================
RAM::RAM():
m_(0),
n_(2)
{
}
//========================================================================================
// CONSTRUCTOR QUE ESTABLECE EL Nº DE FILAS Y COLUMNAS
//========================================================================================
RAM::RAM(index m, index n) :
m_(m),
n_(n)
{
}
//========================================================================================
// OBTENER LA DIMENSIÓN DE LA FILA
//========================================================================================
vector<string> RAM::get_I()
{
return I_;
}
//========================================================================================
// OBTENER LA DIMENSIÓN DE LA FILA
//========================================================================================
index RAM::get_m()
{
return m_;
}
//========================================================================================
// OBTENER LA DIMENSIÓN DE LA COLUMNA
//========================================================================================
index RAM::get_n()
{
return n_;
}
//========================================================================================
// OBTENER EL VALOR DE LA POSICIÓN i,j DE LA MATRIZ
//========================================================================================
string RAM::get_item(index i, index j)
{
return I_[position(i, j)];
}
//========================================================================================
// ESTABLECE LA DIMENSIÓN DE LA FILA
//========================================================================================
void RAM::set_m(index m)
{
m_ = m;
}
//========================================================================================
// ESTABLECE LA DIMENSIÓN DE LA COLUMNA
//========================================================================================
void RAM::set_n(index n)
{
n_ = n;
}
//========================================================================================
// ESTABLECER EL VALOR DE LA POSICIÓN i,j DE LA MATRIZ
//========================================================================================
void RAM::set_item(index i, index j, string item)
{
I_[position(i, j)] = item;
}
//========================================================================================
// LECTURA DEL FICHERO DE UNA MATRIZ CUADRADA
// ---------------------------------------------------------------------------------------
// Aunque lo dejé preparado para guardar los comentarios, en principio no se almacenan
// ya que no se pedía. Cuando se profundice en la práctica sabré si lo necesitaré o no.
// Para ello, lo que tengo pensado es hacer un enum de entrada, y dependiendo la opción
// que soliciten (WITHTAG, WITHCOMMTAG, WITHOUT) se habilitará una zona. Y con ello, la
// n será 2, 3 ó 4 depediendo las columnas a guardar.
//========================================================================================
istream& RAM::readFile(istream& file)
{
vector<string> vTag;
I_.clear();
set_n(2); // En esta opción sólo se almacena el opcode y su operando
string line, tag, comm, instruc, oper; // Cada línea, etiqueta, comentario, opcode, operando/etiqueta
size_t indexC, indexT; // Índice de Comentario, Etiqueta y Et Fin
int nLine = 1; // Nº de líneas con código sin comentario. Útil para saber donde van las etiquetas
while (getline(file, line)) {
// ****** ETIQUETA
indexT = line.find(":"); // Índice que guarda el lugar donde termina la etiqueta
if (indexT < 1000) { // Si existe etiqueta (el índice no puede ser el npos).
tag = line.substr(0, indexT); // Substring que contiene desde el inicio hasta el :, almacena el nombre de la etiqueta
tag = Trim(tag.begin(), tag.end()); // Eliminamos los espacios en blanco
vTag.push_back(to_string(nLine)); // Se añade a un vector el nº de línea donde aparece
vTag.push_back(tag); // Se añade el nombre de la etiqueta
indexT++; // Se añade uno al índice de la etiqueta para no coger el caracter : en las instrucciones
}
else {
indexT = 0; // Sino se encuentra etiqueta coloco el índice en 0, para iniciar desde ahí la búsqueda de la instrucción
tag = ""; // Si me hiciera almacenar más adelante el nombre de la etiqueta, saber que aquí no tenía
}
// ****** COMENTARIO
indexC = line.find(";"); // Almacena el índice donde empieza el comentario
if (indexC < 1000) { // Si existe comentario su índice es distinto del npos (un nº muy grande)
comm = line.substr(indexC + 1); // Almacena el comentario, es un substring desde ; hasta el final (se le añade +1 para eliminar ;
}
else {
indexC = line.size(); // Sino se encuentra comentario es hasta el final de la línea la instrucción
comm = ""; // Sino existe comentario es ""
}
// ****** INSTRUCCIONES = OPCODE + OPERANDO/ETIQUETA/NADA
if (indexT != indexC) { // Cuando existe código
instruc = line.substr(indexT, indexC - indexT); // Substring que contiene la instrucción y el operando/tag juntos
instruc.append(" "); // Añado un espacio al final para no exista fallo con el último comando
instruc = TrimFirst(instruc.begin(), instruc.end()); // Captura la primera palabra
instruc = codOpcode(instruc); // Codifico el opcode
if (instruc != "12") { // 12 = HALT, entra salvo que sea 12, que no tiene ni tag ni operando
oper = line.substr(indexT, indexC - indexT); // Substring que contiene la instrucción y el operando/tag juntos
//oper.append(" ");
oper = TrimSecond(oper.begin(), oper.end()); // Se toma la segunda palabra que corresponde con el operando/etiqueta
}
else
oper = ""; // Halt no tiene nada
I_.push_back(instruc); // Se añade el opcode a la estructura
I_.push_back(oper); // Se añade el operando/etiqueta a la estructura
nLine++; // Se incrementa el nº de líneas con código, para identificar la etiqueta
}
}
set_m(nLine - 1); // Al querer sólo contar las líneas con código, y estar el tag primero que instrucc
// se me hace necesario incrementarlo dentro de éste, por lo que, al final me sobra uno
for (int i = 0; i < vTag.size() / 2; i++) { // Se recorre el vector que almacena las etiquetas y su posición
for (int j = 0; j < get_m(); j++) { // Se recorre el vector que almacena el opcode y su operando/etiqueta
if (vTag[2 * i + 1] == get_item(j, 1)) // Si coincide el nombre de la etiqueta con el etiqueta del opcode
set_item(j, 1, vTag[2 * i]); // Se cambia por el nº de línea
}
}
return file;
}
//========================================================================================
// IMPRIME LA ESTRUCTURA, QUE SE GUARDA CODIFICADA
//========================================================================================
void RAM::printCod() {
cout << "\t OPCODE \t OPERANDO/ETIQUETA \n";
cout << " *******************************\n";
for (int i = 0; i < get_m(); i++)
cout << "L" << i + 1 << "\t " << get_item(i, 0) << "\t\t" << get_item(i, 1) << "\n";
}
//========================================================================================
// IMPRIME LA ESTRUCTURA, QUE SE GUARDA CODIFICADA
//========================================================================================
void RAM::printDesc() {
cout << "\t OPCODE \t OPERANDO/ETIQUETA \n";
cout << " *******************************\n";
for (int i = 0; i < get_m(); i++)
if ((get_item(i, 0) == "9") || (get_item(i, 0) == "10") || (get_item(i, 0) == "11"))
cout << "L" << i+1 << "\t " << desOpcode(get_item(i, 0)) << "\t\t\tL" << get_item(i, 1) << "\n";
else
cout << "L" << i + 1 << "\t " << desOpcode(get_item(i, 0)) << "\t\t\t" << get_item(i, 1) << "\n";
}
//========================================================================================
// DESTRUCTOR
//========================================================================================
RAM::~RAM()
{
I_.clear();
}