-
Notifications
You must be signed in to change notification settings - Fork 3
/
mem.cpp
239 lines (224 loc) · 7.02 KB
/
mem.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
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
#include "mem.h"
static unsigned long long sext64(unsigned long long num, int sbit)
{
unsigned long long mask = 0x0;
if ((num & ((unsigned long long)0x1 << sbit)) != 0)
mask = (unsigned long long)((long long)0x8000000000000000 >> (63 - sbit));
return num | mask;
}
MEM::MEM()
{
rd = 0;
pc = 0;
alu = 0;
data = 0;
mem_content = 0;
ctrl.BrEq = false;
ctrl.BrLT = false;
ctrl.inst32_25_31 = 0;
ctrl.inst32_2_6 = 0;
ctrl.inst32_12_14 = 0;
err_no = NOTHING;
}
void MEM::set_Reg(unsigned int arg_rd,
unsigned long long arg_alu,
unsigned long long arg_data)
{
rd = arg_rd;
alu = arg_alu;
data = arg_data;
}
void MEM::set_pc(REG arg_pc)
{
pc = arg_pc;
}
void MEM::set_ctrl(INST32_CTRL_BIT arg_ctrl)
{
ctrl.BrEq = arg_ctrl.BrEq;
ctrl.BrLT = arg_ctrl.BrLT;
ctrl.inst32_25_31 = arg_ctrl.inst32_25_31;
ctrl.inst32_2_6 = arg_ctrl.inst32_2_6;
ctrl.inst32_12_14 = arg_ctrl.inst32_12_14;
}
MEMRW_CTRL MEM::get_MemRW()
{
switch (ctrl.inst32_2_6)
{
case (0x03 >> 2):
switch(ctrl.inst32_12_14)
{
case 0x0: return MEM_READ_B;
case 0x1: return MEM_READ_H;
case 0x2: return MEM_READ_W;
case 0x4: return MEM_READ_BU;
case 0x5: return MEM_READ_HU;
case 0x6: return MEM_READ_WU;
case 0x3: return MEM_READ_D;
default: return MEM_UNKNOWN;
}
case (0x23 >> 2):
switch(ctrl.inst32_12_14)
{
case 0x0: return MEM_WRITE_B;
case 0x1: return MEM_WRITE_H;
case 0x2: return MEM_WRITE_W;
case 0x3: return MEM_WRITE_D;
default: return MEM_UNKNOWN;
}
default: return MEM_DONT_CARE;
}
}
ERROR_NUM MEM::MemRW(char* memory)
{
switch(get_MemRW())
{
case MEM_READ_B:
mem_content = sext64((unsigned long long)(*((unsigned char*)(memory + alu))), 7);
break;
case MEM_READ_H:
if (alu % 2 != 0)
return INVALID_ADDR_ALIGNMENT;
mem_content = sext64((unsigned long long)(*((unsigned short*)(memory + alu))), 15);
break;
case MEM_READ_W:
if (alu % 4 != 0)
return INVALID_ADDR_ALIGNMENT;
mem_content = sext64((unsigned long long)(*((unsigned int*)(memory + alu))), 31);
break;
case MEM_READ_BU:
mem_content = (unsigned long long)(*((unsigned char*)(memory + alu)));
break;
case MEM_READ_HU:
if (alu % 2 != 0)
return INVALID_ADDR_ALIGNMENT;
mem_content = (unsigned long long)(*((unsigned short*)(memory + alu)));
break;
case MEM_READ_WU:
if (alu % 4 != 0)
return INVALID_ADDR_ALIGNMENT;
mem_content = (unsigned long long)(*((unsigned int*)(memory + alu)));
break;
case MEM_READ_D:
if (alu % 8 != 0)
return INVALID_ADDR_ALIGNMENT;
mem_content = sext64((unsigned long long)(*((unsigned long long*)(memory + alu))), 63);
break;
case MEM_WRITE_B:
*((unsigned char*)(memory + alu)) = (unsigned char)(data & 0xff);
mem_content = 0;
break;
case MEM_WRITE_H:
if (alu % 2 != 0)
return INVALID_ADDR_ALIGNMENT;
*((unsigned short*)(memory + alu)) = (unsigned short)(data & 0xffff);
mem_content = 0;
break;
case MEM_WRITE_W:
if (alu % 4 != 0)
return INVALID_ADDR_ALIGNMENT;
*((unsigned int*)(memory + alu)) = (unsigned int)(data & 0xffffffff);
mem_content = 0;
break;
case MEM_WRITE_D:
if (alu % 8 != 0)
return INVALID_ADDR_ALIGNMENT;
*((unsigned long long*)(memory + alu)) = (unsigned long long)(data & 0xffffffffffffffff);
mem_content = 0;
break;
case MEM_DONT_CARE: mem_content = 0; break;
case MEM_UNKNOWN: return INVALID_INST;
default: return INVALID_INST;
}
return NOTHING;
}
void MEM::get_meminfo(unsigned long long &addr, int &read, int &size)
{
switch(get_MemRW())
{
case MEM_READ_B: addr = alu; read = 1; size = 1; break;
case MEM_READ_H: addr = alu; read = 1; size = 2; break;
case MEM_READ_W: addr = alu; read = 1; size = 4; break;
case MEM_READ_BU: addr = alu; read = 1; size = 1; break;
case MEM_READ_HU: addr = alu; read = 1; size = 2; break;
case MEM_READ_WU: addr = alu; read = 1; size = 4; break;
case MEM_READ_D: addr = alu; read = 1; size = 8; break;
case MEM_WRITE_B: addr = alu; read = 0; size = 1; break;
case MEM_WRITE_H: addr = alu; read = 0; size = 2; break;
case MEM_WRITE_W: addr = alu; read = 0; size = 4; break;
case MEM_WRITE_D: addr = alu; read = 0; size = 8; break;
case MEM_DONT_CARE: addr = alu; read = -1; size = -1; break;
case MEM_UNKNOWN: addr = alu; read = -1; size = -1; break;
default: addr = alu; read = -1; size = -1; break;
}
}
unsigned long long MEM::get_mem_content()
{
return mem_content;
}
REG MEM::get_pc()
{
return pc;
}
INST32_CTRL_BIT MEM::get_ctrl()
{
return ctrl;
}
unsigned long long MEM::get_alu()
{
return alu;
}
unsigned int MEM::get_rd()
{
return rd;
}
void MEM::print()
{
printf(" MEM:\n");
printf(" status = %s\n", (err_no==NOTHING?"NTH":"STH"));
printf(" PC = %llx\n", pc);
printf(" inst type = %x\n", (unsigned char)type);
switch (get_MemRW())
{
case MEM_READ_B:
printf(" Read unsigned byte at %llx\n", alu);
printf(" Mem content = %llx\n", mem_content); break;
case MEM_READ_BU:
printf(" Read byte at %llx\n", alu);
printf(" Mem content = %llx\n", mem_content); break;
case MEM_READ_H:
printf(" Read half-word at %llx\n", alu);
printf(" Mem content = %llx\n", mem_content); break;
case MEM_READ_HU:
printf(" Read unsigned half-word at %llx\n", alu);
printf(" Mem content = %llx\n", mem_content); break;
case MEM_READ_W:
printf(" Read word at %llx\n", alu);
printf(" Mem content = %llx\n", mem_content); break;
case MEM_READ_WU:
printf(" Read unsigned word at %llx\n", alu);
printf(" Mem content = %llx\n", mem_content); break;
case MEM_READ_D:
printf(" Read double-word at %llx\n", alu);
printf(" Mem content = %llx\n", mem_content); break;
case MEM_WRITE_B:
printf(" Write byte at %llx\n", alu);
printf(" data = %llx\n", data); break;
case MEM_WRITE_H:
printf(" Write half-word at %llx\n", alu);
printf(" data = %llx\n", data); break;
case MEM_WRITE_W:
printf(" Write word at %llx\n", alu);
printf(" data = %llx\n", data); break;
case MEM_WRITE_D:
printf(" Write double-word at %llx\n", alu);
printf(" data = %llx\n", data); break;
case MEM_DONT_CARE:
printf(" Do nothing to mem.\n"); break;
case MEM_UNKNOWN: break;
default: break;
}
}
unsigned long long MEM::get_cycles()
{
return 1;
}