-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatePc.cpp
More file actions
128 lines (111 loc) · 2.38 KB
/
updatePc.cpp
File metadata and controls
128 lines (111 loc) · 2.38 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
#include "headers.h"
#include "methods.h"
// #include<bits/stdc++.h>
// using namespace std;
// struct elements {
// string pc;
// string start;
// string registers[7];
// map<string, string> memory;
// int flags[8];
// };
int convert2int(char ch) {
static bool isFirst = true;
static std::map<char,int> lookup;
if(isFirst) {
for(int i=0;i<16;++i)
lookup["0123456789ABCDEF"[i]] = i;
isFirst = false;
}
return lookup[std::toupper(ch)];
}
char convert2char(int digi) {
static bool isFirst = true;
static std::map<int,char> lookup;
if(isFirst) {
for(int i=0;i<16;++i)
lookup[i] = "0123456789ABCDEF"[i];
isFirst = false;
}
return lookup[digi];
}
string addPc(string hex1,string hex2) {
int carry = 0;
string sum = "";
for(int i=0 ; i<hex1.length() ; ++i)
sum += " ";
for(int i=hex1.length()-1 ; i>=0 ; --i) {
int tmp = convert2int(hex1[i])+convert2int(hex2[i])+carry;
if(tmp < 16)
carry = 0;
else {
carry = 1;
tmp -= 16;
}
sum[i] = convert2char(tmp);
}
return sum;
}
int instSize(string str) {
int i, pos;
string temp = "";
string one[] = {"MOV", "XCHG", "ADD", "SUB", "INR", "DCR", "INX", "DCX", "DAD", "CMA", "CMP", "HLT"};
string two[] = {"MVI", "LXI", "ADI", "SUI"};
string three[] = {"JMP", "JC", "JNC", "JZ", "JNZ", "LDA", "STA", "LHLD", "SHLD"};
string four[] = {"SET"};
pos = str.find(' ');
if(pos == -1)
temp = str;
else
for(i=0;i<pos;i++)
temp += str[i];
for(i=0;i<12;i++) {
if(temp == one[i])
return 1;
}
for(i=0;i<4;i++) {
if(temp == two[i])
return 2;
}
for(i=0;i<9;i++) {
if(temp == three[i])
return 3;
}
if(temp == four[0])
return 4;
return -1;
}
int updatePc(elements *e) {
int size;
string str;
size = instSize(e->memory[e->pc]);
if(size == 1)
str = "0001";
else if(size == 2)
str = "0002";
else if(size == 3)
str = "0003";
else if(size == 4)
str = "0004";
e->pc = addPc(e->pc, str);
return 0;
}
// int main() {
// elements e;
// e.memory["2000"] = "MOV A,B";
// e.memory["2001"] = "XCHG";
// e.memory["2002"] = "JMP 2001";
// e.memory["2007"] = "HLT";
// e.memory["2008"] = "LDA 2050";
// e.pc = "2000";
// cout << e.pc << endl;
// updatePc(&e);
// cout << e.pc << endl;
// updatePc(&e);
// cout << e.pc << endl;
// updatePc(&e);
// cout << e.pc << endl;
// updatePc(&e);
// cout << e.pc << endl;
// return 0;
// }