-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaria.cpp
166 lines (143 loc) · 3 KB
/
maria.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
#include <string.h>
#include <iostream>
#include <algorithm>
#include <io.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <iomanip>
#include <memory>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
int i;
char *in, *out;
in = NULL;
out = NULL;
for ( i = 1; i < argc; i++ ) {
if ( strcmp ( argv[i], "--in" ) == 0 ) {
if ( (i+1) < argc ) {
in = argv[i+1];
//cout << "The file name is: " << in << endl;
i++;
} else {
cerr << "Error: --in missing file name\n" ;
exit(1);
}
} else if ( strcmp ( argv[i], "--out" ) == 0 ) {
if ( (i+1) < argc ) {
out = argv[i+1];
i++;
} else {
cerr << "Error: --out missing file name\n";
exit(1);
}
} else {
cerr << "Error: invalid parameter >" << argv[i] << "<\n";
exit(1);
}
}
if ( in == NULL ) {
cerr << "Error: missing --in <fn> parameter\n" ;
exit(1);
}
if ( out == NULL ) {
cerr << "Error: missing --out <fn> parameter\n" ;
exit(1);
}
// TODO: Add more code
// Read in Input file into `memory`
//char hex[30];
//string line;
long hexNum;
char sharp = '#';
vector<int> hexValue;
ifstream inFile;
char *ptr;
int final_ret;
//open the file, read input
//push all inputs from file into a queue
inFile.open(in);
if (inFile.is_open()) {
while (!inFile.eof()) {
string line;
getline(inFile, line);
char hex[30];
for (int i = 0; i < line.length(); i++) {
hex[i] = line[i];
//cout << "hex: " << hex[i] << " ";
}
if (sharp == hex[0]) {
continue;
}
else if(line.empty()) {
continue;
}
else {
hexNum = strtol(hex, &ptr, 16);
final_ret = int(hexNum);
hexValue.push_back(final_ret);
}
/*for (int i = 0; i < hexValue.size(); i++) {
printf("hex Value: 0x%04\n", hexValue.at(i));
}*/
}
}
inFile.close();
int pc = 0;
int ir;
int ac;
int mar;
int mdr;
int memory[0xFFFF];
//fetch cycle
for (int i = 0; i <= hexValue.size(); i++) {
ir = hexValue.at(i);
//cout << "ir: " << ir << endl;
int hand = 0;
int op = 0;
ir = memory[pc];
pc++;
hand = ir & 0xFFF;
op = (ir & 0xF000);
op = op >> 12;
//cout << "op: " << op << endl;
//cout << "hand: " << hand << endl;
//execute
switch(op) {
case 0: mdr = pc;
mar = hand;
memory[hand] = mdr;
ac = hand;
ac = ac +1;
pc = ac;
break;
case 1: mar = hand;
mdr = memory[mar];
ac = mdr;
break;
case 2: mar = hand;
mdr = ac;
memory[mar] = mdr;
break;
case 3: mar = hand;
mdr = memory[mar];
ac = ac - mdr;
break;
case 4: mar = hand;
mdr = memory[mar];
ac = ac - mdr;
break;
case 5: break;
case 6: break;
case 7: exit(1);
break;
case 8: break;
case 9: pc = hand;
break;
case 10: ac = 0;
break;
}
}
return 0;
}