-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.cpp
294 lines (259 loc) · 9.8 KB
/
commands.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
/* ****************************************************************
RISC-V Instruction Set Simulator
Computer Architecture, Semester 1, 2023
Command interpreter
**************************************************************** */
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
#include "memory.h"
#include "processor.h"
#include "commands.h"
using namespace std;
void command_skip_optional_whitespace(string& command, unsigned int& i) {
while (i < command.length() && isspace(command[i])) i++;
}
bool command_skip_required_whitespace(string& command, unsigned int& i) {
if (i == command.length() || !isspace(command[i])) return false;
i++;
while (i < command.length() && isspace(command[i])) i++;
return true;
}
bool command_match_decimal_number(string& command, unsigned int& i, unsigned int& num) {
unsigned int j = i;
while (j < command.length() && isdigit(command[j])) j++;
if (j == i) return false;
stringstream(command.substr(i, j - i)) >> num;
i = j;
return true;
}
bool command_match_hex_number(string& command, unsigned int& i, uint64_t& num) {
unsigned int j = i;
while (j < command.length() && isxdigit(command[j])) j++;
if (j == i) return false;
stringstream(command.substr(i, j - i)) >> hex >> num;
i = j;
return true;
}
bool command_match_blank(string& command, unsigned int i) {
return i == command.length() || command[i] == '#';
}
bool command_match_x(string& command, unsigned int i, bool& data_present, unsigned int& num, uint64_t& data) {
data_present = false;
if (i == command.length() || command[i] != 'x') return false;
i++;
if (!command_match_decimal_number(command, i, num)) return false;
command_skip_optional_whitespace(command, i);
if (i < command.length() && command[i] == '=') {
i++;
data_present = true;
command_skip_optional_whitespace(command, i);
if (!command_match_hex_number(command, i, data)) return false;
command_skip_optional_whitespace(command, i);
}
return i == command.length() || command[i] == '#';
}
bool command_match_pc(string& command, unsigned int i, bool& address_present, uint64_t& address) {
address_present = false;
if (i == command.length() || command[i] != 'p') return false;
i++;
if (i == command.length() || command[i] != 'c') return false;
i++;
command_skip_optional_whitespace(command, i);
if (i < command.length() && command[i] == '=') {
i++;
address_present = true;
command_skip_optional_whitespace(command, i);
if (!command_match_hex_number(command, i, address)) return false;
command_skip_optional_whitespace(command, i);
}
return i == command.length() || command[i] == '#';
}
bool command_match_m(string& command, unsigned int i, bool& data_present, uint64_t& address, uint64_t& data) {
data_present = false;
if (i == command.length() || command[i] != 'm') return false;
i++;
if (!command_skip_required_whitespace(command, i)) return false;
if (!command_match_hex_number(command, i, address)) return false;
command_skip_optional_whitespace(command, i);
if (i < command.length() && command[i] == '=') {
i++;
data_present = true;
command_skip_optional_whitespace(command, i);
if (!command_match_hex_number(command, i, data)) return false;
command_skip_optional_whitespace(command, i);
}
return i == command.length() || command[i] == '#';
}
bool command_match_dot(string& command, unsigned int i, bool& num_present, unsigned int& num) {
num_present = false;
if (i == command.length() || command[i] != '.') return false;
i++;
if (i == command.length() || command[i] == '#') return true;
if (!command_skip_required_whitespace(command, i)) return false;
if (command_match_decimal_number(command, i, num)) {
num_present = true;
command_skip_optional_whitespace(command, i);
}
return i == command.length() || command[i] == '#';
}
bool command_match_b(string& command, unsigned int i, bool& address_present, uint64_t& address) {
address_present = false;
if (i == command.length() || command[i] != 'b') return false;
i++;
if (i == command.length() || command[i] == '#') return true;
if (!command_skip_required_whitespace(command, i)) return false;
if (command_match_hex_number(command, i, address)) {
address_present = true;
command_skip_optional_whitespace(command, i);
}
return i == command.length() || command[i] == '#';
}
bool command_match_l(string& command, unsigned int i, string& filename) {
unsigned int j;
if (i == command.length() || command[i] != 'l') return false;
i++;
if (!command_skip_required_whitespace(command, i)) return false;
if (i == command.length() || command[i] != '"') return false;
i++;
j = i;
while (j < command.length() && command[j] != '"') j++;
filename = command.substr(i, j - i);
i = j;
if (i == command.length() || command[i] != '"') return false;
i++;
command_skip_optional_whitespace(command, i);
return i == command.length() || command[i] == '#';
}
bool command_match_prv(string& command, unsigned int i, bool& num_present, unsigned int& num) {
num_present = false;
if (i == command.length() || command[i] != 'p') return false;
i++;
if (i == command.length() || command[i] != 'r') return false;
i++;
if (i == command.length() || command[i] != 'v') return false;
i++;
command_skip_optional_whitespace(command, i);
if (i < command.length() && command[i] == '=') {
i++;
num_present = true;
command_skip_optional_whitespace(command, i);
if (!command_match_decimal_number(command, i, num)) return false;
command_skip_optional_whitespace(command, i);
}
return i == command.length() || command[i] == '#';
}
bool command_match_csr(string& command, unsigned int i, bool& data_present, uint64_t& address, uint64_t& data) {
data_present = false;
if (i == command.length() || command[i] != 'c') return false;
i++;
if (i == command.length() || command[i] != 's') return false;
i++;
if (i == command.length() || command[i] != 'r') return false;
i++;
if (!command_skip_required_whitespace(command, i)) return false;
if (!command_match_hex_number(command, i, address)) return false;
command_skip_optional_whitespace(command, i);
if (i < command.length() && command[i] == '=') {
i++;
data_present = true;
command_skip_optional_whitespace(command, i);
if (!command_match_hex_number(command, i, data)) return false;
command_skip_optional_whitespace(command, i);
}
return i == command.length() || command[i] == '#';
}
// Command interpreter function
void interpret_commands(memory* main_memory, processor* cpu, bool verbose) {
string command;
unsigned int i;
bool address_present, data_present, num_present;
uint64_t address, data;
unsigned int num;
string filename;
while (true) {
getline(cin, command); // Read the next line of input
if (!cin) break; // Exit if end of input file
i = 0;
command_skip_optional_whitespace(command, i);
if (command_match_blank(command, i)) { // Check for blank command
// Nothing to do
}
else if (command_match_x(command, i, data_present, num, data)) { // Check for x command
if (num > 31) {
cout << "Incorrect register number" << endl;
}
else if (!data_present) { // No new value
cpu->show_reg(num); // so just show register value
}
else {
cpu->set_reg(num, data); // Update register
}
}
else if (command_match_pc(command, i, address_present, address)) { // Check for pc command
if (!address_present) { // No new value
cpu->show_pc(); // so just show pc value
}
else {
cpu->set_pc(address); // Update pc
}
}
else if (command_match_m(command, i, data_present, address, data)) { // Check for m command
if (!data_present) { // No new value, so just show memory word value
data = main_memory->read_doubleword(address);
cout << setw(16) << setfill('0') << hex << data << endl;
}
else { // Update memory doubleword
main_memory->write_doubleword(address, data, 0xffffffffffffffffULL);
}
}
else if (command_match_dot(command, i, num_present, num)) { // Check for . command
if (!num_present) { // No instruction count value
cpu->execute(1, false); // so just execute one instruction without breakpoint check
}
else {
cpu->execute(num, true); // Execute specified number of instructions with breakpoint check
}
}
else if (command_match_b(command, i, address_present, address)) { // Check for b command
if (!address_present) { // No address value
cpu->clear_breakpoint(); // so just clear breakpoint
}
else {
cpu->set_breakpoint(address); // Set breakpoint at the address
}
}
else if (command_match_l(command, i, filename)) { // Check for l command
uint64_t start_address;
if (main_memory->load_file(filename, start_address)) { // Load using the specified file name
cpu->set_pc(start_address);
}
}
else if (command_match_prv(command, i, num_present, num)) { // Check for prv command
if (!num_present) { // No new privilege level
cpu->show_prv(); // so just show current privilege level
} else if (num == 0 || num == 3) {
cpu->set_prv(num); // Set the current privilege level
} else {
cout << "Incorrect privilege level" << endl;
}
}
else if (command_match_csr(command, i, data_present, address, data)) { // Check for csr command
if (address > 0xfffU) {
cout << "Incorrect CSR number" << endl;
}
else if (!data_present) { // No new value
cpu->show_csr(address); // so just show memory word value
}
else {
cpu->set_csr(address, data); // Update memory word
}
}
else {
cout << "Unrecognized command" << endl;
}
}
}