-
Notifications
You must be signed in to change notification settings - Fork 1
/
utility_functions.cpp
91 lines (82 loc) · 2.24 KB
/
utility_functions.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
#include "utility_functions.hpp"
string getBinValue(uint32_t num, int width){
if(width<=8) {
std::bitset<8> bin(num);
return bin.to_string();
} else if(width<=16) {
std::bitset<16> bin(num);
return bin.to_string();
} else {
std::bitset<32> bin(num);
return bin.to_string();
}
}
string intToBinFmtStr(uint32_t num, int width, int space_gap){
string bin = getBinValue(num, width);
int zeros = width - bin.size();
int spaces = space_gap - width;
string prefix="";
for(int i=0;i<spaces;i++){
prefix += " ";
}
prefix += "0b";
for(int i=0;i<zeros;i++){
prefix += "0";
}
string bin_str = prefix + bin;
return bin_str;
}
string intToHexFmtStr(uint32_t num, int width, int space_gap){
std::stringstream sstream;
sstream << std::hex << num;
int zeros = width/4.0 - sstream.str().size();
int spaces = space_gap - width/4.0;
string prefix="";
for(int i=0;i<spaces;i++){
prefix += " ";
}
prefix += "0x";
for(int i=0;i<zeros;i++){
prefix += "0";
}
string hex_str = prefix + sstream.str();
return hex_str;
}
string intToHexStr(uint32_t num){
std::stringstream sstream;
sstream << std::hex << num;
string hex_str = "0x" + sstream.str();
return hex_str;
}
// input format: 0xfffffffff
uint32_t hexToInt(string hex_str){
std::istringstream converter(hex_str.substr(2));
uint32_t dec_num;
converter >> std::hex >> dec_num;
return dec_num;
}
uint32_t readDispalcement(Reader *reader, uint32_t disp_bytes){
uint32_t displacement = 0;
for(uint32_t i=0;i<disp_bytes;i++){
uint32_t tmp = reader->readNextByte();
displacement = (tmp<<(8*i)) | displacement;
}
return displacement;
}
// source: https://stackoverflow.com/questions/17350906/computing-the-parity
bool findParity(uint32_t x){
uint32_t y;
y = x ^ (x >> 1);
y = y ^ (y >> 2);
y = y ^ (y >> 4);
y = y ^ (y >> 8);
y = y ^ (y >>16);
return y & 1;
}
void opcodeExtUnsupportedError(uint8_t opcode) {
printf("Opcode: %s is noopcode_byte_4t suporting!\n", intToHexStr(opcode).c_str());
}
void print(ostream &os1, ostream &os2, const string &str) {
os1 << str;
os2 << str;
}