-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu.h
307 lines (227 loc) · 7.18 KB
/
cpu.h
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
#ifndef _APEX_CPU_H_
#define _APEX_CPU_H_
#define IQ_ENTRIES_NUMBER 8
#define ROB_ENTRIES_NUMBER 12
#define LSQ_ENTRIES_NUMBER 6
#define PRF_ENTRIES_NUMBER 24
#define ARF_ENTRIES_NUMBER 16
#define ALLOCATE_PHY_REGISTER 16
#define DEALLOCATE_PHY_REGISTER 16
#define BIS_ENTRIES_NUMBER 8
enum STAGES
{
F,
DRF,
Int_FU,
Int_FU2,
Mul_FU,
Mul_FU2,
Mul_FU3,
MEM,
NUM_STAGES
};
/* Format of an APEX instruction */
typedef struct APEX_Instruction
{
char opcode[128]; // Operation Code
int rd; // Destination Register Address
int rs1; // Source-1 Register Address
int rs2; // Source-2 Register Address
int rs3;
int imm; // Literal Value
} APEX_Instruction;
/* Model of CPU stage latch */
typedef struct CPU_Stage
{
int pc; // Program Counter
char opcode[128]; // Operation Code
int arch_rs1; // Source-1 Architectural Register Address
int arch_rs2; // Source-2 Architectural Register Address
int arch_rs3; // Source-3 Architectural Register Address
int phys_rs1; // Source-1 Physical Register Address
int phys_rs2; // Source-2 Physical Register Address
int phys_rs3; // Source-2 Physical Register Address
int rs1_value; // Source-1 Register Value
int rs2_value; // Source-2 Register Value
int rs3_value; // Source-2 Register Value
int rs1_valid; // Source-1 Register Value
int rs2_valid; // Source-2 Register Value
int rs3_valid; // Source-3 Register Value
int arch_rd; // Destination Architectural Register Address
int phys_rd; // Destination Physical Register Address
int imm; // Literal Value
int buffer; // Latch to hold some value
int mem_address; // Computed Memory Address
int busy; // Flag to indicate, stage is performing some action
int stalled; // Flag to indicate, stage is stalled
int rob_entry_id;
int branch_id;
int LSQ_index;
int target_address;
} CPU_Stage;
/* Issue Queue entry */
typedef struct ISSUE_QUEUE_Entry
{
int pc; // Program Counter
char opcode[128]; // Operation Code
int counter; // counts the number of cycles of an instruction in Issue Queue
int free; // indicates if the entry is allocated or free
enum STAGES FU_type; // function unit type
int imm; // Literal Value
/* Source-1 fields */
int arch_rs1;
int rs1_ready; // source-1 ready bit
int phys_rs1; // source-1 physical address
int rs1_value; // source-1 value
/* Source-2 fields */
int arch_rs2;
int rs2_ready; // source-2 ready bit
int phys_rs2; // source-2 physical address
int rs2_value; // source-2 value
int arch_rs3;
int rs3_ready; // source-3 ready bit
int phys_rs3; // source-3 physical address
int rs3_value; // source-3 value
int arch_rd;
int phys_rd;
int rob_entry_id;
int LSQ_index;
int branch_id;
} ISSUE_QUEUE_Entry;
/* Issue Queue */
typedef struct ISSUE_QUEUE
{
int free_entry; // points to free entry in Issue Queue
ISSUE_QUEUE_Entry iq_entry[IQ_ENTRIES_NUMBER];
} ISSUE_QUEUE;
typedef struct ROB_Entry
{
int free; // indicates if the entry is allocated or free
char opcode[128]; // Operation Code
int pc; // PC value of an instruction
int branch_id;
int status; // indicates if the result value is valid
int arch_rd; // Destination architectural address
int phys_rd; // Destination physical address
int arch_rs1;
int phys_rs1; // source-1 physical address
int arch_rs2;
int phys_rs2; // source-2 physical address
int arch_rs3;
int phys_rs3; // source-3 physical address
int imm;
} ROB_Entry;
typedef struct ROB
{
int tail;
int head;
ROB_Entry rob_entry[ROB_ENTRIES_NUMBER];
} ROB;
typedef struct PHYSICAL_REGISTER_FILE_Entry
{
int value; // Value of physical register
int free; // Status bit indicating whether physical register is free or allocated
int valid; // Valid bit indicating whether physical register holds valid value or not
int which_arch_reg;
} PHYSICAL_REGISTER_FILE_Entry;
typedef struct ARCHITECTURAL_REGISTER_FILE_Entry
{
int value; // Value of physical register
int free; // Status bit indicating whether physical register is free or allocated
int valid; // Valid bit indicating whether physical register holds valid value or not
// RENAME_ALIAS_TABLE_Entry part
int allocate_phys_reg; // The most recent physical register for an architectural register in prf
} ARCHITECTURAL_REGISTER_FILE_Entry;
typedef struct BIS_Entry
{
int free; // indicates if the entry is allocated or free
int phys_src; // Physical Register Source for branch
} BIS_Entry;
typedef struct BACKUP_Entry
{
// RENAME_ALIAS_TABLE_Entry prf[ALLOCATE_PHY_REGISTER];
ARCHITECTURAL_REGISTER_FILE_Entry arf[ARF_ENTRIES_NUMBER];
} BACKUP_Entry;
typedef struct BIS
{
int tail; // branch instruction gets BIS id from the tail
int head;
BIS_Entry bis_entry[BIS_ENTRIES_NUMBER];
BACKUP_Entry backup_entry[BIS_ENTRIES_NUMBER];
} BIS;
typedef struct LSQ_Entry
{
int free;
char opcode[128];
int pc;
int mem_address_valid;
int mem_address;
int branch_id;
int rob_entry_id;
/* Source-1 fields */
int arch_rs1;
int rs1_ready; // source-1 ready bit
int phys_rs1; // source-1 physical address
int rs1_value; // source-1 value
/* Source-2 fields */
int rs2_ready;
int arch_rs2;
int phys_rs2; // source-2 physical address
int rs2_value;
int rs3_ready;
int arch_rs3;
int phys_rs3; // source-3 physical address
int rs3_value;
int imm;
int arch_rd;
int phys_rd;
} LSQ_Entry;
typedef struct LSQ
{
int tail;
int head;
LSQ_Entry lsq_entry[LSQ_ENTRIES_NUMBER];
} LSQ;
typedef struct APEX_CPU
{
/* Clock cycles elasped */
int clock;
int fill_in_rob;
int mul_cycle;
int mem_cycle;
int last_branch_id;
int last_arith_phys_rd;
int commitments;
/* Current program counter */
int pc;
PHYSICAL_REGISTER_FILE_Entry prf[PRF_ENTRIES_NUMBER];
ARCHITECTURAL_REGISTER_FILE_Entry arf[ARF_ENTRIES_NUMBER];
/* Issue Queue with 2 entries */
ISSUE_QUEUE iq;
ROB rob;
LSQ lsq;
BIS bis;
/* Array of 5 CPU_stage */
CPU_Stage stage[8];
/* Code Memory where instructions are stored */
APEX_Instruction* code_memory;
int code_memory_size;
/* Data Memory */
int data_memory[4096];
/* Some stats */
int simulation_completed;
} APEX_CPU;
APEX_Instruction* create_code_memory(const char* filename, int* size);
APEX_CPU* APEX_cpu_init(const char* filename, const char* function, const int cycles);
int get_source_values(APEX_CPU* cpu, int rs2_exist);
int exception_handler(int code, char* opcode);
int APEX_cpu_run(APEX_CPU* cpu);
void APEX_cpu_stop(APEX_CPU* cpu);
int fetch(APEX_CPU* cpu);
int decode(APEX_CPU* cpu);
int execute_int(APEX_CPU* cpu);
int execute_int2(APEX_CPU* cpu);
int execute_mul(APEX_CPU* cpu);
int memory(APEX_CPU* cpu);
void print_instruction(int fetch_decode, CPU_Stage* stage);
#endif