📄 microprocessingunit.java
字号:
/**
* MicroProcessingUnit.java 2007/11/16 version1.0
*/
package com.vmpu.core;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.Timer;
/**
* 微处理器单元:包括一个时钟部件、取指部件、控制器部件和执行部件
* 能处理的代码空间4KB,数据空间1KB
*
* @author 周良
*
*/
public class MicroProcessingUnit {
/**
* MPU 状态寄存器8位:不同取值表示的状态含义如下 0x01:初始化 0x02:取指 0x04:译码 0x08:执行 其他:异常
*/
private byte statusReg = MPU_UNDEFINED;
/**
* 状态常量
*/
public static final byte MPU_INIT = 0x01;
public static final byte MPU_INSTR_FETCH = 0x02;
public static final byte MPU_DECODE = 0x04;
public static final byte MPU_EXE = 0x08;
public static final byte MPU_UNDEFINED = 0x00;
public byte getStatusReg() {
return statusReg;
}
public void setStatusReg(byte status) {
this.statusReg = status;
}
/**
* 取指部件
*/
private InstructionFetcher instrFetcher = null;
/**
* 控制器部件
*/
private Controller controller = null;
/**
* 执行部件
*/
private Executor executor = null;
public InstructionFetcher getInstrFetcher() {
return instrFetcher;
}
public Controller getController() {
return controller;
}
public Executor getExecutor() {
return executor;
}
/**
* 时钟部件
*/
private Timer timer = null;
/**
* 时钟周期:默认为1ms
*/
private int cycle = 1;
/**
* 是否从控制台打印用于调试的信息:
* 主要是各种寄存器的值
*/
private boolean consoleDebugInfo = true;
public boolean isConsoleDebugInfo() {
return consoleDebugInfo;
}
public void setConsoleDebugInfo(boolean consoleDebugInfo) {
this.consoleDebugInfo = consoleDebugInfo;
}
/**
* 构造函数
*
* @param cycle
* 执行一条指令的时钟周期(ms),最小值为1ms
*/
public MicroProcessingUnit(int cycle) {
this.instrFetcher = new InstructionFetcher(this);
this.controller = new Controller(this);
this.executor = new Executor(this);
//初始化时钟
if (cycle > 0)
this.cycle = cycle;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MicroProcessingUnit.this.step();
// MicroProcessingUnit.this.printDebugInfo();
}
};
this.timer = new Timer(this.cycle, taskPerformer);
}
/**
* 打印VMPU的各种寄存器信息,供调试程序用
*/
public void printDebugInfo() {
System.out.println("VMPU_statusReg:" + (int)this.statusReg);
System.out.println("VMPU_pc:" + (int)this.instrFetcher.getPc());
System.out.println("VMPU_instrReg:" + (int)this.instrFetcher.getInstrReg());
System.out.println("VMPU_ctrlSgnlReg:" + (int)this.controller.getCtrlSgnlReg());
System.out.println("VMPU_immReg1:" + (int)this.controller.getImmReg1());
System.out.println("VMPU_immReg2:" + (int)this.controller.getImmReg2());
for(int i = 0;i < this.executor.getRegs().length;i ++)
System.out.println("VMPU_regs[" + i + "]:" + (int)this.executor.getRegs()[i]);
System.out.println("VMPU_flagReg:" + (int)this.executor.getFlagReg());
System.out.println("_______________________________________");
}
/**
* 读取可执行文件到MPU的缓存
* rom格式:
* 第1、2字节指出指令长度instrLen(字节)
* 第3、4字节指出数据长度dataLen(字节)
* 5~5+instrLen-1为指令部分(不能超过4KB)
* 5+instrLen~5+instrLen+dataLen-1为数据部分(不能超过1KB)
*
* @param rom
* @throws IOException
*/
public void load(String romFile) throws IOException {
this.statusReg = MicroProcessingUnit.MPU_INIT;
//各部件初始化
this.instrFetcher.init();
this.controller.init();
this.executor.init();
//读取rom
InputStream in = new FileInputStream(romFile);
int instrLen = in.read() << 8 | in.read();
int dataLen = in.read() << 8 | in.read();
// 读取指令
for (int i = 0; i < instrLen; i++)
this.instrFetcher.loadHalfInstr((byte) in.read(), i);
// 读取数据
for (int j = 0; j < dataLen; j++)
this.executor.loadData((byte) in.read(), j);
//初始取指
this.instrFetcher.firstFetchInstr();
}
/**
* 执行,需先装载可执行文件,否则,执行过程是不可预料的
*/
public void start() {
if(this.statusReg != MicroProcessingUnit.MPU_UNDEFINED)
if(!this.timer.isRunning())
this.timer.start();
}
/**
* 停止
*/
public void halt() {
if(this.timer.isRunning()) {
this.timer.stop();
this.statusReg = MicroProcessingUnit.MPU_UNDEFINED;
}
}
/**
* 暂停
*/
public void pause() {
if(this.timer.isRunning())
this.timer.stop();
}
/**
* 单步执行
*/
public void step() {
this.controller.decode();
this.executor.execute();
this.instrFetcher.fetchInstr();
}
public int getCycle() {
return cycle;
}
/**
* MPU是否在运行
* @return 在运行,返回True;否则为False
*/
public boolean isRunning() {
return timer.isRunning();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -