-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisa_helper.c
455 lines (436 loc) · 15.7 KB
/
isa_helper.c
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "sim.h"
#include "isa_helper.h"
uint32_t rotate_right(uint32_t shiftee, uint8_t shifter)
{
return (shiftee << (32 - shifter)) | (shiftee >> (shifter));
}
uint8_t get_bit(uint32_t from, uint8_t bitid)
{
return (from >> bitid) & 1;
}
uint32_t get_bits(uint32_t from, uint8_t msb_id, uint8_t lsb_id)
{
uint8_t nb_bits = msb_id - lsb_id + 1;
uint32_t mask = (1 << nb_bits) - 1;
return (from >> lsb_id) & mask;
}
bool check_add_carry(uint32_t a, uint32_t b)
{
uint32_t res = a + b;
return (res < a);
}
bool check_sub_borrow(uint32_t a, uint32_t b)
{
uint32_t res = a - b;
return (res > a);
}
bool check_overflow(uint32_t a, uint32_t b)
{
uint32_t res = a + b;
// overflow = !a_31.!b_31.res_31 || a_31.b_31.!res_31
return (!(a >> 31) && !(b >> 31) && (res >> 31)) ||
((a >> 31) && (b >> 31) && !(res >> 31));
}
void set_bit(uint32_t *reg, uint8_t bit_id, uint8_t bit_val)
{
if(bit_val == 1){
*(reg) |= (1 << bit_id);
}
else{
*(reg) &= ~(1 << bit_id);
}
}
struct ShifterOperand * shifter_operand(struct CPUState state, uint32_t instruction)
{
struct ShifterOperand *retval = malloc(sizeof(struct ShifterOperand));
enum ShifterType {
LSLIMM = 0, //Logical shift left by immediate
LSLREG = 1, //Logical shift left by reg.
LSRIMM = 2, //Logical shift right by immediate
LSRREG = 3,
ASRIMM = 4, //Arithmetic shift right by immediate
ASRREG = 5,
RORIMM = 6, //Rotate right by immediate
RORREG = 7
} s_type;
if (instruction & (1 << I_BIT)) { //immediate
uint8_t rotate_imm = (instruction >> 8) & 0xF; //bits 11-8.
rotate_imm <<= 1; //actually have to rotate by 2x
uint8_t immed_8 = instruction & 0xFF; //bits 7-0.
retval->shifter_operand = immed_8;
retval->shifter_operand = rotate_right(retval->shifter_operand, rotate_imm);
if (rotate_imm == 0) {
retval->shifter_carry = get_bit(state.CPSR, CPSR_C);
} else {
retval->shifter_carry = get_bit(retval->shifter_operand, 31);
}
} else {
s_type = (instruction >> 4) & 0x7; //bits 6-4
uint32_t Rm = state.regs[instruction & 0xF]; //bits 3-0.
switch (s_type) {
case LSLIMM :
{
uint8_t shift_imm = (instruction >> 7) & 0x1F; //bits 11-7
retval->shifter_operand = Rm << shift_imm;
if (shift_imm == 0) {
retval->shifter_carry = get_bit(state.CPSR, CPSR_C);
} else {
retval->shifter_carry = get_bit(retval->shifter_operand, 32 - shift_imm);
}
break;
}
case LSLREG:
{
uint8_t reg_id = (instruction >> 8) & 0xF; //bits 11-8.
uint8_t shift = state.regs[reg_id]; //take bits 7-0 of reg_id.
retval->shifter_operand = Rm << shift;
if (shift == 0) {
retval->shifter_carry = get_bit(state.CPSR, CPSR_C);
} else if (shift <= 32) {
retval->shifter_carry = get_bit(retval->shifter_operand, 32 - shift);
} else {
retval->shifter_carry = 0;
}
break;
}
case LSRIMM:
{
uint8_t shift_imm = (instruction >> 7) & 0x1F; //bits 11-7
retval->shifter_operand = Rm >> shift_imm;
if (shift_imm == 0) {
retval->shifter_carry = get_bit(Rm, 31);
} else {
retval->shifter_carry = get_bit(Rm, shift_imm - 1);
}
break;
}
case LSRREG:
{
uint8_t reg_id = (instruction >> 8) & 0xF; //bits 11-8.
uint8_t shift = state.regs[reg_id]; //take bits 7-0 of reg_id.
retval->shifter_operand = Rm >> shift;
if (shift == 0) {
retval->shifter_carry = get_bit(state.CPSR, CPSR_C);
} else if (shift <= 32) {
retval->shifter_carry = get_bit(retval->shifter_operand, shift - 1);
} else {
retval->shifter_carry = 0;
}
break;
}
case ASRIMM:
{
uint8_t shift_imm = (instruction >> 7) & 0x1F; //bits 11-7
if (shift_imm == 0) {
retval->shifter_carry = get_bit(Rm, 31);
if (get_bit(Rm, 31) == 0) {
retval->shifter_operand = 0;
} else {
retval->shifter_operand = 0xFFFFFFFF;
}
} else {
retval->shifter_operand = arithmetic_right_shift(Rm, shift_imm);
retval->shifter_carry = get_bit(Rm, shift_imm - 1);
}
break;
}
case ASRREG:
{
uint8_t reg_id = (instruction >> 8) & 0xF; //bits 11-8.
uint8_t shift = state.regs[reg_id]; //take bits 7-0 of reg_id.
if (shift <= 32) {
retval->shifter_operand = arithmetic_right_shift(Rm, shift);
if (shift == 0) {
retval->shifter_carry = get_bit(state.CPSR, CPSR_C);
} else {
retval->shifter_carry = get_bit(Rm, shift - 1);
}
} else {
retval->shifter_carry = get_bit(Rm, 31);
if (get_bit(Rm, 31) == 0) {
retval->shifter_operand = 0;
} else {
retval->shifter_operand = 0xFFFFFFFF;
}
}
break;
}
case RORIMM:
{
uint8_t shift_imm = (instruction >> 7) & 0x1F; //bits 11-7
if (shift_imm == 0) { //rotate right with extend.
retval->shifter_operand = (Rm >> 1) | ((uint32_t)get_bit(state.CPSR, CPSR_C) << 31);
retval->shifter_carry = get_bit(Rm, 0);
} else {
retval->shifter_operand = rotate_right(Rm, shift_imm);
retval->shifter_carry = get_bit(Rm, 31);
}
break;
}
case RORREG:
{
uint8_t reg_id = (instruction >> 8) & 0xF; //bits 11-8.
uint8_t shift = state.regs[reg_id]; //take bits 7-0 of reg_id.
if (shift == 0) {
retval->shifter_operand = Rm;
retval->shifter_carry = get_bit(state.CPSR, CPSR_C);
} else if ((shift & 0xF) == 0) { //bits 4-0 are 0.
retval->shifter_operand = Rm;
retval->shifter_carry = get_bit(Rm, 31);
} else {
retval->shifter_operand = rotate_right(Rm, shift & 0xF);
retval->shifter_carry = get_bit(Rm, (shift & 0xF) - 1);
}
break;
}
}
}
return retval;
}
uint32_t ld_str_addr_mode(struct CPUState curr_state,
struct CPUState *next_state, uint32_t instruction)
{
enum ShifterType {
LSL = 0x00, // logical shift left
LSR = 0x01, // logical shift right
ASR = 0x10, // arithmetic shift right
ROR = 0x11, // rotate right (optionally with extend RRX)
} s_type;
uint32_t address;
uint32_t offset;
uint8_t rn_id = (instruction >> 16) & 0xf; // bits 19-16
int32_t rn_val = curr_state.regs[rn_id];
if (get_bit(instruction, I_BIT) == 0) { // Immediate offset
offset = instruction & 0x7ff; // bits 11-0
} else { // Register offset
uint8_t rm_id = instruction & 0xf; // bits 3-0
int32_t rm_val = curr_state.regs[rm_id];
uint8_t shift_imm = (instruction >> 7) & 0x1f; // bits 11-7
s_type = (instruction >> 5) & 0x3; // bits 6-5
switch (s_type) {
case LSL:
{
offset = rm_val << shift_imm; // this also covers unscaled case
break;
}
case LSR:
{
if (shift_imm == 0) { // shift_imm is 32
offset = 0;
} else {
offset = ((uint32_t) rm_val) >> shift_imm;
}
break;
}
case ASR:
{
if (shift_imm == 0) { // shift_imm is 32
if (get_bit(rm_val, 31) == 1) {
offset = 0xffffffff;
} else {
offset = 0;
}
} else {
offset = arithmetic_right_shift(rm_val, shift_imm);
}
break;
}
case ROR:
{
if (shift_imm == 0) { // RRX rotate right with extend
offset = (((uint32_t) get_bit(curr_state.CPSR, CPSR_C)) << 31) |
(((uint32_t) rm_val) >> 1);
} else {
offset = rotate_right(rm_val, shift_imm);
}
break;
}
}
}
if (instruction & (1 << U_BIT)) {
address = rn_val + offset;
} else {
address = rn_val - offset;
}
uint32_t ret_val;
const uint8_t P = get_bit(instruction, P_BIT), W = get_bit(instruction, W_BIT);
if (!P && !W) { // post-indexed
ret_val = rn_val;
if (condition_check(curr_state, (uint8_t) (instruction >> 28))) {
next_state->regs[rn_id] = address;
}
} else if (!P && W) { // user mode access, we are not implementing this
next_state->halted = 1;
ret_val = address;
} else if (P && !W) { // normal
ret_val = address;
} else { // (P = 1, W = 1) pre-indexed
ret_val = address;
if (condition_check(curr_state, (uint8_t) (instruction >> 28))) {
next_state->regs[rn_id] = address;
}
}
return ret_val;
}
uint32_t arithmetic_right_shift(uint32_t shiftee, uint8_t shifter)
{
#define SIGN_BIT 31
uint32_t sign_bit = get_bit(shiftee, SIGN_BIT);
if (sign_bit == 0) { //just ordinary right shifting
return (shiftee >> shifter);
} else { //shift in 1s on the left.
const uint32_t one = 1;
//evil bitwise magic incoming.
return (shiftee >> shifter) | ~((one << (32 - shifter)) - 1);
//yah no this isn't that hard to understand
//so we take 1 and move it to the first new bit
//example if 1000`0000`0000`0001`1000`1111`1000`0011`
//was shifted to the right twice, the right most "new" bit
//would be at index 32 - 2
//so we make mask like 0100`0000`0000`0000`0000`0000`0000`0000`
//subtract 1 like `0011`1111`1111`1111`1111`1111`1111`1111
//take complement et voila `1100`0000`0000`0000`0000`0000`0000`0000
}
#undef SIGN_BIT
}
bool condition_check(struct CPUState curr_state, uint8_t cond)
{
enum Condition {
EQ = 0x00,
NE = 0x01,
CS = 0x02,
CC = 0x03,
MI = 0x04,
PL = 0x05,
VS = 0x06,
VC = 0x07,
HI = 0x08,
LS = 0x09,
GE = 0x0A,
LT = 0x0B,
GT = 0x0C,
LE = 0x0D,
AL = 0x0E
};
switch(cond){
case EQ:
{
return (curr_state.CPSR & (1 << CPSR_Z));
break;
}
case NE:
{
return (!(curr_state.CPSR & (1 << CPSR_Z)));
break;
}
case CS:
{
return (curr_state.CPSR & (1 << CPSR_C));
break;
}
case CC:
{
return (!(curr_state.CPSR & (1 << CPSR_C)));
break;
}
case MI:
{
return (curr_state.CPSR & (1 << CPSR_N));
break;
}case PL:
{
return (!(curr_state.CPSR & (1 << CPSR_N)));
break;
}
case VS:
{
return (curr_state.CPSR & (1 << CPSR_V));
break;
}
case VC:
{
return (!(curr_state.CPSR & (1 << CPSR_V)));
break;
}
case HI:
{
return (
(curr_state.CPSR & (1 << CPSR_C)) &&
!(curr_state.CPSR & (1 << CPSR_Z))
);
break;
}
case LS:
{
return (
!(curr_state.CPSR & (1 << CPSR_C)) ||
(curr_state.CPSR & (1 << CPSR_Z))
);
break;
}
case GE:
{
return (
((curr_state.CPSR & (1 << CPSR_N)) && (curr_state.CPSR & (1 << CPSR_V))) ||
(!(curr_state.CPSR & (1 << CPSR_N)) && !(curr_state.CPSR & (1 << CPSR_V)))
);
break;
}
case LT:
{
return (
(!(curr_state.CPSR & (1 << CPSR_N)) && (curr_state.CPSR & (1 << CPSR_V))) ||
((curr_state.CPSR & (1 << CPSR_N)) && !(curr_state.CPSR & (1 << CPSR_V)))
);
break;
}
case GT:
{
return (
( !(curr_state.CPSR & (1 << CPSR_Z)) ) &&
( ( (curr_state.CPSR & (1 << CPSR_N)) && (curr_state.CPSR & (1 << CPSR_V)) ) || ( !(curr_state.CPSR & (1 << CPSR_N)) && !(curr_state.CPSR & (1 << CPSR_V)) ) )
);
break;
}
case LE:
{
return (
( (curr_state.CPSR & (1 << CPSR_Z)) ) ||
( ( !(curr_state.CPSR & (1 << CPSR_N)) && (curr_state.CPSR & (1 << CPSR_V)) ) || ( (curr_state.CPSR & (1 << CPSR_N)) && !(curr_state.CPSR & (1 << CPSR_V)) ) )
);
break;
}
case AL:
{
return true;
break;
}
default: return false;
}
}
uint32_t sign_extend(uint32_t num, uint8_t curr_width, uint8_t req_width)
{
const uint32_t one = 1;
if (req_width > 32) {
fprintf(stderr, "Error: requirements too wide, I'm not even going to try");
return num;
}
if (req_width < curr_width) {
fprintf(stderr, "Warning: curr_width(=%d) > req_width(=%d), may lead to overflow", curr_width, req_width);
num &= ((one << req_width) - 1);
return num;
} else if (req_width == curr_width) {
return num;
} else {
if (num & (one << (curr_width - 1))) { //negative
uint32_t mask_big = (one << req_width) - 1;
uint32_t mask_small = (one << curr_width) - 1;
num |= (mask_big ^ mask_small);
return num;
} else { //not negative.
uint32_t mask_small = (one << curr_width) - 1;
num &= mask_small;
return num;
}
}
}