Skip to content

Commit 51d19fe

Browse files
committed
feat: implement multi-cycle simulator
1 parent 7f5170a commit 51d19fe

File tree

10 files changed

+595
-4
lines changed

10 files changed

+595
-4
lines changed

sim/include/cpu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ void init_cpu();
1818
void halt_trap(uint64_t pc, uint64_t code);
1919

2020
// ------------ ISS SIM ------------
21+
2122
void iss_cpu_exec();
2223
void iss_exec_once();
2324

2425
// --------- Multi-cycle SIM ---------
26+
2527
void mc_cpu_exec();
2628
void mc_exec_once();
2729

2830
// ---------- Pipeline SIM -----------
31+
2932
void pl_cpu_exec();
3033
void pl_exec_once();
3134

sim/include/isa_decode.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#ifndef ISA_DECODE_H
22
#define ISA_DECODE_H
33

4-
#include<stdint.h>
4+
#include <stdint.h>
5+
#include <pattern.h>
6+
#include <macro.h>
7+
#include <dbg.h>
8+
#include <memory.h>
59

610
typedef enum {
711
TYPE_R, TYPE_I, TYPE_S, TYPE_B, TYPE_U, TYPE_J, TYPE_N
@@ -38,4 +42,10 @@ void decode_operand(Decode *s, int *rd, uint64_t *src1, uint64_t *src2, uint64_t
3842
BITS(i, 30, 25) << 5 | \
3943
BITS(i, 11, 8) << 1, 13);} while(0)
4044

45+
#define INSTPAT_INST(s) ((s)->inst)
46+
#define INSTPAT_MATCH(s, name, type, ... /* execute body */ ) { \
47+
decode_operand(s, &rd, &src1, &src2, &imm, concat(TYPE_, type)); \
48+
__VA_ARGS__ ; \
49+
}
50+
4151
#endif

sim/include/mc_core.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef MC_CORE_H
2+
#define MC_CORE_H
3+
4+
#include <isa_decode.h>
5+
6+
typedef enum {
7+
STAGE_IF,
8+
STAGE_ID,
9+
STAGE_EX,
10+
STAGE_MEM,
11+
STAGE_WB,
12+
STAGE_DONE // check inst finish
13+
} Multi_Cycle_Stage;
14+
15+
void mc_IF(Decode *s);
16+
void mc_ID(Decode *s);
17+
void mc_EX(Decode *s, uint64_t *alu_result);
18+
void mc_MEM(Decode *s, uint64_t alu_result, uint64_t *mem_result);
19+
void mc_WB(Decode *s, uint64_t alu_result, uint64_t mem_result);
20+
21+
void push_stage(Decode *s, Multi_Cycle_Stage *stage);
22+
23+
#endif

sim/include/memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdint.h>
55

66
void load_image(char *filepath);
7-
void load_elf_symbols(char *filepath);
7+
void load_elf_symbols(const char *filepath);
88
uint8_t* guest_to_host(uint64_t vaddr);
99
uint32_t inst_fetch(uint64_t pc);
1010
uint64_t mem_read(uint64_t addr, int len);

sim/include/pattern.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef PATTERN_H
22
#define PATTERN_H
33

