📄 testvmpu.java
字号:
package com.vmpu.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.vmpu.core.MicroProcessingUnit;
public class TestVMPU {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException, InterruptedException {
MicroProcessingUnit mpu = new MicroProcessingUnit(2000);
String rom = "test.rom";
makeRom(rom);
mpu.load(rom);
mpu.start();
Thread.currentThread().sleep(5000);
System.in.read();
}
/**
* 生成一个可执行文件
* 计算1+2+……+100=?
* 汇编语言如下:
* MVRD R0,1
* MVRD R1,0
* MVRD R2,100
* continue:
* ADD R1,R0
* INC R0
* DEC R2
* JNZ continue
* 对应的机器码如下:
* opcode DR SR imm
* 0111 00 00 0000 0001 ;0x7001,adr=0
* 0111 01 00 0000 0000 ;0x7400,adr=2
* 0111 10 00 0110 0100 ;0x7864,adr=4
* 1000 01 00 0000 0000 ;0x8400,adr=6(continue)
* 1001 00 00 0000 0001 ;0x9001,adr=8
* 1011 10 00 0000 0001 ;0xB801,adr=10
* 0110 0000 0000 0110 ;0x6006,adr=12
*
* @param rom
* @throws IOException
*/
public static void makeRom(String rom) throws IOException {
int codeLen = 7 * 2;//代码段长度
int dataLen = 0;//数据段长度
OutputStream out = new FileOutputStream(rom);
out.write(codeLen >>> 8);//代码段长度的高8位
out.write(codeLen);//代码段长度的低8位
out.write(dataLen >>> 8);//数据段的高8位
out.write(dataLen);//数据段的低8位
//代码段
out.write(0x70);
out.write(0x01);
out.write(0x74);
out.write(0x00);
out.write(0x78);
out.write(0x64);
out.write(0x84);
out.write(0x00);
out.write(0x90);
out.write(0x01);
out.write(0xB8);
out.write(0x01);
out.write(0x60);
out.write(0x06);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -