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;
1213int indentaton_level = 0 ;
1314CPU_state cpu = {};
1415static int running = 1 ;
16+ uint64_t global_cycle_count = 0 ;
17+ uint64_t ninst = 0 ;
1518
1619void 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+
2227void 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+
43108void halt_trap (uint64_t pc , uint64_t code ){
44109 if (code ){
45110 printf (ANSI_FMT ("HIT BAD TRAP!\n" , ANSI_FG_RED ));
0 commit comments