-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode2.cpp
More file actions
226 lines (221 loc) · 5.79 KB
/
code2.cpp
File metadata and controls
226 lines (221 loc) · 5.79 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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <cstdlib>
#include <unistd.h>
#include "code2.h"
#include "parser.h"
#define UINT16_MAX 65535
#define INT16_MAX 32767
using namespace std;
//n[izq..der]
int32_t sub(int32_t n, short izq, short der)
{
n >>= der;
return n & ((unsigned) 0xFFFFFFFF >> 31 - (izq - der));
}
void Code2::ejecutar_instruccion()
{
fetching();
char cnds[] = { 'R', 'Z', 'S', 'C', 'V' };
uint8_t opcode = sub(RI, 15, 12);
switch (opcode) {
case LD:
registros[sub(RI, 11, 8)] = mem[registros[0xD] + sub(RI, 7, 0)];
printf("LD R%X,%X\n", sub(RI, 11, 8), sub(RI, 7, 0));
break;
case ST:
mem[registros[0xD] + sub(RI, 7, 0)] = registros[sub(RI, 11, 8)];
printf("ST %X,R%X\n", sub(RI, 7, 0), sub(RI, 11, 8));
break;
case LLO:
registros[sub(RI, 11, 8)] = sub(RI, 7, 0);
printf("LLO R%X,%X\n", sub(RI, 11, 8), sub(RI, 7, 0));
break;
case LHI:
registros[sub(RI, 11, 8)] =
(sub(RI, 7, 0) << 8) + sub(registros[sub(RI, 11, 8)], 7, 0);
printf("LHI R%X,%X\n", sub(RI, 11, 8), sub(RI, 7, 0));
break;
case IN:
printf("IN\n");
break;
case OUT:
printf("OUT\n");
break;
case ADDS: {
int16_t a = registros[sub(RI, 7, 4)];
int16_t b = registros[sub(RI, 3, 0)];
int16_t r = a + b;
registros[sub(RI, 11, 8)] = r;
flags[Z] = (r == 0);
flags[S] = r < 0;
flags[V] = ((flags[S] && a > 0 && b > 0) || (!flags[S] && a < 0 && b < 0));
flags[C] = ((uint32_t) a + (uint32_t) b > 0xFFFF);
printf("ADDS R%X,R%X,R%X\n", sub(RI, 11, 8), sub(RI, 7, 4),
sub(RI, 3, 0));
break;
}
case SUBS: {
int16_t a = registros[sub(RI, 7, 4)];
int16_t b = registros[sub(RI, 3, 0)];
int16_t r = a - b;
registros[sub(RI, 11, 8)] = r;
flags[Z] = (r == 0);
flags[S] = r < 0;
flags[V] = ((!flags[S] && a < 0 && b > 0) || (flags[S] && a > 0 && b < 0));
flags[C] = ((uint32_t) a - (uint32_t) b > 0xFFFF); //REV
printf("SUBS R%X,R%X,R%X\n", sub(RI, 11, 8), sub(RI, 7, 4),
sub(RI, 3, 0));
break;
}
case NAND: {
int16_t a = registros[sub(RI, 7, 4)];
int16_t b = registros[sub(RI, 3, 0)];
int16_t r = ~(a & b);
registros[sub(RI, 11, 8)] = r;
flags[Z] = (r == 0);
flags[S] = (r < 0);
printf("NAND R%X,R%X,R%X\n", sub(RI, 11, 8), sub(RI, 7, 4),
sub(RI, 3, 0));
break;
}
case SHL: {
int16_t r = registros[sub(RI, 11, 8)];
flags[C] = sub(r, 15, 15);
r <<= 1;
flags[S] = sub(r, 15, 15) == 1;
registros[sub(RI, 11, 8)] = r;
printf("SHL R%X\n", sub(RI, 11, 8));
break;
}
case SHR: {
int16_t r = registros[sub(RI, 11, 8)];
flags[C] = sub(r, 0, 0);
flags[S] = false;
r = (uint16_t) r >> 1;
flags[Z] = (r == 0);
registros[sub(RI, 11, 8)] = r;
printf("SHR R%X\n", sub(RI, 11, 8));
break;
}
case SHRA: {
int16_t r = registros[sub(RI, 11, 8)];
flags[C] = sub(r, 0, 0);
r >>= 1;
flags[Z] = (r == 0);
registros[sub(RI, 11, 8)] = r;
printf("SHRA R%X\n", sub(RI, 11, 8));
break;
}
case B: {
int16_t cnd = sub(RI, 11, 8);
if ((cnd == 0x0) || (cnd == 0x1 && flags[Z]) || (cnd == 0x2 && flags[S]) ||
(cnd == 0x3 && flags[C]) || (cnd == 0x4 && flags[V]))
PC = registros[0xD];
printf("B%c\n", cnds[cnd]);
break;
}
case CALL: {
int16_t cnd = sub(RI, 11, 8);
if ((cnd == 0x0) || (cnd == 0x1 && flags[Z]) || (cnd == 0x2 && flags[S]) ||
(cnd == 0x3 && flags[C]) || (cnd == 0x4 && flags[V])) {
mem[--registros[0xE]] = PC;
PC = registros[0xD];
}
printf("CALL%c\n", cnds[cnd]);
break;
}
case RET:
PC = mem[registros[0xE]];
registros[0xE] -= 1;
printf("RET\n");
break;
case HALT:
halt = true;
printf("HALT\n");
break;
default:
break;
}
}
void uso(const char* argv0)
{
cout << "Uso: " << argv0 << " [-OPCION] [FICHERO]" << endl << endl;
cout << "\t-o\t Para especificar la direccion de carga del programa."
"\n\t \t Por defecto es 0x100." << endl;
}
int main(int argc, char *argv[])
{
uint16_t offset = 0x100;
int c;
while ((c = getopt(argc, argv, "o:")) != -1) {
long off;
switch (c) {
case 'o': {
off = strtol(optarg, NULL, 16);
if (off < 0 || off > UINT16_MAX) {
cout << "offset fuera de rango" << endl;
exit(1);
} else
offset = (uint16_t)off;
break;
}
default:
break;
}
}
if (optind >= argc) {
uso(argv[0]);
exit(1);
}
ifstream arch(argv[optind]);
if (!arch.is_open()) {
cout << "No se pudo abrir el archivo" << endl;
exit(1);
}
Code2 code2(offset);
string s_instr;
int linea = 1;
cout << "Cargando programa... ";
while (getline(arch, s_instr)) {
uint16_t instr = parsear_instruccion(s_instr);
if (instr == INSTR_INVALIDA) {
cout << "Instruccion invalida en la linea " << linea
<< ": " << s_instr << endl;
exit(1);
}
code2.set_mem(offset, (int16_t)instr);
++offset;
++linea;
}
cout << "Listo." << endl << endl;
cout << "Ingrese h para ver los comandos" << endl;
bool halt = false;
bool ejecutar_todo = false;
while (!halt) {
if (!ejecutar_todo) {
string comando;
getline(cin, comando);
istringstream iss(comando);
vector<string> tokens;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokens));
if (tokens.size() > 0) {
if (tokens[0] == "n")
code2.ejecutar_instruccion();
else if (tokens[0] == "f")
code2.print_flags();
}
} else {
code2.ejecutar_instruccion();
}
halt = halt || code2.termino_ejecucion();
}
return 0;
}