-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
280 lines (247 loc) · 8.06 KB
/
Copy pathprogram.cpp
File metadata and controls
280 lines (247 loc) · 8.06 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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdexcept>
#include <cstdint>
#include <vector>
#include "program.h"
#include "processor.h"
#include "macros.h"
#include "strings.h"
uint32_t prog_instruction_count = 0;
uint32_t executed_instructions = 0;
void memread(uint32_t addr, uint32_t num_bytes) {
switch(num_bytes) {
case 1:
std::cout << std::hex << "0x" << read_byte(addr) << std::endl;
break;
case 2:
std::cout << std::hex << "0x" << read_2_bytes(addr) << std::endl;
break;
case 3:
std::cout << std::hex << "0x" << read_3_bytes(addr) << std::endl;
break;
case 4:
std::cout << std::hex << "0x" << read_4_bytes(addr) << std::endl;
break;
default:
std::cout << "Invalid number of bytes to read provided: " << num_bytes << std::endl;
break;
}
}
void memwrite(uint32_t addr, uint32_t num_bytes, uint32_t data) {
switch(num_bytes) {
case 1:
write_byte(addr, data);
break;
case 2:
write_2_bytes(addr, data);
break;
case 3:
write_3_bytes(addr, data);
break;
case 4:
write_4_bytes(addr, data);
break;
default:
std::cout << "Invalid number of bytes to write provided: " << num_bytes << std::endl;
break;
}
}
void display_memory(uint32_t addr, uint32_t num_32_blocks) {
uint32_t curr_addr = addr;
for(uint32_t i = 0; i < num_32_blocks; i++) {
std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << curr_addr << ": ";
for(int i = 0; i < 32; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << read_byte(curr_addr) << " ";
curr_addr++;
}
std::cout << std::endl;
}
}
/* Displays hex memory of total program */
void display_program() {
display_memory(TEXT_SEGMENT_BEGIN, prog_instruction_count);
}
/* Loads a 4 byte value into a memory address for our program */
void load_instruction(const std::string& instruction, uint32_t address) {
if(instruction.length() != 32) {
throw std::invalid_argument("Instruction is not 32 bits long");
}
uint32_t data = 0;
for(char c : instruction) {
if(c != '0' && c != '1') {
throw std::invalid_argument("Invalid character in instruction");
}
data = (data << 1) | (c - '0');
}
if(address > TEXT_SEGMENT_END || address < TEXT_SEGMENT_BEGIN) {
throw std::invalid_argument("Address outside text segment");
} else {
memwrite(address, 4, data);
DEBUG_PRINT_V("Wrote: 0x" << std::hex << data << " to 0x" << address);
}
}
/* Takes the first argument given when running program
* and loads the binary file into memory. If an instruction
is malformed the program ends. */
void load_program(const std::string& filename) {
std::string filepath = "programs/" + filename + ".txt";
std::ifstream MyReadFile(filepath);
uint32_t curr_address = TEXT_SEGMENT_BEGIN;
if(!MyReadFile.is_open()) {
std::cerr << "Error: Unable to open file: " << filepath << std::endl;
std::cerr << "Ensure that the program binary you wish to run is in /programs" << std::endl;
exit(1);
}
std::string line;
// For each line, parse the instruction and ensure it is a valid length string
while(getline(MyReadFile, line)) {
DEBUG_PRINT(line);
std::string instruction;
for(char& c : line) {
if(c == '#' && instruction.length() < 32) {
std::cerr << "Error: Invalid or malformed instruction, found character " << c << " before parsing 32 bits" << std::endl;
exit(1);
} else if(c == '#' && instruction.length() >= 32) {
break;
}
if(c == '0' || c == '1') instruction.push_back(c);
}
if(instruction.length() != 32) {
std::cerr << "Error: Invalid instruction length: " << instruction.length() << std::endl;
exit(1);
}
try {
load_instruction(instruction, curr_address);
curr_address += 4;
prog_instruction_count++;
} catch(const std::invalid_argument& e) {
std::cerr << "Error processing instruction: " << e.what() << std::endl;
exit(1);
}
}
MyReadFile.close();
}
/* COMMAND HANDLING FUNCTIONS */
bool handle_step() {
return execute_instruction();
}
bool handle_reg(const std::vector<std::string> tokens) {
if(tokens.size() == 1) {
display_registers();
return false;
} else {
for(size_t arg = 1; arg < tokens.size(); arg++) {
try {
display_register(RegisterParser::parse_register(tokens[arg]));
} catch(const std::invalid_argument& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
return false;
}
}
bool handle_mem(const std::vector<std::string> tokens) {
if (tokens[1] == "regions" && tokens.size() == 2) {
std::cout << "Stack begin: " << STACK_SEGMENT_BEGIN << " end: " << STACK_SEGMENT_END << std::endl;
std::cout << "Heap begin: " << HEAP_SEGMENT_BEGIN << " end: " << HEAP_SEGMENT_END << std::endl;
std::cout << "BSS segment: " << BSS_SEGMENT_BEGIN << " end: " << BSS_SEGMENT_END << std::endl;
std::cout << "Data segment: " << DATA_SEGMENT_BEGIN << " end: " << DATA_SEGMENT_END << std::endl;
std::cout << "Text segment: " << TEXT_SEGMENT_BEGIN << " end: " << TEXT_SEGMENT_END << std::endl;
return false;
}
if(tokens.size() < 3) return false;
try {
uint32_t addr = string_to_int32(tokens[1]);
uint32_t blocks = string_to_int32(tokens[2]);
if (addr < TEXT_SEGMENT_BEGIN || addr > STACK_SEGMENT_BEGIN) {
std::cerr << "Invalid memory address, out of bounds: " << addr << std::endl;
return false;
}
display_memory(addr, blocks);
return false;
} catch(const std::invalid_argument& e) {
std::cerr << "Error, invalid memory argument: " << e.what() << std::endl;
}
return false;
}
/* END COMMAND HANDLING FUNCTIONS */
/* Handle a given command in interactive mode
Current supported commands: reg, mem, step, end */
bool handle_command(const std::vector<std::string> tokens) {
/* First, handle first token */
if(tokens[0] == "step") {
executed_instructions++;
return handle_step();
} else if(tokens[0] == "reg") {
return handle_reg(tokens);
} else if(tokens[0] == "mem") {
return handle_mem(tokens);
} else if(tokens[0] == "end") {
std::cout << "Exiting program early. Total instructions: " << prog_instruction_count << " Executed instructions: " << executed_instructions << std::endl;
return true;
}
print("Unknown command, try: mem, reg, step, end");
return false;
}
/* Allows user to step through program and read variables for debugging purposes
This is probably going to end up quite a mess from all the string parsing */
void handle_interactive_mode() {
std::string command;
bool halt = false;
while(command != "end" && !halt) {
std::cout << "Command: ";
std::getline(std::cin, command);
std::vector<std::string> tokens = split_string(command);
halt = handle_command(tokens);
}
}
void execute_program(bool interactive_mode) {
/* Interactive mode allows user to step through code
Otherwise, we execute the entire program */
if(interactive_mode) {
handle_interactive_mode();
} else {
bool halt = false;
while(!halt) {
halt = execute_instruction();
executed_instructions++;
}
}
std::cout << "Program terminated." << std::endl;
}
/* Currently supported modes: interactive and non-interactive. */
bool handle_interactive(const std::string& int_string) {
if(int_string.length() != 1) {
std::cerr << "Invalid argument for interactive mode: " << int_string << std::endl;
exit(1);
}
bool interactive_mode;
if(int_string[0] == '0') interactive_mode = false;
if(int_string[0] == '1') interactive_mode = true;
if(interactive_mode != 0 && interactive_mode != 1) {
std::cerr << "Invalid argument for interactive mode: " << interactive_mode << std::endl;
exit(1);
}
return interactive_mode;
}
int main(int argc, char* argv[]) {
print("Welcome to the program");
/* User needs to provide a program file to run and a choice of execution mode. */
if(argc < 3) {
std::cerr << "Usage: " << argv[0] << " <filename> <interactive>" << std::endl;
std::cerr << "Filename is binary you wish to run, interactive mode is 1 or 0." << std::endl;
exit(1);
}
bool interactive_mode = handle_interactive(argv[2]);
std::string filename = argv[1];
load_program(filename);
init_registers(STACK_SEGMENT_BEGIN, TEXT_SEGMENT_BEGIN);
#if DEBUG
display_program();
#endif
execute_program(interactive_mode);
return EXIT_SUCCESS;
}