generated from cc3-ug/proj01-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
66 lines (50 loc) · 1.77 KB
/
utils.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
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
//sign extends a bitfield with given size
/* You may find implementing this function helpful */
int bitSigner(unsigned int field, unsigned int size) {
/* YOUR CODE HERE */
int signer = (int) field;
signer = signer << (32-size);
signer = signer >>(32-size);
return signer;
}
/* Remember that the offsets should return the offset in BYTES */
int get_branch_offset(Instruction instruction) {
/* YOUR CODE HERE */
unsigned int imm12 = (instruction.btype.imm7 & 0b1000000) << 6;
unsigned int imm11 = (instruction.btype.imm5 & 0b00001) << 11;
unsigned int imm10 = (instruction.btype.imm7 & 0b0111111) << 5;
unsigned int prov = imm12 | imm11 | imm10 | (instruction.btype.imm5 & 0b11110);
int res = bitSigner(prov, 13);
return res;
}
int get_jump_offset(Instruction instruction) {
/* YOUR CODE HERE */
unsigned int imm20 = (instruction.utype.imm & 0x00080000) << 1;
unsigned int imm19 = (instruction.utype.imm & 0xFF) << 12;
unsigned int imm11 = (instruction.utype.imm & 0x00000100) << 3;
unsigned int imm10 = (instruction.utype.imm & 0x0007FE00) >> 8;
unsigned int prov = imm20 | imm19 | imm11 | imm10;
int res = bitSigner(prov, 21);
return res;
}
int get_store_offset(Instruction instruction) {
/* YOUR CODE HERE */
unsigned int prov = instruction.stype.imm5 | (instruction.stype.imm7 << 5);
int res = bitSigner(prov, 12);
return res;
}
void handle_invalid_instruction(Instruction instruction) {
printf("Invalid Instruction: 0x%08x\n", instruction.bits);
}
void handle_invalid_read(Address address) {
printf("Bad Read. Address: 0x%08x\n", address);
exit(-1);
}
void handle_invalid_write(Address address) {
printf("Bad Write. Address: 0x%08x\n", address);
exit(-1);
}