-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCPU.h
More file actions
47 lines (41 loc) · 1.56 KB
/
Copy pathCPU.h
File metadata and controls
47 lines (41 loc) · 1.56 KB
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
#pragma once
#include "common.h"
#include "RegisterFile.h"
#include "Memory.h"
// CPU:取指 → 译码 → 执行 → 写回 的循环
class CPU {
private:
u32 fetch() const; // 取指:读 mem_[pc_]
void execute(u32 inst); // 译码 + 执行 + 写回,一条指令在这里完成
//见头文件
RegisterFile rf_;
Memory mem_;
u32 pc_ = 0;
u32 progEnd_ = 0; // 程序结束地址,run() 靠它判断何时停
u32 lastInst_ = 0; // 最近执行的那条指令(界面高亮数据通路用)
static void (*handlers[128])(CPU&, u32); // 指令处理函数表
static void initHandlers(); // 初始化指令处理函数表
public:
CPU();
void loadProgram(const std::vector<u32>& code, u32 base = 0);
void step(); // 单步执行一条指令
void run(size_t maxSteps = 1000000); // 连续执行到程序结束(maxSteps 防死循环)
void reset();
//表驱动
using handler = void (*)(CPU&, u32); // 指令处理函数指针类型
void setpc(u32 a){
pc_ = a;
}
void setreg(int n, u32 v){
rf_.write(n,v);
}
Memory& getmem(){
return mem_;
}
// —— 给“界面 / 测试”同学用的只读接口 ——
u32 pc() const { return pc_; }
u32 reg(int n) const { return rf_.read(n); }
const Memory& mem() const { return mem_; }
bool finished() const { return pc_ >= progEnd_; } // 程序是否已执行完
u32 inst() const { return lastInst_; } // 最近执行的一条指令
};