This repository has been archived by the owner on Oct 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.hpp
109 lines (97 loc) · 2.39 KB
/
tools.hpp
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
# include <iostream>
# include <string.h>
using namespace std;
# ifndef _TOOLS_HPP_
# define _TOOLS_HPP_
uint decode16(char c) {
if(c >= '0' && c <= '9') return c - '0';
else return c - 'A' + 10;
}
uint decode16(char *str) {
uint res = 0;
for (int i=0; str[i]; ++i)
res = (res << 4) + decode16(str[i]);
return res;
}
uint sgnextend(uint x, int top) {
if((x >> top) & 1) x |= (0xffffffff >> top << top);
return x;
}
uint setlowzero(uint x) {
if(x & 1) return x ^ 1;
else return x;
}
int tosgn(uint x) {
if ((x >> 31) & 1) {
x ^= 0xffffffff;
return -(x + 1);
} else return x;
}
void putinbinary(uint x) {
int w[33], wn=0;
for (int i=1; i<=32; ++i) w[i] = 0;
while(x) {
w[++wn] = (x&1);
x >>= 1;
}
for (int i=32; i; --i) {
cout << w[i];
if(i == 8) cout << ' ';
}
cout << endl;
}
void puttype(InstructionType type) {
switch(type) {
case EMPTY: puts("EMPTY"); break;
case LUI: puts("LUI"); break;
case AUIPC: puts("AUIPC"); break;
case JAL: puts("JAL"); break;
case JALR: puts("JALR"); break;
case BEQ: puts("BEQ"); break;
case BNE: puts("BNE"); break;
case BLT: puts("BLT"); break;
case BGE: puts("BGE"); break;
case BLTU: puts("BLTU"); break;
case BGEU: puts("BGEU"); break;
case LB: puts("LB"); break;
case LH: puts("LH"); break;
case LW: puts("LW"); break;
case LBU: puts("LBU"); break;
case LHU: puts("LHU"); break;
case SB: puts("SB"); break;
case SH: puts("SH"); break;
case SW: puts("SW"); break;
case ADDI: puts("ADDI"); break;
case SLTI: puts("SLTI"); break;
case SLTIU: puts("SLTIU"); break;
case XORI: puts("XORI"); break;
case ORI: puts("ORI"); break;
case ANDI: puts("ANDI"); break;
case SLLI: puts("SLLI"); break;
case SRLI: puts("SRLI"); break;
case SRAI: puts("SRAI"); break;
case ADD: puts("ADD"); break;
case SUB: puts("SUB"); break;
case SLL: puts("SLL"); break;
case SLT: puts("SLT"); break;
case SLTU: puts("SLTU"); break;
case XOR: puts("XOR"); break;
case SRL: puts("SRL"); break;
case SRA: puts("SRA"); break;
case OR: puts("OR"); break;
case AND: puts("AND"); break;
}
}
inline void putinhex(uint x) {
int w[6], wn=0;
for (int i=0;i<6; ++i) w[i] = 0;
while(x) {
int p = (x&15);
w[++wn] = p;
x >>= 4;
}
for (int i=wn; i; --i)
if(w[i] < 10) printf("%d", w[i]);
else printf("%c", w[i]-10+'A');
}
# endif