This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cycles.cpp
58 lines (44 loc) · 1.7 KB
/
Cycles.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
function u32 cycles_estimation_table(OperandType source, OperandType destination, OperationType operation) {
// NOTE(fz): Effective Address Calculation component (Table 2-20)
u32 EA = 0;
// NOTE(fz): Table 2-21
CycleReference mov_cycle_table[] = {
{{ OperandType_Register, OperandType_Immediate }, 4},
{{ OperandType_Register, OperandType_Register }, 2},
{{ OperandType_Register, OperandType_Memory }, 8 + EA},
{{ OperandType_Memory, OperandType_Register }, 9 + EA},
};
// NOTE(fz): Table 2-21
CycleReference add_cycle_table[] = {
{{ OperandType_Register, OperandType_Register }, 3},
{{ OperandType_Memory, OperandType_Register }, 16 + EA},
{{ OperandType_Register, OperandType_Immediate }, 4},
};
CycleReference* table = mov_cycle_table;
u32 table_count = ArrayCount(mov_cycle_table);
if (operation == OP_add) {
table = add_cycle_table;
table_count = ArrayCount(add_cycle_table);
}
u32 result = 0;
for (u32 i = 0; i < table_count; i++) {
if (source == table[i].operands[0] &&
destination == table[i].operands[1]) {
result = table[i].cycle_count;
}
}
// NOTE(fz): I don't think that any instruction has 0 cycle cost.
// Therefore, if we get a zero cycle, we know this operation
// Is not on the table.
return result;
}
u32 estimate_cycles(Instruction instruction) {
// NOTE(fz): I will assume all pointers are in even addresses
// Which could not be the case a lot of times!
u32 cycles = 0;
OperandType source = instruction.operands[0].type;
OperandType destination = instruction.operands[1].type;;
cycles = cycles_estimation_table(source, destination, instruction.operation);
printf("[Cycle Estimation: %02u] ", cycles);
return cycles;
}