4+
#include <dbg.h>
5+
46
// --- pattern matching mechanism ---
57
__attribute__((always_inline))
68
static inline void pattern_decode(const char *str, int len,

sim/src/cpu.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <common.h>
22
#include <iss_core.h>
3+
#include <mc_core.h>
34
#include <memory.h>
45
#include <disasm.h>
56
#include <macro.h>
@@ -12,13 +13,17 @@ extern LLVMDisasmContextRef disasm_ctx;
1213
int indentaton_level = 0;
1314
CPU_state cpu = {};
1415
static int running = 1;
16+
uint64_t global_cycle_count = 0;
17+
uint64_t ninst = 0;
1518

1619
void init_cpu(){
1720
cpu.pc = MEM_BASE;
1821
memset(cpu.reg, 0, sizeof(cpu.reg));
1922
memset(cpu.csr, 0, sizeof(cpu.csr));
2023
}
2124

25+
// ------------ ISS SIM ------------
26+
2227
void iss_exec_once() {
2328
Decode s;
2429
s.pc = cpu.pc;
@@ -40,6 +45,66 @@ void iss_cpu_exec() {
4045
}
4146
}
4247

48+
// --------- Multi-cycle SIM ---------
49+
50+
void mc_exec_once() {
51+
// uint64_t record = global_cycle_count;
52+
++ninst;
53+
Decode s;
54+
Multi_Cycle_Stage stage = STAGE_IF;
55+
uint64_t alu_result = 0;
56+
uint64_t mem_result = 0;
57+
while (stage != STAGE_DONE) {
58+
switch (stage) {
59+
case STAGE_IF:
60+
mc_IF(&s);
61+
if (itrace_enabled)
62+
handle_itrace(&s);
63+
push_stage(&s, &stage);
64+
global_cycle_count++;
65+
break;
66+
case STAGE_ID:
67+
// add cycle count in func decode_ID
68+
mc_ID(&s);
69+
push_stage(&s, &stage);
70+
break;
71+
case STAGE_EX:
72+
// alu_result used to pass result to MEM or WB stage
73+
// add cycle count in func decode_EX
74+
mc_EX(&s, &alu_result);
75+
push_stage(&s, &stage);
76+
break;
77+
case STAGE_MEM:
78+
// mem_result used to pass result to WB stage
79+
// add cycle count in func decode_MEM
80+
mc_MEM(&s, alu_result, &mem_result);
81+
push_stage(&s, &stage);
82+
break;
83+
case STAGE_WB:
84+
mc_WB(&s, alu_result, mem_result);
85+
push_stage(&s, &stage);
86+
break;
87+
case STAGE_DONE:
88+
printf("INST END\n");
89+
goto loop_end;
90+
}
91+
}
92+
loop_end:
93+
cpu.pc = s.dnpc;
94+
// printf("spend %ld cycle\n", global_cycle_count - record);
95+
}
96+
97+
static inline void show_performance() {
98+
printf(ANSI_FMT("Performance: \n\tINST NUM = %4ld\n\tCYCLE NUM = %4ld\n\tCPI = %.3f\n", ANSI_FG_YELLOW), ninst, global_cycle_count, (float)global_cycle_count/(float)ninst);
99+
}
100+
101+
void mc_cpu_exec() {
102+
while (running) {
103+
mc_exec_once();
104+
}
105+
show_performance();
106+
}
107+
43108
void halt_trap(uint64_t pc, uint64_t code){
44109
if(code){
45110
printf(ANSI_FMT("HIT BAD TRAP!\n", ANSI_FG_RED));

sim/src/isa_decode.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <common.h>
22
#include <macro.h>
33
#include <pattern.h>
4+
#include <dbg.h>
45
#include <cpu.h>
56
#include <memory.h>
67
#include <isa_decode.h>
@@ -10,6 +11,7 @@ void decode_operand(Decode *s, int *rd, uint64_t *src1, uint64_t *src2, uint64_t
1011
int rs1 = BITS(i, 19, 15);
1112
int rs2 = BITS(i, 24, 20);
1213
*rd = BITS(i, 11, 7);
14+
s->type = type;
1315
switch(type) {
1416
case TYPE_R: src1R(); src2R(); break;
1517
case TYPE_I: src1R(); immI(); break;

sim/src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ int run_mc_model(int argc, char *argv[]) {
9797
char image_file[128] = "";
9898
sprintf(image_file, "test/build/%s.bin", argv[1]);
9999
load_image(image_file);
100+
101+
if (argc > 2 && strcmp(argv[2], "--itrace") == 0) {
102+
itrace_enabled = 1;
103+
init_llvm_disassembler();
104+
}
100105

101-
// TODO: multi_cycle_cpu_exec()
106+
init_cpu();
107+
mc_cpu_exec();
102108

103109
free(mem);
104110
return 0;

0 commit comments

Comments
 (0)